Class: OptionLab::Models::BinomialModelInputs

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/option_lab/models.rb

Overview

Binomial Tree model inputs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BinomialModelInputs

Returns a new instance of BinomialModelInputs.



626
627
628
629
630
631
632
633
634
# File 'lib/option_lab/models.rb', line 626

def initialize(attributes = {})
  # Set defaults
  @option_type = 'call'
  @steps = 100
  @is_american = true
  @dividend_yield = 0.0

  super(attributes)
end

Instance Attribute Details

#dividend_yieldObject

Returns the value of attribute dividend_yield.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def dividend_yield
  @dividend_yield
end

#interest_rateObject

Returns the value of attribute interest_rate.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def interest_rate
  @interest_rate
end

#is_americanObject

Returns the value of attribute is_american.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def is_american
  @is_american
end

#option_typeObject

Returns the value of attribute option_type.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def option_type
  @option_type
end

#stepsObject

Returns the value of attribute steps.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def steps
  @steps
end

#stock_priceObject

Returns the value of attribute stock_price.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def stock_price
  @stock_price
end

#strikeObject

Returns the value of attribute strike.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def strike
  @strike
end

#volatilityObject

Returns the value of attribute volatility.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def volatility
  @volatility
end

#years_to_maturityObject

Returns the value of attribute years_to_maturity.



616
617
618
# File 'lib/option_lab/models.rb', line 616

def years_to_maturity
  @years_to_maturity
end

Instance Method Details

#greeksObject



662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/option_lab/models.rb', line 662

def greeks
  OptionLab.get_binomial_greeks(
    option_type,
    stock_price,
    strike,
    interest_rate,
    volatility,
    years_to_maturity,
    steps,
    is_american,
    dividend_yield,
  )
end

#priceObject



648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/option_lab/models.rb', line 648

def price
  OptionLab.price_binomial(
    option_type,
    stock_price,
    strike,
    interest_rate,
    volatility,
    years_to_maturity,
    steps,
    is_american,
    dividend_yield,
  )
end

#treeObject



676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/option_lab/models.rb', line 676

def tree
  OptionLab.get_binomial_tree(
    option_type,
    stock_price,
    strike,
    interest_rate,
    volatility,
    years_to_maturity,
    [steps, 15].min,
    is_american,
    dividend_yield,
  )
end

#validate!Object

Raises:

  • (ArgumentError)


636
637
638
639
640
641
642
643
644
645
646
# File 'lib/option_lab/models.rb', line 636

def validate!
  raise ArgumentError, "option_type must be 'call' or 'put'" unless OPTION_TYPES.include?(option_type)
  raise ArgumentError, 'stock_price must be positive' unless stock_price.is_a?(Numeric) && stock_price.positive?
  raise ArgumentError, 'strike must be positive' unless strike.is_a?(Numeric) && strike.positive?
  raise ArgumentError, 'interest_rate must be non-negative' unless interest_rate.is_a?(Numeric) && interest_rate >= 0
  raise ArgumentError, 'volatility must be positive' unless volatility.is_a?(Numeric) && volatility.positive?
  raise ArgumentError, 'years_to_maturity must be non-negative' unless years_to_maturity.is_a?(Numeric) && years_to_maturity >= 0
  raise ArgumentError, 'steps must be positive' unless steps.is_a?(Integer) && steps.positive?
  raise ArgumentError, 'is_american must be boolean' unless is_american.is_a?(TrueClass) || is_american.is_a?(FalseClass)
  raise ArgumentError, 'dividend_yield must be non-negative' unless dividend_yield.is_a?(Numeric) && dividend_yield >= 0
end