Пример #1
0
def predictor(PCE, x_pred):
    """inputs:
       PCE: PCE model, dictionary
       x_pred: random inputs to be predicted for
       outputs:
       y_pred: predition on model response, array
    """

    # define array of random inputs on the definition range of polynomial basis
    x_prediction = npy.zeros(x_pred.shape)
    # PCE basis for 1D random variables
    basis = npy.zeros((len(x_pred[:,
                                  0]), PCE['n_deg'] + 1, len(PCE['x_prob'])))
    # PCE basis for mD random variables
    basis_total = npy.ones(
        (len(x_pred), math.factorial(PCE['n_deg'] + len(PCE['x_prob'])) /
         math.factorial(PCE['n_deg']) / math.factorial(len(PCE['x_prob']))))

    i = 0
    for key, value in PCE['x_prob'].iteritems():
        # generate Gaussian quadrature weighting factors and points first
        [alpha, beta, x_quad,
         weight] = algPCE.gen_quad(PCE['n_deg'], key, value)
        # convert x_pred to the definition range of polynomial basis
        x_prediction[:, i] = algPCE.convert_x_inv(x_pred[:, i], key, value)
        # use the obtained weighting factors, points, coefficients to generate basis
        [basis[:, :, i],
         non] = algPCE.gen_basis(PCE['n_deg'], key, value, alpha, beta,
                                 x_prediction[:, i], weight)
        i = i + 1

    for i in xrange(0, len(x_pred[:, 0])):
        for iters in xrange(
                0,
                math.factorial(PCE['n_deg'] + len(PCE['x_prob'])) /
                math.factorial(PCE['n_deg']) /
                math.factorial(len(PCE['x_prob']))):
            for j in xrange(0, len(PCE['x_prob'])):
                basis_total[i, iters] = basis_total[i, iters] * basis[
                    i,
                    int(PCE['trunc_index'][iters * len(PCE['x_prob']) + j]), j]

    y_pred = (PCE['PCE_coef'] * npy.transpose(basis_total)).sum(0)

    PCE_pred = collections.OrderedDict([('y_pred', y_pred),
                                        ('basis_total', basis_total)])

    return PCE_pred, y_pred
Пример #2
0
def collocation(n_deg, x_prob, x_exp, y_exp, meta_type):
    """inputs:
       n_deg: required degree of PCE, scalar
       x_prob: random-input information, dictionary
       x_exp: sample points of random inputs
       y_exp: corresponding response of x_exp
       meta_type: OLS or LARS, string
       outputs:
       PCE: key information of generated PCE model, dictionary
    """

    # PCE basis, weighting factors, quadrature points, for 1D random variables
    weight = npy.zeros((n_deg + 1, len(x_prob)))
    x_quad = npy.zeros((n_deg + 1, len(x_prob)))
    basis_coef = npy.zeros((len(x_exp), n_deg + 1, len(x_prob)))

    # PCE coefficients, PCE basis, in multi-dimensional scale
    alpha_total = npy.zeros(
        (math.factorial(n_deg + len(x_prob)) / math.factorial(n_deg) /
         math.factorial(len(x_prob)), 1))
    basis_coef_total = npy.ones(
        (len(x_exp), math.factorial(n_deg + len(x_prob)) /
         math.factorial(n_deg) / math.factorial(len(x_prob)), 1))

    i = 0
    for key, value in x_prob.iteritems():
        # generate Gaussian quadrature weighting factors and points first
        [alpha, beta, x_quad[:, i],
         weight[:, i]] = algPCE.gen_quad(n_deg, key, value)
        # convert x_exp to the specified range for each polynomial basis
        x_exp[:, i] = algPCE.convert_x_inv(x_exp[:, i], key, value)
        [basis_coef[:, :, i],
         non] = algPCE.gen_basis(n_deg, key, value, alpha, beta, x_exp[:, i],
                                 weight[:, i])
        # convert x_exp back to the real range
        x_exp[:, i] = algPCE.convert_x(x_exp[:, i], key, value)
        i = i + 1

    index = npy.array([])
    for iters in xrange(0, n_deg + 1):
        index = npy.append(index, multiIters.multichoose(len(x_prob), iters))

    for i in xrange(0, len(x_exp)):
        for iters in xrange(
                0,
                math.factorial(n_deg + len(x_prob)) / math.factorial(n_deg) /
                math.factorial(len(x_prob))):
            for j in xrange(0, len(x_prob)):
                basis_coef_total[
                    i, iters, 0] = basis_coef_total[i, iters, 0] * basis_coef[
                        i, int(index[iters * len(x_prob) + j]), j]

    weight_total = multiIters.iter_weights(weight, x_prob, n_deg)

    if meta_type == 'OLS':
        reg = linear_model.LinearRegression()
        reg.__init__(fit_intercept=False,
                     normalize=True,
                     copy_X=True,
                     n_jobs=1)

        reg.fit(basis_coef_total[:, :, 0], y_exp)

        alpha_total[:, 0] = reg.coef_

        coef_index = [
            i for i, alpha_value in enumerate(alpha_total)
            if npy.abs(alpha_value) > 1e-4
        ]

    elif meta_type == 'LARS':

        reg = linear_model.LassoLarsCV()
        reg.__init__(fit_intercept=True,
                     verbose=False,
                     normalize=True,
                     copy_X=True,
                     positive=False,
                     cv=5)

        reg.fit(basis_coef_total[:, :, 0], y_exp)
        alpha_total[:, 0] = reg.coef_
        alpha_total[0, 0] = reg.intercept_

        # this is actually the hybrid lars as mentioned in [Blatman and Sudret, 2011]
        # make the last step of LARS as OLS on selected basis (which have nonzero coefficients)
        coef_index = [
            i for i, alpha_value in enumerate(alpha_total)
            if npy.abs(alpha_value) > 1e-4
        ]

        reg_ols = linear_model.LinearRegression()
        reg_ols.__init__(fit_intercept=False,
                         normalize=True,
                         copy_X=True,
                         n_jobs=1)

        reg_ols.fit(basis_coef_total[:, coef_index, 0], y_exp)

        alpha_total[coef_index, 0] = reg_ols.coef_

    else:
        print 'no such meta_type found'
        print 'now exiting!'
        sys.exit()

    PCE = collections.OrderedDict([
        ('PCE_coef', alpha_total), ('x_quad', x_quad),
        ('weight_total', weight_total), ('mean', alpha_total[0]),
        ('variance', [npy.sum(alpha_total[1:] * alpha_total[1:])]),
        ('n_deg', n_deg), ('x_prob', x_prob), ('trunc_index', index),
        ('basis_coef_total', basis_coef_total), ('coef_index', coef_index)
    ])

    return PCE
