Exemplo n.º 1
0
    def __init__(self,
                 num_vars,
                 rank,
                 num_qoi,
                 obs=None,
                 noise_covariance=None,
                 Amatrix=None):
        self.num_vars = num_vars
        self.rank = rank
        self.num_qoi = num_qoi
        if Amatrix is None:
            self.Amatrix = get_low_rank_matrix(num_qoi, num_vars, rank)
        else:
            self.Amatrix = Amatrix

        if obs is None:
            self.obs = np.zeros(num_qoi)
        else:
            self.obs = obs
        if noise_covariance is None:
            self.noise_covariance = np.eye(num_qoi)
        else:
            self.noise_covariance = noise_covariance

        self.noise_covariance_inv = np.linalg.inv(self.noise_covariance)
Exemplo n.º 2
0
    def test_prior_conditioned_misfit_covariance_operator(self):
        num_dims = 3
        rank = 2
        num_qoi = 2

        # define prior
        prior_mean = np.zeros((num_dims), float)
        prior_covariance = np.eye(num_dims)
        prior_hessian = np.eye(num_dims)
        prior_density = NormalDensity(prior_mean, covariance=prior_covariance)

        # define observations
        noise_sigma2 = 0.5
        noise_covariance = np.eye(num_qoi)*noise_sigma2
        noise_covariance_inv = np.linalg.inv(noise_covariance)
        noise_covariance_chol_factor = np.linalg.cholesky(noise_covariance)
        truth_sample = prior_density.generate_samples(1)[:, 0]
        Amatrix = get_low_rank_matrix(num_qoi, num_dims, rank)
        noise = np.dot(
            noise_covariance_chol_factor, np.random.normal(0., 1., num_qoi))
        obs = np.dot(Amatrix, truth_sample)+noise

        # define mistit model
        misfit_model = QuadraticMisfitModel(
            num_dims, rank, num_qoi, obs, noise_covariance, Amatrix)

        # Get analytical mean and covariance
        exact_laplace_mean, exact_laplace_covariance = \
            laplace_posterior_approximation_for_linear_models(
                misfit_model.Amatrix, prior_mean, prior_hessian,
                noise_covariance_inv, obs)

        objective = LogUnormalizedPosterior(
            misfit_model, misfit_model.gradient_set, prior_density.pdf,
            prior_density.log_pdf, prior_density.log_pdf_gradient)
        map_point, obj_min = find_map_point(objective, prior_mean)
        sample = map_point
        assert np.allclose(objective.gradient(sample), np.zeros(num_dims))
        prior_covariance_sqrt_operator = CholeskySqrtCovarianceOperator(
            prior_density.covariance)
        misfit_hessian_operator = MisfitHessianVecOperator(
            misfit_model, sample, fd_eps=1e-7)
        standard_svd_opts = {
            'num_singular_values': rank, 'num_extra_samples': 10}
        svd_opts = {'single_pass': False, 'standard_opts': standard_svd_opts}
        laplace_covariance_sqrt = get_laplace_covariance_sqrt_operator(
            prior_covariance_sqrt_operator, misfit_hessian_operator,
            svd_opts)

        identity = np.eye(num_dims)
        laplace_covariance_chol_factor = laplace_covariance_sqrt.apply(
            identity)
        laplace_covariance = np.dot(
            laplace_covariance_chol_factor, laplace_covariance_chol_factor.T)
        # print laplace_covariance
        # print exact_laplace_covariance
        assert np.allclose(laplace_covariance, exact_laplace_covariance)
Exemplo n.º 3
0
def setup_quadratic_misfit_problem(prior, rank, noise_sigma2=1):
    # Define observations
    num_qoi = 2 * rank
    #assert num_qoi>=rank

    noise_covariance = np.eye(num_qoi) * noise_sigma2
    noise_covariance_inv = np.linalg.inv(noise_covariance)
    # In high dimensions computing cholesky factor is too expensive.
    # That is why we use PDE based operator
    noise_covariance_chol_factor = np.linalg.cholesky(noise_covariance)
    truth_sample = prior.generate_samples(1)[:, 0]
    num_vars = truth_sample.shape[0]
    Amatrix = get_low_rank_matrix(num_qoi, num_vars, rank)
    noise = np.dot(noise_covariance_chol_factor,
                   np.random.normal(0., noise_sigma2, num_qoi))
    obs = np.dot(Amatrix, truth_sample) + noise

    # Define mistit model
    misfit_model = QuadraticMisfitModel(num_vars, rank, num_qoi, Amatrix)

    return misfit_model, noise_covariance_inv, obs