Example #1
0
    def test_hess(self):
        #NOTE: I had to overwrite this to lessen the tolerance
        for test_params in self.params:
            he = self.mod.hessian(test_params)
            hefd = numdiff.approx_fprime_cs(test_params, self.mod.score)
            assert_almost_equal(he, hefd, decimal=DEC8)

            #NOTE: notice the accuracy below and the epsilon changes
            # this doesn't work well for score -> hessian with non-cs step
            # it's a little better around the optimum
            assert_almost_equal(he, hefd, decimal=7)
            hefd = numdiff.approx_fprime(test_params, self.mod.score,
                                         centered=True)
            assert_almost_equal(he, hefd, decimal=4)
            hefd = numdiff.approx_fprime(test_params, self.mod.score, 1e-9,
                                         centered=False)
            assert_almost_equal(he, hefd, decimal=2)

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

            hecs = numdiff.approx_hess_cs(test_params, self.mod.loglike)
            assert_almost_equal(he, hecs, decimal=5)
            #NOTE: these just don't work well
            #hecs = numdiff.approx_hess1(test_params, self.mod.loglike, 1e-3)
            #assert_almost_equal(he, hecs, decimal=1)
            #hecs = numdiff.approx_hess2(test_params, self.mod.loglike, 1e-4)
            #assert_almost_equal(he, hecs, decimal=0)
            hecs = numdiff.approx_hess3(test_params, self.mod.loglike, 1e-4)
            assert_almost_equal(he, hecs, decimal=0)
Example #2
0
 def test_hess_fun1_fd(self):
     for test_params in self.params:
         #hetrue = 0
         hetrue = self.hesstrue(test_params)
         if hetrue is not None:  #Hessian does not work for 2d return of fun
             fun = self.fun()
             #default works, epsilon 1e-6 or 1e-8 is not precise enough
             hefd = numdiff.approx_hess1(
                 test_params,
                 fun,  #epsilon=1e-8,
                 # TODO: should be kwds
                 args=self.args)
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
             #TODO: I reduced precision to DEC3 from DEC4 because of
             #    TestDerivativeFun
             hefd = numdiff.approx_hess2(
                 test_params,
                 fun,  #epsilon=1e-8,
                 # TODO: should be kwds
                 args=self.args)
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
             hefd = numdiff.approx_hess3(
                 test_params,
                 fun,  #epsilon=1e-8,
                 # TODO: should be kwds
                 args=self.args)
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
Example #3
0
    def test_hess(self):
        #NOTE: I had to overwrite this to lessen the tolerance
        for test_params in self.params:
            he = self.mod.hessian(test_params)
            hefd = numdiff.approx_fprime_cs(test_params, self.mod.score)
            assert_almost_equal(he, hefd, decimal=DEC8)

            #NOTE: notice the accuracy below and the epsilon changes
            # this does not work well for score -> hessian with non-cs step
            # it's a little better around the optimum
            assert_almost_equal(he, hefd, decimal=7)
            hefd = numdiff.approx_fprime(test_params,
                                         self.mod.score,
                                         centered=True)
            assert_almost_equal(he, hefd, decimal=4)
            hefd = numdiff.approx_fprime(test_params,
                                         self.mod.score,
                                         1e-9,
                                         centered=False)
            assert_almost_equal(he, hefd, decimal=2)

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

            hecs = numdiff.approx_hess_cs(test_params, self.mod.loglike)
            assert_almost_equal(he, hecs, decimal=5)
            #NOTE: these just do not work well
            #hecs = numdiff.approx_hess1(test_params, self.mod.loglike, 1e-3)
            #assert_almost_equal(he, hecs, decimal=1)
            #hecs = numdiff.approx_hess2(test_params, self.mod.loglike, 1e-4)
            #assert_almost_equal(he, hecs, decimal=0)
            hecs = numdiff.approx_hess3(test_params, self.mod.loglike, 1e-4)
            assert_almost_equal(he, hecs, decimal=0)
