Given some data x
:
from pandas_datareader.data import DataReader as dr
x = np.squeeze(dr('DTWEXB', 'fred').dropna().values)
I want to calculate another vector y
as follows:
Where alpha equals 0.03, in this case.
Can I do this with scipy.lfilter?
. Similar question here, but in that case the starting value of the result is 0 and that is throwing something off.
My attempt:
from scipy.signal import lfilter
a = 0.03
b = 1 - a
y0 = x[0]
y = lfilter([a], [y0, -b], x)
The results should be:
true_y = np.empty(len(x))
for k in range(0, len(true_y)):
if k == 0:
true_y[k] = x[0]
else:
true_y[k] = a*x[k] + b*true_y[k-1]
print(true_y)
[ 101.1818 101.176862 101.16819314 ..., 120.9813121 120.92484874
120.85786628]