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

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

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...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...