楼主: 葛新龙
16373 31

[有偿编程] matlab做三叉树的美式和欧式期权 [推广有奖]

11
葛新龙(未真实交易用户) 学生认证  发表于 2011-6-16 18:47:29
Appendix:

二叉树模型:

欧式期权:

function [] = binomialeuro(T,n,k,r,sigma,s0,z)

T % expiration date

n % total number of periods

k % exercise price

r % risk free interest rate

sigma % volatility of

s0 % present price

deltat = T/n

z % devidend

u=exp(sigma*sqrt(deltat));

d=exp(-sigma*sqrt(deltat));

p=(exp((r-z)*deltat)-d)/(u-d);

q=1-p;

for i=1:n+1


s(i)=s0*u^(n+1-i)*d^(i-1); % stock price


w(i)=nchoosek(n,i-1)*p^(n+1-i)*q^(i-1); % probability


x(i)=max(s(i)-k,0); % call option price


y(i)=max(k-s(i),0); % put option price


cv(i)=w(i)*x(i);


pv(i)=w(i)*y(i);

end

C=sum(cv(:))*exp(-r*T)

P=sum(pv(:))*exp(-r*T)

美式看跌期权:

function [] = binomialamer(T,n,k,r,sigma,s0,z)

T % expiration date

n % total number of periods

k % exercise price

r % risk free interest rate

sigma % volatility of

s0 % present price

deltat = T/n

z % devidend

u=exp(sigma*sqrt(deltat));

d=exp(-sigma*sqrt(deltat));

p=(exp((r-z)*deltat)-d)/(u-d);

q=1-p;

for j=1:n


for i=1:j+1


s(j,i)=s0*u^(j+1-i)*d^(i-1);


y(j,i)=max(k-s(j,i),0);


end

end

for w=1:n


f(n-1,w)=max(y(n-1,w),exp(-r*deltat)*(p*y(n,w)+q*y(n,w+1)));

end

for g=n-2:-1:1


for h=1:g+1


f(g,h)=max(y(g,h),exp(-r*deltat)*(p*f(g+1,h)+q*f(g+1,h+1)));


end

end

P=max(exp(-r*deltat)*(p*f(1,1)+q*f(1,2)),0)

12
葛新龙(未真实交易用户) 学生认证  发表于 2011-6-16 18:48:19
三叉树模型:

欧式期权:
function [] = Trinomialeuro(s0,k,T,n,r,sigma)

s0  % underlying asset price
k  % exercise price
T  % expiration date
n  % numbers of steps
r  % risk free rate
sigma  % volitility of stock
deltat=T/n

u=exp(sigma*sqrt(deltat));
d=1/u;
m=1;

P=(exp(2*r*deltat)+sigma^2*deltat-(d+1)*exp(r*deltat)+d)/((u^2-1)-(u-1)*(d+1));
Q=(exp(r*deltat)-1-(u-1)*P)/(d-1);
M=1-Q-P;

for i=1:n+1
    for j=1:n+2-i
    s(i,j)=s0*u^(i-1)*m^(j-1)*d^(n+2-i-j);
    w(i,j)=nchoosek(n,i-1)*P^(i-1)*nchoosek(n+1-i,j-1)*M^(j-1)*Q^(n+2-i-j);
    x(i,j)=max(s(i,j)-k,0);
    y(i,j)=max(k-s(i,j),0);
    cv(i,j)=w(i,j)*x(i,j);
    pv(i,j)=w(i,j)*y(i,j);
    end
end

C=sum(cv(:))*exp(-r*T)
P=sum(pv(:))*exp(-r*T)




美式看跌期权:
function [] =  trinomialAmerican(T,n,K,r,sigma,S0)

T ;% experiation date
n ;%total number of periods
K; %exercise price
r ;%risk free interest rate
sigma ;%vloatility of
S0 ;
deltat=T/n;

u=exp(sigma*sqrt(deltat));
d=exp(-sigma*sqrt(deltat));

p=(sigma^2*deltat+exp(2*r*deltat)-exp(r*deltat)*(1+d)+d)/((u-1)*(u-d)); %risk adjusted probability
q=(exp(r*deltat)-1-p*(u-1))/(d-1);
m=1-p-q;

    for i=1:n
        for j=1:(2*i+1)
            s(j,i)=d^(i-j+1)*S0; % stock price
        end
    end

for i=1:(2*n+1)
        x(i,n)=max(s(i,n)-K,0);
        y(i,n)=max(-s(i,n)+K,0);
