I have the following equations where I need to lag and substitute one equation into another back and forth to obtain equation 1 expressed in terms of $i_{t-2}$, $i_{t-3}$ and earlier.
$x_{t}=a_{0}+a_{1}x_{t-1}+a_{2}y_{t-1}+\epsilon_{t} \quad (Eq. 1)$
$y_{t}=b_{0}+b_{1}y_{t-1}+b_{2}i_{t-1}+\eta_{t} \quad (Eq. 2)$
e.g.
{x[t]->a[0]+a[1] x[t-1] + a[2] y[t-1] + \[Epsilon][t], y[t]->b[0]+b[1] y[t-1] + b[2] i[t-1] + \[Eta][t]}
I look for an automated way to the following steps that I am able to manually, yet.
Step 1) Lag equation 2 once and substitute it into equation 1 to get:
$x_{t}=a_{0}+a_{1}x_{t-1}+a_{2}(b_{0}+b_{1}y_{t-2}+b_{2}i_{t-2}+\eta_{t-1})+\epsilon_{t}$
where rearranging the terms give
$x_{t}=a_{0}+a_{2}b_{0}+a_{1}x_{t-1}+a_{2}b_{1}y_{t-2}+a_{2}b_{2}i_{t-2}+\epsilon_{t}+a_{2}\eta_{t-1} \quad (Eq. 3)$
Step 2) As $x_{t-1}$ is a function of $y_{t-2}$, we substitute it in equation 3 to obtain:
$x_{t}=a_{0}+a_{2}b_{0}+a_{1}(a_{0}+a_{1}x_{t-2}+a_{2}y_{t-2}+\epsilon_{t-1})+a_{2}b_{1}y_{t-2}+a_{2}b_{2}i_{t-2}+\epsilon_{t}+a_{2}\eta_{t-1}$
Collecting and rearranging the terms gives the following:
$x_{t}=a_{0}(1+a_{1})+a_{2}b_{0}+a_{1}^{2}x_{t-2}+a_{2}(a_{1}+b_{1})y_{t-2}+a_{2}b_{2}i_{t-2}+\epsilon_{t}+a_{1}\epsilon_{t-1}+a_{2}\eta_{t-1} \quad (Eq. 4)$
Step 3) Note that $y_{t-2}$ is a function of $i_{t-3}$ i.e. $y_{t-2}=b_{0}+b_{1}y_{t-3}+b_{2}i_{t-3}+\eta_{t-2}$
Hence, substituting in for $y_{t-2}$ into equation 4 gives the following:
$x_{t}=a_{0}(1+a_{1})+a_{2}b_{0}+a_{1}^{2}x_{t-2}+a_{2}(a_{1}+b_{1})(b_{0}+b_{1}y_{t-3}+b_{2}i_{t-3}+\eta_{t-2})+a_{2}b_{2}i_{t-2}+\epsilon_{t}+a_{1}\epsilon_{t-1}+a_{2}\eta_{t-1}$
Rearranging ad collecting the terms give the solution as:
$x_{t}=a_{0}(1+a_{1})+a_{2}b_{0}(1+a_{1}+b_{1})+a_{1}^{2}x_{t-2}+a_{2}b_{1}(a_{1}+b_{1})y_{t-3}+a_{2}b_{2}i_{t-2}+a_{2}b_{2}(a_{1}+b_{1})i_{t-3}+\epsilon_{t}+a_{1}\epsilon_{t-1}+a_{2}\eta_{t-1}+a_{2}(a_{1}+b_{1})\eta_{t-2}$
As you can tell, expressing $x_{t}$ in terms of older lags would make the derivation extremely tedious. Hence, I look for an automated way of doing the steps I outlined above using Mathematica (or an alternative software if any).
Thanks in advance for your help. Best,
Answer
Explicit numeric time $t$
If you're interested in evaluating for specific numeric $t$, you can always do something like:
Clear[x]; Clear[y]
x[t_?NumberQ] :=
x[t] = a[0] + a[1] x[t - 1] + a[2] y[t - 1] + \[Epsilon][t]
y[t_?NumberQ] :=
y[t] = b[0] + b[1] y[t - 1] + b[2] i[t - 1] + \[Eta][t]
x[0] = x0;
y[0] = y0;
Then e.g. x[3]
yields:
a[0] + \[Epsilon][3] +
a[1] (a[0] +
a[1] (a[0] + x0 a[1] + y0 a[2] + \[Epsilon][1]) + \[Epsilon][2] +
a[2] (b[0] + y0 b[1] + b[2] i[0] + \[Eta][1])) +
a[2] (b[0] + b[2] i[1] +
b[1] (b[0] + y0 b[1] + b[2] i[0] + \[Eta][1]) + \[Eta][2])
(See below for a pretty TeX'd version)
Symbolic nested time $t\mapsto t-1$
If you want to do the formal $t$ manipulation symbolically you can go a different route:
xtRule = x[t] ->
a[0] + a[1] x[t - 1] + a[2] y[t - 1] + \[Epsilon][t];
ytRule = y[t] -> b[0] + b[1] y[t - 1] + b[2] i[t - 1] + \[Eta][t];
Let's first build a helper function to get the closest to $t$ time that appears in an x[_]
or a y[_]
in a rule.
getClosestT[
Rule[from_,
to_]] := (to /. x[a_] :> Sow[a] /. y[a_] :> Sow[a] // Reap //
Last // Flatten // Union // Last)
Usage: getClosestT[xtRule]
yields -1 + t
.
Here's how we can use it. Let's iterate on xtRule
.
newRule =
xtRule /. ({xtRule, ytRule} /. t -> getClosestT[xtRule]) //
ExpandAll // Collect[#, {x[_], y[_]}, FullSimplify] &
yielding:
x[t] -> a[0] + a[0] a[1] + a[2] b[0] + a[1]^2 x[-2 + t] +
a[2] (a[1] + b[1]) y[-2 + t] +
a[1] \[Epsilon][-1 + t] + \[Epsilon][t] +
a[2] (b[2] i[-2 + t] + \[Eta][-1 + t])
Now let's iterate manually substituting the closest to $t$ time that appears in xtRule
.
newRule =
xtRule /. ({xtRule, ytRule} /.
t -> (Last[xtRule] /. x[a_] :> Sow[a] /. y[a_] :> Sow[a] //
Reap // Last // Flatten // Union // Last)) // ExpandAll //
Collect[#, {x[_], y[_]}, FullSimplify] &
yielding
x[t] -> a[0] + a[0] a[1] + a[2] b[0] + a[1]^2 x[-2 + t] +
a[2] (a[1] + b[1]) y[-2 + t] +
a[1] \[Epsilon][-1 + t] + \[Epsilon][t] +
a[2] (b[2] i[-2 + t] + \[Eta][-1 + t])
We can do this again:
newRule /. ({xtRule, ytRule} /. t -> getClosestT[newRule]) //
ExpandAll // Collect[#, {x[_], y[_]}, FullSimplify] &
yielding
x[t] -> a[0] (1 + a[1] + a[1]^2) + a[2] b[0] (1 + a[1] + b[1]) +
a[2] (a[1] + b[1]) b[2] i[-3 + t] + a[1]^3 x[-3 + t] +
a[2] (a[1]^2 + a[1] b[1] + b[1]^2) y[-3 + t] +
a[1] (a[1] \[Epsilon][-2 + t] + \[Epsilon][-1 + t]) + \[Epsilon][
t] + a[2] (b[
2] i[-2 + t] + (a[1] + b[1]) \[Eta][-2 + t] + \[Eta][-1 + t])
In fact we can do this as many times as we like. Let's build a helper function:
applyMultTimesTo[rule_, num_] :=
Nest[ (# /. ({xtRule, ytRule} /. t -> getClosestT[#]) // ExpandAll //
Collect[#, {x[_], y[_]}, FullSimplify] &) &,
rule, num]
Usage: applyMultTimesTo[xRule,4]
yielding:
x[t] -> a[0] (1 + a[1] (1 + a[1]) (1 + a[1]^2)) +
a[2] b[0] (a[1]^3 + a[1]^2 (1 + b[1]) + (1 + b[1]) (1 + b[1]^2) +
a[1] (1 + b[1] + b[1]^2)) +
a[2] (a[1] + b[1]) (a[1]^2 + b[1]^2) b[2] i[-5 + t] +
a[2] (a[1]^2 + a[1] b[1] + b[1]^2) b[2] i[-4 + t] +
a[1] a[2] b[2] i[-3 + t] + a[2] b[1] b[2] i[-3 + t] +
a[2] b[2] i[-2 + t] + a[1]^5 x[-5 + t] +
a[2] (a[1]^4 + a[1]^3 b[1] + a[1]^2 b[1]^2 + a[1] b[1]^3 +
b[1]^4) y[-5 + t] + a[1]^4 \[Epsilon][-4 + t] +
a[1]^3 \[Epsilon][-3 + t] + a[1]^2 \[Epsilon][-2 + t] +
a[1] \[Epsilon][-1 + t] + \[Epsilon][t] +
a[1]^3 a[2] \[Eta][-4 + t] + a[1]^2 a[2] b[1] \[Eta][-4 + t] +
a[1] a[2] b[1]^2 \[Eta][-4 + t] + a[2] b[1]^3 \[Eta][-4 + t] +
a[1]^2 a[2] \[Eta][-3 + t] + a[1] a[2] b[1] \[Eta][-3 + t] +
a[2] b[1]^2 \[Eta][-3 + t] + a[1] a[2] \[Eta][-2 + t] +
a[2] b[1] \[Eta][-2 + t] + a[2] \[Eta][-1 + t]
We can see that we land on the same expressions as our explicit time calculation, by substituting in an explicit time. E.g.
(applyMultTimesTo[xtRule, 5] /. t -> 6 // ExpandAll) /. Rule :> Equal
yields True
.
Bonus: Pretty Formatting
If it's easier for you to read with everything written as subscripts, and you're using an appropriate Display, i.e. notebook interface, you can format appropriately using something as follows.
Format[a[i_]] := SubscriptBox[a, i] // DisplayForm
Format[y[i_]] :=
SubscriptBox[Style[y, {Red, Bold}], i] // DisplayForm
Format[x[i_]] := SubscriptBox[Style[x, {Blue, Bold}], i] // DisplayForm
Format[b[i_]] := SubscriptBox[b, i] // DisplayForm
Format[\[Eta][i_]] := SubscriptBox[\[Eta], i] // DisplayForm
Format[\[Epsilon][i_]] := SubscriptBox[\[Epsilon], i] // DisplayForm
Format[i[j_]] := SubscriptBox[i, j] // DisplayForm
You'll note I made all my $x$'s display Blue and all my $y$'s display Red to make it easier to see I was at the same level after using applyMultTimesTo[___]
.
Giving formatting definitions allows, btw., a nice rendering via TeXForm
:
i.e. x[3]//TeXForm
yields a string which renders as: $a_1 \left(a_2 \left(b_2 i_0+b_1 \text{y0}+b_0+\eta _1\right)+a_1 \left(a_1 \text{x0}+a_2 \text{y0}+a_0+\epsilon _1\right)+a_0+\epsilon _2\right)+a_2 \left(b_1 \left(b_2 i_0+b_1 \text{y0}+b_0+\eta _1\right)+b_2 i_1+b_0+\eta _2\right)+a_0+\epsilon _3$
Comments
Post a Comment