Class: Finrb::Rate
Overview
the Rate class provides an interface for working with interest rates.
Constant Summary collapse
- TYPES =
Accepted rate types
{ apr: 'effective', apy: 'effective', effective: 'effective', nominal: 'nominal' }.freeze
Instance Attribute Summary collapse
-
#duration ⇒ Integer
The duration for which the rate is valid, in months.
-
#effective ⇒ Flt::DecNum
readonly
The effective interest rate.
-
#nominal ⇒ Flt::DecNum
readonly
The nominal interest rate.
Class Method Summary collapse
-
.to_effective(rate, periods) ⇒ Flt::DecNum
convert a nominal interest rate to an effective interest rate.
-
.to_nominal(rate, periods) ⇒ Flt::DecNum
convert an effective interest rate to a nominal interest rate.
Instance Method Summary collapse
-
#<=>(other) ⇒ Numeric
compare two Rates, using the effective rate.
-
#apr ⇒ Flt::DecNum
The effective interest rate.
-
#apy ⇒ Flt::DecNum
The effective interest rate.
-
#initialize(rate, type, opts = {}) ⇒ Rate
constructor
create a new Rate instance.
- #inspect ⇒ Object
-
#monthly ⇒ Flt::DecNum
The monthly effective interest rate.
Constructor Details
#initialize(rate, type, opts = {}) ⇒ Rate
create a new Rate instance
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/finrb/rates.rb', line 64 def initialize(rate, type, opts = {}) # Default monthly compounding. opts = { compounds: :monthly }.merge(opts) # Set optional attributes.. opts.each do |key, value| __send__("#{key}=", value) end # Set the rate in the proper way, based on the value of type. begin __send__("#{TYPES.fetch(type)}=", Flt::DecNum.new(rate.to_s)) rescue KeyError raise(ArgumentError, "type must be one of #{TYPES.keys.join(', ')}", caller) end end |
Instance Attribute Details
#duration ⇒ Integer
Returns the duration for which the rate is valid, in months.
83 84 85 |
# File 'lib/finrb/rates.rb', line 83 def duration @duration end |
#effective ⇒ Flt::DecNum
Returns the effective interest rate.
86 87 88 |
# File 'lib/finrb/rates.rb', line 86 def effective @effective end |
#nominal ⇒ Flt::DecNum
Returns the nominal interest rate.
89 90 91 |
# File 'lib/finrb/rates.rb', line 89 def nominal @nominal end |
Class Method Details
.to_effective(rate, periods) ⇒ Flt::DecNum
convert a nominal interest rate to an effective interest rate
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/finrb/rates.rb', line 22 def self.to_effective(rate, periods) rate = Flt::DecNum.new(rate.to_s) periods = Flt::DecNum.new(periods.to_s) if periods.infinite? rate.exp - 1 else (((rate / periods) + 1)**periods) - 1 end end |
.to_nominal(rate, periods) ⇒ Flt::DecNum
convert an effective interest rate to a nominal interest rate
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/finrb/rates.rb', line 41 def self.to_nominal(rate, periods) rate = Flt::DecNum.new(rate.to_s) periods = Flt::DecNum.new(periods.to_s) if periods.infinite? (rate + 1).log else periods * (((rate + 1)**(1.to_f / periods)) - 1) end end |
Instance Method Details
#<=>(other) ⇒ Numeric
compare two Rates, using the effective rate
99 100 101 |
# File 'lib/finrb/rates.rb', line 99 def <=>(other) @effective <=> other.effective end |
#apr ⇒ Flt::DecNum
Returns the effective interest rate.
105 106 107 |
# File 'lib/finrb/rates.rb', line 105 def apr effective end |
#apy ⇒ Flt::DecNum
Returns the effective interest rate.
111 112 113 |
# File 'lib/finrb/rates.rb', line 111 def apy effective end |
#inspect ⇒ Object
143 144 145 |
# File 'lib/finrb/rates.rb', line 143 def inspect "Rate.new(#{apr.round(6)}, :apr)" end |
#monthly ⇒ Flt::DecNum
Returns the monthly effective interest rate.
153 154 155 |
# File 'lib/finrb/rates.rb', line 153 def monthly (effective / 12).round(15) end |