/* A Deterministic Path on Trinomial Tree */ /* Notice: This version will not go into overflow in tsampler2 itself. */ /* However, its probability pt2 will become too small to zero. */ /* This way of thinking can not be applied with probability. */ /* Use stochastic vesion of it instead if the number of time */ /* steps is large and then each probablity becomes zero. */ new; cls; S0=50; r=0.10; sig=0.40; T=5/12; x={2,1,2,2,0,1,0,1,1,0,0,1,1,1,0,1,0,2,2,1,2,0,1,0,1,1,0,0,1,1,1,0,1,0,1,1,0, 0,1,2,2,2,1,2,1,1,1,0,1,0,2,2,2,2,2}; /* 0, 1 or 2 you could change */ S=tsampler2(S0,sig,T,x); print S; print "p=" pt2(S0,r,sig,T,x); library pgraph; graphset; title("A Path on Trinomial Tree"); ylabel("S"); xlabel("t"); xy(seqa(0,T/(rows(S)-1),rows(S)),S); /* ** tsampler2.txt - Deterministic Trinomial Path Sampler. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets a deterministic path on trinomial tree for given ternary index vector x ** without overflow problem. ** ** Format: S=tsampler2(S0,sig,T,x) ** ** Input: S0 scalar, initial value ** ** sig scalar, volatility ** ** T scalar, maturity ** ** x vector, nn x 1 of ternary index vector ** ** ** Output: S vector, (nn+1) x 1 of resulting values including S0 ** */ proc tsampler2(S0,sig,T,x); local nn,delt,u,S; nn=rows(x); x=x-1*(x.==0)-1*(x.==1)-1*(x.==2); delt=T/nn; u=exp(sig*sqrt(3*delt)); S=S0*u^cumsumc(x); S=S0|S; retp(S); endp; /* ** tsampler2.txt - Probability of the Deterministic Trinomial Path. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets the probability of the path for given ternary index vector x ** without overflow problem. ** ** Format: p=pt2(S0,r,sig,T,nn,i); ** ** Input: S0 scalar, initial value ** ** r scalar, risk-free interest rate ** ** sig scalar, volatility ** ** T scalar, maturity ** ** x vector, nn x 1 of ternary index vector ** ** ** Output: p scalar, probability of the path ** */ proc pt2(S0,r,sig,T,x); local nn,delt,pd,pm,pu,p; nn=rows(x); 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=pd*(x.==0)+pm*(x.==1)+pu*(x.==2); p=prodc(p); retp(p); endp;