<jupyter_start><jupyter_text>Polynomial Chaos Expansion Example 6 Author: <NAME> \Date: December 8, 2020 In this example, PCE is used to generate a surrogate model for a given set of 8D data.  Robot arm function **Dimensions:** 8**Description:**  Models the position of a robot arm which has four segments. **Input Domain:**  The input variables and their usual input ranges are: $[0,1]$ for the $L_i$ inputs and $[0,2\pi]$ for the $\theta_i$ inputs.**Function:**   $ f(\textbf{x}) = \Big( \sum_{i=1}^{4}L_i cos\Big(\sum_{j=1}^{i} \theta_j\Big) \Big)^{2} + \Big( \sum_{i=1}^{4}L_i sin\Big(\sum_{j=1}^{i} \theta_j\Big) \Big)^{2} $**Output:** The square distance from the end of the robot arm to the origin.**Reference:**  <NAME>., & <NAME>. (2001). Quasi-regression. Journal of Complexity, 17(4), 588-607. Import necessary libraries.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from UQpy.Surrogates import *<jupyter_output><empty_output><jupyter_text>Define the function.<jupyter_code>def function(x):
    # without square root
    u1 = x[:,4]*np.cos(x[:,0])
    u2 = x[:,4]*np.cos(x[:,0]) + x[:,5]*np.cos(np.sum(x[:,:2], axis=1))
    u3 = x[:,4]*np.cos(x[:,0]) + x[:,5]*np.cos(np.sum(x[:,:2], axis=1)) + x[:,6]*np.cos(np.sum(x[:,:3], axis=1))
    u4 = x[:,4]*np.cos(x[:,0]) + x[:,5]*np.cos(np.sum(x[:,:2], axis=1)) + x[:,6]*np.cos(np.sum(x[:,:3], axis=1)) + x[:,7]*np.cos(np.sum(x[:,:4], axis=1))

    v1 = x[:,4]*np.sin(x[:,0])
    v2 = x[:,4]*np.sin(x[:,0]) + x[:,5]*np.sin(np.sum(x[:,:2], axis=1))
    v3 = x[:,4]*np.sin(x[:,0]) + x[:,5]*np.sin(np.sum(x[:,:2], axis=1)) + x[:,6]*np.sin(np.sum(x[:,:3], axis=1))
    v4 = x[:,4]*np.sin(x[:,0]) + x[:,5]*np.sin(np.sum(x[:,:2], axis=1)) + x[:,6]*np.sin(np.sum(x[:,:3], axis=1)) + x[:,7]*np.sin(np.sum(x[:,:4], axis=1))                                                                                                  
                                                                                                                               
    return (u1+u2+u3+u4)**2 + (v1+v2+v3+v4)**2<jupyter_output><empty_output><jupyter_text>Create a distribution object, generate samples and evaluate the function at the samples.<jupyter_code>np.random.seed(1)

dist_1 = Uniform(loc=0, scale=2*np.pi)    
dist_2 = Uniform(loc=0, scale=1)

marg = [dist_1]*4
marg_1 = [dist_2]*4
marg.extend(marg_1)

joint = JointInd(marginals=marg)

n_samples = 9000
x = joint.rvs(n_samples)
y = function(x)<jupyter_output><empty_output><jupyter_text>Create an object from the PCE class.<jupyter_code>max_degree = 6
polys = Polynomials(dist_object=joint, degree=max_degree)<jupyter_output><empty_output><jupyter_text>Compute PCE coefficients using least squares regression.<jupyter_code>lstsq = PolyChaosLstsq(poly_object=polys)
pce = PCE(method=lstsq) 
pce.fit(x,y)<jupyter_output><empty_output><jupyter_text>Compute PCE coefficients using Lasso regression.<jupyter_code>lasso = PolyChaosLasso(poly_object=polys, learning_rate=0.05, iterations=2000, penalty=1)
pce2 = PCE(method=lasso) 
pce2.fit(x,y)<jupyter_output><empty_output><jupyter_text>Compute PCE coefficients using Ridge regression.<jupyter_code>ridge = PolyChaosRidge(poly_object=polys, learning_rate=0.01, iterations=2000, penalty=0.1)
pce3 = PCE(method=ridge) 
pce3.fit(x,y)<jupyter_output><empty_output><jupyter_text>Error Estimation Validation error.<jupyter_code>n_samples = 500
x_val = joint.rvs(n_samples)
y_val = function(x_val)

error = ErrorEstimation(surr_object=pce)
error2 = ErrorEstimation(surr_object=pce2)
error3 = ErrorEstimation(surr_object=pce3)
print('Error from least squares regression is: ', error.validation(x_val, y_val))
print('Error from LASSO is: ', error2.validation(x_val, y_val))
print('Error from Ridge is: ', error3.validation(x_val, y_val))<jupyter_output>Error from least squares regression is:  0.0510225
Error from LASSO is:  0.0510114
Error from Ridge is:  0.0503678<jupyter_text>Moment Estimation Returns mean and variance of the PCE surrogate.<jupyter_code>n_mc = 1000000
x_mc = joint.rvs(n_mc)
y_mc = function(x_mc)    
mu = np.mean(y_mc)

print('Moments from least squares regression :', MomentEstimation(surr_object=pce).get())
print('Moments from LASSO regression :', MomentEstimation(surr_object=pce2).get())
print('Moments from Ridge regression :', MomentEstimation(surr_object=pce3).get())
print('Moments from MC integration: ', (round((1/n_mc)*np.sum(y_mc),6), round((1/n_mc)*np.sum((y_mc-mu)**2),6)))<jupyter_output>Moments from least squares regression : (9.984848, 92.713617)
Moments from LASSO regression : (9.98418, 92.683269)
Moments from Ridge regression : (9.959297, 91.861373)
Moments from MC integration:  (10.018975, 92.526962)
{'fraction_non_alphanumeric': 0.1774984018751332, 'fraction_numerical': 0.0530577455785212, 'mean_word_length': 4.759509202453987, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '42182', 'license': 'MIT', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1816, 'n_words': 351, 'path': 'example/Surrogates/PCE/PCE_Example6.ipynb', 'repo_name': 'GuoQiang-Fu/UQpy', 'size': '4704'}