<jupyter_start><jupyter_text>Computing the autocorrelation of a time seriesWhen timeseries data is available, the **autocorrelation** function is a useful method to evaluate correlation between timeseries events.  In this example we will look at the correlation in baby names over the last several decades.  Do you see a trend?Autocorrelation measures the relationship between a variable's current value and its past values. When computing autocorrelation, the resulting output can range from 1 to negative 1, in line with the traditional correlation statistic. An autocorrelation of +1 represents a perfect positive correlation (an increase seen in one time series leads to a proportionate increase in the other time series). An autocorrelation of negative 1, on the other hand, represents perfect negative correlation (an increase seen in one time series results in a proportionate decrease in the other time series). Autocorrelation measures linear relationships; even if the autocorrelation is minuscule, there may still be a nonlinear relationship between a time series and a lagged version of itself.Key Takeaways:- Autocorrelation represents the degree of similarity between a given time series and a lagged version of itself over successive time intervals.- Autocorrelation measures the relationship between a variable's current value and its past values.- An autocorrelation of +1 represents a perfect positive correlation, while an autocorrelation of negative 1 represents a perfect negative correlation.- Technical analysts can use autocorrelation to see how much of an impact past prices for a security have on its future price.<jupyter_code>import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import io
import requests
import zipfile
zipfile.ZipFile('data/babies.zip').extractall('babies')
%ls babies
files = [file for file in os.listdir('babies')
         if file.startswith('yob')]
years = np.array(sorted([int(file[3:7])
                         for file in files]))
data = {year:
        pd.read_csv('babies/yob%d.txt' % year,
                    index_col=0, header=None,
                    names=['First name',
                           'Gender',
                           'Number'])
        for year in years}
data[2016].head()
def get_value(name, gender, year):
    """Return the number of babies born a given year,
    with a given gender and a given name."""
    dy = data[year]
    try:
        return dy[dy['Gender'] == gender] \
                 ['Number'][name]
    except KeyError:
        return 0
def get_evolution(name, gender):
    """Return the evolution of a baby name over
    the years."""
    return np.array([get_value(name, gender, year)
                     for year in years])
def autocorr(x):
    result = np.correlate(x, x, mode='full')
    return result[result.size // 2:]
def autocorr_name(name, gender, color, axes=None):
    x = get_evolution(name, gender)
    z = autocorr(x)

    # Evolution of the name.
    axes[0].plot(years, x, '-o' + color,
                 label=name)
    axes[0].set_title("Baby names")
    axes[0].legend()

    # Autocorrelation.
    axes[1].plot(z / float(z.max()),
                 '-' + color, label=name)
    axes[1].legend()
    axes[1].set_title("Autocorrelation")
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
autocorr_name('Olivia', 'F', 'k', axes=axes)
autocorr_name('Maria', 'F', 'y', axes=axes)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08691885648281837, 'fraction_numerical': 0.007507941091539128, 'mean_word_length': 3.4524421593830334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '327812', 'license': 'MIT', 'n_tokens_mistral': 979, 'n_tokens_neox': 911, 'n_words': 396, 'path': 'notebooks/D6_L2_Filtering/08_signal_autocorrelation.ipynb', 'repo_name': 'highrain2/myTestProjs', 'size': '3463'}