Exemplo n.º 1
0
 def eval_nllh(beta):
     llh = l2_clogistic_llh(X=self.solver.grouping_designs["judge"],
                            Y=self.solver.response,
                            alpha=alpha,
                            beta=beta,
                            offset=None,
                            penalty_matrix=self.solver.sparse_inv_covs["judge"].sparse_matrix)
     return -1 * llh
Exemplo n.º 2
0
 def eval_nllh(alpha_beta):
     alpha = alpha_beta[:4]
     beta = alpha_beta[4:]
     llh = l2_clogistic_llh(X=self.solver.main_design,
                            Y=self.solver.response,
                            alpha=alpha,
                            beta=beta,
                            offset=None,
                            penalty_matrix=PI)
     return -1 * llh
Exemplo n.º 3
0
    def test_gradient_ranef(self, h=1e-6):
        " Use finite differences to test gradient of random effects "

        grad = \
            l2_clogistic_gradient(X=self.solver.grouping_designs["judge"],
                                  Y=self.solver.response,
                                  alpha=self.results["intercepts"],
                                  beta=self.results["judge"]["intercept"].values,
                                  penalty_matrix=self.solver.sparse_inv_covs["judge"].sparse_matrix,
                                  # adjust for main effects
                                  offset=self.solver.main_design.dot(self.results["main"]["main_value"].values))
        # this function includes derivatives of the intercepts, which we checked above
        # so restrict to just the random effects
        grad = grad[4:]

        for i in xrange(len(grad)):
            delta = np.zeros(len(grad))
            delta[i] = h

            grad_fwd = \
                l2_clogistic_llh(X=self.solver.grouping_designs["judge"],
                                 Y=self.solver.response,
                                 alpha=self.results["intercepts"],
                                 beta=self.results["judge"]["intercept"].values + delta,
                                 penalty_matrix=self.solver.sparse_inv_covs["judge"].sparse_matrix,
                                 offset=self.solver.main_design.dot(self.results["main"]["main_value"].values)
                                 )
            grad_bwd = \
                l2_clogistic_llh(X=self.solver.grouping_designs["judge"],
                                 Y=self.solver.response,
                                 alpha=self.results["intercepts"],
                                 beta=self.results["judge"]["intercept"].values - delta,
                                 penalty_matrix=self.solver.sparse_inv_covs["judge"].sparse_matrix,
                                 offset=self.solver.main_design.dot(self.results["main"]["main_value"].values)
                                 )
            grad_fd = (grad_fwd - grad_bwd) / (2 * h)
            self.assertAlmostEqual(grad[i], grad_fd, places=6)
Exemplo n.º 4
0
    def test_gradient_fixef(self, h=1e-6):
        " Use finite differences to test gradient of main effects "

        grad = l2_clogistic_gradient(
            X=self.solver.main_design,
            Y=self.solver.response,
            intercept=True,
            beta=self.results["main"]["main_value"].values,
            alpha=self.results["intercepts"],
            # need to adjust for random effects
            offset=self.solver.grouping_designs["judge"].dot(self.results["judge"]["intercept"].values)
        )

        for i in xrange(len(grad)):
            delta = np.zeros(len(grad))
            delta[i] = h

            grad_fwd = \
                l2_clogistic_llh(X=self.solver.main_design,
                                 Y=self.solver.response,
                                 alpha=self.results["intercepts"] + delta[:4],
                                 beta=self.results["main"]["main_value"].values + delta[4:],
                                 penalty_matrix=np.zeros((2, 2)),
                                 offset=self.solver.grouping_designs["judge"].dot(
                                     self.results["judge"]["intercept"].values)
                                 )
            grad_bwd = \
                l2_clogistic_llh(X=self.solver.main_design,
                                 Y=self.solver.response,
                                 alpha=self.results["intercepts"] - delta[:4],
                                 beta=self.results["main"]["main_value"].values - delta[4:],
                                 penalty_matrix=np.zeros((2, 2)),
                                 offset=self.solver.grouping_designs["judge"].dot(
                                     self.results["judge"]["intercept"].values)
                                 )
            grad_fd = (grad_fwd - grad_bwd) / (2 * h)
            self.assertAlmostEqual(grad[i], grad_fd, places=6)