Code for Simple Moving Average and Weighted Moving Average.
df = simulate_data.pandas_time_series()
ts = df['time_series'].to_numpy()
for n in (1, 4):
df[f'ma_{n}'] = simple(ts=ts, n=n)
fig = df.plot(
backend='plotly',
title=f'Simple Moving Average',
)
fig.update_layout(template="plotly_dark",)
a = np.array([1,2])
if type(a) is list:
print(a)
df = simulate_data.pandas_time_series(num_points=50)
ts = df['time_series'].to_numpy()
for ws in ([1], [1, 1.5, 2]):
#nws = np.array(ws, dtype=float)
df[f'weights_{ws}'] = weighted(ts=ts, ws=ws)
fig = df.plot(
backend='plotly',
title=f'Weighted Moving Average',
)
fig.update_layout(template="plotly_dark",)
_,fig = SMA(1,4)
fig.show()
df = pd.read_csv(
'../data/Electric_Production.csv',
index_col='DATE',
parse_dates=['DATE'],
)
ts_col = 'Electric Production'
df.columns = [ts_col]
_, fig = SMA(
1,
4,
df=df,
ts_col=ts_col,
)
fig.update_layout(
autosize=False,
width=1100,
height=450,
)
fig.update_traces(line=dict(width=0.8))
fig.show()
_, fig = WMA([1, 1, 2], [2, 1, 1])
fig.show()
df = pd.read_csv(
'../data/Electric_Production.csv',
index_col='DATE',
parse_dates=['DATE'],
)
ts_col = 'Electric Production'
df.columns = [ts_col]
_, fig = WMA(
[1, 2, 3, 4],
[3, 2, 1],
df=df,
ts_col=ts_col,
)
fig.update_layout(
autosize=False,
width=1100,
height=450,
)
fig.update_traces(line=dict(width=0.8))
fig.show()
window = 5
dff = simulate_data.pandas_time_series()
ts_col = dff.columns[0]
dff, _ = SMA(
window,
df=dff.copy(),
ts_col=ts_col,
)
dff['pandas_MA'] = dff[ts_col].rolling(window=window).mean().shift(+1)
delta_df = dff['pandas_MA'] - dff['simple_5']
delta_df.dropna(inplace=True)
assert all(delta_df < 10**(-10))