R corset: Bringing Math models back in shape

So your perfect ideal mathematical model returns values that are impossible; probabilities bigger than one or smaller than zero, negative stock market values, et cetera, and now you feel like quoting George Box… again.


Sometimes the mathematical model embeds a solution to keep things real like, for example, logistic regressions. However, very often many popular models like ARIMA offer no possibility to bound its results within business or scientific constrains, and then what? These are a few common options:

  1. You quote Cox : “All models are wrong…”
  2. You replace all imposible values with a default value.
  3. You keep the realistic part and remove the impossible one.
  4. You roll up your sleeves and expand the math capabilities of the model you intend to use by introducing peer reviewed extensions that allows to arbitrarily constrain… Oh, just kidding, not common… at all.

Though I would personally love to go for option number four, given my own constrains and keeping it real to myself as well, I went for option five:

5. You develop a general solution for any mathematical model and share it with the world via the  R package corset.

This is an example on how corset works:

Let’s say your model returns a forecast object (or any series like object) that should not return any negative values, and yet, the model returns the values shown below:


However, you want to show something realistic to your colleagues so you apply corset to the forecast object. The new forecast object now looks like this:


There are literally an infinite number of ways in which we can constrain the results of our models, and the choice of one or another depends entirely on our modelling decisions. The default algorithm in the R corset package uses an unidimensional variation of the Bezier Curves formulation which, for most situations, will work just fine.

So there you go, no more need for options one, two or three in your modelling endeavors… Well, keep option number one at hand just in case,  and get back in shape!

Note: I would like to thank MSD (Merck in USA & Canada) for allowing and promoting the development of FOSS projects within the company, and in particular to all the people that contributed in making possible the existence of MOSC (MSD/Merck Open Source Committee). It feels really good when you are surrounded with people that understand the value of cooperative work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s