Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a function $$F(X_{t+1},Y_{t}^{med})= \alpha X_{t+1} + (1-\alpha) Y_{t}^{med},$$ where $$Y_{t}^{med} = Median(Y_1, Y_2,... Y_t).$$ Moreover, $Y_1 = X_1$ and $Y_t = F(X_{t},Y_{t-1}^{med})$ $\forall t>1$.

Suppose $X_t \sim X$ is a IID random variable following a Beta distribution, $X \sim Beta(2,1)$.

I wish to plot the evolution of $Y_{t}^{med}$ over time.

Currently, I have:

1) The function:

f[a_, {x_, y_}] := a x + (1 - a) y

2) Generate data. Here, I don't know how to generate the running median, that is $Y_{t}^{med}$ in Mathematica.

sim[length_] := 
 Module[{rv = RandomVariate[BetaDistribution[2, 1], length], y, yMed},
  y[1] = First@rv;
  yMed[t_Integer] := yMed[t] = MovingMedian[rv, t];
  y[t_Integer] := y[t] = f[0.5, {rv[[t]], yMed[t - 1]}];
  yMed /@ Range[length]]

3) Create the plot/graph for 100 iterations, 5 random walks:

ListPlot[Table[sim[100], {5}], PlotRange -> All, Joined -> True, 
 Frame -> False, AxesOrigin -> {0, 0}, AxesLabel -> {t, Y}, 
 GridLines -> {{}, {2/3}}, GridLinesStyle -> Directive[Gray, Dashed]]
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Using

sim[length_] := 
 Module[{rv = RandomVariate[BetaDistribution[2, 1], length], y, yMed},
  y[1] = First@rv;
  yMed[t_Integer] := yMed[t] = Median[y /@ Range[t]];
  y[t_Integer] := y[t] = f[0.5, {rv[[t]], yMed[t - 1]}];
  yMed /@ Range[length]
 ]

results in plots like

randomMedianBetaWalk

Here Median[y /@ Range[t]] calculates the median for y[1] to y[t], as stated in your question.

MovingMedian is used to calculate the median values for a moving window with fixed length, e.g. the median for y[t-10] to y[t].
However, for such a simulation

sim2[length_Integer, medianW_Integer] := 
 Module[{rv = RandomVariate[BetaDistribution[2, 1], length], y, yMed},
 y[1] = First@rv;
 yMed[t_Integer] := 
 yMed[t] = Median[y /@ Range[Max[1, t - medianW], t]];
 y[t_Integer] := y[t] = f[0.5, {rv[[t]], yMed[t - 1]}];
 yMed /@ Range[length]]

ListPlot[Table[sim2[100, 10], {5}], PlotRange -> All, Joined -> True, 
 Frame -> False, AxesOrigin -> {0, 0}, AxesLabel -> {t, Y}, 
 GridLines -> {{}, {2/3}}, GridLinesStyle -> Directive[Gray, Dashed]]

randomMedianBetaWalk-10Window

would be the better implementation in your case.

share|improve this answer
    
Thanks a lot @Karsten. –  flo Aug 28 at 9:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.