Meta-Model Structure Selection (MetaMSS) algorithm for building Polynomial NARX models¶
Example created by Wilson Rocha Lacerda Junior
In [1]:
Copied!
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sysidentpy.model_structure_selection import MetaMSS, FROLS
from sysidentpy.metrics import root_relative_squared_error
from sysidentpy.basis_function._basis_function import Polynomial
from sysidentpy.utils.display_results import results
from sysidentpy.utils.plotting import plot_residues_correlation, plot_results
from sysidentpy.residues.residues_correlation import (
compute_residues_autocorrelation,
compute_cross_correlation,
)
import pandas as pd import numpy as np import matplotlib.pyplot as plt from sysidentpy.model_structure_selection import MetaMSS, FROLS from sysidentpy.metrics import root_relative_squared_error from sysidentpy.basis_function._basis_function import Polynomial from sysidentpy.utils.display_results import results from sysidentpy.utils.plotting import plot_residues_correlation, plot_results from sysidentpy.residues.residues_correlation import ( compute_residues_autocorrelation, compute_cross_correlation, )
In [2]:
Copied!
df1 = pd.read_csv("examples/datasets/x_cc.csv")
df2 = pd.read_csv("examples/datasets/y_cc.csv")
df2[5000:80000].plot(figsize=(10, 4))
df1 = pd.read_csv("examples/datasets/x_cc.csv") df2 = pd.read_csv("examples/datasets/y_cc.csv") df2[5000:80000].plot(figsize=(10, 4))
In [3]:
Copied!
# we will decimate the data using d=500 in this example
x_train, x_test = np.split(df1.iloc[::500].values, 2)
y_train, y_test = np.split(df2.iloc[::500].values, 2)
# we will decimate the data using d=500 in this example x_train, x_test = np.split(df1.iloc[::500].values, 2) y_train, y_test = np.split(df2.iloc[::500].values, 2)
In [4]:
Copied!
basis_function = Polynomial(degree=2)
model = MetaMSS(
norm=-2,
xlag=3,
ylag=3,
estimator="recursive_least_squares",
k_agents_percent=10,
estimate_parameter=True,
maxiter=30,
n_agents=10,
loss_func="metamss_loss",
basis_function=basis_function,
random_state=42,
)
model.fit(X=x_train, y=y_train, X_test=x_test, y_test=y_test)
basis_function = Polynomial(degree=2) model = MetaMSS( norm=-2, xlag=3, ylag=3, estimator="recursive_least_squares", k_agents_percent=10, estimate_parameter=True, maxiter=30, n_agents=10, loss_func="metamss_loss", basis_function=basis_function, random_state=42, ) model.fit(X=x_train, y=y_train, X_test=x_test, y_test=y_test)
c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\utils\deprecation.py:37: FutureWarning: Passing a string to define the estimator will rise an error in v0.4.0. You'll have to use MetaMSS(estimator=LeastSquares()) instead. The only change is that you'll have to define the estimator first instead of passing a string like 'least_squares'. This change will make easier to implement new estimators and it'll improve code readability. warnings.warn(message, FutureWarning) c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\utils\deprecation.py:37: FutureWarning: You will not need to pass X_test and y_test in v0.4.0. You'll have to use MetaMSS(test_size=0.25) instead. This change will make easier to use the MetaMSS model and will follow the same structure of the other methods. warnings.warn(message, FutureWarning) c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\narmax_base.py:704: RuntimeWarning: overflow encountered in power regressor_value[j] = np.prod(np.power(raw_regressor, model_exponent)) c:\Users\wilso\miniconda3\envs\sysidentpy\lib\site-packages\numpy\core\fromnumeric.py:87: RuntimeWarning: invalid value encountered in reduce return ufunc.reduce(obj, axis, dtype, out, **passkwargs) c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\model_structure_selection\meta_model_structure_selection.py:451: RuntimeWarning: overflow encountered in square sum_of_squared_residues = np.sum(residues**2) c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\metrics\_regression.py:216: RuntimeWarning: overflow encountered in square numerator = np.sum(np.square((yhat - y))) c:\Users\wilso\miniconda3\envs\sysidentpy\lib\site-packages\numpy\core\fromnumeric.py:87: RuntimeWarning: overflow encountered in reduce return ufunc.reduce(obj, axis, dtype, out, **passkwargs) c:\Users\wilso\miniconda3\envs\sysidentpy\lib\site-packages\numpy\linalg\linalg.py:2567: RuntimeWarning: divide by zero encountered in power absx **= ord c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\metaheuristics\bpsogsa.py:194: RuntimeWarning: invalid value encountered in subtract agent_mass = (fitness_value - 0.99 * worst_fitness_value) / ( c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\metaheuristics\bpsogsa.py:194: RuntimeWarning: invalid value encountered in true_divide agent_mass = (fitness_value - 0.99 * worst_fitness_value) / (
Out[4]:
<sysidentpy.model_structure_selection.meta_model_structure_selection.MetaMSS at 0x1d3bb8b51f0>
In [5]:
Copied!
yhat = model.predict(X=x_test, y=y_test, steps_ahead=None)
rrse = root_relative_squared_error(y_test, yhat)
print(rrse)
r = pd.DataFrame(
results(
model.final_model,
model.theta,
model.err,
model.n_terms,
err_precision=8,
dtype="sci",
),
columns=["Regressors", "Parameters", "ERR"],
)
print(r)
plot_results(y=y_test, yhat=yhat, n=1000)
ee = compute_residues_autocorrelation(y_test, yhat)
plot_residues_correlation(data=ee, title="Residues", ylabel="$e^2$")
x1e = compute_cross_correlation(y_test, yhat, x_test)
plot_residues_correlation(data=x1e, title="Residues", ylabel="$x_1e$")
yhat = model.predict(X=x_test, y=y_test, steps_ahead=None) rrse = root_relative_squared_error(y_test, yhat) print(rrse) r = pd.DataFrame( results( model.final_model, model.theta, model.err, model.n_terms, err_precision=8, dtype="sci", ), columns=["Regressors", "Parameters", "ERR"], ) print(r) plot_results(y=y_test, yhat=yhat, n=1000) ee = compute_residues_autocorrelation(y_test, yhat) plot_residues_correlation(data=ee, title="Residues", ylabel="$e^2$") x1e = compute_cross_correlation(y_test, yhat, x_test) plot_residues_correlation(data=x1e, title="Residues", ylabel="$x_1e$")
0.026982533271740158 Regressors Parameters ERR 0 1 -4.4063E+02 0.00000000E+00 1 y(k-1) 1.3010E+00 0.00000000E+00 2 y(k-2) -3.6332E-01 0.00000000E+00 3 x1(k-2) 3.8003E+02 0.00000000E+00 4 x1(k-3) -1.4636E-01 0.00000000E+00 5 y(k-3)y(k-1) 1.8825E-05 0.00000000E+00 6 x1(k-1)y(k-1) -1.6079E-01 0.00000000E+00 7 x1(k-2)y(k-1) -7.8499E-02 0.00000000E+00 8 y(k-3)y(k-2) -1.3456E-05 0.00000000E+00 9 x1(k-1)y(k-2) 9.0411E-02 0.00000000E+00 10 x1(k-2)y(k-2) 2.3358E-02 0.00000000E+00 11 y(k-3)^2 2.7231E-06 0.00000000E+00 12 x1(k-1)y(k-3) -1.8255E-02 0.00000000E+00 13 x1(k-2)y(k-3) -4.0612E-03 0.00000000E+00 14 x1(k-3)y(k-3) -1.2115E-03 0.00000000E+00 15 x1(k-1)^2 1.1774E+02 0.00000000E+00 16 x1(k-2)x1(k-1) -2.0391E+00 0.00000000E+00 17 x1(k-3)x1(k-1) 3.5835E+00 0.00000000E+00 18 x1(k-3)^2 -7.3179E-01 0.00000000E+00