Matlab Code for Monte Carlo Simulation of Hedging Strategy
Feb 24, 2008 in Columbia, Matlab
This simple matlab code was for one of the homework problems. I wanted to generate n monte carlo paths, but have each one colored at random. I could modify this to rank order the final values and color accordingly, but was in a hurry to submit it. This code calculates the P&L from hedging at implied volatility but the underlying converges to realized volatility.
Â
clf
num_paths = 100; % number of paths to average
hold on
kfrac = 0.95 ; % K = 95% of stock
rf = 0.10; % risk free rate, per annum
sig_i = 0.10; % implied volatility per annum
sig_r = 0.30; % realized volatility per annum
d = 0; % dividend yield, percent
T = 0.5; % time to expiry, years
num_days = 250; %number of days in year
mu = 0; % drift, percent per annum
S0 = 100; % initial stock price
%% assume stock price follows GBM dS = mu S dt + sigma*S*dZ where dZ is a
%% draw from standard normal distribution
hedging_frequency = 1; % hedging frequency in days, so 1 corresponds to daily
dt = hedging_frequency/(T*num_days); %% eg 1/250 for daily over a one year period
t = [0:hedging_frequency:T*num_days];
%% evolve stock price according to GBM
S=zeros(j,T*num_days);
gamma = S; %% initialization
profit = S;
totalprofit = zeros(1,num_paths);
for j=1:num_paths
profit(1,j) = 0;
S(1,j) =S0;
d1 = (log(1/kfrac)+(rf-d+0.5*sig_i^2*T))/(sig_i*sqrt(T));
gamma(1) = exp(-d1^2/2)/(S(1)*sig_i*sqrt(2*pi*T));
for i = 1:T*num_days-1
S(i+1,j) =S(i,j)*exp((rf-1/2*sig_i^2)*dt + sig_i*randn*sqrt(dt));
tau = T*num_days – i*dt;
d1 = (log(1/kfrac)+(rf-d+0.5*sig_i^2*tau))/(sig_i*sqrt(tau));
gamma(i+1,j) = exp(-d1^2/2)/(S(i,j)*sig_i*sqrt(2*pi*tau));
profit(i+1,j) =profit(i,j)+ gamma(i,j)*S(i,j)^2*(sig_r^2-sig_i^2)*1/T*exp(-rf*tau);
end
plot(S(:,j),’Color’,[0.01+.99*abs(rand),0.01+.99*abs(rand),0.01+.99*abs(rand)])
%%title(‘Simulated Stock Price paths by GBM’);
totalprofit(j) = 1/2*profit(i,j);
end
mean(totalprofit)
%%printf(‘Average expected profit = $%n’,mean(profit))