Ejemplo n.º 1
0
    def solve_approx(self):

        self.solve()
        self.setup_sampler()
        p = self.inactive.sum()
        self.feasible_point = self.observed_scaling
        self._overall = np.zeros(p, dtype=bool)
        #print(self.selection_variable['variables'])
        self._overall[self.selection_variable['variables']] = 1

        self.observed_opt_state = np.hstack(
            [self.observed_scaling, self.observed_subgradients])

        _opt_linear_term = np.concatenate((np.atleast_2d(
            self.maximizing_subgrad).T, self.losing_padding_map), 1)
        self._opt_linear_term = np.concatenate(
            (_opt_linear_term[self._overall, :],
             _opt_linear_term[~self._overall, :]), 0)

        self.opt_transform = (self._opt_linear_term, np.zeros(p))

        (self._score_linear_term, _) = self.score_transform

        self.inactive_lagrange = self.observed_scaling * self.penalty.weights[
            0] * np.ones(p - 1)

        X, _ = self.loss.data
        n, p = X.shape
        self.p = p
        bootstrap_score = pairs_bootstrap_glm(self.loss,
                                              self.active,
                                              inactive=~self.active)[0]

        bootstrap_target, target_observed = pairs_bootstrap_glm(self.loss,
                                                                self._overall,
                                                                beta_full=None,
                                                                inactive=None)

        sampler = lambda: np.random.choice(n, size=(n, ), replace=True)
        self.target_cov, target_score_cov = bootstrap_cov(
            sampler, bootstrap_target, cross_terms=(bootstrap_score, ))
        self.score_target_cov = np.atleast_2d(target_score_cov).T
        self.target_observed = target_observed

        nactive = self._overall.sum()
        self.nactive = nactive

        self.B_active = self._opt_linear_term[:nactive, :nactive]
        self.B_inactive = self._opt_linear_term[nactive:, :nactive]
    def setup_sampler(self,
                      score_mean,
                      scaling=1,
                      solve_args={
                          'min_its': 20,
                          'tol': 1.e-10
                      }):

        X, _ = self.loss.data
        n, p = X.shape
        bootstrap_score = pairs_bootstrap_glm(self.loss,
                                              self._overall,
                                              beta_full=self._beta_full,
                                              inactive=~self._overall)[0]

        score_cov = bootstrap_cov(
            lambda: np.random.choice(n, size=(n, ), replace=True),
            bootstrap_score)
        #score_cov = np.zeros((p,p))
        #X_E = X[:, self._active_groups]
        #X_minusE = X[:, ~self._active_groups]
        #score_cov[:self._active_groups.sum(), :self._active_groups.sum()] = np.linalg.inv(np.dot(X_E.T, X_E))
        #residual_mat = np.identity(n)-np.dot(X_E, np.linalg.pinv(X_E))
        #score_cov[self._active_groups.sum():, self._active_groups.sum():] = np.dot(X_minusE.T, np.dot(residual_mat, X_minusE))

        self.score_cov = score_cov
        self.score_cov_inv = np.linalg.inv(self.score_cov)

        #self.score_mat = -self.score_transform[0]
        #self.score_mat_inv = np.linalg.inv(self.score_mat)
        #self.total_cov = np.dot(self.score_mat, self.score_cov).dot(self.score_mat.T)
        #self.total_cov_inv = np.linalg.inv(self.total_cov)
        self.reference = score_mean
