Skip to content

Portfolio

Mean-variance portfolios

Portfolio expected return and risk, the minimum-variance frontier with short sales, and the minimum-variance and tangency portfolios.

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

Formula

E[Rp]=w′μ,σp2=w′ΣwE[R_p] = w'\mu, \quad \sigma_p^2 = w'\Sigma wwg=Σ−11A,A=1′Σ−11,σg2=1Aw_g = \frac{\Sigma^{-1}\mathbf{1}}{A}, \quad A = \mathbf{1}'\Sigma^{-1}\mathbf{1}, \quad \sigma_g^2 = \frac{1}{A}σ2(m)=1A+(m−μg)2q\sigma^2(m) = \frac{1}{A} + \frac{(m - \mu_g)^2}{q}q=d′Σ−1d,d=μ−μg1q = d'\Sigma^{-1} d, \quad d = \mu - \mu_g \mathbf{1}wT=Σ−1(μ−rf1)1′Σ−1(μ−rf1)w_T = \frac{\Sigma^{-1}(\mu - r_f \mathbf{1})}{\mathbf{1}'\Sigma^{-1}(\mu - r_f\mathbf{1})}slope=(μ−rf1)′Σ−1(μ−rf1)\text{slope} = \sqrt{(\mu - r_f \mathbf{1})'\Sigma^{-1}(\mu - r_f \mathbf{1})}

Where

E[Rp]E[R_p]
The portfolio's expected return expected_return
ww
The portfolio's weights weights
μ\mu
The assets' expected returns expected_returns
σp2\sigma_p^2
The portfolio's variance variance
Σ\Sigma
Covariance matrix of the assets' returns covariance
wgw_g
Weights of the global minimum-variance portfolio weights
AA
The sum of the elements of the inverse covariance matrix
σg2\sigma_g^2
Variance of the global minimum-variance portfolio
σ2(m)\sigma^2(m)
The smallest variance for the expected return m variances
mm
A target expected return on the frontier target_returns
μg\mu_g
Expected return of the global minimum-variance portfolio minimum_variance_return
qq
How fast the frontier's variance rises away from the minimum- variance return
dd
The assets' expected returns less the minimum-variance return
wTw_T
Weights of the tangency portfolio weights
rfr_f
Risk-free rate risk_free_rate
slope\text{slope}
Slope of the capital market line: the tangency portfolio's Sharpe ratio capital_market_line_slope

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

