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 ⇒ DecNum
readonly
The effective interest rate.
-
#nominal ⇒ DecNum
readonly
The nominal interest rate.
Class Method Summary collapse
-
.to_effective(rate, periods) ⇒ DecNum
convert a nominal interest rate to an effective interest rate.
-
.to_nominal(rate, periods) ⇒ 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 ⇒ DecNum
The effective interest rate.
-
#apy ⇒ DecNum
The effective interest rate.
-
#initialize(rate, type, opts = {}) ⇒ Rate
constructor
create a new Rate instance.
- #inspect ⇒ Object
-
#monthly ⇒ DecNum
The monthly effective interest rate.
Constructor Details
#initialize(rate, type, opts = {}) ⇒ Rate
create a new Rate instance
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/finrb/rates.rb', line 23 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.
45 46 47 |
# File 'lib/finrb/rates.rb', line 45 def duration @duration end |
#effective ⇒ DecNum
Returns the effective interest rate.
48 49 50 |
# File 'lib/finrb/rates.rb', line 48 def effective @effective end |
#nominal ⇒ DecNum
Returns the nominal interest rate.
51 52 53 |
# File 'lib/finrb/rates.rb', line 51 def nominal @nominal end |
Class Method Details
.to_effective(rate, periods) ⇒ DecNum
convert a nominal interest rate to an effective interest rate
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/finrb/rates.rb', line 135 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 ((1 + (rate / periods))**periods) - 1 end end |
.to_nominal(rate, periods) ⇒ DecNum
convert an effective interest rate to a nominal interest rate
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/finrb/rates.rb', line 154 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 * (((1 + rate)**(1 / periods)) - 1) end end |
Instance Method Details
#<=>(other) ⇒ Numeric
compare two Rates, using the effective rate
61 62 63 |
# File 'lib/finrb/rates.rb', line 61 def <=>(other) @effective <=> other.effective end |
#apr ⇒ DecNum
Returns the effective interest rate.
67 68 69 |
# File 'lib/finrb/rates.rb', line 67 def apr effective end |
#apy ⇒ DecNum
Returns the effective interest rate.
73 74 75 |
# File 'lib/finrb/rates.rb', line 73 def apy effective end |
#inspect ⇒ Object
105 106 107 |
# File 'lib/finrb/rates.rb', line 105 def inspect "Rate.new(#{apr.round(6)}, :apr)" end |
#monthly ⇒ DecNum
Returns the monthly effective interest rate.
115 116 117 |
# File 'lib/finrb/rates.rb', line 115 def monthly (effective / 12).round(15) end |