Module: OptionLab::BinomialTree

Defined in:
lib/option_lab/binomial_tree.rb

Overview

Implementation of the Cox-Ross-Rubinstein (CRR) binomial tree model for American and European options pricing

Class Method Summary collapse

Class Method Details

.get_greeks(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0) ⇒ Hash

Calculate option Greeks using the CRR model and finite difference methods

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • n_steps (Integer) (defaults to: 100)

    Number of time steps

  • is_american (Boolean) (defaults to: true)

    True for American options, false for European

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Hash)

    Option Greeks (delta, gamma, theta, vega, rho)



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/option_lab/binomial_tree.rb', line 190

def get_greeks(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0)
  # Small increment for finite difference calculation
  h_s = s0 * 0.001    # For Delta and Gamma
  h_t = 1.0 / 365     # For Theta (1 day)
  h_v = 0.001         # For Vega
  h_r = 0.0001        # For Rho

  # Base price
  price = price_option(option_type, s0, x, r, volatility, years_to_maturity, n_steps, is_american, dividend_yield)

  # Delta: ∂V/∂S
  price_up = price_option(option_type, s0 + h_s, x, r, volatility, years_to_maturity, n_steps, is_american, dividend_yield)
  price_down = price_option(option_type, s0 - h_s, x, r, volatility, years_to_maturity, n_steps, is_american, dividend_yield)
  delta = (price_up - price_down) / (2 * h_s)

  # Gamma: ∂²V/∂S²
  gamma = (price_up - 2 * price + price_down) / (h_s * h_s)

  # Theta: -∂V/∂t
  price_t_down = if years_to_maturity - h_t > 0
    price_option(option_type, s0, x, r, volatility, years_to_maturity - h_t, n_steps, is_american, dividend_yield)
  else
    option_payoff(option_type, s0, x)
  end
  theta = -(price_t_down - price) / h_t

  # Vega: ∂V/∂σ
  price_v_up = price_option(option_type, s0, x, r, volatility + h_v, years_to_maturity, n_steps, is_american, dividend_yield)
  vega = (price_v_up - price) / h_v

  # Rho: ∂V/∂r
  price_r_up = price_option(option_type, s0, x, r + h_r, volatility, years_to_maturity, n_steps, is_american, dividend_yield)
  rho = (price_r_up - price) / h_r

  # Return Greeks
  {
    delta: delta,
    gamma: gamma,
    theta: theta,
    vega: vega,
    rho: rho,
  }
end

.get_tree(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 15, is_american = true, dividend_yield = 0.0) ⇒ Hash

Get a full binomial tree as a structured output

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • n_steps (Integer) (defaults to: 15)

    Number of time steps

  • is_american (Boolean) (defaults to: true)

    True for American options, false for European

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Hash)

    Tree structure with stock prices and option values



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/option_lab/binomial_tree.rb', line 102

