示例#1
0
 def get_fisher(self, initial_state=fm.REP_COVAR, silent=True):
     """Get FisherMatrix object for the covariance matrix computed by the basis."""
     if initial_state == fm.REP_FISHER:
         return fm.FisherMatrix(self.get_fisher_array(),
                                input_type=fm.REP_FISHER,
                                initial_state=fm.REP_FISHER,
                                silent=silent)
     elif initial_state == fm.REP_COVAR:
         return fm.FisherMatrix(self.get_covar_array(),
                                input_type=fm.REP_COVAR,
                                initial_state=fm.REP_COVAR,
                                silent=silent)
     elif initial_state == fm.REP_CHOL:
         return fm.FisherMatrix(self.get_cov_cholesky_array(),
                                input_type=fm.REP_CHOL,
                                initial_state=fm.REP_CHOL,
                                silent=silent)
     else:
         return fm.FisherMatrix(self.get_covar_array(),
                                input_type=fm.REP_COVAR,
                                initial_state=initial_state,
                                silent=silent)
示例#2
0
def fisher_params(request, fisher_input):
    """iterate thorugh fisher matrix starting params and fisher matrices"""
    if request.param[0] == fm.REP_FISHER:
        internal_mat = fisher_input.fab.copy('F')
    elif request.param[0] == fm.REP_COVAR:
        internal_mat = fisher_input.cov.copy('F')
    elif request.param[0] == fm.REP_CHOL:
        internal_mat = fisher_input.chol_cov.copy('F')
    elif request.param[0] == fm.REP_CHOL_INV:
        internal_mat = fisher_input.chol_cov_i.copy('F')
    fisher = fm.FisherMatrix(internal_mat,
                             input_type=request.param[0],
                             initial_state=request.param[1])
    return FisherWithManual(fisher_input, fisher)
示例#3
0
 def get_sw_fisher(self, f_spec, fisher_from_lw):
     """ helper for get_fisher, get a sw fisher with given projected lw matrix
         with or without gaussian and nongaussian components"""
     if fisher_from_lw is None:
         if f_spec['sw_g'] or f_spec['sw_ng']:
             sw_result = fm.FisherMatrix(np.zeros((self.n_sw, self.n_sw)),
                                         fm.REP_COVAR,
                                         silent=True)
         else:
             return None
     else:
         sw_result = copy.deepcopy(fisher_from_lw)
     if f_spec['sw_g']:
         sw_result.add_covar(self.sw_g_covar)
     if f_spec['sw_ng']:
         sw_result.add_covar(self.sw_ng_covar)
     return sw_result
示例#4
0
    def __init__(self, de_model, params, fisher_in=None, labels_in=None):
        """ de_model: 'jdem','constant_w','w0wa',or 'none'
            params:
                row_strip: indexes of rows to remove from source file
                fisher_source: source file
                n_full: how many total params there are
                n_row: how many de params there are
                z_step: z spacing of de params for jdem
            fisher_in: if not None, use this matrix instead of reading from file
            labels_in: array of labels for the axis elements"""
        self.params = params
        self.de_model = de_model
        #read in matrix
        if fisher_in is None:
            self.unprocessed_mat = read_prior_fisher(self.params)
            self.unprocessed_labels = JDEM_LABELS
        else:
            self.unprocessed_mat = fisher_in
            if labels_in is None:
                raise ValueError('labels_in must be set if fisher_in is set')
            self.unprocessed_labels = labels_in

        #strip unwanted rows
        self.stripped_mat, self.stripped_labels = fix_elements(
            self.unprocessed_mat, params, self.unprocessed_labels)

        #project to correct de model
        if de_model == 'jdem':
            self.processed_mat = self.stripped_mat
            self.processed_labels = self.stripped_labels.copy()
        elif de_model == 'constant_w':
            self.processed_mat, self.processed_labels = project_w0(
                self.stripped_mat, params, self.stripped_labels)
        elif de_model == 'w0wa':
            self.processed_mat, self.processed_labels = project_w0wa(
                self.stripped_mat, params, self.stripped_labels)
        elif de_model == 'none':
            self.processed_mat, self.processed_labels = project_no_de(
                self.stripped_mat, params, self.stripped_labels)
        else:
            raise ValueError('unrecognized de_model ' + str(de_model))

        self.fisher_matrix = fm.FisherMatrix(self.processed_mat,
                                             input_type=fm.REP_FISHER,
                                             initial_state=fm.REP_FISHER,
                                             fix_input=True)
