new; cls; /* Stochastic Binomial alpha-percentile Call Options */ S0=50; K=50; r=0.10; sig=0.40; T=5/12; alpha=0.75; nn=500; times=5000; print/lz " nn=" nn; print/lz " times=" times; print/lz " alpha=" alpha; print "floating strike:"; print "C=" PBalphapC(S0,r,sig,T,nn,alpha,times); print; print "fixed strike:"; print "C=" PBalphapCf(S0,K,r,sig,T,nn,alpha,times); /* ** pbalphap.txt - Stochastic Binomial alpha-percentile(Floating Strike) Call Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates alpha-percentile option prices by stochastic binomial simulation. ** ** Format: C=PBalphapCf(S0,r,sig,T,nn,alpha,times); ** ** ** Input: S0 scalar, initial value ** ** r scalar, risk-free interest rate ** ** sig scalar, volatility ** ** T scalar, maturity ** ** nn scalar, number of time steps ** ** alpha scalar, percentile ( 0 <= alpha <= 1 ) ** ** times scalar, number of simulations ** ** ** Output: C scaler, call option price ** */ proc PBalphapC(S0,r,sig,T,nn,alpha,times); local OV,i,S,STT,C; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); STT=S[nn+1]; S=S[2:nn+1]; S=sortc(S,1); if round(nn*alpha)==0; OV[i]=STT-S[1]; else; OV[i]=STT-S[round(nn*alpha)]; endif; i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); endp; /* ** pbalphap.txt - Stochastic Binomial alpha-percentile(Fixed Strike) Call Options. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates alpha-percentile option prices by stochastic binomial simulation. ** ** Format: C=PBalphapCf(S0,K,r,sig,T,nn,alpha,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 ** ** alpha scalar, percentile ( 0 <= alpha <= 1 ) ** ** times scalar, number of simulations ** ** ** Output: C scaler, call option price ** */ proc PBalphapCf(S0,K,r,sig,T,nn,alpha,times); local OV,i,S,C; OV=zeros(times,1); i=1; do while i<=times; S=pbsampler(S0,r,sig,T,nn); S=S[2:nn+1]; S=sortc(S,1); if round(nn*alpha)==0; OV[i]=S[1]-K; else; OV[i]=S[round(nn*alpha)]-K; endif; i=i+1; endo; C=exp(-r*T)*meanc(maxc((OV~zeros(times,1))')); retp(C); 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;