Calculate the Variance of an investment portfolio or asset’s returns for a given period based on the daily historical returns.

Variance measures the spread or dispersion of returns around the mean. A higher Variance indicates more variability in the returns, while a lower Variance suggests that the returns are closer to the mean.

The daily Variance is scaled to the given period by multiplying it with the number of trading days within that period (e.g. 252 / 52 for weekly).

Also known as: dispersion, spread.

No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Variance for you. Just ask in plain English.

Calculate the Variance in Python

The Variance is available in the Risk module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_variance as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(["AMZN", "TSLA"], api_key="FINANCIAL_MODELING_PREP_KEY")

toolkit.risk.get_variance(period="yearly")

Which returns:

Date AMZN TSLA Benchmark
2021 0.058 0.2999 0.0172
2022 0.2508 0.4446 0.0589
2023 0.109 0.2922 0.0174
2024 0.0789 0.4032 0.0158
2025 0.1184 0.4031 0.0379
2026 0.0999 0.1859 0.02

Parameters

get_variance accepts the following parameters:

  • period (str, optional): The data frequency for returns (weekly, monthly, quarterly, or yearly). Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”.
  • rolling (int, optional): The rolling window size to use for the calculation. If set, Variance is calculated over a rolling window of this many periods (e.g. period=’monthly’ and rolling=6 gives the rolling 6-month Variance) instead of one value per period. Defaults to None.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.
  • growth (bool, optional): Whether to calculate the growth of the Variance values over time. Defaults to False.
  • lag (int | list[int], optional): The lag to use for the growth calculation. Defaults to 1.
  • standardize (bool, optional): Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.

The Risk module page introduces the module, and the sidebar lists all of its functions.

Share