Correlation

Calculate the Pearson Correlation of every series with another: how closely they move together, with the benchmark, another ticker of the Toolkit (or series of the data) or another price column of the same ticker, such as the high against the low as in TA-Lib.

The correlation is +1 when the two series move up and down together in a straight-line relation, -1 when they move in opposite directions and around 0 when they are unrelated. By default it correlates the returns, so it shows whether the moves of two assets go together. On price levels (the adjusted close), two series that both trend have a high correlation even when their daily moves are unrelated, so it then shows whether two prices follow the same course rather than whether their returns are related.

The formula is as follows:

\[\text{Correlation} = \frac{\operatorname{Covariance}(X,\; Y)}{\operatorname{StdDev}(X) \cdot \operatorname{StdDev}(Y)}\]

with X the column series of each asset and Y that of against, over the whole sample or over every rolling window.

Also known as: CORREL, Pearson correlation coefficient, rolling correlation, correlation with the benchmark.

Example · AAPL, MSFT · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_correlation:

from financetoolkit import Toolkit

toolkit = Toolkit(
    tickers=["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_correlation(rolling=30)

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

What was the 30-day rolling correlation of Apple's and Microsoft's daily returns with the S&P 500 over the last five trading days of 2025?

Which returns:

Using Your Own Data

get_correlation runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_correlation()

Parameters

get_correlation accepts the following parameters.

ParameterTypeDescriptionExample
periodstr | NoneThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to None, which uses “daily”."weekly"
columnstrThe historical data column that is correlated: “Return” for the returns (the changes of the series), “Adj Close” for the prices (the series as given with Econometrics(data=...)) or another price column such as “High”. Defaults to “Return”."Return"
rollingint | NoneThe number of periods of a rolling window, at least 2. Defaults to None, which calculates one value over the full history of every asset.4
againststr | NoneWhat to correlate with: a ticker of the Toolkit (or a series of the data), such as “Benchmark” or “MSFT”, or another column of the same ticker, such as “Low” with column=”High”. Defaults to None, which uses the Benchmark or, without one, the second series.None, which uses the Benchmark or, without one, the second series
include_benchmarkboolWhether to include “Benchmark” among the assets. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Pearson, K. (1895). “Notes on Regression and Inheritance in the Case of Two Parents.” Proceedings of the Royal Society of London, 58, 240-242.
  • Library Referenced against TA-Lib’s CORREL
  • Library Referenced against pandas-ta-classic’s correl
Share