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 - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.