Class: Option::Calculator

Inherits:
Object
  • Object
show all
Extended by:
Math
Defined in:
lib/options_library/option_calculator.rb

Class Method Summary collapse

Class Method Details

.d_one(underlying, strike, time, interest, sigma, dividend) ⇒ Object

probability of being exercised at maturity (must be greater than d2 by (sigma*sqrt(time)) if exercised)



93
94
95
96
97
# File 'lib/options_library/option_calculator.rb', line 93

def d_one( underlying, strike, time, interest, sigma, dividend )
  numerator = ( log(underlying / strike) + (interest - dividend + 0.5 * sigma ** 2.0 ) * time)
  denominator = ( sigma * sqrt(time) )
  numerator / denominator
end

.d_two(underlying, strike, time, interest, sigma, dividend) ⇒ Object

probability of underlying reaching the strike price (must be smaller than d1 by (sigma*sqrt(time)) if exercised.



100
101
102
# File 'lib/options_library/option_calculator.rb', line 100

def d_two( underlying, strike, time, interest, sigma, dividend ) 
  d_one( underlying, strike, time, interest, sigma, dividend ) - ( sigma * sqrt(time) )
end

.delta_call(underlying, strike, time, interest, sigma, dividend) ⇒ Object

computes the call price sensitivity to a change in underlying price



17
18
19
# File 'lib/options_library/option_calculator.rb', line 17

def delta_call( underlying, strike, time, interest, sigma, dividend )
  norm_sdist( d_one( underlying, strike, time, interest, sigma, dividend ) )
end

.delta_put(underlying, strike, time, interest, sigma, dividend) ⇒ Object

computes the put price sensitivity to a change in underlying price



22
23
24
# File 'lib/options_library/option_calculator.rb', line 22

def delta_put( underlying, strike, time, interest, sigma, dividend )
  delta_call( underlying, strike, time, interest, sigma, dividend ) - 1
end

.gamma(underlying, strike, time, interest, sigma, dividend) ⇒ Object

computes the option price sensitivity to a change in delta



27
28
29
# File 'lib/options_library/option_calculator.rb', line 27

def gamma( underlying, strike, time, interest, sigma, dividend )
  phi( d_one( underlying, strike, time, interest, sigma, dividend ) ) / ( underlying * sigma * sqrt(time) )
end

.implied_vol_call(underlying, strike, time, interest, target_price, dividend) ⇒ Object

finds the implied volatility based on the target_price passed in.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/options_library/option_calculator.rb', line 63

def implied_vol_call( underlying, strike, time, interest, target_price, dividend )
  low, high = LOW_VOL, HIGH_VOL
		
  while( high - low > VOL_TOLERANCE )
    if( price_call( underlying, strike, time, interest, (high+low)/2.0, dividend ) > target_price )
      high = (high + low) / 2.0
    else
      low = (high + low) / 2.0
    end
  end
		
  (high + low) / 2.0
end

.implied_vol_put(underlying, strike, time, interest, target_price, dividend) ⇒ Object

finds the implied volatility based on the target_price passed in.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/options_library/option_calculator.rb', line 78

def implied_vol_put( underlying, strike, time, interest, target_price, dividend )
  low, high = LOW_VOL, HIGH_VOL
		
		while( high - low > VOL_TOLERANCE )
    if( price_put( underlying, strike, time, interest, (high+low)/2.0, dividend ) > target_price )
      high = (high + low) / 2.0
    else
      low = (high + low) / 2.0
    end
  end
		
  (high + low) / 2.0
end

.norm_sdist(z) ⇒ Object

Normal Standard Distribution using Taylor’s approximation



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/options_library/option_calculator.rb', line 106

def norm_sdist( z )
  return 0.0 if z < MIN_Z_SCORE
  return 1.0 if z > MAX_Z_SCORE
		
  i, sum, term = 3.0, 0.0, z
		
  while( sum + term != sum )
    sum = sum + term
    term = term * z * z / i
		  i += 2.0
  end
		
  0.5 + sum * phi(z)
end

.phi(x) ⇒ Object

Standard Gaussian pdf



122
123
124
125
126
# File 'lib/options_library/option_calculator.rb', line 122

def phi(x)
  numerator = exp(-1.0 * x*x / 2.0)
  denominator = sqrt(2.0 * PI)
  numerator / denominator
end

.price_call(underlying, strike, time, interest, sigma, dividend) ⇒ Object

computes the fair value of the call based on the knowns and assumed volatility (sigma)



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/options_library/option_calculator.rb', line 37

def price_call( underlying, strike, time, interest, sigma, dividend )
  d1 = d_one( underlying, strike, time, interest, sigma, dividend )
  discounted_underlying = exp(-1.0 * dividend * time) * underlying
  probability_weighted_value_of_being_exercised = discounted_underlying * norm_sdist( d1 )

  d2 = d1 - ( sigma * sqrt(time) )
  discounted_strike = exp(-1.0 * interest * time) * strike
  probability_weighted_value_of_discounted_strike = discounted_strike * norm_sdist( d2 )
		
  expected_value = probability_weighted_value_of_being_exercised - probability_weighted_value_of_discounted_strike
end

.price_put(underlying, strike, time, interest, sigma, dividend) ⇒ Object

computes the fair value of the put based on the knowns and assumed volatility (sigma)



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/options_library/option_calculator.rb', line 50

def price_put( underlying, strike, time, interest, sigma, dividend )
  d2 = d_two( underlying, strike, time, interest, sigma, dividend )
  discounted_strike = strike * exp(-1.0 * interest * time)
  probabiltity_weighted_value_of_discounted_strike = discounted_strike * norm_sdist( -1.0 * d2 )
		
  d1 = d2 + ( sigma * sqrt(time) )
  discounted_underlying = underlying * exp(-1.0 * dividend * time)
  probability_weighted_value_of_being_exercised = discounted_underlying * norm_sdist( -1.0 * d1 )
		
  expected_value = probabiltity_weighted_value_of_discounted_strike - probability_weighted_value_of_being_exercised
end

.vega(underlying, strike, time, interest, sigma, dividend) ⇒ Object

computes the option price sensitivity to a change in volatility



32
33
34
# File 'lib/options_library/option_calculator.rb', line 32

def vega( underlying, strike, time, interest, sigma, dividend )
  0.01 * underlying * sqrt(time) * phi(d_one(underlying, strike, time, interest, sigma, dividend))
end