0
$\begingroup$

I would have the following derivation to calculate the recursive moving average and I'd like to follow it as closely as possible in Mathematica.

For the sequence where $y(i)$ is considered as an observation of a at the ith instant:

$y(i) = a + e(i); i = 1,2,...,N$

The mean of the entire sequence is $\widetilde{a}$:

$\widetilde{a}(k) = \frac{1}{k}\sum\limits_{i=1}^{k}{y(i)}$

At for the $(k - 1)^{th}$ instant:

$\widetilde{a}(k-1)=\frac{1}{k-1}\sum\limits_{i=1}^{k-1}{y(i)}$

I am trying to use Mathematica to rearrange the above equations to obtain the following equation for the recursive mean:

$\widetilde{a}(k) = \widetilde{a}(k-1)+\frac{1}{k}[y(k)-\widetilde{a}(k-1)]$

I have the definitions of for each of the expressions:

OverTilde[a][k_, y_] := (1/k) Sum[y[i], { i, 1, k}]
eq1 = OverTilde[a][k, y]
eq2 = OverTilde[a][k - 1, y]

How do I substitute eq1 into eq2 in order to obtain the equation for the recursive mean?

$\endgroup$

1 Answer 1

2
$\begingroup$

Mathematica currently has a gap in its sum functionality, but it is still possible to verify the recursive mean identity. Here is the code:

OverTilde[a][k_, y_] := (1/k) Sum[y[i], {i, 1, k}];
eq1 = OverTilde[a][k, y];
eq2 = OverTilde[a][k - 1, y];
rule = Sum[x_[j_], {j_, a_, b_}] :> x[b] + Sum[x[j], {j, a, b - 1}];
(eq1 /. rule) == eq2 + (y[k] - eq2)/k // Simplify

which returns True. The reason for rule is that Mathematica currently can not do this automatically.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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