Exemplo n.º 1
0
    def fit_quasi_newton(self,
                         lamb_l2,
                         lamb_smth,
                         lamb_log,
                         w_init,
                         factr=1e7,
                         maxls=50,
                         m=10,
                         lb=-np.Inf,
                         ub=np.Inf,
                         maxiter=15000,
                         verbose=True):
        """
        """
        obj = Objective(self.sumstats, self.graph)
        obj.lamb_l2 = lamb_l2
        obj.lamb_smth = lamb_smth
        obj.lamb_log = lamb_log
        res = fmin_l_bfgs_b(func=loss_wrapper,
                            x0=np.log(w_init),
                            args=[obj],
                            factr=factr,
                            m=m,
                            maxls=maxls,
                            maxiter=maxiter,
                            approx_grad=False,
                            bounds=[(lb, ub)
                                    for _ in range(obj.graph.Delta.shape[1])])
        if maxiter >= 100:
            assert res[2]["warnflag"] == 0, "did not converge"
        self.graph.w = np.exp(res[0])

        # print update
        self.train_loss, _ = loss_wrapper(res[0], obj)
        self.train_nll = neg_log_lik_wrapper(res[0], obj)
        self.pen = self.train_loss - self.train_nll
        if verbose:
            sys.stdout.write(
                ("lambda_l2={:.7f}, "
                 "lambda={:.7f}, "
                 "alpha={:.7f}, "
                 "converged in {} iterations, "
                 "train_loss={:.7f}\n").format(lamb_l2, lamb_smth, lamb_log,
                                               res[2]["nit"], self.train_loss))
Exemplo n.º 2
0
def comp_fit_cov(feems,
                 lamb_l2,
                 lamb_smth,
                 lamb_log,
                 projection=True,
                 include_var=False,
                 ind_level=False):
    # create obj
    obj = Objective(feems.sumstats, feems.graph)
    obj.lamb_l2 = lamb_l2
    obj.lamb_smth = lamb_smth
    obj.lamb_log = lamb_log

    # update laplacian
    obj.graph.comp_lap(obj.graph.w)

    # update nll and grad
    obj.inv()
    obj.grad()

    if ind_level is True:
        # number of individuals
        n, p = obj.sumstats.data.shape

        # row index of assn matrix
        row = np.arange(n)

        # col index of assn matrix
        col = obj.graph.obs_ids

        # fill up J
        J = sp.csc_matrix(
            (np.ones(n), (row, col)),
            shape=(n, obj.graph.d))[:, obj.graph.perm_ids][:, :obj.graph.o]

        # diagonal component
        q_full_samples = 1.0 / obj.sumstats.s2 * np.ones(obj.graph.o)

        # fitted covariance
        fit_cov = J @ (obj.Linv_block['oo'] - 1 / obj.graph.d) @ J.T + np.diag(
            J @ (1. / q_full_samples))

        # empirical covariance
        emp_cov = obj.sumstats.data @ obj.sumstats.data.T / p
    else:
        # fitted covariance
        fit_cov = obj.Linv_block[
            'oo'] - 1 / obj.graph.d + obj.sumstats.q_inv_diag.toarray()

        # empirical covariance
        emp_cov = obj.sumstats.S

    # project to the space of contrast
    if projection is True:
        C = comp_contrasts(n) if ind_level is True else comp_contrasts(
            obj.graph.o)
        fit_cov = C @ fit_cov @ C.T
        emp_cov = C @ emp_cov @ C.T

    if include_var is True:
        return (np.triu(fit_cov, k=0), np.triu(emp_cov, k=0))
    else:
        return (np.triu(fit_cov, k=1), np.triu(emp_cov, k=1))