Example #1
0
def candidate_based_christoffel_leja_rule_1d(
        recursion_coeffs, generate_candidate_samples, num_candidate_samples,
        level, initial_points=None, growth_rule=leja_growth_rule,
        samples_filename=None, return_weights_for_all_levels=True):

    num_vars = 1
    num_leja_samples = growth_rule(level)

    leja_sequence = get_candidate_based_christoffel_leja_sequence_1d(
        num_leja_samples, recursion_coeffs, generate_candidate_samples,
        num_candidate_samples, initial_points, samples_filename)

    from pyapprox.polynomial_sampling import christoffel_weights

    def generate_basis_matrix(x):
        return evaluate_orthonormal_polynomial_deriv_1d(
            x[0, :], num_leja_samples, recursion_coeffs, deriv_order=0)

    def weight_function(x): return christoffel_weights(
        generate_basis_matrix(x))
    ordered_weights_1d = get_leja_sequence_quadrature_weights(
        leja_sequence, growth_rule, generate_basis_matrix, weight_function,
        level, return_weights_for_all_levels)

    return leja_sequence[0, :], ordered_weights_1d
Example #2
0
def beta_leja_quadrature_rule(alpha_stat, beta_stat, level,
                              growth_rule=leja_growth_rule,
                              samples_filename=None,
                              return_weights_for_all_levels=True,
                              initial_points=None):
    """
    Return the samples and weights of the Leja quadrature rule for the beta
    probability measure. 

    By construction these rules have polynomial ordering.

    Parameters
    ----------
    level : integer
        The level of the isotropic sparse grid.

    alpha_stat : integer
        The alpha shape parameter of the Beta distribution

    beta_stat : integer
        The beta shape parameter of the Beta distribution

    samples_filename : string
         Name of file to save leja samples and weights to

    Return
    ------
    ordered_samples_1d : np.ndarray (num_samples_1d)
        The reordered samples.

    ordered_weights_1d : np.ndarray (num_samples_1d)
        The reordered weights.
    """
    from pyapprox.multivariate_polynomials import PolynomialChaosExpansion
    from pyapprox.leja_sequences import get_leja_sequence_1d,\
        get_quadrature_weights_from_samples
    num_vars = 1
    num_leja_samples = growth_rule(level)
    # print(('num_leja_samples',num_leja_samples))

    # freezing beta rv like below has huge overhead
    # it creates a docstring each time which adds up to many seconds
    # for repeated calls to pdf
    # univariate_weight_function=lambda x: beta_rv(
    #    alpha_stat,beta_stat).pdf((x+1)/2)/2
    # univariate_weight_function = lambda x: beta_rv.pdf(
    #    (x+1)/2,alpha_stat,beta_stat)/2
    def univariate_weight_function(x): return beta_pdf(
        alpha_stat, beta_stat, (x+1)/2)/2

    def univariate_weight_function_deriv(x): return beta_pdf_derivative(
        alpha_stat, beta_stat, (x+1)/2)/4

    weight_function = partial(
        evaluate_tensor_product_function,
        [univariate_weight_function]*num_vars)

    weight_function_deriv = partial(
        gradient_of_tensor_product_function,
        [univariate_weight_function]*num_vars,
        [univariate_weight_function_deriv]*num_vars)

    # assert np.allclose(
    #     (univariate_weight_function(0.5+1e-8)-
    #          univariate_weight_function(0.5))/1e-8,
    #     univariate_weight_function_deriv(0.5),atol=1e-6)

    poly = PolynomialChaosExpansion()
    # must be imported locally otherwise I have a circular dependency
    from pyapprox.variable_transformations import \
        define_iid_random_variable_transformation
    from scipy.stats import uniform
    var_trans = define_iid_random_variable_transformation(
        uniform(-1, 2), num_vars)

    poly_opts = {'poly_type': 'jacobi', 'alpha_poly': beta_stat-1,
                 'beta_poly': alpha_stat-1, 'var_trans': var_trans}
    poly.configure(poly_opts)

    if samples_filename is None or not os.path.exists(samples_filename):
        ranges = [-1, 1]
        from scipy.stats import beta as beta_rv
        if initial_points is None:
            initial_points = np.asarray(
                [[2*beta_rv(alpha_stat, beta_stat).ppf(0.5)-1]]).T
        leja_sequence = get_leja_sequence_1d(
            num_leja_samples, initial_points, poly,
            weight_function, weight_function_deriv, ranges)
        if samples_filename is not None:
            np.savez(samples_filename, samples=leja_sequence)
    else:
        leja_sequence = np.load(samples_filename)['samples']
        #print (leja_sequence.shape[1],growth_rule(level),level)
        assert leja_sequence.shape[1] >= growth_rule(level)
        leja_sequence = leja_sequence[:, :growth_rule(level)]

    indices = np.arange(growth_rule(level))[np.newaxis, :]
    poly.set_indices(indices)
    ordered_weights_1d = get_leja_sequence_quadrature_weights(
        leja_sequence, growth_rule, poly.basis_matrix, weight_function, level,
        return_weights_for_all_levels)
    return leja_sequence[0, :], ordered_weights_1d
