How MATLAB PORTVAR Computes Portfolio Variance
Summary
The document explains the inputs and calculation used by MATLAB’s PORTVAR function. Asset data are arranged with one security’s historical observations in each column, and portfolio weights correspond to those columns. If weights are omitted, the function assigns equal weights; a matrix of weight rows produces a separate variance result for each row.
The implementation calculates the covariance matrix from the supplied asset observations, then combines each asset’s variance weighted by its squared portfolio weight with the pairwise covariance contributions. This answers the question about which correlation or covariance information underlies the result: it is estimated from the input data rather than supplied as a separate matrix. The excerpt does not specify the observation frequency, missing-data treatment, return preprocessing, or whether the input should be prices or returns. Those choices affect interpretation, and users should check the function’s surrounding documentation and ensure the data match their intended variance horizon.
Key ideas
- PORTVAR estimates covariance from the columns of the supplied asset data.
- Each column represents one security, and weights must correspond to those columns.
- Omitting weights results in equal weighting across the securities.
- The variance combines weighted individual variances and pairwise covariance terms.
- Input frequency and preprocessing determine how the resulting variance should be interpreted.
Tags
Full text
# What are the parameters of the function PORTVAR in Matlab?
# What are the parameters of the function PORTVAR in Matlab?
According to the Matlab help, Portvar will give the "Variance for portfolio of assets" by entering the returns of the Assets and the corresponding weight. However, it does not explain the parameters behind this formula.
For example, on which correlation matrix is `Portvar` based? What are the hidden "hypothesis" made by the function?
## Answer by ash (score 2, accepted)
https://quant.stackexchange.com/a/16283
The code is
```
function v = portvar(asset,ws)
%PORTVAR Portfolio variance.
% V = PORTVAR(ASSET,WS) returns the variance for a portfolio of assets
% where ASSET is a matrix of asset data and WS are the corresponding
% weights of each asset. ASSET is an MxN matrix of N securities and
% WS is a 1xN vector where each column of ASSET is a time series of
% historical data for a single security and each column of WS is a
% corresponding weight for each security in ASSET. If WS is a matrix
% of size RxN, the portfolio variance, V, is returned as an Rx1 vector
% with each row representing a variance calculation for each row of WS.
%
% V = PORTVAR(ASSET) assigns each security an equal weight when
% calculating the portfolio variance.
%
% See also FRONTCON, PORTROR, PORTRAND.
%
% Reference: Bodie, Kane, and Marcus, Investments, Chapter 7.
% Copyright 1995-2006 The MathWorks, Inc.
[m,n] = size(asset);
if nargin < 2
ws = ones(1,n)/n;
end
if nargin < 1
error(message('finance:portvar:missingInputs'))
end
[r,c] = size(ws);
if n ~= c
error(message('finance:portvar:mismatchAssetsWeights'))
end
covmat = cov(asset); % Calculate covariance of assets
va = diag(covmat)'; % Get variance for each column
ca = tril(covmat,-1); % Get covariance values of columns
v = zeros(r,1); % Preallocate matrices
for n = 1:r % Weights are not always square matrix, using for loop
x = ws(n,:)'*ws(n,:);
v(n) = sum(ws(n,:).^2.*va)+2*sum(sum(x.*ca)); % Equation 7.11, pg. 217
end
```
Hope that answers your covariance question .Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.