Esempio n. 1
0
    def test_grad_fun1_cs(self):
        for test_params in self.params:
            #gtrue = self.x.sum(0)
            gtrue = self.gradtrue(test_params)
            fun = self.fun()

            gcs = numdiff.approx_fprime_cs(test_params, fun, args=self.args)
            assert_almost_equal(gtrue, gcs, decimal=DEC13)
Esempio n. 2
0
    def test_grad_fun1_cs(self):
        for test_params in self.params:
            #gtrue = self.x.sum(0)
            gtrue = self.gradtrue(test_params)
            fun = self.fun()

            gcs = numdiff.approx_fprime_cs(test_params, fun, args=self.args)
            assert_almost_equal(gtrue, gcs, decimal=DEC13)
Esempio n. 3
0
    def test_score(self):
        pass
        #assert_almost_equal(self.res1.params, self.res2.params, DECIMAL_4)

        for test_params in self.params:
            sc = self.mod.score(test_params)
            scfd = numdiff.approx_fprime1(test_params.ravel(), self.mod.loglike)
            assert_almost_equal(sc, scfd, decimal=1)

            sccs = numdiff.approx_fprime_cs(test_params.ravel(), self.mod.loglike)
            assert_almost_equal(sc, sccs, decimal=13)
Esempio n. 4
0
    def test_score(self):
        pass
        #assert_almost_equal(self.res1.params, self.res2.params, DECIMAL_4)

        for test_params in self.params:
            sc = self.mod.score(test_params)
            scfd = numdiff.approx_fprime1(test_params.ravel(), self.mod.loglike)
            assert_almost_equal(sc, scfd, decimal=1)

            sccs = numdiff.approx_fprime_cs(test_params.ravel(), self.mod.loglike)
            assert_almost_equal(sc, sccs, decimal=13)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
0
    epsilon = 1e-6
    nobs = 200
    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)
Esempio n. 8
0
    epsilon = 1e-6
    nobs = 200
    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