Variance
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 formula is as follows:
\[\text{Variance} = \operatorname{Var}(\text{Daily Returns within the period}) \cdot \text{Trading Days per Period}\]Where Var is the sample variance (ddof=1) and the trading days per period are 252 / 52 (weekly), 252 / 12 (monthly), 252 / 4 (quarterly) or 252 (yearly). With rolling, the result is the sample variance of the last rolling returns of the chosen period itself, without scaling.
Also known as: dispersion, spread.
Variance in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_variance as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
["WMT", "TGT", "COST"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.risk.get_variance(period="yearly")
Or, after setting up the Finance Toolkit MCP, ask:
What was the annualized variance of the returns of Walmart, Target, Costco and the S&P 500 in each year from 2021 to 2025?
Which returns:
| Date | WMT | TGT | COST | Benchmark |
|---|---|---|---|---|
| 2021 | 0.0278 | 0.0559 | 0.0359 | 0.0167 |
| 2022 | 0.0714 | 0.2131 | 0.0984 | 0.0588 |
| 2023 | 0.0245 | 0.0965 | 0.0365 | 0.0171 |
| 2024 | 0.0313 | 0.1377 | 0.0355 | 0.0158 |
| 2025 | 0.063 | 0.1166 | 0.0467 | 0.038 |
Parameters
get_variance accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency for returns (weekly, monthly, quarterly, or yearly). Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”. | "quarterly" |
rolling | int | 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. | 4 |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise). | 2 |
growth | bool | Whether to calculate the growth of the Variance values over time. Defaults to False. | True |
lag | int | list[int] | The lag to use for the growth calculation. Defaults to 1. | 4 |
standardize | bool | Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False. | True |