📄 montevar.m
字号:
function [mvar] = montevar(delta,C,legame,valuta,cambi,fattori,prezzi,vm,t,p,modello,l,rendimento)%MONTEVAR Monte Carlo VaR.%% [mvar] = montevar(delta,C,legame,valuta,cambi,fattori,prezzi,vm,t,p,modello,l,rendimento) % calculates the monte Carlo VaR of a portfolio, given the vector vm of% portfolio value, the holding period t, and the probability p. Delta is% the sensibility vector, C the variance-covariance matrix of risk% factors yields. Cambi is the vector of current exchange rates, fattori% of the risk factors, and prezzi are the asset prices.% The vector legame set the relations between assets and risk factors.% Given 2 risk factors and 4 assets the vector (1 1 1 2) means that the% first three assets are related with the first risk factors, the fourth% asset with the second risk factor. The vector valuta set the relation% between tha asset and the exchange rate in the same manner of the% vector legame, with the convention that 0 is for the referred exchange% rate. Modello = 0 implies a linear simulation model, =1 a log-normal% simulation model, l is the number of simulations. Rendimento = 0 if the% yields are calculated with aritmetical method, 1 if yields are% logarithmic.%% All data must be in colums.%% See DELTAVAR, SIMULVAR%% Copyright (c) 2004 by Flavio Bazzana% Department of Computer and Management Sciences% University of Trento% 38100 - Trento ITALY% flavio.bazzana@economia.unitn.itn=size(C,1);m=length(valuta);r=length(fattori);A=chol(C);% calculation of the actual portfolio valuecp = vm./prezzi;for j=1:m if valuta(j)==0 x1(j)=1; else x1(j)=cambi(valuta(j)); endendx2 = prezzi.*x1';vp0 = x2'*cp;if modello==0 % returns simulation of exchange rates and risk factors with normal method ... for i=1:l z=0; for j=1:t y=randn(n,1); z=z+y; end fc=A*z; % ... calculation of final price of exchange rates and assets ... if rendimento==0 cam=cambi.*fc(r+1:n)+cambi; for k=1:m h=legame(k); pre(k)=prezzi(k)*(1+delta(k)*fc(h)); end else cam=cambi.*exp(fc(r+1:n)); for k=1:m h=legame(k); pre(k)=prezzi(k)*exp(delta(k)*fc(h)); end end % ... and of portfolio value for k=1:m if valuta(k)==0 x(k)=1; else x(k)=cam(valuta(k)); end end vp(i)=(pre.*x)*cp; end% returns simulation of exchange rates and risk factors with log-normal method ... else for i=1:l z=0; for j=1:t y=randn(n,1); z=z+y; end fc=sqrt(diag(C)).*z; % ... calculation of final price of exchange rates and assets ... if rendimento==0 cam=cambi.*fc(r+1:n)+cambi; for k=1:m h=legame(k); pre(k)=prezzi(k)*(1+delta(k)*fc(h)); end else cam=cambi.*exp(fc(r+1:n)); for k=1:m h=legame(k); pre(k)=prezzi(k)*exp(delta(k)*fc(h)); end end % ... and of portfolio value for k=1:m if valuta(k)==0 x(k)=1; else x(k)=cam(valuta(k)); end end vp(i)=(pre.*x)*cp; endenddvp = vp-vp0;sdvp = sort(dvp);mvar = -sdvp(fix(l-l*p));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -