Path: blob/master/lessons/lesson_16/solution-code/02_rolling_statistics_solutions.ipynb
1904 views
Kernel: Python 2
Time Series: Rolling Statistics
Independent Practice
Instructor Note: These are optional and can be assigned as student practice questions outside of class.
1) Load the Unemployment data set. Perform any necessary cleaning and preprocess the data by creating a datetime
index.
In [1]:
In [2]:
Out[2]:
In [3]:
Out[3]:
In [4]:
In [5]:
Out[5]:
In [7]:
Out[7]:
year_quarter object
unemployment_rate float64
dtype: object
In [8]:
Out[8]:
2) Plot the unemployment rate.
In [9]:
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x109177240>
3) Calculate the rolling mean of years with window=3
, without centering, and plot both the unemployment rates and the rolling mean data.
In [10]:
Out[10]:
date
1948 NaN
1949 NaN
1950 5.002833
1951 4.847333
1952 3.838917
Freq: A-DEC, Name: unemployment_rate, dtype: float64
In [12]:
In [14]:
Out[14]:
4) Calculate the rolling median with window=5
and window=15
. Plot both together with the original data.
In [16]:
In [17]:
Out[17]:
5) Calculate and plot the expanding mean. Resample by quarter. Plot the rolling mean and the expanding mean together.
In [19]:
Out[19]:
6) Calculate and plot the exponentially weighted sum along with the rolling sum.
In [20]:
In [21]:
Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x10e6ae748>
7) Difference the unemployment rate and plot.
In [24]:
In [23]:
Out[23]:
<matplotlib.axes._subplots.AxesSubplot at 0x10eb191d0>