R-Squared

Calculate the R-squared of an investment portfolio or asset against its benchmark: the share of the variance of its returns that the market explains in the CAPM regression on the benchmark.

The formula is as follows:

\[R \;\text{Squared} = \frac{\operatorname{Covariance}(\text{Returns},\; \text{Benchmark Returns})^{2}}{\operatorname{Variance}(\text{Returns}) \cdot \operatorname{Variance}(\text{Benchmark Returns})}\]

With one regressor this is the squared correlation with the benchmark. An R-squared close to 1 means the asset moves almost entirely with the market, so its Beta and Alpha describe it well; a low R-squared means most of its risk is its own (see get_risk_decomposition), and a Beta or Treynor ratio says little about it. Funds that hug their index have an R-squared above 0.95.

Also known as: coefficient of determination, R2.

Example · SPY, QQQ, GLD · 2021 to 2025

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

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["SPY", "QQQ", "GLD"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.performance.get_r_squared()

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

How much of the daily moves of QQQ and gold (GLD) did the S&P 500 explain in each year from 2021 to 2025?

Which returns:

Parameters

get_r_squared accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe period to use for the calculation. Options are: “daily”, “weekly”, “monthly”, “quarterly”, “yearly”. Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”."weekly"
rollingintThe rolling window size to use for the calculation. If you select period = ‘monthly’ and set rolling to 36 you obtain the rolling three year R-squared of the monthly returns. At least 2. Defaults to None, which uses the daily returns within each period.4
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
growthboolWhether to calculate the growth of the ratios. Defaults to False.True
lagint | list[int]The lag to use for the growth calculation. Defaults to 1.4
standardizeboolWhether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.True
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 Bacon, C.R. (2008). “Practical Portfolio Performance Measurement and Attribution.” 2nd edition, John Wiley & Sons.
  • Library Referenced against quantstats’ r_squared
Share