## Archive for the 'Matlab' Category

### 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))

Jan 30, 2008 in Financial Engineering, Matlab

If you are learning Matlab, one of the best resources is completely free.Â

Check out this free online book Numerical Computing with Matlab by the father of Matlab, Cleve Moler.Â  This book is great because it introduces the reader to matlab via mathematics and numerical analysis.Â

Programming Tutorial in MatlabÂ This document is a tutorial to the basic functions of matlab, with applications in financial math.Â  The document is directed towards creating code for a binomial model so is very appropriate, includes graphics.

Statistical Analysis in Matlab, including MLE, multiple regression and stochastic processes, is covered in these documents from a university author. Tutorial Part IÂ  covers the basics, Tutorial Part II is applied to statistics.

This document Intro to Financial Engineering via MatlabÂ byÂ Dr Brad Baxter, School of Economics, Mathematics and Statistics Birkbeck College, London W1CE 7HX provides an overview of options with code for matlab implementation, as well as analystic derivations of put and call prices for comparison.Â

This is another document by the same author who goes into much greater detail on financial engineering approached via Matlab. James Lesage has a wonderful tutorial on econometrics and matlab.