Simulate a plain Call- and Put Option
8 views (last 30 days)
Show older comments
Hi
run a Monte Carlo Simulation on a plain Call option based on the Black-Scholes Model. This is as far as i came:
Call Option:
clear all
randn('state',3)
K=100;
r=0.1;
sigma=0.4;
T=1;
s0=80;
n=50;
h=1/n;
M=1000;
%Simultaneous Creation of the Wiender-process with M tracks
dW=sqrt(h)*randn(n,M);
%Simultaneous Calculation of the equity Market for all M tracks
S=zeros (n+1,M);
S(1,:)=s0; %initial value
for i=1:n
S(i+1,:)=S(i,:).*(1+r*h+sigma*dW(i,:));
end
%Simultaneous Calculation of a payoff function
payoff=max(0,S(n+1,:)-K);
%Simultaneous Calculation of the option price estimator
V=exp(-r*T)*(cumsum(payoff)./(1:M));
%Graphical output
Vexakt=call(s0,0,K,r,sigma,T);
plot(abs(V-Vexakt*ones(1,M)/Vexakt)
However it always results:
??? Undefined function or method 'call'
for input arguments of type 'char'.
Why is this?
And is the rest of the code correct?
0 Comments
Answers (2)
Matt Tearle
on 14 Oct 2011
It looks like there should be a function call that takes a bunch of inputs and returns the call price. Unless that function is somewhere on your path, MATLAB can't find it, so returns an error. There doesn't seem to be a call function in any of the financial MATLAB toolboxes, so this is probably supposed to be a user-defined function.
EDIT TO ADD: Here's what I have that successfully creates a plot (right or wrong, I don't know).
randn('state',3)
K=100;
r=0.1;
sigma=0.4;
T=1;
S0=80;
n=50;
h=1/n;
M=10000;
%Simulataneous Creation of the Wiener-Process for M Path
dW=sqrt(h)*randn(n,M);
%Simultaneous Calculation of the Equity Prices for all M Path
S=zeros(n+1,M);
S(1,:)=S0; %Initial Values
for i=1:n
S(i+1,:)=S(i,:).*(1+r*h+sigma*dW(i,:));
end
%Simultaneous Calculation of the Payoff
payoff= max(0,S(n+1,:)-K);
%Simultaneous Calculationof the Estimator and the Option Prices
V=exp(-r*T)*(cumsum(payoff)./(1:M));
%Grafical Output
Vexakt=call(S,0,K,r,sigma,T);
plot (abs(bsxfun(@minus,V,Vexakt))./Vexakt)
And in call.m:
function result = call(S,t,K,r,sigma,T)
d1=(log(S/K)+(r+0.5*sigma^2)*(T-t))/(sigma*sqrt(T-t));
d2 = d1-sigma*sqrt(T-t);
n1=0.5*(1+erf(d1/sqrt(2)));
n2=0.5*(1+erf(d2/sqrt(2)));
result=S.*n1-K*exp(-r*(T-t))*n2;
The biggest change was the last line of the script (the plot command). I'm not sure what the intent is there, but Vexakt is (n+1)-by-M, so matrix multiplication with a 1-by-M won't work. Multiplication by M-by-1 works, but will be a different size to V, so the subtraction won't work. Elementwise multiplication also has issues, so I just went with something that at least makes that line run, but could be completely different in intent.
Also note that t wasn't defined, so I just hard-coded in a value (0).
0 Comments
Nathalie Frischknecht
on 17 Oct 2011
1 Comment
Matt Tearle
on 17 Oct 2011
That error usually indicates that you're trying to put function code within a script. Functions need to be defined in their own file (in this case, call.m). There are one or two other bugs in your code, but I was able to make something work -- no idea if it's correct. See edit to my answer (above) for code.
See Also
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!