class BondCalculator::SpreadToBenchmark

Class that handle the calculation of the spread to benchmark Calculate the yield spread (return) between a corporate bond and its government bond benchmark.

A government bond is a good benchmark if it is as close as possible to the corporate bond in terms of years to maturity (term).

@author Lucas Sant' Anna @since 0.0.1

Attributes

bonds[R]

Public Class Methods

new(bond_file_path) click to toggle source

@param [File] bond_file_path Csv benchmark file path

@return [<void>]

# File lib/bond_calculator/spread_to_benchmark.rb, line 21
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 Benchmark @return [Hash] :bond_name @return [Hash] :benchmark_name @return [Hash] :spread_to_benchmark

# File lib/bond_calculator/spread_to_benchmark.rb, line 29
def calculate
  response = []
  corps = bonds_by_type('corporate', @bonds)
  govs = bonds_by_type('government', @bonds)

  corps.each do |corp|
    benchmark = nil
    govs.each do |gov|
      if benchmark.nil?
        benchmark = best_benchmark(corp, gov)
        next
      end
      if benchmark[:term_spread] > term_spread(corp, gov)
        benchmark.merge(best_benchmark(corp, gov))
      end
    end

    response << { bond_name: corp.name,
                  benchmark_name: benchmark[:candidate].name,
                  spread_to_benchmark: spread_to_benchmark(benchmark, corp) }
  end
  print(response)

  response
end

Private Instance Methods

best_benchmark(corporate, government) click to toggle source
# File lib/bond_calculator/spread_to_benchmark.rb, line 57
def best_benchmark(corporate, government)
  { candidate: government,
    term_spread: term_spread(corporate, government) }
end
print(response) click to toggle source
spread_to_benchmark(benchmark, corporate) click to toggle source
# File lib/bond_calculator/spread_to_benchmark.rb, line 62
def spread_to_benchmark(benchmark, corporate)
  (benchmark[:candidate].yield_percent - corporate.yield_percent).round(2).abs
end
term_spread(bond1, bond2) click to toggle source
# File lib/bond_calculator/spread_to_benchmark.rb, line 73
def term_spread(bond1, bond2)
  (bond1.term_years - bond2.term_years).abs
end