示例#5
0
f_set_lihu = np.zeros(3, dtype=object)
f_set_mat_jdem1 = np.zeros(3, dtype=object)
for i in range(0, 3):
    f_set_mat_jdem1[i] = f_set[i][2].get_fisher()
    f_set_lihu_prior[i] = np.zeros(3, dtype=object)
    f_set_lihu[i] = np.zeros(3, dtype=object)
    f_set_jdem_prior[i] = np.zeros(3, dtype=object)

C = CosmoPie(cosmo.copy(), 'jdem')
f_set_mat_lihu1 = rotate_jdem_to_lihu(deepcopy(f_set_mat_jdem1), C)
f_set_mat_lihu_h_prior = deepcopy(f_set_mat_lihu1)
f_set_mat_lihu_h_prior[0][3, 3] += 1.e4
f_set_mat_lihu_h_prior[1][3, 3] += 1.e4
f_set_mat_lihu_h_prior[2][3, 3] += 1.e4
f_set_mat_jdem_h_prior = deepcopy(
    rotate_lihu_to_jdem(f_set_mat_lihu_h_prior, C))
for i in range(0, 3):
    f_set_lihu_prior[i][2] = fm.FisherMatrix(f_set_mat_lihu_h_prior[i],
                                             fm.REP_FISHER)
    f_set_lihu[i][2] = fm.FisherMatrix(f_set_mat_lihu1[i], fm.REP_FISHER)
    f_set_jdem_prior[i][2] = fm.FisherMatrix(f_set_mat_jdem_h_prior[i],
                                             fm.REP_FISHER)
fig3 = make_standard_ellipse_plot(f_set_jdem_prior,
                                  cosmo_par_list,
                                  fontsize=12,
                                  labelsize=6,
                                  fontsize_legend=10,
                                  left_space=0.08,
                                  bottom_space=0.06)
