> GeometricBrownian:=procT,n,seed,mu,sigma,x0 local h,sh,X,P,j,z,mh; # # Procedure generates a list [{[j*h,Xj*h],j=0..n}] where Xj*h is the position at time jh of the geometric Brownian
Trang 1xð1Þe xþ1=xð Þ=2 on supportð0; 1Þ using a proposal density proportional
to xð1Þex=2 for selected values of and The envelope has beenoptimized by setting
¼22 ffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffi
arbitrary ratio of uniforms generators
The procedure ‘ratioaccep’ is used to compute the acceptance probability for aratio of uniforms generator for a p.d.f proportional to h(x) The parametersare:
h¼ a procedure supplied by the user,
½x1; x2 ¼ the connected support of h,
xinit¼ a suggested start point for the numerical maximization of h(x) over
Trang 2xinit1¼ a suggested start point for the numerical maximization of x2hðxÞ
Use ‘ratioaccep’ to compute the acceptance probabilities and uþ; þ; for
a density proportional to 1= 1ð þ x2=3Þ2on supportð1; 1Þ
3p
Trang 3The warning arises because the initial point chosen for the maximization of hover the entire support happens to be the maximizing point.
Trang 4This is a polar implementation of the Box–Mu¨ller generator, as described inSection 4.1 Note the warning to set i and X2to global variables and to set i to
‘false’ on the first call
> STDNORM:=proc( ) local ij,U1,U2,S,B,X1;global i,X2;
#
# PROCEDURE GENERATES A STANDARD RANDOM NORMAL
# DEVIATE, USING THE POLAR BOX MULLER METHOD
# SET i to ’false’ ON FIRST CALL Note that i and
Trang 5# X2 are global variables and have to be declared as
# such in any procedure that calls STDNORM
if type(i,boolean)then
if (i) theni:=not(i);X2;
elsefor ij from 1 to infinity doU1:=evalf(2.0*rand()/10^12)-1.0;
> beta:=proc(a,b) local z,r1,r2,w,rho,m,la;
if a>0 and b>0 thenrho:=a/b;m:=min(a,b);if m<=1 then la:=m elsela:=sqrt((2*a*b-a-b)/(a+b-2)) end if;
(a+b)))<0 then break end if;
Trang 64.3 Student’s t distribution
This procedure generates a Student’s t variate with n degrees of freedom using
the method described in Section 4.7 If n=1 then a Cauchy variate is delivered
This procedure generates a variate from a p.d.f that is proportional to
x1eðxþ1=xÞ=2 on support ð0; 1Þ where 0 < and 0 < The method is
else ERROR("beta must be positive") end if;
else ERROR("lambda must be positive") end if;
Trang 7if p>0.5 then n-x else x end if;
else ERROR("p should belong to[0,1]") end if;
else ERROR("n should be a positive integer") end if;
Trang 8kis a positive real It uses unstored inversion as mentioned in Section 4.11 It
is fast providing that kp=q is not too large In that case the Poisson gamma
method described in Section 4.11 is better
> negbinom:=proc(k,p) local q,r,y,x;
else ERROR("p should belong to[0,1]") end if;
else ERROR("k should be positive") end if;
Trang 10Appendix 5: Variance reduction
The procedure ‘theta1_2’ samples m unit negative exponential variates, andraises each to the power of 0.9 The sample mean of the latter is an estimate
of ð1:9Þ
> restart;with(stats):
h
> theta1_2:=proc(m) local j,r,x,y,u;
for j from 1 to m do;
print("standard
error"=evalf(describe[standarddeviation[1]](u)/sqrt(m)));u;
Trang 11Now replace r by 1 r and theta1_hat by theta2_hat in the print statement
of procedure ‘theta1_2’ and run the simulation again with the same seed
The procedure ‘theta_combined’ does the two jobs above in one run, giving
an estimate and estimated standard error using primary and antitheticvariates
2
6
> theta_combined:=proc(m) local j,r,x,y,u,r1,z;
for j from 1 to m do;
Trang 12The procedure ‘impbeta’ estimates Pfa <Pn
i¼1Xig where fXig are i.i.d betadistributed with shape parameters and , both greater than one The
importance sampling density is gðxÞ ¼Qn
i¼1xð 1Þi on supportð0; 1Þnwhere
< A good choice is ¼ 1= logðn=aÞ and a should satisfy n e1= a
Trang 13std_dev:=sqrt(n*alpha*beta/(alpha+beta+1)/(alpha+beta)^2);print("alpha"=alpha,"beta"=beta,"n"=n,"mean "=mean,"stddev"=std_dev);
if a<evalf(n*exp(-1/alpha)) then print("STOP, use naive montecarlo" )
return end if;
gam:=evalf(1/ln(n/a));
gam1:=1/gam;
b:=(GAMMA(alpha+beta)/GAMMA(alpha)/GAMMA(beta)/gam)^n;s1:=0;s2:=0;
for j from 1 to m do;
Trang 14‘variance reduction ratio’¼ 2.010769567 107
‘central limit approx’¼ 9.865876451 1010
‘variance reduction ratio’¼ 11.53805257
‘central limit approx’¼ 0.02275013195
‘alpha’¼ 2.5, ‘beta’ ¼ 1.5, ‘n’ ¼ 12, ‘mean’ ¼ 7.500000000, ‘std dev’ ¼ 0.7500000000
‘estimate of probability that sum of’, 12, ‘variates exceeds’, 9, ‘is’, 0.01978833352
‘standard error’¼ 0.0005813529138
‘variance reduction ratio’¼ 11.47834773
‘central limit approx’¼ 0.02275013195
Trang 15‘seed’¼ 6811357
‘alpha’¼ 2.5, ‘beta’ ¼ 1.5, ‘n’ ¼ 24, ‘mean’ ¼ 15.00000000, ‘std dev’ ¼ 1.060660172
‘estimate of probability that sum of’, 24, ‘variates exceeds’, 18, ‘is’, 0.001761757803
‘standard error’¼ 0.00008414497712
‘variance reduction ratio’¼ 49.67684543
‘central limit approx’¼ 0.002338867496
Z 1 0
> weibullnostrat:=proc(k) local j,r1,r2,w,v,y,sum1,sum2,m;sum1:=0;sum2:=0;
for m from 1 to k do;
end proc:
26666666666664
Trang 165.3.2 Stratified version using a single stratification
variable
First plot
Y ¼ ½ ln ðr 1Þ2=3þ ½ lnðr2Þ2=35=4
against stratified variable X¼ r1r2 This confirms that much of the
variation in Y is accounted for by variation in the conditional expectation
of Y given X Therefore, stratification on X will be effective
> with(stats);
Warning, these names have been redefined: anova, describe,
fit, importdata, random, statevalf, statplots, transform
2
6
> strat:=proc(n) local j,r1,r2,x,a1,a2,y,z;
for j from 1 to n do;
0.80.4
0.2
6543Y
X
correlation :=– 0.8369837074
CV_VRF :=0.29945827350
Trang 17The following procedure, ‘weibullstrat’, performs k independent tions Each realization comprises n observations over n equiprobablestrata, with exactly one observation per stratum.
realiza-24
> weibullstrat:=proc(n,k) localj,u1,t,x,u2,r2,r1,y,w,v,tprev,sum1,sum2,mean,s1,m;sum1:=0;sum2:=0;
for m from 1 to k do;
end proc:
2666666666666666666664
> t1:=time();seed:=randomize(639156);weibullstrat(100,200);t2:=time()-t1;
t1:¼ 109.077seed:¼ 639156
‘mean’¼ 2.166441095, ‘std error’ ¼ 0.001321419976
t2:¼ 110.250
26666
estimated_vrr:=(.1321419976e-2/.9127721506e-2)^(-2);estimated_efficiency:=t3*estimated_vrr/t2;
estimated_vrr:=47.71369240estimated_efficiency:=8.871933734
266
The procedure ‘grid’ estimates the same integral using k replicationswhere there are now two stratification variables, r1 and r2 Each replica-tion comprises n2 equiprobable strata on [0, 1]2
Trang 18> grid:=proc(n,k) local j,r1,r2,w,v,y,m,sum1,sum2,k1,s1,
mean;
sum1:=0;sum2:=0;
for k1 from 1 to k do;
s1:=0;
for j from 1 to n do;
for m from 1 to n do;
Trang 20Appendix 6: Simulation and
# PROCEDURE GENERATES A STANDARD RANDOM NORMAL
# DEVIATE, USING THE POLAR BOX MULLER METHOD
# SET i to 'false' ON FIRST CALL Note that i and
# X2 are global variables and have to be declared as
# such in any procedure that calls this STDNORM
if type(i,boolean)then
if (i) theni:=not(i);X2;
elsefor ij from 1 to infinity doU1:=evalf(2.0*rand()/10^12)-1.0;
Trang 226.2 Geometric Brownian motion
Let fBðtÞg denote a standard Brownian motion The procedure
‘Geometric-Brownian’ generates the position, XðtÞ, of a Brownian motion
dX
X ¼ dt þ d
at times 0, h, 2h, .,nh where nh¼ T The solution to the stochastic
differ-ential equation is XðtÞ ¼ XðsÞ eð 2 =2ÞðtsÞþBðtsÞ Suppose XðsÞ ¼ xðsÞ
Since EðXðtÞÞ ¼ xðsÞeðtsÞ; is interpreted as the expected growth rate
> GeometricBrownian:=proc(T,n,seed,mu,sigma,x0) local
h,sh,X,P,j,z,mh;
#
# Procedure generates a list [{[j*h,X(j*h)],j=0 n}] where
X(j*h) is the position at time jh of the geometric Brownian
motion(with expected growth rate, mu, and volatility, sigma)
TITLE("Three independent realizations of a geometric
Brownian motion with expected \n growth rate of 0.1,
volatility 0.3, initial price
Trang 23Three independent realizations of a geometric Brownian motion with expected
growth rate of 0.1, volatility 0.3, initial price = 100 300
250 200 150 100 50
t X(t)
Independent Geometric Brownian motions over 10 years with expected growth rate
of 0.15 p.a., volatility 0.02, 0.04, 0.08 p.a., initial price = 100 pence
Using the built-in ‘blackscholes’ procedure (part of Maple’s finance package),find the price of a European call option on a share that is currently priced at
£100, has volatility 20 % per annum (this means that the standard deviation
Trang 24on the return in 1 year is 0.2), and where the option has 103 trading days till
expiry (252 trading days in a year) The strike price is £97 The risk-free interest
rate is 5 % Assume no dividends
p ffiffiffi2p
p
0:0007936507936 ffiffiffiffiffiffiffiffi
103
p ffiffiffiffiffiffiffiffi252
The procedure ‘BS’ below simulates m independent payoffs together with their
antithetic counterparts; x0 is the current asset price at time t, and te is the
exercise time of the option
Trang 25The procedure ‘hedge’ returns the cost of writing and hedging a call option,where the hedging is performed nnþ 1 times The asset earns interest at rate
rf The risk-free interest rate is r and the (unknown) expected growth rate is .Print statements are currently suppressed using ‘#’ Removing ‘#’ will showthe priceðxÞ of the asset, the D, the change in D since the last hedge, and thecumulative borrowings to finance the hedge, all at each hedging instant.Remember to load the procedure ‘STDNORM’ in Appendix 6.1
> hedge:=proc(K,r,rf,sigma,T,n,x0,mu) locala1,a2,a3,a4,a5,a6,h,x,d,delta,delta_prev,c,j,z,cost,xprev;global i,X2;
Trang 26delta_prev:=delta*a6;# The holding in assets changes due
to the interest earned on them at rate rf
The procedure ‘effic’ calls ‘hedge’ replic times It stores the costs in the list p
and then prints the resulting mean and standard deviation of hedging cost
together with a histogram If the option were continuously hedged, then the
standard deviation would be zero, and the hedging cost would always equal
the Black–Scholes price The current price of the asset is £680 and the strike
price is £700 The risk-free interest rate is 5% per annum and the asset earns
interest continuously at the rate of 3% per annum The volatility is 0.1 per
annum and the exercise time is 0.5 years from now
Trang 27print("mean cost of hedging"=e1, "std dev of cost"=e2,
"number of hedges"=nn+1, "number of contracts"=replic);statplots[histogram](p);
‘mean cost of hedging’=21.53800165, ‘std dev of cost’=22.17558076,
‘number of hedges’=2, ‘number of contracts’=10000
‘mean cost of hedging’=17.26850774, ‘std dev of cost’=13.21884756,
‘number of hedges’=3, ‘number of contracts’=10000
Trang 28‘mean cost of hedging’=16.02928370, ‘std dev of cost’=10.25258825,
‘number of hedges’=4, ‘number of contracts’=10000
‘mean cost of hedging’=15.30498281, ‘std dev of cost’=8.665076040,
‘number of hedges’=5, ‘number of contracts’=10000
Trang 29‘mean cost of hedging’=13.99661047, ‘std dev of cost’=4.750293651,
‘number of hedges’=13, ‘number of contracts’=10000
‘mean cost of hedging’=13.40525657, ‘std dev of cost’=1.452085739,
‘number of hedges’=127, ‘number of contracts’=10000
Trang 30The cost of continuous hedging is the Black–Scholes cost, which is computed
in the procedure ‘bscurrency’
Now, as suggested in the text, perform your own experiments to verify that
(subject to sampling error) the expected cost of writing and discrete hedging the
option is the Black–Scholes price, when the expected growth rate of the
under-lying asset happens to be the same as the risk-free interest rate Of course, there
is still variation in this cost, reflecting the risk from not hedging continuously
The procedure ‘asiannaive’ computes the price of an Asian call option
(arithmetic average) using standard Monte Carlo with no variance
redu-cation The parameters are:
Trang 31r¼ risk-free interest rate
x0¼ known asset price at time zero
¼ volatility
T ¼ expiry (exercise) time for the option
n¼ number of time periods over which the average is taken
h¼ time increment such that T ¼ nhnpath¼ number of paths simulated
K¼ strike price
> asiannaive:=proc(r,x0,sigma,T,n,npath,K) localR,a1,a2,s1,s2,theta,x,xc,i1,i2,z,h,mean,stderr; globali,X2;
# Computes call price for Asian option
print("point estimate of price"=exp(-r*T)*theta);
print("estimated standard error"=exp(-r*T)*stderr);end proc:
2666666666666666666666666664
Now call ‘asiannaive’ for the parameter values given below, ing first to load the procedure STDNORM (Section 6.1) Thesevalues are used in the paper by P Glasserman, P Heidelberger, and
remember-P Shahabuddin (1999), Asymptotically optimal importance samplingand stratification for pricing path-dependent options, MathematicalFinance, 9, 117–52 The present results can be compared with thoseappearing in that paper
266664
Trang 32> randomize(13753);asiannaive(0.05,50,0.3,1,16,25000,55);
13753
‘K’=55, ‘sigma’=0.3,‘n’=16
‘#paths’=25000
‘point estimate of price’=2.214911961
‘estimated standard error’=0.03004765102
‘point estimate of price’=4.166563741
‘estimated standard error’=0.03986122915
13753
‘K’=45, ‘sigma’=0.3, ‘n’=16
‘# paths’=25000
‘point estimate of price’=7.145200333
‘estimated standard error’=0.04857962659
13753
‘K’=55, ‘sigma’=0.1, ‘n’=16
‘# paths’=25000
‘point estimate of price’=0.2012671276
‘estimated standard error’=0.004621266108
13753
‘K’=50, ‘sigma’=0.1, ‘n’=16
‘# paths’=25000
‘point estimate of price’=1.917809699
‘estimated standard error’=0.01401995095
13753
‘K’=45, ‘sigma’=0.1, ‘n’=16
‘# paths’=25000
‘point estimate of price’=6.048215279
‘estimated standard error’=0.01862584391
The procedure ‘asianimpoststrat’ computes the price of an Asian average
price call option (arithmetic average) using importance sampling and post
stratified sampling Let n be the number of time points over which the
average is calculated The drift is set tob where i¼ ð@=@ZiÞ ln (payoff)
at zi¼ i; i¼ 1; ; n, and where payoff is based on the geometric
average This gives ¼ ðn i þ 1Þ where
Trang 33Pn i¼1i2
Pn i¼1ðn i þ 1Þzi
nðn þ 1Þð2n þ 1Þ=6ffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffi
> asianimppoststrat:=proc(r,x0,sigma,T,t,n,m,npath,K,p,upper) local
R,a1,a2,a3,a4,b3,theta,j,s,f,i2,x,xc,x1,i1,z,h,st,xbar,v,c1,c2,mean,stderr,jj,lambda; global i,X2;
#
# Computes price of an Asian average price call option attime t, with expiry time T, and strike K, using importancesampling combined with post stratification
#
# r=risk-free interest rate
# x0=asset price at time t
# sigma=volatility
# T=exercise time
# T=n*h where the average is taken over times h,2h, ,nh
# m=number of strata
# npath=number of paths in one replication It should be
at least 20*m for post stratification to be efficient
# K=strike price
# p=number of replications
# upper=an upper bound for lambda
#i:=false;
Trang 34for jj from 1 to p do:
theta:=0;
for j from 1 to m do s[j]:=0;f[j]:=0 end do;
for i2 from 1 to npath do;
f[j]:=f[j]+1; # increment frequency, this stratum;
s[j]:=s[j]+R; # increment sum, this stratum;
666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666
Appendices 263