Пример #3
0
def quadrature(n_deg, x_prob, full_model):
    """inputs:
       n_deg: required degree of PCE, scalar
       x_prob: random-variable information, dictionary
       full_model: real model for evaluation use, function
       outputs:
       PCE: key information of generated PCE model, dictionary
    """

    # PCE basis, weighting factors, quadrature points, for 1D random variables
    weight = npy.zeros((n_deg + 1, len(x_prob)))
    x_quad = npy.zeros((n_deg + 1, len(x_prob)))
    x_quad_pred = npy.zeros((n_deg + 1, len(x_prob)))
    basis = npy.zeros((n_deg + 1, n_deg + 1, len(x_prob)))
    integration = npy.zeros((n_deg + 1, n_deg + 1, len(x_prob)))

    # PCE coefficients, PCE basis, in multi-dimensional scale
    alpha_total = npy.zeros(
        (math.factorial(n_deg + len(x_prob)) / math.factorial(n_deg) /
         math.factorial(len(x_prob)), 1))
    basis_total = npy.ones(
        ((n_deg + 1)**len(x_prob), math.factorial(n_deg + len(x_prob)) /
         math.factorial(n_deg) / math.factorial(len(x_prob)), 1))
    integration_total = npy.ones(
        ((n_deg + 1)**len(x_prob), math.factorial(n_deg + len(x_prob)) /
         math.factorial(n_deg) / math.factorial(len(x_prob)), 1))

    i = 0
    for key, value in x_prob.iteritems():
        # generate Gaussian quadrature weighting factors and points first
        [alpha, beta, x_quad[:, i],
         weight[:, i]] = algPCE.gen_quad(n_deg, key, value)
        # use the obtained weighting factors, points, coefficients to generate basis
        [basis[:, :, i],
         integration[:, :,
                     i]] = algPCE.gen_basis(n_deg, key, value, alpha, beta,
                                            x_quad[:, i], weight[:, i])
        # first save the x_quad for prediction use
        x_quad_pred[:, i] = x_quad[:, i]
        # then convert x_quad to the real range for real model
        x_quad[:, i] = algPCE.convert_x(x_quad[:, i], key, value)
        i = i + 1

    index = npy.array([])
    for iters in xrange(0, n_deg + 1):
        index = npy.append(index, multiIters.multichoose(len(x_prob), iters))
    for iters in xrange(0, len(index) / len(x_prob)):
        index_cal = index[iters * len(x_prob):(iters + 1) * len(x_prob)]
        basis_total[:, iters,
                    0] = multiIters.iter_basis(basis, x_prob, n_deg, index_cal)
        integration_total[:, iters,
                          0] = multiIters.iter_basis(integration, x_prob,
                                                     n_deg, index_cal)

    weight_total = multiIters.iter_weights(weight, x_prob, n_deg)
    x_cal = multiIters.iter_x(x_quad, x_prob, n_deg)
    y_real = full_model(x_cal)

    alpha_total[:, 0] = npy.matmul(weight_total * y_real, basis_total[:, :, 0])

    PCE = collections.OrderedDict([
        ('PCE_coef', alpha_total), ('x_quad', x_quad_pred),
        ('weight_total', weight_total), ('mean', alpha_total[0]),
        ('variance', [npy.sum(alpha_total[1:] * alpha_total[1:])]),
        ('n_deg', n_deg), ('x_prob', x_prob), ('trunc_index', index)
    ])

    return PCE