Skip to content

Foundations

Risk statistics

Sample volatility and its annualization, semideviation, adjusted skewness and excess kurtosis, and maximum drawdown.

Model id
foundations.risk_statistics, version 1
Domain
Foundations
Evidence
standard
Cost class
instant: A closed-form calculation; a hosted run may take up to 2 seconds.

Formula

s=1n−1∑(rt−rˉ)2,σannual=sms = \sqrt{\frac{1}{n-1}\sum (r_t - \bar{r})^2}, \quad \sigma_{\mathrm{annual}} = s \sqrt{m}SDrˉ=1n−1∑min⁡(rt−rˉ,0)2SD_{\bar{r}} = \sqrt{\frac{1}{n-1}\sum \min(r_t - \bar{r}, 0)^2}DDT=1n∑min⁡(rt−T,0)2DD_T = \sqrt{\frac{1}{n}\sum \min(r_t - T, 0)^2}G1=n(n−1)n−2 m3m23/2G_1 = \frac{\sqrt{n(n-1)}}{n-2}\,\frac{m_3}{m_2^{3/2}}G2=n−1(n−2)(n−3)[(n+1)m4m22−3(n−1)]G_2 = \frac{n-1}{(n-2)(n-3)}\left[(n+1)\frac{m_4}{m_2^2} - 3(n-1)\right]MDD=max⁡j(1−Vjmax⁡i≤jVi)MDD = \max_j \left(1 - \frac{V_j}{\max_{i \le j} V_i}\right)

Where

ss
Sample standard deviation of the returns volatility
rtr_t
The return in period t returns
rˉ\bar{r}
Mean return mean
nn
Number of returns observations
σannual\sigma_{\mathrm{annual}}
Volatility for a year annualized_volatility
mm
Periods in a year periods_per_year
SDrˉSD_{\bar{r}}
Semideviation: the deviation of the returns below the mean below_mean
DDTDD_T
Downside deviation below the target below_target
TT
Target return per period target
G1G_1
Sample skewness skewness
G2G_2
Sample excess kurtosis excess_kurtosis
m2m_2
The returns' second central moment; m_3 and m_4 are the third and fourth
MDDMDD
Maximum drawdown max_drawdown
VjV_j
The value at observation j values

Each symbol's field is named beside it; the inputs and outputs, with their units and bounds, are under Variables, and the citations give each formula's source.

Variables

Rates, returns and yields are decimals: 0.05 is five percent. Bounds in brackets include their end; in parentheses they exclude it.

Inputs

Sample volatility and its annualization (volatility): 3 inputs
Inputs of the calculation volatility
NameDescription
CalculationcalculationtextSample volatility and its annualization
Returnsreturnsreturn (array) · length min 2, max 10080; each [-1, 100]Periodic returns
Periods per yearperiods_per_yearperiods · (0, 100000]Periods in a year, such as 12 or 252
Downside deviation below the mean and a target (semideviation): 3 inputs
Inputs of the calculation semideviation
NameDescription
CalculationcalculationtextDownside deviation below the mean and a target
Returnsreturnsreturn (array) · length min 2, max 10080; each [-1, 100]Periodic returns
Targettargetreturn · [-1, 100]Target return per period
Adjusted skewness (G1) and excess kurtosis (G2) (moments): 2 inputs
Inputs of the calculation moments
NameDescription
CalculationcalculationtextAdjusted skewness (G1) and excess kurtosis (G2)
Returnsreturnsreturn (array) · length min 4, max 10080; each [-1, 100]Periodic returns
Maximum drawdown with its peak and trough (drawdown): 2 inputs
Inputs of the calculation drawdown
NameDescription
CalculationcalculationtextMaximum drawdown with its peak and trough
Valuesvaluesindex_level (array) · length min 1, max 10080; each (0, 1e+09]Values in time order, each above zero

Outputs

