Quantzone

Matlab Finance Code

Approaches to implementing Monte Carlo methods in MATLAB

Monte Carlo methods have long been used in computational finance to solve problems where analytical solutions are not feasible or are difficult to formulate. However, these methods are computationally intensive making it challenging to implement and adopt. In the last decade, advances in hardware, increasing processor speeds and decreasing costs have made it easier to adopt Monte Carlo methods to solve numerically intensive problems. With growing access to data and demand for quicker results, researchers are constantly looking for better ways to implement algorithms using Monte Carlo methods.

In the Wilmott Magazine September 2011 article(http://www.wilmott.com/magazine.cfm), we will share some of our observations and demonstrate various ways MATLAB could be used to implement Monte Carlo methods. We take a case study of pricing Asian options and show various approaches to implementing them in MATLAB.

A draft version of the article is included in this submission.

Asian Option – Pricing using Monte Carlo Control Variate Method

An example to price an Arithmetic Average fixed strike Call option in the Black-Scholes framework using Monte Carlo Control Variate

Monte Carlo simulations using MATLAB

This set of files show some of the principles of Monte Carlo simulations, applied in the financial industry. this is the content of the web seminar called “Simulations de Monte Carlo en MATLAB”.

The slides are in French and a copy in English is also available

You will find here :

* how to code your own monte carlo simulation, for option pricing * a comparison of some of the Variance Reduction Technics * Benfeits of using MATLAB for MonteCarlo simulation

Simple option pricing GUI

This GUI accepts the various constants needed to run a Black-Scholes calculation for pricing several European options:

Put, Call, Straddle, Strangle, Bull Spread, Bear Spread, Butterfly

It plots the pricing surface for the appropriate option and then runs a number of Monte Carlo simulations (daily granularity) for that given set of parameters. It provides some useful information on the Monte Carlo simulation in graphical form.

Monte Carlo Simulation for Portfolio Assets

Monte Carlo simulations using MATLAB

Monte Carlo simulations using MATLAB

American Options via Monte Carlo Simulations

?Download download.txt
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
1;
 
clear all;
 
%%%%%%%%%%%%%%%parameters%%%%%%%%%%%%%%%%%
S=100;
X=100;
T=1;
r=0.04;
dividend=0;
v=0.2; %volatility
nsimulations=10000;
nsteps=10;
CallPutFlag="p";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%AnalyAmerPrice=BjerkPrice(CallPutFlag,S,X,r,dividend,v,T)
r=r-dividend;
%AnalyEquropeanPrice=BlackScholesPrice(CallPutFlag,S,X,T,r,v)
 
if CallPutFlag=="c",
	z=1;
else
	z=-1;
end;
 
 
 
smat=zeros(nsimulations,nsteps);
CC=zeros(nsimulations,nsteps); %cash flow from continuation
CE=zeros(nsimulations,nsteps); %cash flow from exercise
EF=zeros(nsimulations,nsteps); %Excercise flag
dt=T/(nsteps-1);
 
smat(:,1)=S;
 
drift=(r-v^2/2)*dt;
vsqrdt=v*dt^0.5;
for i=1:nsimulations,
	st=S;
	curtime=0;
	for k=2:nsteps,
		curtime=curtime+dt;
		st=st*exp(drift+vsqrdt*randn);
		smat(i,k)=st;
	end
end
 
 
CC=smat*0; %cash flow from continuation
CE=smat*0;
EF=smat*0; %Excercise flag
st=smat(:,nsteps);
CE(:,nsteps)=max(z*(st-X),0); 
CC(:,nsteps)=CE(:,nsteps);
EF(:,nsteps)=(CE(:,nsteps)>0);
 
paramat=zeros(3,nsteps); %coeff of basis functions
 
for k=nsteps-1:-1:2,
	st=smat(:,k);
	CE(:,k)=max(z*(st-X),0);
 
	%only the positive payoff points are input for regression
	idx=find(CE(:,k)>0);
	Xvec=smat(idx,k);
	Yvec=CC(idx,k+1)*exp(-r*dt);
	%Regress discounted continuation value at next time step
	% to S variables at current time step
	regrmat=[ones(size(Xvec,1),1),Xvec,Xvec.^2];
 
	p=ols(Yvec,regrmat);
	CC(idx,k)=p(1)+p(2)*Xvec+p(3)*Xvec.^2;
	%If exercise value is more than continuation value
	%choose to exercise 
	EF(idx,k)=CE(idx,k) > CC(idx,k);
	EF(find(EF(:,k)),k+1:nsteps)=0;
	paramat(:,k)=p;
	idx=find(EF(:,k) == 0);
	%don't store regressed value of CC for next use
	CC(idx,k)=CC(idx,k+1)*exp(-r*dt);
	idx=find(EF(:,k) == 1);
	CC(idx,k)=CE(idx,k);
end
 
payoff_sum=0;
 
for i=1:nsteps,
	idx=find(EF(:,i) == 1);
	st=smat(idx,i);
	payoffvec=exp(-r*(i-1)*dt)*max(z*(st-X),0);
	payoff_sum=payoff_sum+sum(payoffvec);
end
 
 
MCAmericanPrice=payoff_sum/nsimulations
 
 
st=smat(:,nsteps);
payoffvec=exp(-r*(nsteps-1)*dt)*max(z*(st-X),0);
payoff_sum=sum(payoffvec);
MCEurpeanPrice=payoff_sum/nsimulations
  • About

    Matlab Finance Code Collector

  • Tag Cloud

  • Recent Comments

  • Meta