Example #3
0
def gaussian_leja_quadrature_rule(level,
                                  growth_rule=leja_growth_rule,
                                  samples_filename=None,
                                  return_weights_for_all_levels=True,
                                  initial_points=None):
    """
    Return the samples and weights of the Leja quadrature rule for the beta
    probability measure. 

    By construction these rules have polynomial ordering.

    Parameters
    ----------
    level : integer
        The level of the isotropic sparse grid.

    samples_filename : string
         Name of file to save leja samples and weights to

    Return
    ------
    ordered_samples_1d : np.ndarray (num_samples_1d)
        The reordered samples.

    ordered_weights_1d : np.ndarray (num_samples_1d)
        The reordered weights.
    """
    from pyapprox.multivariate_polynomials import PolynomialChaosExpansion
    from pyapprox.leja_sequences import get_leja_sequence_1d,\
        get_quadrature_weights_from_samples

    # freezing scipy gaussian rv like below has huge overhead
    # it creates a docstring each time which adds up to many seconds
    # for repeated calls to pdf
    from pyapprox.utilities import gaussian_pdf, gaussian_pdf_derivative
    univariate_weight_function = partial(gaussian_pdf, 0, 1)
    univariate_weight_function_deriv = partial(gaussian_pdf_derivative, 0, 1)

    num_vars = 1
    num_leja_samples = growth_rule(level)

    weight_function = partial(
        evaluate_tensor_product_function,
        [univariate_weight_function]*num_vars)

    weight_function_deriv = partial(
        gradient_of_tensor_product_function,
        [univariate_weight_function]*num_vars,
        [univariate_weight_function_deriv]*num_vars)

    assert np.allclose(
        (univariate_weight_function(0.5+1e-8) -
         univariate_weight_function(0.5))/1e-8,
        univariate_weight_function_deriv(0.5), atol=1e-6)

    poly = PolynomialChaosExpansion()
    # must be imported locally otherwise I have a circular dependency
    from pyapprox.variable_transformations import \
        define_iid_random_variable_transformation
    from scipy.stats import norm
    var_trans = define_iid_random_variable_transformation(
        norm(), num_vars)
    poly_opts = {'poly_type': 'hermite', 'var_trans': var_trans}
    poly.configure(poly_opts)

    if samples_filename is None or not os.path.exists(samples_filename):
        ranges = [None, None]
        if initial_points is None:
            initial_points = np.asarray([[0.0]]).T
        leja_sequence = get_leja_sequence_1d(
            num_leja_samples, initial_points, poly,
            weight_function, weight_function_deriv, ranges)
        if samples_filename is not None:
            np.savez(samples_filename, samples=leja_sequence)
    else:
        leja_sequence = np.load(samples_filename)['samples']
        assert leja_sequence.shape[1] >= growth_rule(level)
        leja_sequence = leja_sequence[:, :growth_rule(level)]

    indices = np.arange(growth_rule(level))[np.newaxis, :]
    poly.set_indices(indices)
    ordered_weights_1d = get_leja_sequence_quadrature_weights(
        leja_sequence, growth_rule, poly.basis_matrix, weight_function, level,
        return_weights_for_all_levels)
    return leja_sequence[0, :], ordered_weights_1d