plt.show(fig3)
示例#6
0
    def __init__(self,
                 basis,
                 sw_survey,
                 lw_surveys,
                 prior_params,
                 needs_a=False,
                 do_mit=True):
        """
        master class for managing fisher matrix manipulations between bases
        inputs:
            basis: an LWBasis object
            sw_survey: an SWSurvey object
            lw_surveys: a list of LWSurvey objects to be combined into mitigation strategy
            prior_params: params for the prior fisher matrix to use in cosmological parameter space
        """
        print("MultiFisher: began initialization")
        self.basis = basis
        self.sw_survey = sw_survey
        self.lw_surveys = lw_surveys
        self.prior_params = prior_params
        self.needs_a = needs_a
        self.do_mit = do_mit

        #prepare to project lw basis to sw basis
        self.n_sw = self.sw_survey.get_total_dimension()

        self.lw_F_no_mit = None
        self.lw_F_mit = None
        self.lw_to_sw_array = None

        print("MultiFisher: getting projection matrices")
        self.lw_to_sw_array = self.get_lw_to_sw_array()
        self.sw_to_par_array = sw_survey.get_dO_I_dpar_array()

        #TODO eliminate from main loop
        if self.needs_a:
            print("MultiFisher: getting lw no mit variance")
            self.a_vals = np.zeros(2, dtype=object)
            self.lw_F_no_mit = self.get_lw_fisher(f_spec_SSC_no_mit,
                                                  initial_state=fm.REP_CHOL)
            self.project_lw_a = self.basis.get_ddelta_bar_ddelta_alpha(
                self.sw_survey.geo, tomography=True)
            self.a_vals[0] = self.lw_F_no_mit.project_covar(
                self.project_lw_a.T, destructive=True).get_covar()
            self.lw_F_no_mit = None
        else:
            self.a_vals = None
            self.project_lw_a = None

        #self.lw_F_no_mit = self.get_lw_fisher(f_spec_SSC_no_mit,initial_state=fm.REP_CHOL)

        print("MultiFisher: projecting lw no mit covariance")
        #self.sw_f_ssc_no_mit = self.lw_F_no_mit.project_covar(self.get_lw_to_sw_array(),destructive=False)
        vs_perturb, sigma2s_perturb = self.lw_surveys[0].observables[
            0].get_perturbing_vector()
        sw_cov_ssc, sw_cov_ssc_mit = self.basis.perturb_and_project_covar(
            vs_perturb, self.get_lw_to_sw_array(), sigma2s_perturb)
        self.sw_f_ssc_no_mit = fm.FisherMatrix(sw_cov_ssc, fm.REP_COVAR,
                                               fm.REP_COVAR)
        self.sw_f_ssc_mit = fm.FisherMatrix(sw_cov_ssc_mit, fm.REP_COVAR,
                                            fm.REP_COVAR)
        sw_cov_ssc = None
        sw_cov_ssc_mit = None
        vs_perturb = None
        sigma2s_perturb = None
        #self.sw_f_ssc_mit2 = fm.FisherMatrix(self.basis.perturb_and_project_covar(vs_perturb,self.get_lw_to_sw_array(),sigma2s_perturb),fm.REP_COVAR,fm.REP_COVAR)
        #self.lw_F_no_mit = None

        if do_mit:
            print("MultiFisher: getting lw mit covariance")
            #self.lw_F_mit = self.get_lw_fisher(f_spec_SSC_mit,initial_state=fm.REP_FISHER)
            #self.lw_F_mit = self.get_lw_fisher(f_spec_SSC_mit,initial_state=fm.REP_COVAR)

            if self.needs_a:
                print("MultiFisher: getting lw mit variance ")
                self.a_vals[1] = self.lw_F_mit.project_covar(
                    self.project_lw_a.T).get_covar()

            print("MultiFisher: projecting lw mit covariance")
            #self.sw_f_ssc_mit = self.lw_F_mit.project_covar(self.get_lw_to_sw_array(),destructive=False)
            #self.lw_F_mit = None
        else:
            self.sw_f_ssc_mit = None
        #accumulate lw covariances onto fisher_tot

        #for i in range(0,self.lw_surveys.size):
        #    self.lw_surveys[i].fisher_accumulate(self.lw_F_mit)
        #self.lw_F_mit.switch_rep(fm.REP_CHOL_INV)
        #self.lw_F_no_mit.switch_rep(fm.REP_CHOL_INV)

        #self.lw_F_mit = None
        self.lw_to_sw_array = None

        #sw covariances to add
        print("MultiFisher: getting sw covariance matrices")
        self.sw_non_SSC_covars = self.sw_survey.get_non_SSC_sw_covar_arrays()
        self.sw_g_covar = fm.FisherMatrix(self.sw_non_SSC_covars[0],
                                          fm.REP_COVAR,
                                          fm.REP_COVAR,
                                          silent=True)
        self.sw_ng_covar = fm.FisherMatrix(self.sw_non_SSC_covars[1],
                                           fm.REP_COVAR,
                                           fm.REP_COVAR,
                                           silent=True)

        if self.sw_survey.C.p_space == 'jdem':
            self.fisher_prior_obj = prior_fisher.PriorFisher(
                self.sw_survey.C.de_model, self.prior_params)
            self.fisher_priors = self.fisher_prior_obj.get_fisher()
        else:
            warn('Current priors do not support p_space ' +
                 str(self.sw_survey.C.p_space) + ', defaulting to 0 priors')
            self.fisher_prior_obj = None
            self.fisher_priors = fm.FisherMatrix(np.zeros(
                (self.sw_to_par_array.shape[1],
                 self.sw_to_par_array.shape[1])),
                                                 fm.REP_FISHER,
                                                 fm.REP_FISHER,
                                                 silent=True)

        print("MultiFisher: finished initialization")