Expected return and risk of given weights (portfolio): 6 inputs
Inputs of the calculation portfolio
NameDescription
Expected returnsexpected_returnsreturn (array) · length min 1, max 100; each [-1, 100]Expected return of each asset per period
Covariancecovarianceratio (array) (array) · each length min 1, max 100; each [-100, 100]Covariance matrix of the asset returns per period, as rows; or give volatilities and correlations
Volatilitiesvolatilitiesvolatility (array) · each (0, 10]Volatility of each asset per period, with correlations
Correlationscorrelationscorrelation (array) (array) · each length min 1, max 100; each [-1, 1]Correlation matrix of the asset returns, as rows, with volatilities
CalculationcalculationtextExpected return and risk of given weights
Weightsweightsratio (array) · length min 1, max 100; each [-100, 100]Weight of each asset; negative is short. They need not sum to 1
Minimum-variance frontier, short sales allowed (frontier): 8 inputs
Inputs of the calculation frontier
NameDescription
Expected returnsexpected_returnsreturn (array) · length min 1, max 100; each [-1, 100]Expected return of each asset per period
Covariancecovarianceratio (array) (array) · each length min 1, max 100; each [-100, 100]Covariance matrix of the asset returns per period, as rows; or give volatilities and correlations
Volatilitiesvolatilitiesvolatility (array) · each (0, 10]Volatility of each asset per period, with correlations
Correlationscorrelationscorrelation (array) (array) · each length min 1, max 100; each [-1, 1]Correlation matrix of the asset returns, as rows, with volatilities
CalculationcalculationtextMinimum-variance frontier, short sales allowed
Pointspointscount · [2, 200]Number of target returns to trace
Min returnmin_returnreturn · [-1, 100]Lowest target return; by default the minimum-variance return
Max returnmax_returnreturn · [-1, 100]Highest target return; by default the highest expected return of an asset
Global minimum-variance portfolio (minimum_variance): 5 inputs
Inputs of the calculation minimum_variance
NameDescription
Expected returnsexpected_returnsreturn (array) · length min 1, max 100; each [-1, 100]Expected return of each asset per period
Covariancecovarianceratio (array) (array) · each length min 1, max 100; each [-100, 100]Covariance matrix of the asset returns per period, as rows; or give volatilities and correlations
Volatilitiesvolatilitiesvolatility (array) · each (0, 10]Volatility of each asset per period, with correlations
Correlationscorrelationscorrelation (array) (array) · each length min 1, max 100; each [-1, 1]Correlation matrix of the asset returns, as rows, with volatilities
CalculationcalculationtextGlobal minimum-variance portfolio
Tangency portfolio and the capital market line (tangency): 6 inputs
Inputs of the calculation tangency
NameDescription
Expected returnsexpected_returnsreturn (array) · length min 1, max 100; each [-1, 100]Expected return of each asset per period
Covariancecovarianceratio (array) (array) · each length min 1, max 100; each [-100, 100]Covariance matrix of the asset returns per period, as rows; or give volatilities and correlations
Volatilitiesvolatilitiesvolatility (array) · each (0, 10]Volatility of each asset per period, with correlations
Correlationscorrelationscorrelation (array) (array) · each length min 1, max 100; each [-1, 1]Correlation matrix of the asset returns, as rows, with volatilities
CalculationcalculationtextTangency portfolio and the capital market line
Risk free raterisk_free_raterate · [-0.99, 10]Risk-free rate per period

Outputs

Expected return and risk of given weights (portfolio): 5 outputs
Outputs of the calculation portfolio
NameDescription
CalculationcalculationtextExpected return and risk of given weights
Expected returnexpected_returnreturn · [-1e+06, 1e+06]Expected return per period, w'mu
Variancevarianceratio · [0, 1e+10]Variance of the return, w' Sigma w
Volatilityvolatilityvolatility · [0, 100000]Standard deviation of the return
Weight sumweight_sumratio · [-10000, 10000]Sum of the weights
Minimum-variance frontier, short sales allowed (frontier): 7 outputs
Outputs of the calculation frontier
NameDescription
CalculationcalculationtextMinimum-variance frontier, short sales allowed
Minimum variance returnminimum_variance_returnreturn · [-1e+06, 1e+06]Expected return of the minimum-variance portfolio
Minimum variance volatilityminimum_variance_volatilityvolatility · [0, 10]Volatility of the minimum-variance portfolio
Target returnstarget_returnsreturn (array) · length max 200; each [-1e+06, 1e+06]Target expected returns, ascending
Variancesvariancesratio (array) · length max 200; each [0, 1e+10]Lowest variance at each target return
Volatilitiesvolatilitiesvolatility (array) · length max 200; each [0, 100000]Lowest volatility at each target return
Weightsweightsratio (array) (array) · length max 200; each length max 100; each [-1e+06, 1e+06]Weights of the frontier portfolio at each target, as rows
Global minimum-variance portfolio (minimum_variance): 5 outputs
Outputs of the calculation minimum_variance
NameDescription
CalculationcalculationtextGlobal minimum-variance portfolio
Weightsweightsratio (array) · length max 100; each [-1e+06, 1e+06]Weight of each asset; they sum to 1
Expected returnexpected_returnreturn · [-1e+06, 1e+06]Expected return per period
Variancevarianceratio · [0, 100]Variance, 1 / A; never above the least risky asset's
Volatilityvolatilityvolatility · [0, 10]Standard deviation of the return
Tangency portfolio and the capital market line (tangency): 6 outputs
Outputs of the calculation tangency
NameDescription
CalculationcalculationtextTangency portfolio and the capital market line
Weightsweightsratio (array) · length max 100; each [-1e+06, 1e+06]Weight of each asset; they sum to 1
Expected returnexpected_returnreturn · [-1e+06, 1e+06]Expected return per period
Variancevarianceratio · [0, 1e+10]Variance of the return
Volatilityvolatilityvolatility · [0, 100000]Standard deviation of the return
Capital market line slopecapital_market_line_sloperatio · [0, 1e+06]Slope of the capital market line: the tangency portfolio's Sharpe ratio per period, the highest of any portfolio of the assets

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 sixty_forty. Inputs:

