Пример #1
0
 def test_hess_fun1_fd(self):
     for test_params in self.params:
         #hetrue = 0
         hetrue = self.hesstrue(test_params)
         if not hetrue is None: #Hessian doesn't work for 2d return of fun
             fun = self.fun()
             #default works, epsilon 1e-6 or 1e-8 is not precise enough
             hefd = numdiff.approx_hess(test_params, fun, #epsilon=1e-8,
                                          args=self.args)[0] #TODO:should be kwds
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
Пример #2
0
 def test_hess_fun1_fd(self):
     for test_params in self.params:
         #hetrue = 0
         hetrue = self.hesstrue(test_params)
         if not hetrue is None: #Hessian doesn't work for 2d return of fun
             fun = self.fun()
             #default works, epsilon 1e-6 or 1e-8 is not precise enough
             hefd = numdiff.approx_hess(test_params, fun, #epsilon=1e-8,
                                          args=self.args)[0] #TODO:should be kwds
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
Пример #3
0
    #xk = np.zeros(3)
    beta = xk
    y = np.dot(x, beta) + 0.1 * np.random.randn(nobs)
    xkols = np.dot(np.linalg.pinv(x), y)

    print approx_fprime((1, 2, 3), fun, epsilon, x)
    gradtrue = x.sum(0)
    print x.sum(0)
    gradcs = approx_fprime_cs((1, 2, 3), fun, (x, ), h=1.0e-20)
    print gradcs, maxabs(gradcs, gradtrue)
    print approx_hess_cs((1, 2, 3), fun, (x, ),
                         h=1.0e-20)  #this is correctly zero

    print approx_hess_cs((1, 2, 3), fun2,
                         (y, x), h=1.0e-20) - 2 * np.dot(x.T, x)
    print numdiff.approx_hess(xk, fun2, 1e-3, (y, x))[0] - 2 * np.dot(x.T, x)

    gt = (-x * 2 * (y - np.dot(x, [1, 2, 3]))[:, None])
    g = approx_fprime_cs((1, 2, 3), fun1, (y, x),
                         h=1.0e-20)  #.T   #this shouldn't be transposed
    gd = numdiff.approx_fprime1((1, 2, 3), fun1, epsilon, (y, x))
    print maxabs(g, gt)
    print maxabs(gd, gt)

    import statsmodels.api as sm

    data = sm.datasets.spector.load()
    data.exog = sm.add_constant(data.exog)
    #mod = sm.Probit(data.endog, data.exog)
    mod = sm.Logit(data.endog, data.exog)
    #res = mod.fit(method="newton")
Пример #4
0
    xk = np.array([1,2,3])
    xk = np.array([1.,1.,1.])
    #xk = np.zeros(3)
    beta = xk
    y = np.dot(x, beta) + 0.1*np.random.randn(nobs)
    xkols = np.dot(np.linalg.pinv(x),y)

    print approx_fprime((1,2,3),fun,epsilon,x)
    gradtrue = x.sum(0)
    print x.sum(0)
    gradcs = approx_fprime_cs((1,2,3), fun, (x,), h=1.0e-20)
    print gradcs, maxabs(gradcs, gradtrue)
    print approx_hess_cs((1,2,3), fun, (x,), h=1.0e-20)  #this is correctly zero

    print approx_hess_cs((1,2,3), fun2, (y,x), h=1.0e-20)-2*np.dot(x.T, x)
    print numdiff.approx_hess(xk,fun2,1e-3, (y,x))[0] - 2*np.dot(x.T, x)

    gt = (-x*2*(y-np.dot(x, [1,2,3]))[:,None])
    g = approx_fprime_cs((1,2,3), fun1, (y,x), h=1.0e-20)#.T   #this shouldn't be transposed
    gd = numdiff.approx_fprime1((1,2,3),fun1,epsilon,(y,x))
    print maxabs(g, gt)
    print maxabs(gd, gt)


    import statsmodels.api as sm

    data = sm.datasets.spector.load()
    data.exog = sm.add_constant(data.exog)
    #mod = sm.Probit(data.endog, data.exog)
    mod = sm.Logit(data.endog, data.exog)
    #res = mod.fit(method="newton")