end

for g=1:(n-1)
     i=n-g;
           for j=1:(2*i+1)
            x(j,i)=max(max((s(j,i)-K),0),(m*x(j+1,i+1)+p*x(j+2,i+1)+q*x(j,i+1))*exp(-r*deltat)); % call
            y(j,i)=max(max((-s(j,i)+K),0),(m*y(j+1,i+1)+p*y(j+2,i+1)+q*y(j,i+1))*exp(-r*deltat)); % European put  
           end
end

c=(m*x(2,1)+p*x(3,1)+q*x(1,1))*exp(-r*deltat)
p=(m*y(2,1)+p*y(3,1)+q*y(1,1))*exp(-r*deltat)

13
葛新龙(未真实交易用户) 学生认证  发表于 2011-6-16 18:48:54
亚式期权:

function [] =
trinomialAsian(T,n,K,r,sigma,S0,N)

T % expiration date;

n % numbers of steps;

K % exercise price;

r % risk free rate;

sigma % volitility of stock;

S0 % present price of stock;

deltat=T/n;

N % times of monte carlo;

u=exp(sigma*sqrt(deltat));

d=exp(-sigma*sqrt(deltat));

p=(sigma^2*deltat+exp(2*r*deltat)-exp(r*deltat)*(1+d)+d)/((u-1)*(u-d));

q=(exp(r*deltat)-1-p*(u-1))/(d-1);

m=1-p-q;

s(1)=S0;

for i=1:N


z=1;



for v=2:n+1


x=rand(1);


if x<=q


num=0;



end



if q<x


if x<q+m


num=1;


end


end




if x>=q+m


num=2;


end


z=z+num;


s(v)=d^(v-z)*S0;




end


price(i)=(sum(s(:))-S0)/n;



call(i)=max(price(i)-K,0)*exp(-r*T);


put(i)=max(-price(i)+K,0)*exp(-r*T);

end


ss=[1:n+1];


plot(ss-1,s(ss),'k-');


title('Random Stock Price')


xlabel('N'),ylabel('Stock Price')


call=sum(call(:))/N

put=sum(put(:))/N

14
葛新龙(未真实交易用户) 学生认证  发表于 2011-6-16 18:50:08
OVER................

15
tulipsliu(真实交易用户) 在职认证  发表于 2011-6-16 19:22:39
14# 葛新龙 你说有点问题,什么问题?你不说出问题,怎么讨论啊,谢谢你公布出程序。
劳动经济学

16
tulipsliu(真实交易用户) 在职认证  发表于 2011-6-16 19:53:56
13# 葛新龙
我不知道你们的程序跑出来没,如果是你们自己写的,这里有一个错误。
  1. function [] =trinomialAsian(T,n,K,r,sigma,S0,N)
复制代码

这个函数申明是错的,应该说可以是对的,可以申明一个不反悔参数的函数。不过你们的程序要求有返回值,就你程序最后的两个参数来说,一个是call,一个是put。那么,你的函数至少得这样申明:
  1. function [call,put] =trinomialAsian(T,n,K,r,sigma,S0,N)
复制代码

这样申明才对。

同时,你的程序末尾的绘图程序,应该是有问题的,
  1. ss=[1:n+1];
  2. plot(ss-1,ss(ss),...);
复制代码

在这里, ss 变量存储了1,2,3···n+1 个整数。其中plot的y轴,你给的变量只会给它一个数,而x轴,却已经是n+1个。这是不对应的。

同时,一个好的编程习惯。plot 这样的命令最好别在函数里。当然,那些做GA算法的人,希望有一个可视化的优化过程看优化数值的收敛性,在函数里带绘图命令那个也正常。
而这个程序的plot,可以考虑在后面的调用中,在script文件里写入。

还不知道你们说的问题是什么。我暂时找到的就是这个吧。
劳动经济学

