Пример #1
0
 def test_hess_fun1_cs(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()
             hecs = numdiff.approx_hess_cs(test_params, fun, args=self.args)
             assert_almost_equal(hetrue, hecs, decimal=DEC6)
Пример #2
0
 def test_hess_fun1_cs(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()
             hecs = numdiff.approx_hess_cs(test_params, fun, args=self.args)
             assert_almost_equal(hetrue, hecs, decimal=DEC6)
Пример #3
0
    def test_hess(self):
        pass
        #assert_almost_equal(self.res1.params, self.res2.params, DECIMAL_4)

        for test_params in self.params:
            he = self.mod.hessian(test_params)
            #TODO: bug
##            hefd = numdiff.approx_hess(test_params, self.mod.score)
##            assert_almost_equal(he, hefd, decimal=DEC8)

            hescs = numdiff.approx_fprime_cs(test_params.ravel(), self.mod.score)
            assert_almost_equal(he, hescs, decimal=DEC8)

            hecs = numdiff.approx_hess_cs(test_params.ravel(), self.mod.loglike)
            assert_almost_equal(he, hecs, decimal=DEC6)
Пример #4
0
    def test_hess(self):
        pass
        #assert_almost_equal(self.res1.params, self.res2.params, DECIMAL_4)

        for test_params in self.params:
            he = self.mod.hessian(test_params)
            #TODO: bug
##            hefd = numdiff.approx_hess(test_params, self.mod.score)
##            assert_almost_equal(he, hefd, decimal=DEC8)

            hescs = numdiff.approx_fprime_cs(test_params.ravel(), self.mod.score)
            assert_almost_equal(he, hescs, decimal=DEC8)

            hecs = numdiff.approx_hess_cs(test_params.ravel(), self.mod.loglike)
            assert_almost_equal(he, hecs, decimal=DEC6)
Пример #5
0
    x = np.arange(nobs * 3).reshape(nobs, -1)
    x = np.random.randn(nobs, 3)

    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()
Пример #6
0
    x = np.arange(nobs*3).reshape(nobs,-1)
    x = np.random.randn(nobs,3)

    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)