Sample volatility and its annualization (volatility): 5 outputs
Outputs of the calculation volatility
NameDescription
CalculationcalculationtextSample volatility and its annualization
Meanmeanreturn · [-1, 100]Mean periodic return
Volatilityvolatilityvolatility · [0, 100]Sample standard deviation of the periodic returns (n - 1)
Annualized volatilityannualized_volatilityvolatility · [0, 100000]Volatility times the square root of periods_per_year
Degrees of freedomdegrees_of_freedomcount · [1, 10080]n - 1, the denominator of the variance
Downside deviation below the mean and a target (semideviation): 3 outputs
Outputs of the calculation semideviation
NameDescription
CalculationcalculationtextDownside deviation below the mean and a target
Below meanbelow_meanvolatility · [0, 150]Semideviation below the sample mean, with n - 1
Below targetbelow_targetvolatility · [0, 150]Downside deviation below the target, with n
Adjusted skewness (G1) and excess kurtosis (G2) (moments): 4 outputs
Outputs of the calculation moments
NameDescription
CalculationcalculationtextAdjusted skewness (G1) and excess kurtosis (G2)
Skewnessskewnessratio · [-1000, 1000]Adjusted Fisher-Pearson skewness, G1
Excess kurtosisexcess_kurtosisratio · [-100000, 100000]Adjusted excess kurtosis, G2
Observationsobservationscount · [4, 10080]Number of returns
Maximum drawdown with its peak and trough (drawdown): 4 outputs
Outputs of the calculation drawdown
NameDescription
CalculationcalculationtextMaximum drawdown with its peak and trough
Max drawdownmax_drawdownreturn · [0, 1]Largest decline from a peak, as a positive decimal
Peak indexpeak_indexcount · [0, 10080]Position of the peak, from 0
Trough indextrough_indexcount · [0, 10080]Position of the trough, from 0

Calculator

Run the model on your own inputs, starting from its example. Rates are decimals: 0.05 is five percent. The run goes to the pyeconomics API on this site: your values travel only in the request, and nothing is stored.

Loading the calculator…

Worked example

Example monthly_volatility. Inputs:

{
  "calculation": "volatility",
  "periods_per_year": 12,
  "returns": [0.02, -0.01, 0.03, 0.005, -0.02, 0.01]
}

Outputs:

{
  "annualized_volatility": 0.0642650760522385,
  "calculation": "volatility",
  "degrees_of_freedom": 5,
  "mean": 0.005833333333333334,
  "volatility": 0.018551729479125836
}

Assumptions

  • Returns are decimals over equal periods; annualizing by the square root of the periods in a year assumes they are independent and identically distributed.
  • The variance divides by n - 1; the semideviation below the mean by n - 1 and below a given target by n.
  • A drawdown is measured on values above zero, in time order.

Limitations

  • Skewness and kurtosis are undefined for a series with no spread: DomainError.
  • No statistic here corrects for autocorrelation or fat tails.

Evidence

Status: standard. Textbook consensus, such as bond duration or the CAPM formula.

Every model declares one status: standard (textbook consensus), practitioner (a widely used convention), contested (under active debate), rejected (failed out of sample) or historical (a superseded methodology).

Syllabus

The 2027 CFA® Program's topic areas this model relates to, as factual tags. Each links to CFA Institute's page for its level; pyeconomics holds none of the curriculum's text.

Citations

  1. Joanes, D. N. and Gill, C. A. (1998). Comparing measures of sample skewness and kurtosis. Journal of the Royal Statistical Society: Series D (The Statistician), 47(1), 183-189. doi:10.1111/1467-9884.00122 (Section 2 (the estimators b, g and G))
  2. Chan, T. F., Golub, G. H. and LeVeque, R. J. (1983). Algorithms for computing the sample variance: analysis and recommendations. The American Statistician, 37(3), 242-247. doi:10.1080/00031305.1983.10483115 (Section 2 (the corrected two-pass algorithm))
  3. NIST/SEMATECH (2012). e-Handbook of Statistical Methods, section 1.3.5.11, Measures of Skewness and Kurtosis. https://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm (Section 1.3.5.11)
  4. Sortino, F. A. and Price, L. N. (1994). Performance measurement in a downside risk framework. The Journal of Investing, 3(3), 59-64. doi:10.3905/joi.3.3.59 (The downside deviation below a minimum acceptable return)

Changelog

  • Version 1: First version.

Use it elsewhere

Documentation
The model's reference page
REST API
GET or POST /v1/models/foundations.risk_statistics/run
MCP tool
run_model with the model's id, foundations.risk_statistics. Connect an assistant
Command line
pyeconomics models run foundations.risk_statistics --help

The worked example, from Python and from the command line:

Python
import pyeconomics

inputs = {
    "calculation": "volatility",
    "periods_per_year": 12,
    "returns": [0.02, -0.01, 0.03, 0.005, -0.02, 0.01],
}
result = pyeconomics.run("foundations.risk_statistics", inputs)
print(result.to_json())
Command line
pyeconomics models run foundations.risk_statistics --inputs - <<'JSON'
{
  "calculation": "volatility",
  "periods_per_year": 12,
  "returns": [0.02, -0.01, 0.03, 0.005, -0.02, 0.01]
}
JSON