Class: OptionLab::Models::TreeVisualization

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

Overview

Binomial tree visualization data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from OptionLab::Models::BaseModel

Instance Attribute Details

#exercise_flagsObject

Returns the value of attribute exercise_flags.



782
783
784
# File 'lib/option_lab/models.rb', line 782

def exercise_flags
  @exercise_flags
end

#option_valuesObject

Returns the value of attribute option_values.



782
783
784
# File 'lib/option_lab/models.rb', line 782

def option_values
  @option_values
end

#parametersObject

Returns the value of attribute parameters.



782
783
784
# File 'lib/option_lab/models.rb', line 782

def parameters
  @parameters
end

#stock_pricesObject

Returns the value of attribute stock_prices.



782
783
784
# File 'lib/option_lab/models.rb', line 782

def stock_prices
  @stock_prices
end

Instance Method Details

#diagram_dataObject

Get data for rendering a tree diagram



793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/option_lab/models.rb', line 793

def diagram_data
  data = []

  # Process each step in the tree
  stock_prices.size.times do |step|
    step_data = []

    # Process each node in the current step
    (step + 1).times do |node|
      step_data << {
        stock_price: stock_prices[step][node].round(2),
        option_value: option_values[step][node].round(2),
        exercise: exercise_flags[step][node],
      }
    end

    data << step_data
  end

  {
    tree: data,
    parameters: parameters,
  }
end

#get_node(step, node) ⇒ Object



784
785
786
787
788
789
790
# File 'lib/option_lab/models.rb', line 784

def get_node(step, node)
  {
    stock_price: stock_prices[step][node],
    option_value: option_values[step][node],
    exercise: exercise_flags[step][node],
  }
end

#to_csvObject

Export tree data to CSV format



819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/option_lab/models.rb', line 819

def to_csv
  csv = "Step,Node,StockPrice,OptionValue,Exercise\n"

  # Process each step in the tree
  stock_prices.size.times do |step|
    # Process each node in the current step
    (step + 1).times do |node|
      csv += "#{step},#{node},#{stock_prices[step][node]},#{option_values[step][node]},#{exercise_flags[step][node]}\n"
    end
  end

  csv
end