-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex2_script.m
146 lines (101 loc) · 4.41 KB
/
ex2_script.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%% Exercise (2)
%%
%% Computing daily VaR(99%) for portfolio including stocks and
%% European call options.
clear;
format compact;
% Loads all trading days from 2010 onwards into a cell to be used later.
load('trading_days.mat');
tic;
%% Inputs
position_shares = [0,0,0,0,0,0,0,0,0,0,628, 0,0,0,0];
position_calls = [0,0,0,0,0,0,0,0,0, 0,-1000, 0,0,0,0];
moneyness = [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0];
maturity = [0,0,0,0,0,0,0,0,0,0,7,0,0,0,0];
% data_file: market data CSV file to read
data_file = 'market_data.csv';
% conf_level: confidence level of VaR (0 < decimal < 1)
conf_level = 0.99;
%% Preprocessing & Initialization
% reading in financial data and transposing so that rows correspond to assets
prices = transpose(csvread(data_file,1,1));
% transforming prices to log prices, leaving DAX & VDAX unaffected
log_prices = log(prices(1:15,1:end));
% transforming moneyness to strikes (spot price (on 3rd Jan 2011) / moneyness)
strikes = transpose(prices(1:15,262)) ./ moneyness;
% initializing daily log changes
[number_assets, number_trading_days] = size(log_prices);
daily_log_changes = zeros(number_assets, number_trading_days);
% initializing Greeks
delta = zeros(1,number_assets);
theta = zeros(1,number_assets);
vega = zeros(1,number_assets);
% note that changes from day 1 to day 2 are stored in index 2
for i = 2:number_trading_days
daily_log_changes(:,i) = log_prices(:,i) - log_prices(:,i-1);
end
% Initializing output vector
VaR = zeros(number_trading_days-261,1);
%% VaR computation
for T = 262:number_trading_days
%% Computing weighted mean and variance of Gaussian joint log returns.
% No glimpse into the future, first entry (all zeros) ignored.
truncated_log_returns = daily_log_changes(:,2:T);
% Computing the weights used in next block.
weighting = zeros(T-1,1);
factor = 2/(T*(T-1));
for k = 1:T-1
weighting(k,1) = k*factor;
end
% Computing mean and variance of reweighted log returns
w_log_return_mean = truncated_log_returns * weighting;
centralized_data = truncated_log_returns - w_log_return_mean * ones(1,size(truncated_log_returns,2));
weight_diag = diag(weighting);
w_log_return_Sigma = centralized_data*weight_diag*transpose(centralized_data);
% Computing annualized vol
ann_vol = zeros(15,1);
for i=1:15
ann_vol(i) = sqrt(w_log_return_Sigma(i,i)*260);
end
%% Preliminaries for computation of position parameters
for i=1:15
% Options past their maturities are kicked out
time_to_maturity = (maturity(i) * 260 - (T-261))/260; % in years
if time_to_maturity <= 0
position_calls(i) = 0;
delta(i) = 0;
vega(i) = 0;
theta(i) = 0;
else
% Computing greeks and storing coefficients
d1 = (log(prices(i,T)/strikes(i)) + 1/2 * ann_vol(i)^2 * ...
time_to_maturity)/(ann_vol(i) * sqrt(time_to_maturity));
d2 = d1 - ann_vol(i) * sqrt(time_to_maturity);
delta(i) = normcdf(d1);
vega(i) = prices(i,T) * normpdf(d1) * sqrt(time_to_maturity);
theta(i) = -(prices(i,T) * normpdf(d1) * ann_vol(i))/ ...
(2 * sqrt(time_to_maturity));
end
end
%% Computation of parameters of overall position
% Calculating coefficients
delta_coeff = (position_shares + position_calls .* delta) .* transpose(prices(1:15,T));
theta_coeff = position_calls .* theta;
vega_coeff = position_calls .* vega;
% Parameters of total position
total_position_mean = delta_coeff * w_log_return_mean + ...
theta_coeff * repmat(1/260,15,1) + ...
vega_coeff * ann_vol/260;
total_position_var = delta_coeff * w_log_return_Sigma * transpose(delta_coeff);
total_position_stdv = sqrt(total_position_var);
% Computing and writing VaR
VaR(T-261,1) = norminv(1-conf_level, total_position_mean, total_position_stdv);
end
%% CSV file writing
% turning vector into cell, than cell concatenation
VaR = num2cell(VaR);
output = [trading_days(262:end),VaR];
% using S. Fiedlers cell2csv function to write csv file (standard csv write
% only works for numeric values)
cell2csv('More_risk_more_fun_Ex2iii.csv',output);
toc;