Example 1 (using Chaospy (3.0.5))¶
$ x :nbsphinx-math:`in [-2, 2] `$
$ y = x - 0.5x ^ 3 + 0.1x ^ 4 $
[1]:
# true model
def model(x):
return x - 0.5 * x ** 3 + 0.1 * x ** 4
[2]:
import numpy as np
# set the domain of interest
pdom = np . empty ((2 , 1) )
pdom[0 , 0] = -2.0
pdom[1 , 0] = 2.0
[3]:
# load the UQ interface
from surrogate.chaospy_model import UQChaospy as UQ
# setup the UQ analysis ( proj : projection method )
uq = UQ(pdom, order=4, method='proj')
# create the training data
xtrain = uq.generate_quadrature()
ytrain = model(xtrain)
# train the surrogate model
uq.fit(xtrain, ytrain )
# predict the model response on validation points
xval = np.random.uniform(pdom [0 , 0], pdom[1 , 0], (5 , 1))
yval = uq.predict(xval)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-686f0afea405> in <module>
6
7 # create the training data
----> 8 xtrain = uq.generate_quadrature()
9 ytrain = model(xtrain)
10
~/pyOPALTools/surrogate/chaospy_model.py in generate_quadrature(self, rule)
182 rule=rule)
183
--> 184 xx = self._unscale(self._nodes[0, :].T, self._pdom[:, 0].T)
185 for i in range(1, self._npar):
186 xx = np.hstack((xx, self._unscale(self._nodes[i, :].T, self._pdom[:, i])))
~/pyOPALTools/surrogate/chaospy_model.py in _unscale(self, x, pdom)
296 def _unscale(self, x, pdom):
297 LU_transform = MinMaxScaler(feature_range=(pdom)).fit([[-1], [1]])
--> 298 return LU_transform.transform(x.reshape(1, -1)).T
299
300
/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/_data.py in transform(self, X)
433
434 X = self._validate_data(X, copy=self.copy, dtype=FLOAT_DTYPES,
--> 435 force_all_finite="allow-nan", reset=False)
436
437 X *= self.scale_
/usr/local/lib/python3.6/dist-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
435
436 if check_params.get('ensure_2d', True):
--> 437 self._check_n_features(X, reset=reset)
438
439 return out
/usr/local/lib/python3.6/dist-packages/sklearn/base.py in _check_n_features(self, X, reset)
364 if n_features != self.n_features_in_:
365 raise ValueError(
--> 366 f"X has {n_features} features, but {self.__class__.__name__} "
367 f"is expecting {self.n_features_in_} features as input.")
368
ValueError: X has 5 features, but MinMaxScaler is expecting 1 features as input.
[4]:
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6), dpi=150)
# evaluate true model
x = np.linspace(pdom[0, 0], pdom[1, 0], 100)
plt.plot(x, model(x), label='true model')
# plot training points
plt.plot(xtrain, ytrain, 'ro', label='training points')
# plot validation points
plt.plot(xval, yval, 'bo', label='predicted points')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid(linestyle='dashed')
plt.legend()
plt.show()
plt.close()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-5b1147a5449c> in <module>
8
9 # plot training points
---> 10 plt.plot(xtrain, ytrain, 'ro', label='training points')
11
12 # plot validation points
NameError: name 'xtrain' is not defined
Example: Ishigami function (using Chaospy (3.0.5))¶
$ m(x_1, x_2, x_3) = \sin`(x\_1) + a :nbsphinx-math:cdot`:nbsphinx-math:sin`^2(x_2) + b :nbsphinx-math:cdot x_3^4:nbsphinx-math:sin`(x_1) $
with \(x_i\in [-\pi, \pi]\)
[5]:
def ishigami(x1, x2, x3, a, b):
import numpy as np
return np.sin(x1) + a * np.sin(x2) ** 2 + b * x3 ** 4 * np.sin(x1)
[6]:
import numpy as np
l = -np.pi
u = np.pi
order = 9
a = 7
b = 0.05
nsamples = 440
np.random.seed(42)
x1 = np.random.uniform(l, u, nsamples)
np.random.seed(47)
x2 = np.random.uniform(l, u, nsamples)
np.random.seed(53)
x3 = np.random.uniform(l, u, nsamples)
xtrain = np.vstack((x1, x2, x3)).T
y_true = ishigami(xtrain[:, 0], xtrain[:, 1], xtrain[:, 2], a, b)
[7]:
# Analytical solution
D1 = b * np.pi ** 4 / 5.0 + b ** 2 * np.pi ** 8 / 50.0 + 0.5
D2 = a ** 2 / 8.0
D13 = (1.0 / 18 - 1.0 / 50.0) * b ** 2 * np.pi ** 8
D = D1 + D2 + D13
print ( D1 / D )
print ( D2 / D )
print ( 0 )
print ( (D1 + 0 + D13) / D )
print ( (D2 + 0 + 0) / D )
print ( (0 + 0 + D13) / D )
0.21851856442701334
0.686894643648693
0
0.3131053563513071
0.686894643648693
0.09458679192429374
[8]:
from chaospy_model import UQChaospy
import numpy as np
pdom = np.empty((2, 3))
for i in range(3):
pdom[0, i] = l
pdom[1, i] = u
ch = UQChaospy(pdom, order)
ch.fit(xtrain, y_true)
ytrain = ch.predict(xtrain)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-d11213b36c6a> in <module>
----> 1 from chaospy_model import UQChaospy
2 import numpy as np
3
4 pdom = np.empty((2, 3))
5
ModuleNotFoundError: No module named 'chaospy_model'
[9]:
sens_m = ch.main_sensitivity()
sens_t = ch.total_sensitivity()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-389b838a290b> in <module>
----> 1 sens_m = ch.main_sensitivity()
2 sens_t = ch.total_sensitivity()
NameError: name 'ch' is not defined
[10]:
print ( sens_m )
print ( sens_t )
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-9c3e0bef355c> in <module>
----> 1 print ( sens_m )
2 print ( sens_t )
NameError: name 'sens_m' is not defined
[11]:
from bootstrap.bootstrap import bootstrap_sobol
s_m, s_t = bootstrap_sobol(xtrain, y_true, ch, n_boot=20, seed=42)
print ( s_m )
print ( s_t )
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-1045bdb1875a> in <module>
----> 1 from bootstrap.bootstrap import bootstrap_sobol
2
3 s_m, s_t = bootstrap_sobol(xtrain, y_true, ch, n_boot=20, seed=42)
4
5 print ( s_m )
ModuleNotFoundError: No module named 'bootstrap'