def get_tree(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 15, is_american = true, dividend_yield = 0.0)
  # Calculate time step
  dt = years_to_maturity / n_steps.to_f

  # Calculate up and down factors
  u = Math.exp(volatility * Math.sqrt(dt))
  d = 1.0 / u

  # Calculate risk-neutral probability
  effective_r = r - dividend_yield
  p = (Math.exp(effective_r * dt) - d) / (u - d)

  # Calculate discount factor
  discount = Math.exp(-r * dt)

  # Initialize price tree
  stock_prices = Array.new(n_steps + 1) { Array.new(n_steps + 1, 0.0) }
  option_values = Array.new(n_steps + 1) { Array.new(n_steps + 1, 0.0) }
  exercise_flags = Array.new(n_steps + 1) { Array.new(n_steps + 1, false) }

  # Fill stock price tree
  (0..n_steps).each do |i|
    (0..i).each do |j|
      stock_prices[i][j] = s0 * (u**(i - j)) * (d**j)
    end
  end

  # Calculate option values at expiration (i = n_steps)
  (0..n_steps).each do |j|
    option_values[n_steps][j] = option_payoff(option_type, stock_prices[n_steps][j], x)
  end

  # Work backwards through the tree
  (n_steps - 1).downto(0) do |i|
    (0..i).each do |j|
      # Expected value (European option value)
      expected_value = discount * (p * option_values[i + 1][j] + (1 - p) * option_values[i + 1][j + 1])

      if is_american
        # For American options, compare with immediate exercise value
        exercise_value = option_payoff(option_type, stock_prices[i][j], x)

        if exercise_value > expected_value
          option_values[i][j] = exercise_value
          exercise_flags[i][j] = true
        else
          option_values[i][j] = expected_value
        end
      else
        # For European options, use expected value
        option_values[i][j] = expected_value
      end
    end
  end

  # Return tree structure
  {
    stock_prices: stock_prices,
    option_values: option_values,
    exercise_flags: exercise_flags,
    parameters: {
      option_type: option_type,
      spot_price: s0,
      strike_price: x,
      risk_free_rate: r,
      volatility: volatility,
      time_to_maturity: years_to_maturity,
      steps: n_steps,
      is_american: is_american,
      dividend_yield: dividend_yield,
      up_factor: u,
      down_factor: d,
      risk_neutral_probability: p,
    },
  }
end

.option_payoff(option_type, stock_price, strike) ⇒ Float

Calculate option payoff at expiration

Parameters:

  • option_type (String)

    'call' or 'put'

  • stock_price (Float)

    Stock price

  • strike (Float)

    Strike price

Returns:

  • (Float)

    Option payoff



81
82
83
84
85
86
87
88
89
# File 'lib/option_lab/binomial_tree.rb', line 81

def option_payoff(option_type, stock_price, strike)
  if option_type == 'call'
    [stock_price - strike, 0.0].max
  elsif option_type == 'put'
    [strike - stock_price, 0.0].max
  else
    raise ArgumentError, "Option type must be either 'call' or 'put'!"
  end
end

.price_option(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0) ⇒ Float

Price an option using the Cox-Ross-Rubinstein binomial tree model

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • n_steps (Integer) (defaults to: 100)

    Number of time steps

  • is_american (Boolean) (defaults to: true)

    True for American options, false for European

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Float)

    Option price



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/option_lab/binomial_tree.rb', line 24

def price_option(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0)
  # Calculate time step
  dt = years_to_maturity / n_steps.to_f

  # Calculate up and down factors
  u = Math.exp(volatility * Math.sqrt(dt))
  d = 1.0 / u

  # Calculate risk-neutral probability
  effective_r = r - dividend_yield
  p = (Math.exp(effective_r * dt) - d) / (u - d)

  # Calculate discount factor
  discount = Math.exp(-r * dt)

  # Initialize price tree
  stock_prices = Array.new(n_steps + 1) { Array.new(n_steps + 1, 0.0) }
  option_values = Array.new(n_steps + 1) { Array.new(n_steps + 1, 0.0) }

  # Fill stock price tree
  (0..n_steps).each do |i|
    (0..i).each do |j|
      stock_prices[i][j] = s0 * (u**(i - j)) * (d**j)
    end
  end

  # Calculate option values at expiration (i = n_steps)
  (0..n_steps).each do |j|
    option_values[n_steps][j] = option_payoff(option_type, stock_prices[n_steps][j], x)
  end

  # Work backwards through the tree
  (n_steps - 1).downto(0) do |i|
    (0..i).each do |j|
      # Expected value (European option value)
      expected_value = discount * (p * option_values[i + 1][j] + (1 - p) * option_values[i + 1][j + 1])

      if is_american
        # For American options, compare with immediate exercise value
        exercise_value = option_payoff(option_type, stock_prices[i][j], x)
        option_values[i][j] = [expected_value, exercise_value].max
      else
        # For European options, use expected value
        option_values[i][j] = expected_value
      end
    end
  end

  # Root node contains the option price
  option_values[0][0]
end