Repository: statsmodels/statsmodels

Issue:
numdifftools dependency
Original Launchpad bug 653902: https://bugs.launchpad.net/statsmodels/+bug/653902
Reported by: vincent-vincentdavis (Vincent Davis).

statsmodels/_init_.py imports tsa
Which then returns an exception from statsmodels/tsa/var.py "raise Warning("You need to install numdifftools to try out the AR model")"
Should numdifftools be a dependency for all of statsmodels ?



Discussion:
[ LP comment 1 by: Skipper Seabold, on 2010-10-04 15:42:44.878854+00:00 ]

Should we include numdifftools or go ahead and use the versions that we have in the sandbox?

Strictly speaking neither is necessary as the solvers don't yet require the score and hessian methods, but they will if we want to inherit from LikelihoodModel.

[ LP comment 2 by: joep, on 2010-10-04 16:14:56.316846+00:00 ]

The solvers don't need it (not all of them), but the likelihood methods (generic, non-linear or non-normal) need the Jacobian or/and Hessian to calculate the cov_params and bse. Plus there are other places where we need numerical derivatives, e.g. derivatives of moment conditions in gmm and in delta-method to get bse and cov_params for non-linear transformation of estimated parameters. (this function is currently together with generic mle poisson)

(current status copied from the mailinglist)
In generic maximum likelihood I only use our version from numdiff.
There are some problems if the loglikelihood function is not nice, but
I haven't checked yet whether numdifftools would do better. (one
problem I had with zero-inflated poisson was bad scaling when the
parameter estimate should be infinite, i.e. a boundary solution.)

There will always be some problems with numerical differentiation, but
I think the complex method will be the best solution when it works,
but it's not included in our numdiff yet.

I wouldn't change around too much with numerical differentiation,
until we have cleaned up our numdiff. Then, before the release, we can
decide whether we want to include (a robustified) numdifftools, or we
include it anyway just to have a way to check numerical derivatives.

[ LP comment 3 by: joep, on 2011-01-17 13:45:28.184022+00:00 ]

numdifftool (no version information in the installed package)  raises exception with numpy 1.5.1 when running the **main** part in our numdiff"

scikits\statsmodels\sandbox\regression\numdiff.py

Traceback (most recent call last):
  File "C:\Josef\eclipsegworkspace\statsmodels-josef-experimental-gsoc\tools\exe
cdirs.py", line 30, in <module>
    execfile(m)
  File "C:\Josef\eclipsegworkspace\statsmodels-josef-experimental-gsoc\scikits\s
tatsmodels\sandbox\regression\numdiff.py", line 372, in <module>
    hessnd = hnd(xk)
  File "C:\Programs\Python25\lib\site-packages\numdifftools\core.py", line 1146,
 in **call**
    return self._hessian(x00)
  File "C:\Programs\Python25\lib\site-packages\numdifftools\core.py", line 789,
in _hessian
    hess = self._hessdiag(x00);
  File "C:\Programs\Python25\lib\site-packages\numdifftools\core.py", line 771,
in _hessdiag
    return self._partial_der(x00)
  File "C:\Programs\Python25\lib\site-packages\numdifftools\core.py", line 740,
in _partial_der
    PD[ind] = self._derivative(x0[ind])
  File "C:\Programs\Python25\lib\site-packages\numdifftools\core.py", line 694,
in _derivative
    [der_romb,errors] = self._rombextrap(der_init)
  File "C:\Programs\Python25\lib\site-packages\numdifftools\core.py", line 512,
in _rombextrap
    [qromb,rromb] = linalg.qr(rmat,econ=True);
TypeError: qr() got an unexpected keyword argument 'econ'

[ LP comment 4 by: Skipper Seabold, on 2011-02-17 19:01:40.569441+00:00 ]

There is still one unprotected import of numdifftools in tsa/mlemodel.py


Resolving patch:
diff --git a/statsmodels/tsa/mlemodel.py b/statsmodels/tsa/mlemodel.py
--- a/statsmodels/tsa/mlemodel.py
+++ b/statsmodels/tsa/mlemodel.py
@@ -13,7 +13,10 @@
 
 import numpy as np
 
-import numdifftools as ndt
+try:
+    import numdifftools as ndt
+except:
+    pass
 
 from statsmodels.base.model import LikelihoodModel
 


Test patch: