Probability Model

I have the following probability model:

$(X_k|\text{ PastHistory}_{k-1}, \theta_0, \theta_1, \theta_2) \sim (\pi\cdot N(\theta_1+\theta_0\cdot X_{k-1},1)+(1-\pi)\cdot N(\theta_2+\theta_0\cdot X_{k-1},1))$

and $X_k=0.5X_{k-1}+\epsilon_k$ with $\epsilon_k \sim N(0,1)$

I've tried something like:

Sample[n_] :=
Module[{lst},
lst = {RandomVariate[NormalDistribution[0, 1/(1 - 0.5^2)]]};
element = 1;
While[element <= n,
AppendTo[lst,
RandomVariate[
TransformedDistribution[
p*X1 + (1 - p) X2, {
X1 \[Distributed]
NormalDistribution[θ1 + θ0*lst[[element]], 1],
X2 \[Distributed]
NormalDistribution[θ2 + θ0*lst[[element]], 1]
}]]];
element = element + 1;
];
lst
];


Is this correct?

-
This way of collecting results using AppendTo is very slow. See also the Q&A something faster than AppendTo in a loop –  Jacob Akkerboom Mar 3 '14 at 16:46
Note also that TransformedDistribution[p*X1 + (1 - p) X2, {X1 \[Distributed] NormalDistribution[θ1 + θ0*el, 1], X2 \[Distributed] NormalDistribution[θ2 + θ0*el, 1]}] simplifies to NormalDistribution[el θ0+p (θ1-θ2)+θ2,Sqrt[1+2 (-1+p) p]]. It is better to put the simplified version in the code, otherwise this simplification will be done at each iteration. –  Jacob Akkerboom Mar 3 '14 at 17:01
Can you please explain the relevance of the equation $X_k=0.5X_{k-1}+\epsilon_k$ with $\epsilon_k \sim N(0,1)$? This seems to imply that θ0 = 1/2 for instance. You seem to only use the first equation for $X_k$ in your code. –  Jacob Akkerboom Mar 3 '14 at 17:13
You're right Jacob. I've already managed to programme it, but in different way. Thanks anyway. ;) –  An old man in the sea. Mar 3 '14 at 20:05
You are welcome, sir :) –  Jacob Akkerboom Mar 3 '14 at 22:01