How to do it...

Execute the following steps to use the exponential smoothing methods to create forecasts of Google's stock prices.

  1. Import the libraries:
import pandas as pd
import numpy as np
import yfinance as yf
from datetime import date
from statsmodels.tsa.holtwinters import (ExponentialSmoothing,
SimpleExpSmoothing,
Holt)

  1. Download the adjusted stock prices for Google:
df = yf.download('GOOG',
start='2010-01-01',
end='2018-12-31',
adjusted=True,
progress=False)
  1. Aggregate to monthly frequency:
goog = df.resample('M') 
.last()
.rename(columns={'Adj Close': 'adj_close'})
.adj_close
  1. Create the training/test split:
train_indices = goog.index.year < 2018
goog_train = goog[train_indices]
goog_test = goog[~train_indices]

test_length = len(goog_test)
  1. Plot the prices:
goog.plot(title="Google's Stock Price")

The preceding code generates the following plot:

  1. Fit three SES models and create forecasts for them:
ses_1 = SimpleExpSmoothing(goog_train).fit(smoothing_level=0.2)
ses_forecast_1 = ses_1.forecast(test_length)

ses_2 = SimpleExpSmoothing(goog_train).fit(smoothing_level=0.5)
ses_forecast_2 = ses_2.forecast(test_length)

ses_3 = SimpleExpSmoothing(goog_train).fit()
alpha = ses_3.model.params['smoothing_level']
ses_forecast_3 = ses_3.forecast(test_length)
  1. Plot the original prices and the models' results:
goog.plot(color=COLORS[0], 
title='Simple Exponential Smoothing',
label='Actual',
legend=True)

ses_forecast_1.plot(color=COLORS[1], legend=True,
label=r'$alpha=0.2$')
ses_1.fittedvalues.plot(color=COLORS[1])

ses_forecast_2.plot(color=COLORS[2], legend=True,
label=r'$alpha=0.5$')
ses_2.fittedvalues.plot(color=COLORS[2])

ses_forecast_3.plot(color=COLORS[3], legend=True,
label=r'$alpha={0:.4f}$'.format(alpha))
ses_3.fittedvalues.plot(color=COLORS[3])

Executing the code results in the following plot:

In the preceding plot, we can see the characteristic of the SES we described in the introduction to this recipe—the forecast is a flat line. We can also see that the optimal value that was selected by the statsmodels optimization routine is close to 1. Additionally, the fitted line of the third model is effectively the line of the observed prices shifted to the right.

  1. Fit three variants of Holt's smoothing model and create forecasts:
# Holt's model with linear trend
hs_1 = Holt(goog_train).fit()
hs_forecast_1 = hs_1.forecast(test_length)

# Holt's model with exponential trend
hs_2 = Holt(goog_train, exponential=True).fit()
hs_forecast_2 = hs_2.forecast(test_length)

# Holt's model with exponential trend and damping
hs_3 = Holt(goog_train, exponential=False,
damped=True).fit(damping_slope=0.99)
hs_forecast_3 = hs_3.forecast(test_length)
Holt(goog_train, exponential=True) is equivalent to ExponentialSmoothing(goog_train, trend='mul').
  1. Plot the original prices and the models' results:
goog.plot(color=COLORS[0],
title="Holt's Smoothing models",
label='Actual',
legend=True)

hs_1.fittedvalues.plot(color=COLORS[1])
hs_forecast_1.plot(color=COLORS[1], legend=True,
label='Linear trend')

hs_2.fittedvalues.plot(color=COLORS[2])
hs_forecast_2.plot(color=COLORS[2], legend=True,
label='Exponential trend')

hs_3.fittedvalues.plot(color=COLORS[3])
hs_forecast_3.plot(color=COLORS[3], legend=True,
label='Exponential trend (damped)')

Executing the code results in the following plot:

We can already observe an improvement since the lines are not flat anymore, as compared to SES. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.59