Code for Simple Moving Average and Weighted Moving Average.

Simple Moving Average

simple[source]

simple(ts, n=1)

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",)

Weighted Moving Average

a = np.array([1,2])

if type(a) is list:
    print(a)

weighted[source]

weighted(ts, ws)

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",)

Dataframes and Figures

Generates a Time Series Dataframe and a Figure Object

The Values of The Time Series are Simulated

Includes Forecasting with Moving Averages

__moving_average[source]

__moving_average(*args, kind='simple', df=None, ts_col=None, **kwargs)

Forecasts with a Moving Average


Parameters

kind : str , default 'simple'. It can be:

 'simple' : Simple Moving Average.
 'weighted' : Weighted Moving Average.

df : DataFrame, default None. If df is None a dataframe with simulated data is generated.

ts_col : str, default None. The column name with the time series. Set to 'time_series' when df is None. *args : int. Each value represent a Moving Average Forecast and its corresponding window size **kwargs : passed to __pandas_time_series


Returns

tuple: First element is the Pandas Dataframe and the second is ploty's figure object

SMA[source]

SMA(*args, df=None, ts_col=None, **kwargs)

Forecasts with Simple Moving Average


Parameters

df : DataFrame, default None. If df is None a dataframe with simulated data is generated. ts_col : str, default None. The column name with the time series. Set to 'time_series' when df is None. *args : int. Each value represent a Moving Average Forecast and its corresponding window size **kwargs : passed to __pandas_time_series


Returns

tuple: First element is the Pandas Dataframe and the second is ploty's fig object

_,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()

WMA[source]

WMA(*args, df=None, ts_col='time_series', **kwargs)

Forecasts with Weighted Moving Average


Parameters

df : DataFrame, default None. If df is None a dataframe with simulated data is generated. ts_col : str, default 'time_series'. The column name with the time series. Ignored if df is None. *args : int. Each value represent a Moving Average Forecast and its corresponding window size

**kwargs : passed to __pandas_time_series

Returns

tuple: First element is the Pandas Dataframe and the second is ploty's fig object

_, 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()

Further Tests

Test with external libraries

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))