class BondCalculator::SpreadToCurve
Class that handle the calculation of the spread to curve
Since the corporate bond term is not exactly the same as its benchmark term, we use linear interpolation to calculate the spread to the curve.
Attributes
bonds[R]
Public Class Methods
new(bond_file_path)
click to toggle source
@param [File] bond_file_path Csv curve file path
@return [<void>]
# File lib/bond_calculator/spread_to_curve.rb, line 18 def initialize(bond_file_path) @bonds = csv_to_bonds(bond_file_path) end
Public Instance Methods
calculate()
click to toggle source
Execute the bussines rule to calculate the spread to curve @return [Hash] :bond_name @return [Hash] :spread_to_curve
# File lib/bond_calculator/spread_to_curve.rb, line 25 def calculate response = [] corps = bonds_by_type('corporate', @bonds) govs = bonds_by_type('government', @bonds) corps.each do |corp| upper_bond = nil lower_bond = nil govs.each do |gov| if corp.term_years <= gov.term_years lower_bond = gov break else upper_bond = gov end end spread_to_curve = (corp.yield_percent - yield_on_curve(corp, lower_bond, upper_bond)).round(2) response << { bond_name: corp.name, spread_to_curve: spread_to_curve } end print_response(response) response end
Private Instance Methods
print_response(response)
click to toggle source
# File lib/bond_calculator/spread_to_curve.rb, line 53 def print_response(response) puts 'bond, spread to curve' response.each do |r| puts "#{r[:bond_name]}, #{r[:spread_to_curve]}" end end
yield_on_curve(bond, lower_bond, upper_bond)
click to toggle source
# File lib/bond_calculator/spread_to_curve.rb, line 60 def yield_on_curve(bond, lower_bond, upper_bond) lower_bond.yield_percent + (bond.term_years - lower_bond.term_years) * ((upper_bond.yield_percent - lower_bond.yield_percent) / (upper_bond.term_years - lower_bond.term_years)) end