/* 課題　8 解答例 */

/* ［数値処理］→［自動的に数値で出力］*/
if numer#false then numer:false else numer:true;
/*浮動小数点の桁数=7に指定 */
fpprec : 7;
/* 浮動小数点表示桁数を7に指定*/
fpprintprec:7;

/* (I) */
black_scholes_put(S,K,r,sigma,T):=
(
d1 : (log (S/K) + (r+sigma^2/2) *T) / (sigma*sqrt (T)), 
d2 : d1 - sigma*sqrt (T), 
load(distrib), 
P0 : exp (-r*T) *K*cdf_normal (-d2,0,1) - S*cdf_normal(-d1, 0, 1), 
print( " プットオプション価格= "),  P0
);

black_scholes_put (100,110,0.01,0.2,1);

/* (II) */
load(draw);
draw2d(
 yrange=[0, 60], xrange=[45, 200], xtics=50,
 explicit( black_scholes_put(x,110,0.01,0.2,1), x, 50,200), 
 xlabel="S", ylabel="Put Price");

draw2d(
 explicit( black_scholes_put(100,110,x,0.2,1), x, 0,0.5),
 xlabel="r", ylabel="Put Price");

draw2d(
 ytics=5, yrange=[5,26],
 explicit( black_scholes_put(100,110,0.01,x,1), x, 0,0.5),
 xlabel="sigma", ylabel="Put Price");

draw2d(
  yrange=[9.8, 16.2], 
 explicit( black_scholes_put(100,110,0.01,0.2,x), x, 0,2), 
 xlabel="T", ylabel="Put Price");

/* (III) */ 
stradle_price(S0,K,r,sigma,T):=
(
d1 : (log (S0/K) + (r+sigma^2/2) *T) / (sigma*sqrt (T)), 
d2 : d1 - sigma*sqrt (T), 
load(distrib),
C0 : S0*cdf_normal (d1,0,1) - exp (-r*T) *K*cdf_normal (d2,0,1), 
P0 : C0 - S0 + exp (-r*T) *K, 
C0+P0
);

payoff(ST,K):=
(
if(ST < K) then K-ST
else ST-K
);

ST :[0,100,200] /* 満期原資産価格 */;
price : stradle_price(100,100,0.01,0.2,1);

/* 損益計算 */
Profit_Loss : makelist( payoff(ST[i],100)- price , i, 1, 3 );

/* 損益グラフ描画 */
/* 描画用 (ST, Profit_Loss)データ */
ST_PL: makelist([ST[i], Profit_Loss[i]], i, 1, 3);
load(draw);
draw2d(
xaxis=true,
points_joined=true,
points( ST_PL),
xlabel="ST", ylabel="Profit/Loss" );