|
44 | 44 | # %%
|
45 | 45 | # Build term libriary
|
46 | 46 | # -------------------
|
47 |
| -# |
48 | 47 | # To build a polynomial NARX model, it is normally have two steps:
|
49 | 48 | #
|
50 |
| -# 1. Search the structure of the model, i.e., the terms in the model, e.g., |
51 |
| -# :math:`u_0(k-1)u_0(k-3)`, :math:`u_0(k-2)u_1(k-3)`, etc. |
52 |
| -# 2. Learn the coefficients before the terms. |
| 49 | +# #. Search the structure of the model, i.e., the terms in the model, e.g., |
| 50 | +# :math:`u_0(k-1)u_0(k-3)`, :math:`u_0(k-2)u_1(k-3)`, etc. |
| 51 | +# |
| 52 | +# #. Learn the coefficients of the terms. |
53 | 53 | #
|
54 | 54 | # To search the structure of the model, the candidate term libriary should be
|
55 |
| -# constructed. |
| 55 | +# constructed by the following two steps. |
| 56 | +# |
| 57 | +# #. Time-shifted variables: the raw input-output data, i.e., :math:`u_0(k)`, |
| 58 | +# :math:`u_1(k)`, and :math:`y(k)`, are converted into :math:`u_0(k-1)`, |
| 59 | +# :math:`u_1(k-2)`, etc. |
56 | 60 | #
|
57 |
| -# 1. Time-shifted variables: the raw input-output data, i.e., :math:`u0(k)`, |
58 |
| -# :math:`u1(k)`, and :math:`y(k)`, are converted into :math:`u0(k-1)`, :math:`u1(k-2)`, |
59 |
| -# etc. |
60 |
| -# 2. Nonlinear terms: the time-shifted variables are onverted to nonlinear terms |
61 |
| -# via polynomial basis functions, e.g., :math:`u_0(k-1)^2`, :math:`u_0(k-1)u_0(k-3)`, |
62 |
| -# etc. |
| 61 | +# #. Nonlinear terms: the time-shifted variables are onverted to nonlinear terms |
| 62 | +# via polynomial basis functions, e.g., :math:`u_0(k-1)^2`, |
| 63 | +# :math:`u_0(k-1)u_0(k-3)`, etc. |
63 | 64 | #
|
64 | 65 | # .. rubric:: References
|
65 | 66 | #
|
|
116 | 117 | # ----------------
|
117 | 118 | # As the polynomical NARX is a linear function of the nonlinear tems,
|
118 | 119 | # the coefficient of each term can be easily estimated by oridnary least squares.
|
| 120 | +# In the printed NARX model, it is found that :class:`FastCan` selects the correct |
| 121 | +# terms and the coefficients are close to the true values. |
119 | 122 |
|
120 | 123 | from fastcan.narx import NARX, print_narx
|
121 | 124 |
|
|
126 | 129 |
|
127 | 130 | narx_model.fit(X, y)
|
128 | 131 |
|
129 |
| -# In the printed NARX model, it is found that :class:`FastCan` selects the correct |
130 |
| -# terms and the coefficients are close to the true values. |
131 | 132 | print_narx(narx_model)
|
| 133 | +# %% |
| 134 | +# Automaticated NARX modelling workflow |
| 135 | +# ------------------------------------- |
| 136 | +# We provide :meth:`narx.make_narx` to automaticate the workflow above. |
| 137 | + |
| 138 | +from fastcan.narx import make_narx |
| 139 | + |
| 140 | +auto_narx_model = make_narx( |
| 141 | + X=X, |
| 142 | + y=y, |
| 143 | + n_features_to_select=4, |
| 144 | + max_delay=3, |
| 145 | + poly_degree=2, |
| 146 | + verbose=0, |
| 147 | +).fit(X, y) |
| 148 | + |
| 149 | +print_narx(auto_narx_model) |
| 150 | + |
132 | 151 |
|
133 | 152 | # %%
|
134 | 153 | # Plot NARX prediction performance
|
|
0 commit comments