Note
Go to the end to download the full example code.
Polynomial chaos expansion (PCE)#
A PCERegressor is a PCE model
based on OpenTURNS.
from __future__ import annotations
from matplotlib import pyplot as plt
from numpy import array
from gemseo import create_discipline
from gemseo import create_parameter_space
from gemseo import sample_disciplines
from gemseo.mlearning import create_regression_model
Problem#
In this example,
we represent the function \(f(x)=(6x-2)^2\sin(12x-4)\) [FSK08]
by the AnalyticDiscipline
discipline = create_discipline(
"AnalyticDiscipline",
name="f",
expressions={"y": "(6*x-2)**2*sin(12*x-4)"},
)
and seek to approximate it over the input space
input_space = create_parameter_space()
input_space.add_random_variable("x", "OTUniformDistribution")
To do this, we create a training dataset with 6 equispaced points:
training_dataset = sample_disciplines(
[discipline], input_space, "y", algo_name="PYDOE_FULLFACT", n_samples=10
)
INFO - 16:19:13: *** Start Sampling execution ***
INFO - 16:19:13: Sampling
INFO - 16:19:13: Disciplines: f
INFO - 16:19:13: MDO formulation: MDF
INFO - 16:19:13: Running the algorithm PYDOE_FULLFACT:
INFO - 16:19:13: 10%|█ | 1/10 [00:00<00:00, 586.78 it/sec]
INFO - 16:19:13: 20%|██ | 2/10 [00:00<00:00, 875.55 it/sec]
INFO - 16:19:13: 30%|███ | 3/10 [00:00<00:00, 1067.34 it/sec]
INFO - 16:19:13: 40%|████ | 4/10 [00:00<00:00, 1213.72 it/sec]
INFO - 16:19:13: 50%|█████ | 5/10 [00:00<00:00, 1318.47 it/sec]
INFO - 16:19:13: 60%|██████ | 6/10 [00:00<00:00, 1404.11 it/sec]
INFO - 16:19:13: 70%|███████ | 7/10 [00:00<00:00, 1473.90 it/sec]
INFO - 16:19:13: 80%|████████ | 8/10 [00:00<00:00, 1530.00 it/sec]
INFO - 16:19:13: 90%|█████████ | 9/10 [00:00<00:00, 1585.95 it/sec]
INFO - 16:19:13: 100%|██████████| 10/10 [00:00<00:00, 1588.21 it/sec]
INFO - 16:19:13: *** End Sampling execution ***
Basics#
Training#
Then, we train an PCE regression model from these samples:
model = create_regression_model("PCERegressor", training_dataset)
model.learn()
WARNING - 16:19:13: Remove input data transformation because PCERegressor does not support transformers.
Prediction#
Once it is built, we can predict the output value of \(f\) at a new input point:
input_value = {"x": array([0.65])}
output_value = model.predict(input_value)
output_value
{'y': array([-0.81106394])}
as well as its Jacobian value:
jacobian_value = model.predict_jacobian(input_value)
jacobian_value
{'y': {'x': array([[18.2279622]])}}
Plotting#
Of course, you can see that the quadratic model is no good at all here:
test_dataset = sample_disciplines(
[discipline], input_space, "y", algo_name="PYDOE_FULLFACT", n_samples=100
)
input_data = test_dataset.get_view(variable_names=model.input_names).to_numpy()
reference_output_data = test_dataset.get_view(variable_names="y").to_numpy().ravel()
predicted_output_data = model.predict(input_data).ravel()
plt.plot(input_data.ravel(), reference_output_data, label="Reference")
plt.plot(input_data.ravel(), predicted_output_data, label="Regression - Basics")
plt.grid()
plt.legend()
plt.show()

