In the previous post I mentioned the issue of user provided analytical derivative not giving correct results. With the help of Josef, the issue is resolved now. It turned out that, before passing on to leastsq the jacobian values had to transformed into those of "whitened" exogenous data. Here is an example of a typical Nonlinear Model Class:
class Myfunc(NonlinearLS):
def expr(self, params, exog=None):
if exog is None:
x = self.exog
else:
x = exog
x0, x1 = x[:,0], x[:,1]
a, b, c = params
return a + b*x0 + c*x1
def jacobian(self,params, exog=None):
if exog is None:
x = self.exog
else:
x = exog
x0, x1 = x[:,0], x[:,1]
a, b, c = params
a1 = np.ones(len(x0),)
b1 = x0
c1 = x1
jacob = np.column_stack((a1,b1,c1))
return jacob
def expr(self, params, exog=None):
if exog is None:
x = self.exog
else:
x = exog
x0, x1 = x[:,0], x[:,1]
a, b, c = params
return a + b*x0 + c*x1
def jacobian(self,params, exog=None):
if exog is None:
x = self.exog
else:
x = exog
x0, x1 = x[:,0], x[:,1]
a, b, c = params
a1 = np.ones(len(x0),)
b1 = x0
c1 = x1
jacob = np.column_stack((a1,b1,c1))
return jacob
- The above class is for linear model y = a + b*x0 + c*x1 + e, where a,b and c are the parameters to be estimated.
- The expr is the 'expression' of the function of exogenous variable. Simply, it is 'f' in y = f(x) + e. Here f is a + b*x0 + c*x1.
- The jacobian is the analytical derivative matrix of 'f' given above with respect to the parameters to estimated.
- The jacobian function is optional. If it is not provided, jacobian is calculated using numerical derivative.
Next we move on inlcude the result statistics for NonLinearLS class.