# Vector Autoregressive with External inputs (VARX)

In some fields, the use of VARX is widespread. VARX models are special cases of general linear models on the form

\begin{aligned} x^+ &= Ax + Bu + Ke\\ y &= Cx + Du + e \end{aligned}

which we show here by simulating the VARX model

$$$y_t = A_1 y_{t-1} + A_2 y_{t-2} + B_1 u_{t-1}$$$

and estimating a regular statespace model.

using ControlSystemIdentification, Plots

A1 = 0.3randn(2,2)
A2 = 0.3randn(2,2)
B1 = randn(2,2)

N = 300
Y = [randn(2), randn(2)]
u = randn(2, N)

for i = 3:N
yi = A1*Y[i-1] + A2*Y[i-2] + B1*u[:,i-1]
push!(Y, yi)
end

y = hcat(Y...)

d = iddata(y, u, 1)
InputOutput data of length 300, 2 outputs, 2 inputs, Ts = 1

We now estimate two models, one using subspace-based identification (subspaceid) and one using the prediction-error method (newpem). The VARX model we estimated had a state of order 4, two lags of $y$, each of which is a vector of length 2, we thus estimate models of order 4 below.

model1 = subspaceid(d, 4, zeroD=true)
model2, x0 = newpem(d, 4)

plot(
simplot(d, model1, title="Simulation subspaceid", layout=2),
simplot(d, model2, x0, title="Simulation PEM", layout=2),
titlefontsize=10,
)

The simulation indicates that the fit is close to 100%, i.e., the general linear model fit the VARX model perfectly, it does not however have exactly the same structure as the original VARX model.

The estimated models on statespace form can be converted to MIMO transfer functions (polynomial models) by calling tf:

using ControlSystemsBase: tf
tf(model2.sys)
TransferFunction{Discrete{Float64}, ControlSystemsBase.SisoRational{Float64}}
Input 1 to output 1
-0.3209050790118411z^3 - 0.06918115945653361z^2 + 0.04462019217147564z - 4.8037975589904774e-5
------------------------------------------------------------------------------------------------------
1.0z^4 + 0.7779549296992179z^3 + 0.07723928714803846z^2 - 0.027738755781612227z + 0.024842824492225673

Input 1 to output 2
-0.0744366625806423z^3 - 0.04817456509219782z^2 - 0.11602900280824063z - 2.1366444759834957e-5
------------------------------------------------------------------------------------------------------
1.0z^4 + 0.7779549296992179z^3 + 0.07723928714803846z^2 - 0.027738755781612227z + 0.024842824492225673

Input 2 to output 1
1.2125160337264063z^3 + 0.24797206914037484z^2 - 0.11069237983048444z + 0.005344976358345802
------------------------------------------------------------------------------------------------------
1.0z^4 + 0.7779549296992179z^3 + 0.07723928714803846z^2 - 0.027738755781612227z + 0.024842824492225673

Input 2 to output 2
-0.4701867030935567z^3 - 0.21829299481306333z^2 + 0.39322600881178715z - 0.017982840136177968
------------------------------------------------------------------------------------------------------
1.0z^4 + 0.7779549296992179z^3 + 0.07723928714803846z^2 - 0.027738755781612227z + 0.024842824492225673

Sample Time: 1.0 (seconds)
Discrete-time transfer function model

## Summary

The statespace model

\begin{aligned} x^+ &= Ax + Bu + Ke\\ y &= Cx + Du + e \end{aligned}

is very general and subsumes a lot of other, special-structure models, like the VARX model.