LoginSignup
1
1

More than 5 years have passed since last update.

【初心者】Chainer でオプション価格とGreeks計算(3月限C21000)@ブラウザ

Last updated at Posted at 2019-02-09

Chainerで微分方程式を解き、オプションのGreeksを求めている記事 を参考に、実際の市場データを入れてブラウザ上で試してみました。

ブラウザ上で試したノート(colab)

利用した実際の市場データ

 ⇒03/C21000@100 (2月9日先物価格20290)

出力結果:

C21000 = 99.65
delta = 0.21

コメント:

結果は(当然ながら)ほぼ一致。
ブラウザ開いて5分で試せるGoogle Colab は素晴らしい。

ソースコード


import numpy as np
import chainer
from chainer import Variable
import chainer.functions as F

stock_price = 20290.0
strike = 21000.0
years = 1.0/12
risk_free_rate = 0.01
volatility = 0.143
n_paths = 100000
n_steps = 100

times = np.linspace(0, years, n_steps, dtype=np.float64)

r = risk_free_rate
v = volatility
k = strike
T = years
initial_stocks = Variable(stock_price * np.ones((n_paths,), dtype=np.float64))
currents = initial_stocks
for c, n in zip(times[:-1], times[1:]):
  dt = n - c
  zs = np.random.normal(0.0, 1.0, currents.shape)
  currents = currents * F.broadcast(np.exp((r - 0.5 * v * v) * dt + v * dt ** 0.5 * zs))

#call_price = F.average(exp(-r * T) * F.softplus(currents - k, beta=10)) # you should smooth the relu to calculate gamma
call_price = F.average(np.exp(-r * T) * F.relu(currents - k))  
deltas = F.sum(chainer.grad([call_price], [initial_stocks], enable_double_backprop=True)[0])

print(f'C21000 = {call_price.data:.2f}')
print(f'delta = {deltas.data:.2f}')

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1