Code covered by the BSD License  

Highlights from
Financial Seminar Demos

from Financial Seminar Demos by Michael Weidman
Demos commonly used at The MathWorks financial modeling seminars.

PortVaRreturns.m
% calculate portfolio VaR

[fname, pname] = uigetfile('*.xls','Select a file with equity data');
if fname==0
	return
end

fname= [pname, fname];
range='a1:j789';

% get equity data
edat = excelget(fname, 1, range);

eprice = cell2mat(edat(2:end, 2:end)); % price data
eret = log(eprice(2:end,:)./eprice(1:end-1,:));

% positions in number of shares
positions = round(1000*rand(1,9));
% positions in monetary value
posvalues = positions.*eprice(end,:);

% confidence level
alpha = norminv(0.95,0,1);

% volatility = sqrt(positions * cov_mat * positions')
sigma = sqrt(posvalues*cov(eret)*posvalues');
% var = conf_level * volatility (daily var)
var = alpha * sigma

Contact us at files@mathworks.com