
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/statistics/plot_pearson_correlation.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_statistics_plot_pearson_correlation.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_statistics_plot_pearson_correlation.py:


===================
Pearson correlation
===================

This example calculates the rolling pearson correlation coefficient between two synthetic timeseries.

.. GENERATED FROM PYTHON SOURCE LINES 10-52



.. image-sg:: /auto_examples/statistics/images/sphx_glr_plot_pearson_correlation_001.png
   :alt: Time series, Correlation between time series
   :srcset: /auto_examples/statistics/images/sphx_glr_plot_pearson_correlation_001.png
   :class: sphx-glr-single-img





.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd

    from matplotlib.dates import DateFormatter

    from indsl.statistics.correlation import pearson_correlation


    # generate the data
    rng = np.random.default_rng(12345)
    num_datapoints = 100
    y1 = rng.standard_normal(num_datapoints)
    y2 = y1.copy()  # create data2 from data1
    y2 += 5  # add deviation
    y2 += rng.standard_normal(num_datapoints) * 0.5  # add noise
    index = pd.date_range(start="1970", periods=num_datapoints, freq="1min")
    data1, data2 = pd.Series(y1, index=index), pd.Series(y2, index=index)

    # calculate the rolling pearson correlation
    corr = pearson_correlation(data1, data2, time_window=pd.Timedelta(minutes=5), min_periods=1)

    # Plot the two time series and the correlation between them
    fig, ax = plt.subplots(2, 1, figsize=[15, 10])
    ax[0].plot(
        data1,
        label="Time series 1",
    )
    ax[0].plot(data2, label="Time series 2")
    ax[1].plot(corr, label="Rolling pearson correlation")
    ax[0].set_title("Time series")
    ax[1].set_title("Correlation between time series")
    _ = ax[0].legend(loc="best")

    # Formatting
    myFmt = DateFormatter("%b %d, %H:%M")
    for ax_ in ax:
        ax_.xaxis.set_major_formatter(myFmt)
        ax_.xaxis.set_major_formatter(DateFormatter("%b %d, %H:%M"))
        _ = plt.setp(ax_.get_xticklabels(), rotation=45)
    plt.tight_layout()
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.354 seconds)


.. _sphx_glr_download_auto_examples_statistics_plot_pearson_correlation.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_pearson_correlation.ipynb <plot_pearson_correlation.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_pearson_correlation.py <plot_pearson_correlation.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_pearson_correlation.zip <plot_pearson_correlation.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
