3
$\begingroup$

If anyone knows how to write a mathematica code for n-level exponential moving average or someone has written already the code ?

$\endgroup$
0

2 Answers 2

5
$\begingroup$

Just type ExponentialMovingAverage in the Documentation Center (it is part of the language)

$\endgroup$
2
  • $\begingroup$ there is 2nd and 3rd level $\endgroup$
    – Augustus
    May 24, 2014 at 18:17
  • 1
    $\begingroup$ @Augustus - I am not aware of 2nd or 3rd level exponential averaging. Even this "simple" form is dangerous enough because everything depends on the right choice of the smoothing factor (the second argument) which is purely heuristic. Exponential averaging is widely used in economic forecasting (simply google "forecasting"), but you need to constantly compare the actual development with the forecasted to, hopefully, find a meaningful smoothing factor. $\endgroup$
    – eldo
    May 24, 2014 at 19:17
1
$\begingroup$

Here is the 1st and 3rd exponential smoothing. Make sure that the tMax is very long, compared to simulation time. Otherwise the solution will be inaccurate. May need to choose different integration methods. The avgTime = 10 indicates the time duration where the smoothed value sX[t] will reach 63% of the step input of 100% (1st order exponential smoothing*)

x[t] = input, sX[t] = exponentially smoothed input, avgTime = smoothing period. Note that the input x[t] is modeled as a state variable with x'[t]==0 and x[0]==100 (e.g., a step input at t=0). The x'[t] could be any function.

1st order smoothing:

sol1 = NDSolve[{
    avgTime = 10;
    x'[t] == 0.0,
    sX'[t] == (x[t] - sX[t])/avgTime,
    x[0] == 100, sX[0] == 0}, {x, sX}, {t, 0, tMax = 1000}].

Plot[Evaluate[{x[t], sX[t]} /. sol1], {t, 0, 100}, PlotRange -> All]

3rd order smoothing:

sol3 = NDSolve[{
   avgTime = 10;
   x'[t] == 0.0,
   sX3'[t] == (sX2[t] - sX3[t])/(avgTime/3),
   sX2'[t] == (sX1[t] - sX2[t])/(avgTime/3),
   sX1'[t] == (    x[t] - sX1[t])/(avgTime/3),
   x[0] == 100, sX1[0] == sX2[0] == sX3[0] == 0},
  {x, sX1, sX2, sX3}, {t, 0, tMax=1000}]

 Plot[Evaluate[{x[t], sX3[t]} /. sol3], {t, 0, 40}, PlotRange -> All]

The n-th order smoothing is an extention such that (1) n-state variables are added (e.g., sXn[t] and (2) avgTime/n for each sXn[t].

I appreciate if someone could generalize the 3rd order exponential smoothing to n-th order smoothing and create Mathematica functions.

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