<jupyter_start><jupyter_text>Rolling Windows Pandas.DataFrame.rollingYou've just learned about rolling windows.  Let's see how we can use rolling function in pandas to create the rolling windowsFirst, let's create a simple dataframe!<jupyter_code>import numpy as np
import pandas as pd
from datetime import datetime

dates = pd.date_range(datetime.strptime('10/10/2018', '%m/%d/%Y'), periods=11, freq='D')
close_prices = np.arange(len(dates))

close = pd.Series(close_prices, dates)
close<jupyter_output><empty_output><jupyter_text>Here, we will introduce rolling function from pandas.  The rolling function helps to provide rolling windows that can be customized through different parameters.  You can learn more about [rolling function here](https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.DataFrame.rolling.html)Let's take a look at a quick sample.<jupyter_code>close.rolling(window = 3)<jupyter_output><empty_output><jupyter_text>This returns a Rolling object. Just like what you've seen before, it's an intermediate object similar to the GroupBy object which breaks the original data into groups. That means, we'll have to apply an operation to these groups. Let's try with sum function.<jupyter_code>close.rolling(window = 3).sum()<jupyter_output><empty_output><jupyter_text>The window parameter defines the size of the moving window. This is the number of observations used for calculating the statistics which is the "sum" in our case.For example, the output for 2018-10-12 is 3, which equals to the sum of the previous 3 data points, 0 + 1 + 2.Another example is 2018-10-20 is 27, which equals to 8+ 9 + 10Not just for summation, we can also apply other functions that we've learned in the previous lessons, such as max, min or even more.  Let's have a look at another quick example<jupyter_code>close.rolling(window = 3).min()<jupyter_output><empty_output><jupyter_text>Now, the output returns the minimum of the past three data points. By the way, have you noticed that we are getting NaN for close.rolling(window = 3).sum().  Since we are asking to calculate the mininum of the past 3 data points.  For 2018-10-10 and 2018-10-11, there are no enough data points in the past for our calculation, that's why we get NaN as outputs.  There are many other parameters you can play with for this rolling function, such as min_period or so.  Please refer to [the python documentation](https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.DataFrame.rolling.html) for more details  Quiz: Calculate Simple Moving AverageThrough out the program, you will learn to generate alpha factors.  However, signals are always noisy.  A common practise from the industry is to smooth the factors by using simple moving average.  In this quiz, we can create a simple function that you can specify the rolling window and calculate the simple moving average of a time series.<jupyter_code>import quiz_tests

def calculate_simple_moving_average(rolling_window, close):
    """
    Compute the simple moving average.
    
    Parameters
    ----------
    rolling_window: int
        Rolling window length
    close : DataFrame
        Close prices for each ticker and date
    
    Returns
    -------
    simple_moving_average : DataFrame
        Simple moving average for each ticker and date
    """
    # TODO: Implement Function
    
    return close.rolling(window = rolling_window).mean()


quiz_tests.test_calculate_simple_moving_average(calculate_simple_moving_average)<jupyter_output>Tests Passed
{'fraction_non_alphanumeric': 0.0711652962857953, 'fraction_numerical': 0.01814573291749362, 'mean_word_length': 4.57345971563981, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '70363', 'license': 'Apache-2.0', 'n_tokens_mistral': 1034, 'n_tokens_neox': 947, 'n_words': 416, 'path': 'Lesson 14 Exercises/rolling_windows.ipynb', 'repo_name': 'StefanoD/AI-for-Trading', 'size': '3527'}