Example #4
0
    def test_hess(self):
        for test_params in self.params:
            he = self.mod.hessian(test_params)
            hefd = numdiff.approx_fprime_cs(test_params, self.mod.score)
            assert_almost_equal(he, hefd, decimal=DEC8)

            #NOTE: notice the accuracy below
            assert_almost_equal(he, hefd, decimal=7)
            hefd = numdiff.approx_fprime(test_params,
                                         self.mod.score,
                                         centered=True)
            assert_allclose(he, hefd, rtol=1e-9)
            hefd = numdiff.approx_fprime(test_params,
                                         self.mod.score,
                                         centered=False)
            assert_almost_equal(he, hefd, decimal=4)

            hescs = numdiff.approx_fprime_cs(test_params.ravel(),
                                             self.mod.score)
            assert_allclose(he, hescs, rtol=1e-13)

            hecs = numdiff.approx_hess_cs(test_params.ravel(),
                                          self.mod.loglike)
            assert_allclose(he, hecs, rtol=1e-9)

            #NOTE: Look at the lack of precision - default epsilon not always
            #best
            grad = self.mod.score(test_params)
            hecs, gradcs = numdiff.approx_hess1(test_params,
                                                self.mod.loglike,
                                                1e-6,
                                                return_grad=True)
            assert_almost_equal(he, hecs, decimal=1)
            assert_almost_equal(grad, gradcs, decimal=1)
            hecs, gradcs = numdiff.approx_hess2(test_params,
                                                self.mod.loglike,
                                                1e-4,
                                                return_grad=True)
            assert_almost_equal(he, hecs, decimal=3)
            assert_almost_equal(grad, gradcs, decimal=1)
            hecs = numdiff.approx_hess3(test_params, self.mod.loglike, 1e-5)
            assert_almost_equal(he, hecs, decimal=4)
Example #5
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_hess1(test_params, fun, #epsilon=1e-8,
                                          # TODO: should be kwds
                                          args=self.args)
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
             #TODO: I reduced precision to DEC3 from DEC4 because of
             #    TestDerivativeFun
             hefd = numdiff.approx_hess2(test_params, fun, #epsilon=1e-8,
                                          # TODO: should be kwds
                                          args=self.args)
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
             hefd = numdiff.approx_hess3(test_params, fun, #epsilon=1e-8,
                                          # TODO: should be kwds
                                          args=self.args)
             assert_almost_equal(hetrue, hefd, decimal=DEC3)
Example #6
0
    def test_hess(self):
        for test_params in self.params:
            he = self.mod.hessian(test_params)
            hefd = numdiff.approx_fprime_cs(test_params, self.mod.score)
            assert_almost_equal(he, hefd, decimal=DEC8)

            #NOTE: notice the accuracy below
            assert_almost_equal(he, hefd, decimal=7)
            hefd = numdiff.approx_fprime(test_params, self.mod.score,
                                         centered=True)
            assert_allclose(he, hefd, rtol=1e-9)
            hefd = numdiff.approx_fprime(test_params, self.mod.score,
                                         centered=False)
            assert_almost_equal(he, hefd, decimal=4)

            hescs = numdiff.approx_fprime_cs(test_params.ravel(),
                                                        self.mod.score)
            assert_allclose(he, hescs, rtol=1e-13)

            hecs = numdiff.approx_hess_cs(test_params.ravel(),
                                                        self.mod.loglike)
            assert_allclose(he, hecs, rtol=1e-9)

            #NOTE: Look at the lack of precision - default epsilon not always
            #best
            grad = self.mod.score(test_params)
            hecs, gradcs = numdiff.approx_hess1(test_params, self.mod.loglike,
                                              1e-6, return_grad=True)
            assert_almost_equal(he, hecs, decimal=1)
            assert_almost_equal(grad, gradcs, decimal=1)
            hecs, gradcs = numdiff.approx_hess2(test_params, self.mod.loglike,
                                1e-4, return_grad=True)
            assert_almost_equal(he, hecs, decimal=3)
            assert_almost_equal(grad, gradcs, decimal=1)
            hecs = numdiff.approx_hess3(test_params, self.mod.loglike, 1e-5)
            assert_almost_equal(he, hecs, decimal=4)
Example #7
0
           ((y==1) * (norm.cdf(c1 - xb)))  +        
           ((y==2) * (norm.cdf(c2 - xb) - norm.cdf(c1 - xb)))  +
           ((y==3) * (norm.cdf(c3 - xb) - norm.cdf(c2 - xb)))  +
           ((y==4) * (norm.cdf(c4 - xb) - norm.cdf(c3- xb)))  +
           ((y==5) * (1-norm.cdf(c4- xb)))
           ))
   return -llf
   
   
probit_est = minimize(OrderProbit5, bhats, method='nelder-mead', options={'maxiter':500000, 'maxfev':500000})

log_likelihood = -probit_est['fun'] #log likelihood

#Find CRLB
b_estimates = probit_est['x']
Hessian = smt.approx_hess3(b_estimates, OrderProbit5)
invHessian = np.linalg.inv(Hessian)

#Standard Errors from CRLB
SE = np.array([])
for i in range(0, len(invHessian)):
    SE = np.append(SE, np.sqrt(invHessian[i,i]))
#t and p values(b_estimates/SE)
t_statistics = (b_estimates/SE)
pval = (sc.stats.t.sf(np.abs(t_statistics), 999)*2)

def intercept(betas):
    return np.sum((y-betas)**2)

inter = np.array([0.5])
inter_est = minimize(intercept, inter)