# How to extract variables with time subscript and their coefficients

I have a expression of the following form:

$$x_{t}=a_{0}+a_{1}x_{t-1}+a_{2}y_{t-2}$$

where $a$'s are just coefficients and anything with time subscript is a variable. I would like to have list of variables in $x_t$ (that would be $x_{t-1}$ and $y_{t-2}$) and find the coefficient for variable say $y_{t-2}$ (which is $a_{2}$). Mathematica's Variables[x] and Coefficient[x] functions don't work here.

• Show us the expression IN MATHEMATICA CODE, rather than $\LaTeX$. – MarcoB Aug 17 '17 at 14:56

Using @Alucard's setup slightly modified:

b = Thread[Subscript[#1, #2] &[{x, y, z}, {t - 3, t - 4, t - 6}]];
bb2 =  Subscript[x, t] == Subscript[a, 5] Subscript[d, 9 + q] +
Dot[Subscript[c, #] & /@ Range[Length[b]], b]

Block[{Times = List, Plus = List},
Cases[#, {_, Subscript[_, s_ /; Not[FreeQ[s, t]]]}, Infinity]] /.
Subscript[v_, s : {__}] :> Subscript[v, Plus @@ s]& @ bb2


{{Subscript[c, 1], Subscript[x, -3 + t]}, {Subscript[c, 2], Subscript[y, -4 + t]}, {Subscript[c, 3], Subscript[z, -6 + t]}}

TeXForm[%]


$\left( \begin{array}{cc} c_1 & x_{t-3} \\ c_2 & y_{t-4} \\ c_3 & z_{t-6} \\ \end{array} \right)$

(example data)

b = Thread[Subscript[#1, #2 ] &[{x, y,z}, {t - 3, t - 4,t-6}]]

bb = Subscript[x, t] == Dot[Subscript[c, #] & /@ Range[Length[b] ], b]


$$x_t=c_1 x_{t-3}+c_2 y_{t-4}+c_3 z_{t-6}$$ applying

{bb[[2, #, 1]], bb[[2, #, 2]]} & /@ Range[Length[b]  ]


gives a 3x2 list with the variables and the corresponding coefficient

xtRule=x[t]->a+a x[t-1]+a y[t-2]+az[t-1]+\[Epsilon][t];
ytRule=y[t]->b+b y[t-1]+b i[t-2]+bz[t-1]+\[Eta][t];
ztRule=z[t]->pz[t-1]+pi[t-1]+\[Upsilon][t];
itRule=i[t]->r i[t-1]+(1-r)g y[t-2]+\[Omega][t];


You could do something like:

coefValues[Rule[from_, to_]] := Module[{vars, data, construct},
vars = to /. a_[t_ + b_] :> Sow[a[t + b]] // Reap // Last //
Flatten // Union;
construct["variables"] = vars;
(construct[#] = Coefficient[to, #]) & /@ vars;
construct[] = First[CoefficientArrays[to, vars]];
construct]


Usage:

In:= coefValues[xtRule]["variables"]

Out= {x[-1 + t], y[-2 + t], z[-1 + t]}

In:= coefValues[xtRule][x[t - 1]]

Out= a

In:= coefValues[xtRule][z[t - 1]]

Out= a

In:= coefValues[xtRule][]

Out= a + \[Epsilon][t]