How to calculate exponential moving average (EMA) on Explorer?

I’m trying to implement an exponential moving average (EMA) on Nx/Explorer.

The formula for EMA(x) is:
EMA(x1) = x1
EMA(xn) = α * xn + (1 - α) * EMA(xn-1)

I think I need somehow previous value of EMA in rolling series . I can’t figure out how to do it.

Thanks in advance for a hint .

Consider a rearrangement of the equations above:

EMA(x_1) = x_1
(a-1)*EMA(x_(n-1)) + EMA(x_n) = a * x_n

This is a system of linear equations for all the values of EMA(x_n):

A * EMA = b


EMA = column vector of EMA values
b = column vector of [x_1, a * x_2, a * x_3, ... , a * x_N]
A = square NxN matrix
  1     0   0  0 ...
(a-1)   1   0  0 ...
  0   (a-1) 1  0 ...
     ...more rows...
          ...  0  0 (a-1) 1

The straightforward approach from here would be to use Nx.LinAlg.solve/2.

For large data sets, the matrix A will be unwieldy and mostly zero - there are specialized algorithms for solving “banded” equations like this with less storage.

I think I need something similar to Enum.reduce or Enum.map_reduce , for example DataFrame.map_reduce or Series.reduce .

Series.cum_sum needs to store the accumulator to perform somehow the computation .

Is it possible to implement those functionality inside Explorer lib to keep accumulator ?