new; cls; /* Stochastic Binomial Barrier Call & Put Options */ S0=50; K=50; r=0.10; sig=0.40; T=5/12; nn=500; vH=60; vL=15; times=5000; print/lz " nn=" nn; print/lz " times=" times; print; print "double barriers(vL<=St<=vH):"; print/lz " vH=" vH; print/lz " vL=" vL; print "C=" PBbarriersC(S0,K,r,sig,T,nn,vH,vL,times); print "P=" PBbarriersP(S0,K,r,sig,T,nn,vH,vL,times); print; vB=60; print "up and in:"; print/lz " vB=" vB; print "C=" PBupandinC(S0,K,r,sig,T,nn,vB,times); print "P=" PBupandinP(S0,K,r,sig,T,nn,vB,times); print; print "up and out:"; print/lz " vB=" vB; print "C=" PBupandoutC(S0,K,r,sig,T,nn,vB,times); print "P=" PBupandoutP(S0,K,r,sig,T,nn,vB,times); print; print "down and in:"; print/lz " vB=" vB; print "C=" PBdownandinC(S0,K,r,sig,T,nn,vB,times); print "P=" PBdownandinP(S0,K,r,sig,T,nn,vB,times); print; vB=15; print "down and out:"; print/lz " vB=" vB; print "C=" PBdownandoutC(S0,K,r,sig,T,nn,vB,times); print "P=" PBdownandoutP(S0,K,r,sig,T,nn,vB,times); /* ** pbbarrier.txt - Stochastic Binomial Double Barriers Call & Put Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates Barrier option prices by stochastic binomial simulation. ** ** Format: C=PBbarriersC(S0,K,r,sig,T,nn,vH,vL,times); ** P=PBbarriersP(S0,K,r,sig,T,nn,vH,vL,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 ** ** vH, vL scalar, barriers ( effective when vH <= St <= vL ) ** if it is oneside barrier, set 1e8 or 1e-8 to either. ** ** times scalar, number of simulations ** ** ** Output: C scalar, call option price ** P scalar, put option price ** */ proc PBbarriersC(S0,K,r,sig,T,nn,vH,vL,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S>=vL and S<=vH; /* vector evaluation */ OV[i]=S[nn+1]-K; /* effective */ else; OV[i]=0; /* value=0 */ endif; i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; proc PBbarriersP(S0,K,r,sig,T,nn,vH,vL,times); local OV,i,S,P; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S>=vL and S<=vH; /* vector evaluation */ OV[i]=S[nn+1]-K; /* effective */ else; OV[i]=0; /* value=0 */ endif; i=i+1; endo; P=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(P); endp; /* ** pbbarrier.txt - Stochastic Binomial Knock-in(out) Call & Put Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates Knock-in and Knock-out option prices by stochastic binomial simulation. ** ** Format: C=PBupandinC(S0,K,r,sig,T,nn,vB,times); UP and IN ** P=PBupandinP(S0,K,r,sig,T,nn,vB,times); ** ** C=PBupandoutC(S0,K,r,sig,T,nn,vB,times); UP and OUT ** P=PBupandoutP(S0,K,r,sig,T,nn,vB,times); ** ** C=PBdownandinC(S0,K,r,sig,T,nn,vB,times); DOWN and IN ** P=PBdownandinP(S0,K,r,sig,T,nn,vB,times); ** ** C=PBdownandoutC(S0,K,r,sig,T,nn,vB,times); DOWN and OUT ** P=PBdownandoutP(S0,K,r,sig,T,nn,vB,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 ** ** vB scalar, barrier ** ** times scalar, number of simulations ** ** ** Output: C scalar, call option price ** P scalar, put option price ** */ proc PBupandinC(S0,K,r,sig,T,nn,vB,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S<=vB; /* vector evaluation */ OV[i]=0; /* value=0 */ else; OV[i]=S[nn+1]-K; /* effective */ endif; i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; proc PBupandinP(S0,K,r,sig,T,nn,vB,times); local OV,i,S,P; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S<=vB; /* vector evaluation */ OV[i]=0; /* value=0 */ else; OV[i]=K-S[nn+1]; /* effective */ endif; i=i+1; endo; P=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(P); endp; proc PBupandoutC(S0,K,r,sig,T,nn,vB,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S<=vB; /* vector evaluation */ OV[i]=S[nn+1]-K; /* effective */ else; OV[i]=0; /* value=0 */ endif; i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; proc PBupandoutP(S0,K,r,sig,T,nn,vB,times); local OV,i,S,P; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S<=vB; /* vector evaluation */ OV[i]=K-S[nn+1]; /* effective */ else; OV[i]=0; /* value=0 */ endif; i=i+1; endo; P=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(P); endp; proc PBdownandinC(S0,K,r,sig,T,nn,vB,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S>=vB; /* vector evaluation */ OV[i]=0; /* value=0 */ else; OV[i]=S[nn+1]-K; /* effective */ endif; i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; proc PBdownandinP(S0,K,r,sig,T,nn,vB,times); local OV,i,S,P; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S>=vB; /* vector evaluation */ OV[i]=0; /* value=0 */ else; OV[i]=K-S[nn+1]; /* effective */ endif; i=i+1; endo; P=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(P); endp; proc PBdownandoutC(S0,K,r,sig,T,nn,vB,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S>=vB; /* vector evaluation */ OV[i]=S[nn+1]-K; /* effective */ else; OV[i]=0; /* value=0 */ endif; i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; proc PBdownandoutP(S0,K,r,sig,T,nn,vB,times); local OV,i,S,P; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); if S>=vB; /* vector evaluation */ OV[i]=K-S[nn+1]; /* effective */ else; OV[i]=0; /* value=0 */ endif; i=i+1; endo; P=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(P); endp; /* ** pbsampler.txt - Stochastic Binomial Path Sampler. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets a stochastic path on binomial tree. ** ** Format: S=pbsampler(S0,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 'pbisampler' inside. ** */ proc pbsampler(S0,r,sig,T,nn); local delt,a,u,d,p,S; delt=T/nn; a=exp(r*delt); u=exp(sig*sqrt(delt)); d=1/u; p=(a-d)/(u-d); S=pbisampler(p,nn); S=S-1*(S.==0); S=S0*u^cumsumc(S); S=S0|S; retp(S); endp; /* ** pbisampler.txt - Stochastic Binomial 0-1 Sampler. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets stochastic binomial 0-1 index numbers in a very easy way. ** ** Format: x=pbisampler(p,nr); ** ** Input: p scalar, probability(for 1) ** ** nr scalar, number of rows ** ** ** Output: x vector, nr x 1 of resulting 0-1 index vector ** */ proc pbisampler(p,nr); local x; x=rndu(nr,1); x=(x.<=p); retp(x); endp;