Appendix B: Software
267
The second part of the script illustrates the analysis of the portfolio with the riskfree asset. First the excess return above the risk-free rate r is calculated and used
in the loop over ρ to find the portfolio vector w from (3.28) and the corresponding
value of the volatility σ . Plotting ρ versus σ now reveals the capital market line,
shown as a red dashed line in Fig. 3.2. Finally the parameters of the market portfolio
are determined from (3.30) and added to the figure.
B.2 Functions for Call and Put Options
The functions black_scholes_call() and the corresponding equation for the
put option are used in the hedging simulation shown in Fig. 5.3 from Sect. 5.4. The
functions receive the current stock price S 0 , the strike price K , the time until maturity
dt, the risk-free rate r f , and the volatility σ as input parameters and they return the
values of the corresponding option, as well as ,, and defined in Sect. 6.1. Both
functions first define N (z) from (4.39) and then calculate d 1 and d 2 from (4.43).
The function for the call option uses (5.17) to determine the option value c
and (5.22) to calculate c . and are calculated by differentiating c and c ,
respectively.
function [c,delta,gamma,theta] ...
=black_scholes_call(S0,K,dt,rf,sig)
N=@(z)0.5*erfc(-z/sqrt(2));
d1=(log(S0./K)+(rf+0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
d2=(log(S0./K)+(rf-0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
c=S0.*N(d1)-K.*exp(-rf.*dt).*N(d2);
delta=N(d1);
gamma=exp(-0.5*d1.ˆ2)./(S0.*sqrt(2*pi*sig.ˆ2.*dt));
theta=-0.5*(sig.ˆ2).*(S0.ˆ2).*gamma ...
-rf.*K.*exp(-rf.*dt).*N(d2);
The function for the put option works in much the same way, only (4.42) with ρ
replaced by r f is used. Again, p is taken from (5.22).
function [p,delta]=black_scholes_put(S0,K,dt,rf,sig)
N=@(z)0.5*erfc(-z/sqrt(2));
d1=(log(S0./K)+(rf+0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
d2=(log(S0./K)+(rf-0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
p=K.*exp(-rf.*dt).*N(-d2)-S0.*N(-d1);
delta=N(d1)-1;
Calculating and is left as an exercise.
267
The second part of the script illustrates the analysis of the portfolio with the riskfree asset. First the excess return above the risk-free rate r is calculated and used
in the loop over ρ to find the portfolio vector w from (3.28) and the corresponding
value of the volatility σ . Plotting ρ versus σ now reveals the capital market line,
shown as a red dashed line in Fig. 3.2. Finally the parameters of the market portfolio
are determined from (3.30) and added to the figure.
B.2 Functions for Call and Put Options
The functions black_scholes_call() and the corresponding equation for the
put option are used in the hedging simulation shown in Fig. 5.3 from Sect. 5.4. The
functions receive the current stock price S 0 , the strike price K , the time until maturity
dt, the risk-free rate r f , and the volatility σ as input parameters and they return the
values of the corresponding option, as well as ,, and defined in Sect. 6.1. Both
functions first define N (z) from (4.39) and then calculate d 1 and d 2 from (4.43).
The function for the call option uses (5.17) to determine the option value c
and (5.22) to calculate c . and are calculated by differentiating c and c ,
respectively.
function [c,delta,gamma,theta] ...
=black_scholes_call(S0,K,dt,rf,sig)
N=@(z)0.5*erfc(-z/sqrt(2));
d1=(log(S0./K)+(rf+0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
d2=(log(S0./K)+(rf-0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
c=S0.*N(d1)-K.*exp(-rf.*dt).*N(d2);
delta=N(d1);
gamma=exp(-0.5*d1.ˆ2)./(S0.*sqrt(2*pi*sig.ˆ2.*dt));
theta=-0.5*(sig.ˆ2).*(S0.ˆ2).*gamma ...
-rf.*K.*exp(-rf.*dt).*N(d2);
The function for the put option works in much the same way, only (4.42) with ρ
replaced by r f is used. Again, p is taken from (5.22).
function [p,delta]=black_scholes_put(S0,K,dt,rf,sig)
N=@(z)0.5*erfc(-z/sqrt(2));
d1=(log(S0./K)+(rf+0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
d2=(log(S0./K)+(rf-0.5.*sig.ˆ2).*dt)./(sig.*sqrt(dt));
p=K.*exp(-rf.*dt).*N(-d2)-S0.*N(-d1);
delta=N(d1)-1;
Calculating and is left as an exercise.
