-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_narx_msa.py
185 lines (148 loc) · 5.41 KB
/
plot_narx_msa.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
===========================
Multi-step-ahead NARX model
===========================
.. currentmodule:: fastcan
In this example, we will compare one-step-ahead NARX and multi-step-ahead NARX.
"""
# Authors: The fastcan developers
# SPDX-License-Identifier: MIT
# %%
# Nonlinear system
# ----------------
#
# `Duffing equation <https://en.wikipedia.org/wiki/Duffing_equation>`_ is used to
# generate simulated data. The mathematical model is given by
#
# .. math::
# \ddot{y} + 0.1\dot{y} - y + 0.25y^3 = u
#
# where :math:`y` is the output signal and :math:`u` is the input signal, which is
# :math:`u(t) = 2.5\cos(2\pi t)`.
#
# The phase portraits of the Duffing equation are shown below.
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
def duffing_equation(y, t):
"""Non-autonomous system"""
y1, y2 = y
u = 2.5 * np.cos(2 * np.pi * t)
dydt = [y2, -0.1 * y2 + y1 - 0.25 * y1**3 + u]
return dydt
def auto_duffing_equation(y, t):
"""Autonomous system"""
y1, y2 = y
dydt = [y2, -0.1 * y2 + y1 - 0.25 * y1**3]
return dydt
dur = 10
n_samples = 1000
y0 = None
if y0 is None:
n_init = 10
x0 = np.linspace(0, 2, n_init)
y0_y = np.cos(np.pi * x0)
y0_x = np.sin(np.pi * x0)
y0 = np.c_[y0_x, y0_y]
else:
n_init = len(y0)
t = np.linspace(0, dur, n_samples)
sol = np.zeros((n_init, n_samples, 2))
for i in range(n_init):
sol[i] = odeint(auto_duffing_equation, y0[i], t)
for i in range(n_init):
plt.plot(sol[i, :, 0], sol[i, :, 1], c="tab:blue")
plt.title("Phase portraits of Duffing equation")
plt.xlabel("y(t)")
plt.ylabel("dy/dt(t)")
plt.show()
# %%
# Generate training-test data
# ---------------------------
#
# In the phase portraits, it is shown that the system has two stable equilibria.
# We use one to generate training data and the other to generate test data.
dur = 10
n_samples = 1000
rng = np.random.default_rng(12345)
e_train = rng.normal(0, 0.0002, n_samples)
e_test = rng.normal(0, 0.0002, n_samples)
t = np.linspace(0, dur, n_samples)
sol = odeint(duffing_equation, [0.6, 0.8], t)
u_train = 2.5 * np.cos(2 * np.pi * t).reshape(-1, 1)
y_train = sol[:, 0] + e_train
sol = odeint(duffing_equation, [0.6, -0.8], t)
u_test = 2.5 * np.cos(2 * np.pi * t).reshape(-1, 1)
y_test = sol[:, 0]+ e_test
# %%
# One-step-head VS. multi-step-ahead NARX
# ---------------------------------------
#
# First, we use :meth:`make_narx` to obtain the reduced NARX model.
# Then, the NARX model will be fitted with one-step-ahead predictor and
# multi-step-ahead predictor, respectively. Generally, the training of one-step-ahead
# (OSA) NARX is faster, while the multi-step-ahead (MSA) NARX is more accurate.
from sklearn.metrics import r2_score
from fastcan.narx import make_narx
max_delay = 3
narx_model = make_narx(
X=u_train,
y=y_train,
n_terms_to_select=5,
max_delay=max_delay,
poly_degree=3,
verbose=0,
)
def plot_prediction(ax, t, y_true, y_pred, title):
ax.plot(t, y_true, label="true")
ax.plot(t, y_pred, label="predicted")
ax.legend()
ax.set_title(f"{title} (R2: {r2_score(y_true, y_pred):.5f})")
ax.set_xlabel("t (s)")
ax.set_ylabel("y(t)")
narx_model.fit(u_train, y_train)
y_train_osa_pred = narx_model.predict(u_train, y_init=y_train[:max_delay])
y_test_osa_pred = narx_model.predict(u_test, y_init=y_test[:max_delay])
narx_model.fit(u_train, y_train, coef_init="one_step_ahead")
y_train_msa_pred = narx_model.predict(u_train, y_init=y_train[:max_delay])
y_test_msa_pred = narx_model.predict(u_test, y_init=y_test[:max_delay])
fig, ax = plt.subplots(2, 2, figsize=(8, 6))
plot_prediction(ax[0, 0], t, y_train, y_train_osa_pred, "OSA NARX on Train")
plot_prediction(ax[0, 1], t, y_train, y_train_msa_pred, "MSA NARX on Train")
plot_prediction(ax[1, 0], t, y_test, y_test_osa_pred, "OSA NARX on Test")
plot_prediction(ax[1, 1], t, y_test, y_test_msa_pred, "MSA NARX on Test")
fig.tight_layout()
plt.show()
# %%
# Multiple measurement sessions
# -----------------------------
#
# The plot above shows that the NARX model cannot capture the dynamics at
# the left equilibrium shown in the phase portraits. To improve the performance, let us
# combine the training and test data for model training to include the dynamics of both
# equilibria. Here, we need to insert `np.nan` to indicate the model that training data
# and test data are from different measurement sessions. The plot shows that the
# prediction performance of the NARX on test data has been largely improved.
u_all = np.r_[u_train, [[np.nan]], u_test]
y_all = np.r_[y_train, [np.nan], y_test]
narx_model = make_narx(
X=u_all,
y=y_all,
n_terms_to_select=5,
max_delay=max_delay,
poly_degree=3,
verbose=0,
)
narx_model.fit(u_all, y_all)
y_train_osa_pred = narx_model.predict(u_train, y_init=y_train[:max_delay])
y_test_osa_pred = narx_model.predict(u_test, y_init=y_test[:max_delay])
narx_model.fit(u_all, y_all, coef_init="one_step_ahead")
y_train_msa_pred = narx_model.predict(u_train, y_init=y_train[:max_delay])
y_test_msa_pred = narx_model.predict(u_test, y_init=y_test[:max_delay])
fig, ax = plt.subplots(2, 2, figsize=(8, 6))
plot_prediction(ax[0, 0], t, y_train, y_train_osa_pred, "OSA NARX on Train")
plot_prediction(ax[0, 1], t, y_train, y_train_msa_pred, "MSA NARX on Train")
plot_prediction(ax[1, 0], t, y_test, y_test_osa_pred, "OSA NARX on Test")
plot_prediction(ax[1, 1], t, y_test, y_test_msa_pred, "MSA NARX on Test")
fig.tight_layout()
plt.show()