Apache Math Commons: non-linear optimization

106 Views Asked by At

I want to fit the following function:

$f(K) = e^{-qTS} - e^{rTK}$

i.e. find the parameters $q$ and $r$ to minimize the error.

$T$ and $S$ are constants.

I am using the Apache Math Commons library (which I am new to), but if I understood it well, first I need to calculate the Jacobian matrix.

My partial derivatives by $q$ and $r$ look like this:

$\frac{\partial f}{\partial q}(K) = -TSe^{-qTS}$

$\frac{\partial f}{\partial r}(K) = TKe^{rTK}$

I am not sure how to proceed since these are non-linear equations, and since my partial derivatives factor $q$ and $r$ themselves. Is this correct or did I do something wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

It looks like the library uses gradient descent sort of optimization, given that the creation function asks for "Initial guess for the parameters."

I suppose a natural guess would be $[0,0]$ for $q, r$.

You also need to provide a "ParametricUnivariateFunction", which will be a class with two methods to implement. Fortunately, you've done the hard work in implementing them!

You'll want to write the value function, which takes in $K$ and $[q,r]$ and returns the value for $f(K) = e^{-qTS} - e^{rTK}$. Which is easy to write! You will want to check that the array is length $2$ exactly, and you'll be hard-coding that $q$ is first and $r$ is second...

Second part is writing the gradient. Which you've basically done! Just return an array with the values $[-TSe^{-qTS}, TKe^{rTK}]$, again you're getting $K$ and $[q,r]$ as input. (And again, check that the input array is length two).

That's it! You can probably futz around a bit with code quality for a while, but it's just a simple Java class of two stateless functions. Good luck with whatever the thing you're doing is.

Note: If the function was a two-valued function with two variables, and you took the derivative wrt those two variables, and you took the derivative of the resulting matrix, that would be a Jacobian.