{
  "calculation": "portfolio",
  "correlations": [
    [1, 0.3],
    [0.3, 1]
  ],
  "covariance": null,
  "expected_returns": [0.1, 0.06],
  "volatilities": [0.2, 0.1],
  "weights": [0.6, 0.4]
}

Outputs:

{
  "calculation": "portfolio",
  "expected_return": 0.08399999999999999,
  "variance": 0.01888,
  "volatility": 0.13740451229854134,
  "weight_sum": 1
}

Assumptions

  • Expected returns, volatilities and covariances are per period and given, not estimated here; the outputs are in the same period.
  • Short sales are allowed and unlimited, there are no transaction costs or taxes, and the weights of an optimized portfolio sum to one.
  • The outputs follow from the inputs the user supplies, the same for every user: they are formula results, not investment advice, and no portfolio here is advice to hold it.

Limitations

  • A covariance (or correlation) matrix that is not symmetric and positive definite to working precision, or a correlation matrix without ones on its diagonal, raises DomainError.
  • A frontier when every expected return is equal, or over an empty range of returns, raises DomainError; so does a tangency portfolio for a risk-free rate not below the minimum-variance return.
  • A matrix so close to singular that a weight exceeds 1e6 in size, or another output its bounds, raises DomainError.
  • Mean-variance weights are very sensitive to the expected returns; estimation error is not modelled. Long-only and other constraints are not supported.

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. Markowitz, H. (1952). Portfolio selection. The Journal of Finance, 7(1), 77-91. doi:10.1111/j.1540-6261.1952.tb01525.x (p. 81, the expected return E and variance V of a portfolio)
  2. Merton, R. C. (1972). An analytic derivation of the efficient portfolio frontier. Journal of Financial and Quantitative Analysis, 7(4), 1851-1872. doi:10.2307/2329621 (Eqs. (12)-(14), the frontier and its minimum-variance portfolio; Section IV, eq. (44), the tangency portfolio (numbered as in MIT Sloan working paper 493-70))

Changelog

  • Version 1: First version.

Use it elsewhere

Documentation
The model's reference page
REST API
GET or POST /v1/models/portfolio.mean_variance/run
MCP tool
portfolio_mean_variance, or run_model with the model's id; both give the same result. Connect an assistant
Command line
pyeconomics models run portfolio.mean_variance --help

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

Python
import pyeconomics

inputs = {
    "calculation": "portfolio",
    "correlations": [
        [1, 0.3],
        [0.3, 1],
    ],
    "covariance": None,
    "expected_returns": [0.1, 0.06],
    "volatilities": [0.2, 0.1],
    "weights": [0.6, 0.4],
}
result = pyeconomics.run("portfolio.mean_variance", inputs)
print(result.to_json())
Command line
pyeconomics models run portfolio.mean_variance --inputs - <<'JSON'
{
  "calculation": "portfolio",
  "correlations": [
    [1, 0.3],
    [0.3, 1]
  ],
  "covariance": null,
  "expected_returns": [0.1, 0.06],
  "volatilities": [0.2, 0.1],
  "weights": [0.6, 0.4]
}
JSON