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
-
.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.
-
.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.
-
.option_payoff(option_type, stock_price, strike) ⇒ Float
Calculate option payoff at expiration.
-
.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.
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
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
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
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
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 |