-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
59 lines (54 loc) · 1.88 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"/>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<h1>Comparison between PyDynamic's spline interpolation and Monte Carlo</h1>
<py-env>
- PyDynamic
- matplotlib
- numpy
- scipy
</py-env>
<div id="plot">
Computations might take up to a few minutes... please be patient.
</div>
<py-script>
import matplotlib.pyplot as plt
import numpy as np
from PyDynamic.uncertainty.interpolate import interp1d_unc
from scipy.interpolate import interp1d
# base signal
N = 10
t = np.cumsum(0.5 * (1 + np.random.random(N)))
x = np.sin(t) # + 0.01*np.random.randn(N)
# ut = np.full_like(t, 0.05)
ux = np.full_like(x, 0.2)
# interpolate with PyDynamic
ti = np.linspace(np.min(t), np.max(t), 60)
# uti = np.full_like(ti, 0.05)
ti, xi, uxi = interp1d_unc(ti, t, x, ux, kind="cubic")
# interpolate with Monte Carlo
X_mc = []
for i in range(2000):
interp_x = interp1d(t, x + ux * np.random.randn(len(x)), kind="cubic")
xm = interp_x(ti)
X_mc.append(xm)
x_mc = np.mean(X_mc, axis=0)
ux_mc = np.std(X_mc, axis=0)
# visualize
# interpolated signal
plt.plot(ti, xi, "-or", label="interpolation PyDynamic")
plt.fill_between(ti, xi + uxi, xi - uxi, color="r", alpha=0.3)
# interpolated signal
plt.plot(ti, x_mc, "-ok", label="interpolation Monte Carlo")
plt.fill_between(ti, x_mc + ux_mc, x_mc - ux_mc, color="k", alpha=0.3)
# original signal
plt.plot(t, x, "-ob", label="original")
plt.fill_between(t, x + ux, x - ux, color="b", alpha=0.3)
plt.legend()
pyscript.write('plot', plt.gcf())
</py-script>
</body>
</html>