Ejemplo n.º 3
0
    def solve_approx(self):

        self.solve()

        (_opt_linear_term, _opt_affine_term) = self.opt_transform
        self._opt_linear_term = np.concatenate((_opt_linear_term[self._overall, :], _opt_linear_term[~self._overall, :]), 0)
        self._opt_affine_term = np.concatenate((_opt_affine_term[self._overall], _opt_affine_term[~self._overall]), 0)

        self.opt_transform = (self._opt_linear_term, self._opt_affine_term)

        (_score_linear_term, _) = self.score_transform

        self._score_linear_term = np.concatenate((_score_linear_term[self._overall, :], _score_linear_term[~self._overall, :]), 0)
        self.score_transform = (self._score_linear_term, np.zeros(self._score_linear_term.shape[0]))

        self.feasible_point = np.append(self.observed_score_state, np.abs(self.initial_soln[self._overall]))

        lagrange = self.penalty._weight_array

        self.inactive_lagrange = lagrange[~self._overall]

        X, _ = self.loss.data
        n, p = X.shape
        self.p = p
        nactive = self._overall.sum()
        self.nactive = nactive

        self.target_observed = self.observed_score_state[:self.nactive]

        if self.estimation == 'parametric':
            score_cov = np.zeros((p,p))
            inv_X_active = np.linalg.inv(X[:, self._overall].T.dot(X[:, self._overall]))
            projection_X_active = X[:,self._overall].dot(np.linalg.inv(X[:, self._overall].T.dot(X[:, self._overall]))).dot(X[:,self._overall].T)
            score_cov[:self.nactive, :self.nactive] = inv_X_active
            score_cov[self.nactive:, self.nactive:] = X[:,~self._overall].T.dot(np.identity(n)- projection_X_active).dot(X[:,~self._overall])

        elif self.estimation == 'bootstrap':
            bootstrap_score = pairs_bootstrap_glm(self.loss,
                                              self._overall,
                                              beta_full=self._beta_full,
                                              inactive=~self._overall)[0]
            score_cov = bootstrap_cov(lambda: np.random.choice(n, size=(n,), replace=True), bootstrap_score)

        self.score_cov = score_cov
        self.target_cov = score_cov[:nactive, :nactive]
        self.score_cov_inv = np.linalg.inv(self.score_cov)


        self.B = self._opt_linear_term
        self.A = self._score_linear_term

        self.B_active = self.B[:nactive, :nactive]
        self.B_inactive = self.B[nactive:, :nactive]

        self.A_active = self._score_linear_term[:nactive, :]
        self.A_inactive = self._score_linear_term[nactive:, :]

        self.offset_active = self._opt_affine_term[:nactive]
    def solve_approx(self):
        self.solve()
        self.setup_sampler()
        #print("boundary", self.observed_opt_state, self.boundary)
        #self.feasible_point = self.observed_opt_state[self.boundary]
        self.observed_score_state = self.observed_internal_state

        self.feasible_point = np.ones(self.boundary.sum())
        (_opt_linear_term, _opt_offset) = self.opt_transform
        print("shapes", _opt_linear_term[self.boundary, :].shape,
              _opt_linear_term[self.interior, :].shape)
        self._opt_linear_term = np.concatenate(
            (_opt_linear_term[self.boundary, :],
             _opt_linear_term[self.interior, :]), 0)
        self._opt_affine_term = np.concatenate(
            (_opt_offset[self.boundary], _opt_offset[self.interior]), 0)
        self.opt_transform = (self._opt_linear_term, self._opt_affine_term)

        (_score_linear_term, _) = self.score_transform
        self._score_linear_term = np.concatenate(
            (_score_linear_term[self.boundary, :],
             _score_linear_term[self.interior, :]), 0)
        self.score_transform = (self._score_linear_term,
                                np.zeros(self._score_linear_term.shape[0]))
        self._overall = self.boundary
        self.inactive_lagrange = self.threshold[0] * np.ones(
            np.sum(~self.boundary))

        X, _ = self.loss.data
        n, p = X.shape
        self.p = p
        bootstrap_score = pairs_bootstrap_glm(self.loss,
                                              self._overall,
                                              beta_full=self._beta_full,
                                              inactive=~self._overall)[0]

        score_cov = bootstrap_cov(
            lambda: np.random.choice(n, size=(n, ), replace=True),
            bootstrap_score)
        nactive = self._overall.sum()
        self.score_target_cov = score_cov[:, :nactive]
        self.target_cov = score_cov[:nactive, :nactive]
        self.target_observed = self.observed_score_state[:nactive]
        self.nactive = nactive

        self.B_active = self._opt_linear_term[:nactive, :nactive]
        self.B_inactive = self._opt_linear_term[nactive:, :nactive]