17
tulipsliu(真实交易用户) 在职认证  发表于 2011-6-16 21:30:32
修改后的程序,
第一个:
  1. function [Call,Put] = BinomialEuro(T,n,k,r,sigma,s0,z)
  2. % T expiration date
  3. % n  total number of periods
  4. % k  exercise price
  5. % r  risk free interest rate
  6. % sigma  volatility of
  7. % s0  present price
  8. % z devidend
  9. deltat = T/n;
  10. u=exp(sigma*sqrt(deltat));
  11. d=exp(-sigma*sqrt(deltat));

  12. p=(exp((r-z)*deltat)-d)/(u-d);
  13. q=1-p;
  14. s=zeros(n+1,1);
  15. w=s;
  16. x=s;
  17. y=s;
  18. cv=s;
  19. pv=s;
  20. for i=1:n+1
  21.     s(i)=s0*u^(n+1-i)*d^(i-1); % stock price
  22.     w(i)=nchoosek(n,i-1)*p^(n+1-i)*q^(i-1); % probability
  23.     x(i)=max(s(i)-k,0); % call option price
  24.     y(i)=max(k-s(i),0); % put option price
  25.     cv(i)=w(i)*x(i);
  26.     pv(i)=w(i)*y(i);
  27. end
  28. Call=sum(cv(:))*exp(-r*T);
  29. Put=sum(pv(:))*exp(-r*T);
复制代码

第二个:
  1. function [Call,Put] = TrinomialEuro(s0,k,T,n,r,sigma)

  2. % s0 underlying asset price
  3. % k exercise price
  4. % T expiration date
  5. % numbers of steps
  6. %  risk free rate
  7. % sigma volitility of stock

  8. deltat=T/n;

  9. u=exp(sigma*sqrt(deltat));
  10. d=1/u;
  11. m=1;

  12. P=(exp(2*r*deltat)+sigma^2*deltat-(d+1)*exp(r*deltat)+d)/((u^2-1)-(u-1)*(d+1));
  13. Q=(exp(r*deltat)-1-(u-1)*P)/(d-1);
  14. M=1-Q-P;
  15. for i=1:n+1
  16.     for j=1:n+2-i
  17.         s(i,j)=s0*u^(i-1)*m^(j-1)*d^(n+2-i-j);
  18.         w(i,j)=nchoosek(n,i-1)*P^(i-1)*nchoosek(n+1-i,j-1)*M^(j-1)*Q^(n+2-i-j);
  19.         x(i,j)=max(s(i,j)-k,0);
  20.         y(i,j)=max(k-s(i,j),0);
  21.         cv(i,j)=w(i,j)*x(i,j);
  22.         pv(i,j)=w(i,j)*y(i,j);
  23.     end
  24. end

  25. Call=sum(cv(:))*exp(-r*T);
  26. Put=sum(pv(:))*exp(-r*T);
复制代码

第三个:
  1. function [Price] = BinomialAmerican(T,n,k,r,sigma,s0,z)
  2. % T  expiration date
  3. % n  total number of periods
  4. % k  exercise price
  5. %  risk free interest rate
  6. % sigma  volatility of
  7. % s0  present price
  8. % z  devidend
  9. deltat = T/n;
  10. u=exp(sigma*sqrt(deltat));
  11. d=exp(-sigma*sqrt(deltat));

  12. p=(exp((r-z)*deltat)-d)/(u-d);
  13. q=1-p;

  14. for j=1:n
  15.     for i=1:j+1
  16.         s(j,i)=s0*u^(j+1-i)*d^(i-1);
  17.         y(j,i)=max(k-s(j,i),0);
  18.     end
  19. end
  20. f=zeros(n,n);
  21. for w=1:n
  22.     f(n-1,w)=max(y(n-1,w),exp(-r*deltat)*(p*y(n,w)+q*y(n,w+1)));
  23. end
  24. for g=n-2:-1:1
  25.     for h=1:g+1
  26.         f(g,h)=max(y(g,h),exp(-r*deltat)*(p*f(g+1,h)+q*f(g+1,h+1)));
  27.     end
  28. end

  29. Price=max(exp(-r*deltat)*(p*f(1,1)+q*f(1,2)),0);
复制代码
劳动经济学

18
葛新龙(未真实交易用户) 学生认证  发表于 2011-6-17 20:32:07
果然是高手,谢谢指点!我们以前没有学过编程语言,这是被逼出来的,谢谢您的帮助!!我们会继续改正的~~

19
tulipsliu(真实交易用户) 在职认证  发表于 2011-6-18 09:55:45
18# 葛新龙
没什么,你们的代码我还没测试,只是看语法问题。你的程序也会显示结果的,最后的那个没用“;”这个,所以屏幕上会有结果。
不过,一般一个函数申明,最好有返回值。
其他的不说了,等你以后有其他问题再聊。你才开始编程,可以写成这样不错了。呵呵。慢慢来。
劳动经济学

20
colin19881014(未真实交易用户) 发表于 2012-1-27 16:53:57
好东西支持一下,谢谢了

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-26 06:31