比如说欧式期权定价的程序是这个
function [callprice,putprice]=euro1(S,X,r,T,sigma,N)dt=T/N;u=exp(sigma*sqrt(dt));d=1/u;p=(exp(r*dt)-d)/(u-d);
for i=1:N+1 St(i)=S*power(u,i-1)*power(d,N+1-i);end
for i=1:N+1 Call(i)=max(St(i)-X,0); Put(i)=max(X-St(i),0);end
for i=N:-1:1 for j=1:i Call(j)=exp(-r*dt)*(p*Call(j+1)+(1-p)*Call(j)); Put(j)=exp(-r*dt)*(p*Put(j+1)+(1-p)*Put(j)); endend
callprice=Call(1);putprice=Put(1); |