Skip to content

Commit 6f882fd

Browse files
committed
MNT fix wrong grad when having missing values
1 parent 9c5bfba commit 6f882fd

File tree

4 files changed

+349
-168
lines changed

4 files changed

+349
-168
lines changed

examples/plot_narx_msa.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,18 @@ def auto_duffing_equation(y, t):
8282
dur = 10
8383
n_samples = 1000
8484

85+
rng = np.random.default_rng(12345)
86+
e_train = rng.normal(0, 0.001, n_samples)
87+
e_test = rng.normal(0, 0.001, n_samples)
8588
t = np.linspace(0, dur, n_samples)
8689

8790
sol = odeint(duffing_equation, [0.6, 0.8], t)
8891
u_train = 2.5 * np.cos(2 * np.pi * t).reshape(-1, 1)
89-
y_train = sol[:, 0]
92+
y_train = sol[:, 0] + e_train
9093

91-
sol = odeint(auto_duffing_equation, [0.6, -0.8], t)
94+
sol = odeint(duffing_equation, [0.6, -0.8], t)
9295
u_test = 2.5 * np.cos(2 * np.pi * t).reshape(-1, 1)
93-
y_test = sol[:, 0]
96+
y_test = sol[:, 0]+ e_test
9497

9598
# %%
9699
# One-step-head VS. multi-step-ahead NARX
@@ -105,12 +108,12 @@ def auto_duffing_equation(y, t):
105108

106109
from fastcan.narx import make_narx
107110

108-
max_delay = 2
111+
max_delay = 3
109112

110113
narx_model = make_narx(
111114
X=u_train,
112115
y=y_train,
113-
n_terms_to_select=10,
116+
n_terms_to_select=5,
114117
max_delay=max_delay,
115118
poly_degree=3,
116119
verbose=0,
@@ -159,7 +162,7 @@ def plot_prediction(ax, t, y_true, y_pred, title):
159162
narx_model = make_narx(
160163
X=u_all,
161164
y=y_all,
162-
n_terms_to_select=10,
165+
n_terms_to_select=5,
163166
max_delay=max_delay,
164167
poly_degree=3,
165168
verbose=0,

fastcan/narx.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,10 @@ def _evaluate_term(feat_ids, delay_ids, X, y_hat, k):
648648
@staticmethod
649649
def _expression(X, y_hat, coef, intercept, feat_ids, delay_ids, output_ids, k):
650650
y_pred = np.copy(intercept)
651-
for i, term_id in enumerate(feat_ids):
651+
for i, feat_id in enumerate(feat_ids):
652652
output_i = output_ids[i]
653653
y_pred[output_i] += coef[i] * NARX._evaluate_term(
654-
term_id, delay_ids[i], X, y_hat, k
654+
feat_id, delay_ids[i], X, y_hat, k
655655
)
656656
return y_pred
657657

@@ -759,13 +759,22 @@ def _update_dydx(X, y_hat, coef, feat_ids, delay_ids, output_ids, cfd_ids):
759759
x_ids = np.arange(n_x)
760760

761761
dydx = np.zeros((n_samples, n_y, n_x), dtype=float)
762-
for k in range(max_delay, n_samples):
762+
at_init = True
763+
init_k = 0
764+
for k in range(n_samples):
765+
if not np.all(np.isfinite(X[k])):
766+
at_init = True
767+
init_k = k + 1
768+
continue
769+
if k - init_k == max_delay:
770+
at_init = False
771+
772+
if at_init:
773+
continue
763774
# Compute terms for time step k
764775
terms = np.ones(n_x, dtype=float)
765776
for j in range(n_coefs):
766777
terms[j] = NARX._evaluate_term(feat_ids[j], delay_ids[j], X, y_hat, k)
767-
if not np.all(np.isfinite(terms)):
768-
break
769778

770779
# Update constant terms of Jacobian
771780
dydx[k, y_ids, x_ids] = terms

0 commit comments

Comments
 (0)