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

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...