示例#7
0
def test_get_eig_metric_rand(fisher_params):
    """test get_cov_eig_metric"""
    cov = fisher_params.fisher_input.cov.copy()
    fisher = copy.deepcopy(fisher_params.fisher)
    metric_mat = np.random.rand(cov.shape[0], cov.shape[1])
    metric_mat = np.dot(metric_mat.T, metric_mat)
    metric_mat_inv = np.linalg.pinv(metric_mat)
    metric = fm.FisherMatrix(metric_mat,
                             input_type=fm.REP_COVAR,
                             initial_state=fm.REP_COVAR)

    eigs_1 = fisher.get_cov_eig_metric(metric)
    atol_loc = np.max(np.abs(eigs_1[0] - 1.)) * atol_rel_use

    m_mat = np.dot(cov, metric_mat_inv)
    eigs_3 = spl.eig(m_mat)
    #check matrix is positive semidefinite
    assert np.all(eigs_3[0] > 0.)
    assert np.all(np.abs(np.imag(eigs_3[0] - 1.))[::-1] < atol_loc)
    #check eigenvalues match
    assert np.allclose(np.sort(eigs_1[0] - 1.),
                       np.sort(np.real(eigs_3[0] - 1.)),
                       atol=atol_loc,
                       rtol=rtol_use)

    chol_metric = spl.cholesky(metric_mat, lower=True)
    chol_inv_metric = npl.pinv(chol_metric)
    alt_mat = np.dot(chol_inv_metric, np.dot(cov, chol_inv_metric.T))
    #u vectors
    eigvs_4 = np.dot(chol_inv_metric, eigs_3[1])
    #v vectors
    eigvs_5 = np.dot(chol_metric, eigs_1[1])

    #check eigenvalues work
    assert np.allclose(np.dot(m_mat, eigs_3[1]),
                       eigs_3[0] * eigs_3[1],
                       atol=atol_loc,
                       rtol=rtol_use)
    assert np.allclose(np.dot(alt_mat, eigs_1[1]),
                       eigs_1[0] * eigs_1[1],
                       atol=atol_loc,
                       rtol=rtol_use)
    assert np.allclose(np.identity(cov.shape[0]),
                       np.dot(eigs_1[1].T, eigs_1[1]),
                       atol=atol_loc,
                       rtol=rtol_use)
    p_mat0 = np.dot(eigs_3[1].T, np.dot(metric_mat_inv, eigs_3[1]))
    p_mat0_use = p_mat0 - np.diag(np.diag(p_mat0))
    assert np.allclose(np.zeros(cov.shape),
                       p_mat0_use,
                       atol=atol_loc,
                       rtol=rtol_use)

    #check cholesky transforms eigenvalues as expected
    p_mat1 = np.dot(eigvs_4.T, eigvs_4)
    p_mat1_use = p_mat1 - np.diag(np.diag(p_mat1))
    assert np.allclose(np.zeros(cov.shape),
                       p_mat1_use,
                       atol=atol_loc,
                       rtol=rtol_use)
    assert np.allclose(np.dot(alt_mat, eigvs_4), (eigs_3[0] * eigvs_4),
                       atol=atol_loc,
                       rtol=rtol_use)
    p_mat2 = np.dot(eigvs_5.T, np.dot(metric_mat_inv, eigvs_5))
    p_mat2_use = p_mat2 - np.diag(np.diag(p_mat2))
    assert np.allclose(np.zeros(cov.shape),
                       p_mat2_use,
                       atol=atol_loc,
                       rtol=rtol_use)
    assert np.allclose(np.dot(m_mat, eigvs_5),
                       eigs_1[0] * eigvs_5,
                       atol=atol_loc,
                       rtol=rtol_use)

    #test eig product matches identity
    eigs_1_prod = np.product(eigs_1[0])
    eigs_1_prod_true = np.linalg.det(cov) / np.linalg.det(metric_mat)
    assert np.isclose(eigs_1_prod, eigs_1_prod_true)
示例#8
0
文件: Dn.py 项目: mcdigman/SuperSCRAM
 def get_fisher(self):
     """get the fisher matrix, cholesky is sqrt"""
     Nab_f = fm.FisherMatrix(np.diagflat(np.sqrt(self.Nab_i)),
                             input_type=fm.REP_CHOL_INV)
     return Nab_f.project_fisher(self.vs)