Skip to content
Snippets Groups Projects

Add tutorial notebooks

Merged Nando Hegemann requested to merge add-notebooks into main
4 files
+ 586
0
Compare changes
  • Side-by-side
  • Inline
Files
4
%% Cell type:markdown id:00c2cc0d-24ab-4a35-bc11-459feebe2455 tags:
# Tutorial 1 - Function Approximation with PyThia
%% Cell type:code id:7bd31586-38d1-4e41-8963-a0e02a070f7e tags:
``` python
import sys
import numpy as np
import pythia as pt # see doc: www.pythia-uq.com
import matplotlib.pyplot as plt
sys.path.append("../../src")
from utils import Batman # local imports
```
%% Cell type:markdown id:31a019e4-dcc1-47d9-be36-24b95e24abe9 tags:
## Define Target Function
%% Cell type:code id:ceb41891-6c8e-40ed-b55e-979e00713ea9 tags:
``` python
def target_function(y):
return Batman().eval(y)
```
%% Cell type:code id:d4a36225-6620-4919-95fa-cffe3446fef6 tags:
``` python
y = np.linspace(-7.5, 7.5, 401)
fig, ax = plt.subplot_mosaic([["batman"]], constrained_layout=True, figsize=(10, 5))
ax["batman"].plot(y, target_function(y)[:, 0], label="$f_1(y)$")
ax["batman"].plot(y, target_function(y)[:, 1], label="$f_2(y)$")
ax["batman"].set_xlabel("y")
ax["batman"].legend()
plt.show();
```
%% Cell type:markdown id:fde90e4d-be77-4b89-ae03-9ede7faa2447 tags:
## Setup Polynomial Chaos Expansion
%% Cell type:code id:b3f735c8-debd-42ba-b642-d4fb0f1ad62e tags:
``` python
param1 = pt.parameter.Parameter(index=1, name="y_1", domain=[-7.5, 7.5], distribution='uniform')
params = [param1]
```
%% Cell type:code id:67d2abd1-2ceb-442e-ae7e-417890a86e4b tags:
``` python
sdim = [75]
indices = pt.index.tensor(sdim)
index_set = pt.index.IndexSet(indices)
print(f"Multiindex information:")
print(f" number of indices: {index_set.shape[0]}")
print(f" dimension: {index_set.shape[1]}")
print(f" maximum dimension: {index_set.max}")
print(f" number of sobol indices: {len(index_set.sobol_tuples)}")
```
%% Cell type:code id:28be82de-2de1-45e6-8d83-7b61175b2e87 tags:
``` python
s = pt.sampler.ParameterSampler(params)
y_train = s.sample(1000)
w_train = s.weight(y_train)
f_train = target_function(y_train)
```
%% Cell type:markdown id:59364c66-9409-4214-8c35-4e7ed1afaa72 tags:
## Run Polynomial Chaos Approximation
%% Cell type:code id:5bea2800-edb2-4941-b238-d94825b0db23 tags:
``` python
surrogate = pt.chaos.PolynomialChaos(
params, index_set, y_train, w_train, f_train)
```
%% Cell type:markdown id:34011607-9de1-4ce0-9bd7-6b843a662703 tags:
## Compute Approximation Error
%% Cell type:code id:269991dd-bbca-4a64-a9e6-89fe2ee0f845 tags:
``` python
test_sampler = pt.sampler.ParameterSampler(params)
y_test = test_sampler.sample(1000)
f_test = target_function(y_test)
f_approx = surrogate.eval(y_test)
```
%% Cell type:code id:baa189c4-55d9-4087-8330-7de826406097 tags:
``` python
y = np.linspace(-7.1, 7.1, 200).reshape(-1, 1)
f_target = target_function(y)
f_approx = surrogate.eval(y)
f_error = np.abs(f_target-f_approx)/np.abs(f_target)
fig, ax = plt.subplot_mosaic([["target", "target"],
["approx", "error"]],
constrained_layout=True, figsize=(10, 4))
fig.suptitle("Approximation of function with PyThia")
ax["target"].scatter(y_train[:,0], f_train[:,0], s=1)
ax["target"].scatter(y_train[:,0], f_train[:,1], s=1)
ax["target"].set_title("training data points")
ax["approx"].plot(y, f_approx[:,0])
ax["approx"].plot(y, f_approx[:,1])
ax["approx"].set_title("surrogate")
ax["error"].semilogy(y, f_error[:,0])
ax["error"].semilogy(y, f_error[:,1])
ax["error"].set_title("pointwise relaive error")
plt.show()
```
%% Cell type:code id:5d1f5311-1a1e-42c7-a20e-2f8a274ce434 tags:
``` python
```
Loading