INFO - 16:19:13: *** Start Sampling execution ***
INFO - 16:19:13: Sampling
INFO - 16:19:13: Disciplines: f
INFO - 16:19:13: MDO formulation: MDF
INFO - 16:19:13: Running the algorithm PYDOE_FULLFACT:
INFO - 16:19:13: 1%| | 1/100 [00:00<00:00, 2970.47 it/sec]
INFO - 16:19:13: 2%|▏ | 2/100 [00:00<00:00, 2367.66 it/sec]
INFO - 16:19:13: 3%|▎ | 3/100 [00:00<00:00, 2244.94 it/sec]
INFO - 16:19:13: 4%|▍ | 4/100 [00:00<00:00, 2195.97 it/sec]
INFO - 16:19:13: 5%|▌ | 5/100 [00:00<00:00, 2171.41 it/sec]
INFO - 16:19:13: 6%|▌ | 6/100 [00:00<00:00, 2163.87 it/sec]
INFO - 16:19:13: 7%|▋ | 7/100 [00:00<00:00, 2156.30 it/sec]
INFO - 16:19:13: 8%|▊ | 8/100 [00:00<00:00, 2146.25 it/sec]
INFO - 16:19:13: 9%|▉ | 9/100 [00:00<00:00, 2155.22 it/sec]
INFO - 16:19:13: 10%|█ | 10/100 [00:00<00:00, 2149.49 it/sec]
INFO - 16:19:13: 11%|█ | 11/100 [00:00<00:00, 2152.43 it/sec]
INFO - 16:19:13: 12%|█▏ | 12/100 [00:00<00:00, 2140.68 it/sec]
INFO - 16:19:13: 13%|█▎ | 13/100 [00:00<00:00, 2146.78 it/sec]
INFO - 16:19:13: 14%|█▍ | 14/100 [00:00<00:00, 2134.66 it/sec]
INFO - 16:19:13: 15%|█▌ | 15/100 [00:00<00:00, 2141.26 it/sec]
INFO - 16:19:13: 16%|█▌ | 16/100 [00:00<00:00, 2132.13 it/sec]
INFO - 16:19:13: 17%|█▋ | 17/100 [00:00<00:00, 2134.12 it/sec]
INFO - 16:19:13: 18%|█▊ | 18/100 [00:00<00:00, 2133.78 it/sec]
INFO - 16:19:13: 19%|█▉ | 19/100 [00:00<00:00, 2132.39 it/sec]
INFO - 16:19:13: 20%|██ | 20/100 [00:00<00:00, 2132.50 it/sec]
INFO - 16:19:13: 21%|██ | 21/100 [00:00<00:00, 2135.70 it/sec]
INFO - 16:19:13: 22%|██▏ | 22/100 [00:00<00:00, 2135.10 it/sec]
INFO - 16:19:13: 23%|██▎ | 23/100 [00:00<00:00, 2135.12 it/sec]
INFO - 16:19:13: 24%|██▍ | 24/100 [00:00<00:00, 2135.91 it/sec]
INFO - 16:19:13: 25%|██▌ | 25/100 [00:00<00:00, 2137.29 it/sec]
INFO - 16:19:13: 26%|██▌ | 26/100 [00:00<00:00, 2138.99 it/sec]
INFO - 16:19:13: 27%|██▋ | 27/100 [00:00<00:00, 2138.01 it/sec]
INFO - 16:19:13: 28%|██▊ | 28/100 [00:00<00:00, 2137.85 it/sec]
INFO - 16:19:13: 29%|██▉ | 29/100 [00:00<00:00, 2134.77 it/sec]
INFO - 16:19:13: 30%|███ | 30/100 [00:00<00:00, 2134.83 it/sec]
INFO - 16:19:13: 31%|███ | 31/100 [00:00<00:00, 2133.74 it/sec]
INFO - 16:19:13: 32%|███▏ | 32/100 [00:00<00:00, 2133.25 it/sec]
INFO - 16:19:13: 33%|███▎ | 33/100 [00:00<00:00, 2136.94 it/sec]
INFO - 16:19:13: 34%|███▍ | 34/100 [00:00<00:00, 2131.16 it/sec]
INFO - 16:19:13: 35%|███▌ | 35/100 [00:00<00:00, 2135.03 it/sec]
INFO - 16:19:13: 36%|███▌ | 36/100 [00:00<00:00, 2131.85 it/sec]
INFO - 16:19:13: 37%|███▋ | 37/100 [00:00<00:00, 2135.74 it/sec]
INFO - 16:19:13: 38%|███▊ | 38/100 [00:00<00:00, 2133.71 it/sec]
INFO - 16:19:13: 39%|███▉ | 39/100 [00:00<00:00, 2137.27 it/sec]
INFO - 16:19:13: 40%|████ | 40/100 [00:00<00:00, 2135.67 it/sec]
INFO - 16:19:13: 41%|████ | 41/100 [00:00<00:00, 2139.15 it/sec]
INFO - 16:19:13: 42%|████▏ | 42/100 [00:00<00:00, 2137.28 it/sec]
INFO - 16:19:13: 43%|████▎ | 43/100 [00:00<00:00, 2139.75 it/sec]
INFO - 16:19:13: 44%|████▍ | 44/100 [00:00<00:00, 2136.63 it/sec]
INFO - 16:19:13: 45%|████▌ | 45/100 [00:00<00:00, 2139.20 it/sec]
INFO - 16:19:13: 46%|████▌ | 46/100 [00:00<00:00, 2135.69 it/sec]
INFO - 16:19:13: 47%|████▋ | 47/100 [00:00<00:00, 2137.91 it/sec]
INFO - 16:19:13: 48%|████▊ | 48/100 [00:00<00:00, 2138.04 it/sec]
INFO - 16:19:13: 49%|████▉ | 49/100 [00:00<00:00, 2137.55 it/sec]
INFO - 16:19:13: 50%|█████ | 50/100 [00:00<00:00, 2137.49 it/sec]
INFO - 16:19:13: 51%|█████ | 51/100 [00:00<00:00, 2136.85 it/sec]
INFO - 16:19:13: 52%|█████▏ | 52/100 [00:00<00:00, 2129.84 it/sec]
INFO - 16:19:13: 53%|█████▎ | 53/100 [00:00<00:00, 2128.99 it/sec]
INFO - 16:19:13: 54%|█████▍ | 54/100 [00:00<00:00, 2128.49 it/sec]
INFO - 16:19:13: 55%|█████▌ | 55/100 [00:00<00:00, 2128.75 it/sec]
INFO - 16:19:13: 56%|█████▌ | 56/100 [00:00<00:00, 2128.76 it/sec]
INFO - 16:19:13: 57%|█████▋ | 57/100 [00:00<00:00, 2127.33 it/sec]
INFO - 16:19:13: 58%|█████▊ | 58/100 [00:00<00:00, 2126.59 it/sec]
INFO - 16:19:13: 59%|█████▉ | 59/100 [00:00<00:00, 2125.70 it/sec]
INFO - 16:19:13: 60%|██████ | 60/100 [00:00<00:00, 2125.44 it/sec]
INFO - 16:19:13: 61%|██████ | 61/100 [00:00<00:00, 2121.57 it/sec]
INFO - 16:19:13: 62%|██████▏ | 62/100 [00:00<00:00, 2120.94 it/sec]
INFO - 16:19:13: 63%|██████▎ | 63/100 [00:00<00:00, 2122.03 it/sec]
INFO - 16:19:13: 64%|██████▍ | 64/100 [00:00<00:00, 2122.84 it/sec]
INFO - 16:19:13: 65%|██████▌ | 65/100 [00:00<00:00, 2122.28 it/sec]
INFO - 16:19:13: 66%|██████▌ | 66/100 [00:00<00:00, 2121.18 it/sec]
INFO - 16:19:13: 67%|██████▋ | 67/100 [00:00<00:00, 2118.37 it/sec]
INFO - 16:19:13: 68%|██████▊ | 68/100 [00:00<00:00, 2118.26 it/sec]
INFO - 16:19:13: 69%|██████▉ | 69/100 [00:00<00:00, 2117.84 it/sec]
INFO - 16:19:13: 70%|███████ | 70/100 [00:00<00:00, 2118.00 it/sec]
INFO - 16:19:13: 71%|███████ | 71/100 [00:00<00:00, 2117.91 it/sec]
INFO - 16:19:13: 72%|███████▏ | 72/100 [00:00<00:00, 2118.08 it/sec]
INFO - 16:19:13: 73%|███████▎ | 73/100 [00:00<00:00, 2119.61 it/sec]
INFO - 16:19:13: 74%|███████▍ | 74/100 [00:00<00:00, 2117.58 it/sec]
INFO - 16:19:13: 75%|███████▌ | 75/100 [00:00<00:00, 2119.26 it/sec]
INFO - 16:19:13: 76%|███████▌ | 76/100 [00:00<00:00, 2117.18 it/sec]
INFO - 16:19:13: 77%|███████▋ | 77/100 [00:00<00:00, 2119.03 it/sec]
INFO - 16:19:13: 78%|███████▊ | 78/100 [00:00<00:00, 2118.10 it/sec]
INFO - 16:19:13: 79%|███████▉ | 79/100 [00:00<00:00, 2119.72 it/sec]
INFO - 16:19:13: 80%|████████ | 80/100 [00:00<00:00, 2119.79 it/sec]
INFO - 16:19:13: 81%|████████ | 81/100 [00:00<00:00, 2121.59 it/sec]
INFO - 16:19:13: 82%|████████▏ | 82/100 [00:00<00:00, 2121.18 it/sec]
INFO - 16:19:13: 83%|████████▎ | 83/100 [00:00<00:00, 2120.94 it/sec]
INFO - 16:19:13: 84%|████████▍ | 84/100 [00:00<00:00, 2119.60 it/sec]
INFO - 16:19:13: 85%|████████▌ | 85/100 [00:00<00:00, 2121.10 it/sec]
INFO - 16:19:13: 86%|████████▌ | 86/100 [00:00<00:00, 2119.06 it/sec]
INFO - 16:19:13: 87%|████████▋ | 87/100 [00:00<00:00, 2120.48 it/sec]
INFO - 16:19:13: 88%|████████▊ | 88/100 [00:00<00:00, 2119.08 it/sec]
INFO - 16:19:13: 89%|████████▉ | 89/100 [00:00<00:00, 2120.55 it/sec]
INFO - 16:19:13: 90%|█████████ | 90/100 [00:00<00:00, 2120.79 it/sec]
INFO - 16:19:13: 91%|█████████ | 91/100 [00:00<00:00, 2120.81 it/sec]
INFO - 16:19:13: 92%|█████████▏| 92/100 [00:00<00:00, 2120.93 it/sec]
INFO - 16:19:13: 93%|█████████▎| 93/100 [00:00<00:00, 2121.23 it/sec]
INFO - 16:19:13: 94%|█████████▍| 94/100 [00:00<00:00, 2121.52 it/sec]
INFO - 16:19:13: 95%|█████████▌| 95/100 [00:00<00:00, 2120.69 it/sec]
INFO - 16:19:13: 96%|█████████▌| 96/100 [00:00<00:00, 2121.05 it/sec]
INFO - 16:19:13: 97%|█████████▋| 97/100 [00:00<00:00, 2120.99 it/sec]
INFO - 16:19:13: 98%|█████████▊| 98/100 [00:00<00:00, 2121.08 it/sec]
INFO - 16:19:13: 99%|█████████▉| 99/100 [00:00<00:00, 2121.34 it/sec]
INFO - 16:19:13: 100%|██████████| 100/100 [00:00<00:00, 2103.76 it/sec]
INFO - 16:19:13: *** End Sampling execution ***
Settings#
The PCERegressor has many options
defined in the PCERegressor_Settings Pydantic model.
Degree#
model = create_regression_model("PCERegressor", training_dataset, degree=3)
model.learn()
WARNING - 16:19:13: Remove input data transformation because PCERegressor does not support transformers.
and see that this model seems to be better:
predicted_output_data_ = model.predict(input_data).ravel()
plt.plot(input_data.ravel(), reference_output_data, label="Reference")
plt.plot(input_data.ravel(), predicted_output_data, label="Regression - Basics")
plt.plot(input_data.ravel(), predicted_output_data_, label="Regression - Degree(3)")
plt.grid()
plt.legend()
plt.show()

Total running time of the script: (0 minutes 0.260 seconds)