예제 #1
0
    def test_fekete_rosenblatt_interpolation(self):
        np.random.seed(2)
        degree=3

        __,__,joint_density,limits = rosenblatt_example_2d(num_samples=1)
        num_vars=len(limits)//2

        rosenblatt_opts = {'limits':limits,'num_quad_samples_1d':20}
        var_trans_1 = RosenblattTransformation(
            joint_density,num_vars,rosenblatt_opts)
        # rosenblatt maps to [0,1] but polynomials of bounded variables
        # are in [-1,1] so add second transformation for this second mapping
        var_trans_2 = define_iid_random_variable_transformation(
            uniform(),num_vars)
        var_trans = TransformationComposition([var_trans_1, var_trans_2])

        poly = PolynomialChaosExpansion()
        poly.configure({'poly_type':'jacobi','alpha_poly':0.,
                        'beta_poly':0.,'var_trans':var_trans})
        indices = compute_hyperbolic_indices(num_vars,degree,1.0)
        poly.set_indices(indices)
        
        num_candidate_samples = 10000
        generate_candidate_samples=lambda n: np.cos(
            np.random.uniform(0.,np.pi,(num_vars,n)))

        precond_func = lambda matrix, samples: christoffel_weights(matrix)
        canonical_samples, data_structures = get_fekete_samples(
            poly.canonical_basis_matrix,generate_candidate_samples,
            num_candidate_samples,preconditioning_function=precond_func)
        samples = var_trans.map_from_canonical_space(canonical_samples)
        assert np.allclose(
            canonical_samples,var_trans.map_to_canonical_space(samples))

        assert samples.max()<=1 and samples.min()>=0.

        c = np.random.uniform(0.,1.,num_vars)
        c*=20/c.sum()
        w = np.zeros_like(c); w[0] = np.random.uniform(0.,1.,1)
        genz_function = GenzFunction('oscillatory',num_vars,c=c,w=w)
        values = genz_function(samples)
        # function = lambda x: np.sum(x**2,axis=0)[:,np.newaxis]
        # values = function(samples)
        
        # Ensure coef produce an interpolant
        coef = interpolate_fekete_samples(
            canonical_samples,values,data_structures)
        poly.set_coefficients(coef)
        
        assert np.allclose(poly(samples),values)

        # compare mean computed using quadrature and mean computed using
        # first coefficient of expansion. This is not testing that mean
        # is correct because rosenblatt transformation introduces large error
        # which makes it hard to compute accurate mean from pce or quadrature
        quad_w = get_quadrature_weights_from_fekete_samples(
            canonical_samples,data_structures)
        values_at_quad_x = values[:,0]
        assert np.allclose(
            np.dot(values_at_quad_x,quad_w),poly.mean())
    def test_rosenblatt_transformation(self):

        true_samples, true_canonical_samples, joint_density, limits = \
          rosenblatt_example_2d(num_samples=10)

        num_vars = 2
        opts = {'limits': limits, 'num_quad_samples_1d': 100}
        var_trans = RosenblattTransformation(joint_density, num_vars, opts)

        samples = var_trans.map_from_canonical_space(true_canonical_samples)
        assert np.allclose(true_samples, samples)

        canonical_samples = var_trans.map_to_canonical_space(samples)
        assert np.allclose(true_canonical_samples, canonical_samples)
    def test_pickle_rosenblatt_transformation(self):
        import pickle, os
        true_samples, true_canonical_samples, joint_density, limits = \
          rosenblatt_example_2d(num_samples=10)

        num_vars = 2
        opts = {'limits': limits, 'num_quad_samples_1d': 100}
        var_trans = RosenblattTransformation(joint_density, num_vars, opts)

        filename = 'rv_trans.pkl'
        with open(filename, 'wb') as f:
            pickle.dump(var_trans, f)

        with open(filename, 'rb') as f:
            file_var_trans = pickle.load(f)

        os.remove(filename)
    def test_transformation_composition_I(self):

        np.random.seed(2)
        true_samples, true_canonical_samples, joint_density, limits = \
          rosenblatt_example_2d(num_samples=10)

        #  rosenblatt_example_2d is defined on [0,1] remap to [-1,1]
        true_canonical_samples = true_canonical_samples * 2 - 1

        num_vars = 2
        opts = {'limits': limits, 'num_quad_samples_1d': 100}
        var_trans_1 = RosenblattTransformation(joint_density, num_vars, opts)
        var_trans_2 = define_iid_random_variable_transformation(
            uniform(0, 1), num_vars)
        var_trans = TransformationComposition([var_trans_1, var_trans_2])

        samples = var_trans.map_from_canonical_space(true_canonical_samples)
        assert np.allclose(true_samples, samples)

        canonical_samples = var_trans.map_to_canonical_space(samples)
        assert np.allclose(true_canonical_samples, canonical_samples)