I have found that optimisers (R’s optim to be precise) are sometime unstable. Not in a numerical sense, but in a more practical sense, i.e. choking on certain inputs (I wasn’t able to figure out when exactly) and in the end crashing, without the possibility of recovery or error-processing. Which is quite bad, when you’re in the mid of some long calculations, e.g. a Monte-Carlo experiment. I found that optim’s standard method, Nelder-Mead, was unsusceptible to such crashes (Please, geeks, explain me why!). But with Nelder-Mead, you cannot use parameter constraints, so you have to formulate your optimisation problem in an unconstrained way. Here are some of my tricks.
Strictly positive values: I learned this trick from the documentation of the dlm package by Petris et.al. Feed your optimiser with logs, which can be both negative and positive. Then, after optimisation calculate the actual parameters by plugging the optimiser’s output into the exponential function, you are then guaranteed to have positive values. This works very well for variances, period lengths or frequencies, anything that has to be strictly positive.
Values in a certain range: This one is a bit tricky, and probably there are more elegant ways to do this, but I use the arcus tangens function and scale and shift it accordingly. The atan has the basic shape I want, coming asymptotically from , crossing at the origin and going asymptotically to .
An example for a function that converts any real number to something in the range would be:
The parameter controls the steepness of the function. The lower, the flatter the function will become. I find a value of to work pretty well.
You will then let the optimiser run to find the optimal x which can be any real number. However, the optimisation problem converts this x into something between zero and one and uses this for the calculations. This works for things like dampening factors or anything that is supposed to be in a certain range, without the boundaries. This function can easily be adopted to cover any arbitrary range, by adjusting the scaling factor and the vertical shift .
(to be continued …)