# 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?

• Without looking at the source, I would guess that they use the Matlab function cov on the returns to get the covariance matrix. The only thing I'm not sure of is if they use the population or sample covariance matrix. You can think of this like what would have been the variance of a portfolio rebalanced in each time period.
– John
Jan 20, 2015 at 15:59
• I think this may be off-topic because it belongs on stack exchange. Jan 20, 2015 at 18:13

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.
%
%
%   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