Ejemplo n.º 5
0
    def solve_approx(self):
        self.solve()
        (_opt_linear_term, _opt_affine_term) = self.opt_transform
        self._opt_linear_term = np.concatenate(
            (_opt_linear_term[self._overall, :],
             _opt_linear_term[~self._overall, :]), 0)
        self._opt_affine_term = np.concatenate(
            (_opt_affine_term[self._overall],
             _opt_affine_term[~self._overall]), 0)
        self.opt_transform = (self._opt_linear_term, self._opt_affine_term)

        (_score_linear_term, _) = self.score_transform
        self._score_linear_term = np.concatenate(
            (_score_linear_term[self._overall, :],
             _score_linear_term[~self._overall, :]), 0)
        self.score_transform = (self._score_linear_term,
                                np.zeros(self._score_linear_term.shape[0]))
        self.feasible_point = np.abs(self.initial_soln[self._overall])
        lagrange = []
        for key, value in self.penalty.weights.iteritems():
            lagrange.append(value)
        lagrange = np.asarray(lagrange)
        self.inactive_lagrange = lagrange[~self._overall]

        X, _ = self.loss.data
        n, p = X.shape
        self.p = p
        bootstrap_score = pairs_bootstrap_glm(self.loss,
                                              self._overall,
                                              beta_full=self._beta_full,
                                              inactive=~self._overall)[0]

        score_cov = bootstrap_cov(
            lambda: np.random.choice(n, size=(n, ), replace=True),
            bootstrap_score)
        nactive = self._overall.sum()
        self.score_target_cov = score_cov[:, :nactive]
        self.target_cov = score_cov[:nactive, :nactive]
        self.target_observed = self.observed_score_state[:nactive]
        self.nactive = nactive

        self.B_active = self._opt_linear_term[:nactive, :nactive]
        self.B_inactive = self._opt_linear_term[nactive:, :nactive]
Ejemplo n.º 6
0
    def solve_approx(self):

        self.solve()

        (_opt_linear_term, _opt_affine_term) = self.opt_transform
        self._opt_linear_term = np.concatenate(
            (_opt_linear_term[self._overall, :],
             _opt_linear_term[~self._overall, :]), 0)
        self._opt_affine_term = np.concatenate(
            (_opt_affine_term[self._overall],
             _opt_affine_term[~self._overall]), 0)

        self.opt_transform = (self._opt_linear_term, self._opt_affine_term)

        (_score_linear_term, _) = self.score_transform

        self._score_linear_term = np.concatenate(
            (_score_linear_term[self._overall, :],
             _score_linear_term[~self._overall, :]), 0)
        self.score_transform = (self._score_linear_term,
                                np.zeros(self._score_linear_term.shape[0]))

        self.feasible_point = np.append(
            self.observed_score_state,
            np.abs(self.initial_soln[self._overall]))

        lagrange = []
        for key, value in self.penalty.weights.iteritems():
            lagrange.append(value)
        lagrange = np.asarray(lagrange)

        self.inactive_lagrange = lagrange[~self._overall]

        X, _ = self.loss.data
        n, p = X.shape
        self.p = p
        nactive = self._overall.sum()
        self.nactive = nactive

        self.target_observed = self.observed_score_state[:self.nactive]

        if self.estimation == 'parametric':
            score_cov = np.zeros((p, p))

            vec = np.exp(X[:, self._overall].dot(self.target_observed))
            #vec = np.exp(np.zeros(n))
            pi = np.true_divide(vec, np.power(1. + vec, 2))
            weights = np.diag(pi)
            Q_active = X[:, self._overall].T.dot(weights).dot(X[:,
                                                                self._overall])
            Q_active_inv = np.linalg.inv(Q_active)

            P_inactive = X[:, ~self._overall].T.dot(
                np.identity(n) - weights.dot(X[:, self._overall].dot(
                    Q_active_inv).dot(X[:, self._overall].T)))
            score_cov[:self.nactive, :self.nactive] = Q_active_inv
            score_cov[self.nactive:,
                      self.nactive:] = P_inactive.dot(weights).dot(
                          P_inactive.T)

        elif self.estimation == 'bootstrap':
            bootstrap_score = pairs_bootstrap_glm(self.loss,
                                                  self._overall,
                                                  beta_full=self._beta_full,
                                                  inactive=~self._overall)[0]
            score_cov = bootstrap_cov(
                lambda: np.random.choice(n, size=(n, ), replace=True),
                bootstrap_score)

        self.score_cov = score_cov
        self.target_cov = score_cov[:nactive, :nactive]
        self.score_cov_inv = np.linalg.inv(self.score_cov)

        self.B = self._opt_linear_term
        self.A = self._score_linear_term

        self.B_active = self.B[:nactive, :nactive]
        self.B_inactive = self.B[nactive:, :nactive]

        self.A_active = self._score_linear_term[:nactive, :]
        self.A_inactive = self._score_linear_term[nactive:, :]

        self.offset_active = self._opt_affine_term[:nactive]