<jupyter_start><jupyter_text>Statsmodels Time Series Excercises - SolutionsFor this set of exercises we're using data from the Federal Reserve Economic Database (FRED) concerning the Industrial Production Index for Electricity and Gas Utilities from January 1970 to December 1989.Data source: https://fred.stlouisfed.org/series/IPG2211A2NIMPORTANT NOTE! Make sure you don't run the cells directly above the example output shown, otherwise you will end up writing over the example output!<jupyter_code># RUN THIS CELL
import pandas as pd
import numpy as np
%matplotlib inline

df = pd.read_csv('../Data/EnergyProduction.csv',index_col=0,parse_dates=True)
df.head()<jupyter_output><empty_output><jupyter_text>1. Assign a frequency of 'MS' to the DatetimeIndex.<jupyter_code># CODE HERE


# DON'T WRITE HERE
df.index.freq = 'MS'
df.index<jupyter_output><empty_output><jupyter_text>2. Plot the dataset.<jupyter_code># CODE HERE

# DON'T WRITE HERE
df.plot(figsize=(12,6)).autoscale(axis='x',tight=True);<jupyter_output><empty_output><jupyter_text>3. Add a column that shows a 12-month Simple Moving Average (SMA).&nbsp;&nbsp;&nbsp;&nbsp;Plot the result.<jupyter_code># DON'T WRITE HERE
df['SMA12'] = df['EnergyIndex'].rolling(window=12).mean()
df.plot(figsize=(12,6)).autoscale(axis='x',tight=True);<jupyter_output><empty_output><jupyter_text>4. Add a column that shows an Exponentially Weighted Moving Average (EWMA) with a span of 12 using the statsmodels SimpleExpSmoothing function. Plot the result.<jupyter_code># DON'T FORGET TO PERFORM THE IMPORT!




# DON'T WRITE HERE
from statsmodels.tsa.holtwinters import SimpleExpSmoothing

df['SES12']=SimpleExpSmoothing(df['EnergyIndex']).fit(smoothing_level=2/(12+1),optimized=False).fittedvalues.shift(-1)
df.plot(figsize=(12,6)).autoscale(axis='x',tight=True);<jupyter_output><empty_output><jupyter_text>5. Add a column to the DataFrame that shows a Holt-Winters fitted model using Triple Exponential Smoothing with multiplicative models. Plot the result.<jupyter_code># DON'T FORGET TO PERFORM THE IMPORT!



# DON'T WRITE HERE
from statsmodels.tsa.holtwinters import ExponentialSmoothing

df['TESmul12'] = ExponentialSmoothing(df['EnergyIndex'],trend='mul',seasonal='mul',seasonal_periods=12).fit().fittedvalues.shift(-1)
df.plot(figsize=(12,6)).autoscale(axis='x',tight=True);<jupyter_output><empty_output><jupyter_text>OPTIONAL: Plot the same  as above, but for only the first two years.<jupyter_code># DON'T WRITE HERE
df.iloc[:24].plot(figsize=(12,6)).autoscale(axis='x',tight=True);<jupyter_output><empty_output><jupyter_text>BONUS QUESTION: There is a visible decline in the Industrial Production Index around 1982-1983.Why do you think this might be?<jupyter_code># The United States suffered a significant economic recession at that time.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11535739561217269, 'fraction_numerical': 0.02264685067232838, 'mean_word_length': 7.566666666666666, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '50478', 'license': 'Apache-2.0', 'n_tokens_mistral': 1016, 'n_tokens_neox': 936, 'n_words': 244, 'path': '02-Time-Series-Analysis-with-Statsmodels/04-Statsmodels-Time-Series-Exercises.ipynb', 'repo_name': 'vwang0/time_series_analysis', 'size': '2826'}