Exponential Moving Average based on a box filter, matching numpy example EMA
npm install boxemaTo use:
``javascript
const Ema = require('boxema');
// Create an EMA with period 8
const ema8 = Ema.create(8);
[1,3,5,7,9].forEach((x) => {
console.log("EMA is now " + ema8.update(x));
});
console.log("it can also be used as a list converter:");
console.log([2,4,6,8].map(ema8));
console.log("you can get the last result using ema.get()");
console.log(ema8.get());
`
The python code on which this is based is as follows, note the commented line which is not possible
with a rolling EMA.
`python``
def moving_average(x, n):
weights = np.exp(np.linspace(-1., 0., n))
weights /= weights.sum()
a = np.convolve(x, weights, mode='full')[:len(x)]
#a[:n] = a[n] ## This is not possible with a rolling EMA so it is removed.
return a