Skip to content

Instantly share code, notes, and snippets.

@gouf
Last active September 22, 2024 07:23
Show Gist options
  • Select an option

  • Save gouf/92b734071922c00bbd0ed0ada5323a54 to your computer and use it in GitHub Desktop.

Select an option

Save gouf/92b734071922c00bbd0ed0ada5323a54 to your computer and use it in GitHub Desktop.
Ruby で実行できる「ブラックショールズ方程式」について
# frozen_string_literal: true
#
# ブラックショールズ方程式を用いたオプションの価格計算
#
# 標準正規分布の累積分布関数を計算する関数
def cumulative_normal_distribution(x)
(1.0 + Math.erf(x / Math.sqrt(2.0))) / 2.0
end
# ブラックショールズ方程式を用いたコールオプションの価格計算
# 買値が判る
def black_scholes_call(s, x, t, r, sigma)
d1 = (Math.log(s / x) + (r + sigma**2 / 2.0) * t) / (sigma * Math.sqrt(t))
d2 = d1 - sigma * Math.sqrt(t)
# 標準正規分布を使って計算
# call price
s * cumulative_normal_distribution(d1) - x * Math.exp(-r * t) * cumulative_normal_distribution(d2)
end
# ブラックショールズ方程式を用いたプットオプションの価格計算
# 売値が判る
def black_scholes_put(s, x, t, r, sigma)
d1 = (Math.log(s / x) + (r + sigma**2 / 2.0) * t) / (sigma * Math.sqrt(t))
d2 = d1 - sigma * Math.sqrt(t)
# 標準正規分布を使って計算
# put price
x * Math.exp(-r * t) * cumulative_normal_distribution(-d2) - s * cumulative_normal_distribution(-d1)
end
# 使用例 (買値)
s = 100.0 # 現在の株価
x = 100.0 # 行使価格
t = 1.0 # 満期までの時間(1年)
r = 0.05 # リスクフリーレート(5%)
sigma = 0.2 # ボラティリティ(20%)
call_price = black_scholes_call(s, x, t, r, sigma)
puts "コールオプション価格: #{call_price.round(3)}" # => コールオプション価格: 10.451
# 使用例 (売値)
s = 100.0 # 現在の株価
x = 100.0 # 行使価格
t = 1.0 # 満期までの時間(1年)
r = 0.05 # リスクフリーレート(5%)
sigma = 0.2 # ボラティリティ(20%)
put_price = black_scholes_put(s, x, t, r, sigma)
puts "プットオプション価格: #{put_price.round(3)}" # => プットオプション価格: 5.574
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment