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?

  • 3
    $\begingroup$ 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. $\endgroup$
    – John
    Jan 20, 2015 at 15:59
  • $\begingroup$ I think this may be off-topic because it belongs on stack exchange. $\endgroup$
    – chollida
    Jan 20, 2015 at 18:13

1 Answer 1


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; 
if nargin < 1 

[r,c] = size(ws); 
if n ~= c 

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 

Hope that answers your covariance question .

  • $\begingroup$ why did you delete this? It answers the question: the covariance matrix is computed using historical returns. This is the only way given the input in any case. $\endgroup$
    – SRKX
    Jan 21, 2015 at 2:38
  • $\begingroup$ @SRKK , It was same as the comments by John . the use of cov function is already mentioned there so I thought it was redundant $\endgroup$
    – ash
    Jan 21, 2015 at 10:39
  • $\begingroup$ A comment is not an answer. Better to have both. $\endgroup$
    – SRKX
    Jan 21, 2015 at 11:16
  • $\begingroup$ @SRKX understood $\endgroup$
    – ash
    Jan 21, 2015 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.