Skip to main content

replacement - Continuous substitution of one equation into another and summarizing terms


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[___].


enter image description here


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

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...