new; cls; /* Stochastic Trinomial Asian Call & Put Options */ S0=50; K=50; r=0.10; sig=0.40; T=5/12; nn=500; times=5000; print/lz " nn=" nn; print/lz " times=" times; print "C=" PTasianC(S0,K,r,sig,T,nn,times); print "P=" PTasianP(S0,K,r,sig,T,nn,times); print; print "Another version:"; print "C=" PTasianC2(S0,r,sig,T,nn,times); print "P=" PTasianP2(S0,r,sig,T,nn,times); print; print "Geometric Mean Asian:"; print "C=" PTgasianC(S0,K,r,sig,T,nn,times); print "P=" PTgasianP(S0,K,r,sig,T,nn,times); print; print "Another version of Geometric Mean:"; print "C=" PTgasianC2(S0,r,sig,T,nn,times); print "P=" PTgasianP2(S0,r,sig,T,nn,times); /* ** ptasian.txt - Stochaastic Trinomial Asian call & Put Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates Asian option prices by stochastic trinomial simulation. ** ** Format: C=PTasianC(S0,K,r,sig,T,nn,times); ** P=PTasianP(S0,K,r,sig,T,nn,times); ** ** Input: S0 scalar, initial value ** ** K scalar, strike price ** ** r scalar, risk-free interest rate ** ** sig scalar, volatility ** ** T scalar, maturity ** ** nn scalar, number of time steps ** ** times scalar, number of simulations ** ** ** Output: C scalar, call option price ** P scalar, put option price ** */ proc PTasianC(S0,K,r,sig,T,nn,times); local AV,i,S,C; AV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); AV[i]=meanc(S[2:nn+1]); i=i+1; endo; C=exp(-r*T)*meanc(maxc(((AV-K)~zeros(times,1))')); retp(C); endp; proc PTasianP(S0,K,r,sig,T,nn,times); local AV,i,S,P; AV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); AV[i]=meanc(S[2:nn+1]); i=i+1; endo; P=exp(-r*T)*meanc(maxc(((K-AV)~zeros(times,1))')); retp(P); endp; /* ** ptasian.txt - Another version of Stochastic Trinomial Asian Call & Put Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates Asian option prices by stochastic trinomial simulation. ** ** Format: C=PTasianC2(S0,r,sig,T,nn,times); ** P=PTasianP2(S0,r,sig,T,nn,times); ** ** Input: S0 scalar, initial value ** ** r scalar, risk-free interest rate ** ** sig scalar, volatility ** ** T scalar, maturity ** ** nn scalar, number of time steps ** ** times scalar, number of simulations ** ** ** Output: C scalar, call option price ** P scalar, put option price ** */ proc PTasianC2(S0,r,sig,T,nn,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); OV[i]=S[nn+1]-meanc(S[2:nn+1]); i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; proc PTasianP2(S0,r,sig,T,nn,times); local OV,i,S,P; OV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); OV[i]=meanc(S[2:nn+1])-S[nn+1]; i=i+1; endo; P=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(P); endp; /* ** ptasian.txt - Stochastic Trinomial Geometric Asian call & Put Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates Geometric Asian option prices by stochastic trinomial simulation. ** ** Format: C=PTgasianC(S0,K,r,sig,T,nn,times); ** P=PTgasianP(S0,K,r,sig,T,nn,times); ** ** Input: S0 scalar, initial value ** ** K scalar, strike price ** ** r scalar, risk-free interest rate ** ** sig scalar, volatility ** ** T scalar, maturity ** ** nn scalar, number of time steps ** ** times scalar, number of simulations ** ** ** Output: C scalar, call option price ** P scalar, put option price ** */ proc PTgasianC(S0,K,r,sig,T,nn,times); local AV,i,S,C; AV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); AV[i]=geomean(S[2:nn+1]); i=i+1; endo; C=exp(-r*T)*meanc(maxc(((AV-K)~zeros(times,1))')); /* We use arithmetic one here. */ retp(C); endp; proc PTgasianP(S0,K,r,sig,T,nn,times); local AV,i,S,P; AV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); AV[i]=geomean(S[2:nn+1]); i=i+1; endo; P=exp(-r*T)*meanc(maxc(((K-AV)~zeros(times,1))')); /* we use arithmetic one here. */ retp(P); endp; /* ** ptasian.txt - Another version of Stochastic Trinomial Geometric Asian call & Put Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates Geometric Asian option prices by stochastic trinomial simulation. ** ** Format: C=PTgasianC2(S0,r,sig,T,nn,times); ** P=PTgasianP2(S0,r,sig,T,nn,times); ** ** Input: S0 scalar, initial value ** ** r scalar, risk-free interest rate ** ** sig scalar, volatility ** ** T scalar, maturity ** ** nn scalar, number of time steps ** ** times scalar, number of simulations ** ** ** Output: C scalar, call option price ** P scalar, put option price ** */ proc PTgasianC2(S0,r,sig,T,nn,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); OV[i]=S[nn+1]-geomean(S[2:nn+1]); i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; proc PTgasianP2(S0,r,sig,T,nn,times); local OV,i,S,P; OV=zeros(times,1); i=1; do while i<=times; S=ptsampler(S0,r,sig,T,nn); OV[i]=geomean(S[2:nn+1])-S[nn+1]; i=i+1; endo; P=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(P); endp; proc geomean(x); local n; n=rows(x); retp(exp(1/n*sumc(ln(x)))); endp; /* ** ptsampler.txt - Stochastic Trinomial Path Sampler. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets a stochastic path on trinomial tree. ** ** Format: S=ptsampler(S0,r,sig,T,nn) ** ** Input: S0 scalar, initial value ** ** r scalar, risk-free interest rate ** ** sig scalar, volatility ** ** T scalar, maturity ** ** nn scalar, number of time steps ** ** ** Output: S vector, (nn+1) x 1 of resulting values including S0 ** ** Notice: This procedure uses 'ptrisampler' inside. ** */ proc ptsampler(S0,r,sig,T,nn); local delt,pd,pm,pu,p,S,u; delt=T/nn; pd=-sqrt(delt/(12*sig^2))*(r-1/2*sig^2)+1/6; pm=2/3; pu=sqrt(delt/(12*sig^2))*(r-1/2*sig^2)+1/6; p=pu|pm|pd; S=ptrisampler(p,nn); S=S-1; u=exp(sig*sqrt(3*delt)); S=S0*u^cumsumc(S); S=S0|S; retp(S); endp; /* ** ptrisampler.txt - Stochastic Trinomial 0-1-2 Sampler. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets stochastic Trinomial 0-1-2 index numbers in a very easy way. ** ** Format: x=ptrisampler(p,nr); ** ** Input: p vector, 3 x 1 of probability vector(p[1] for 2, p[2] for 1, p[3] for 0) ** ** nr scalar, number of rows ** ** ** Output: x vector, nr x 1 of resulting 0-1 index vector ** */ proc ptrisampler(p,nr); local x; p=cumsumc(p); x=rndu(nr,1); x=2*(x.<=p[1])+1*(x.>p[1] .and x.<=p[2]); retp(x); endp;