Class: Valuation::InterestRate
- Inherits:
-
Object
- Object
- Valuation::InterestRate
- Defined in:
- lib/valuation/interest_rate.rb
Constant Summary collapse
- @@periods =
{:yearly => 1.0, :semiannually => (1/2.0), :bi_annually => (1/2.0), :quarterly => (1/4.0), :monthly => (1/12.0), :weekly => (1/52.0), :daily => (1/365.0)}
Instance Attribute Summary collapse
-
#rate ⇒ Object
Returns the value of attribute rate.
Class Method Summary collapse
-
.add_time_periods(periods) ⇒ Object
Add a unique time period.
Instance Method Summary collapse
-
#continuous(t) ⇒ Object
The continous interest rate for a given time period t.
-
#initialize(rate, type = :yearly, options = {}) ⇒ InterestRate
constructor
All interest rates are converted to EAR rates.
Constructor Details
#initialize(rate, type = :yearly, options = {}) ⇒ InterestRate
All interest rates are converted to EAR rates.
So the base time period is one year.
10% EAR rate: InterestRate.new(0.10)
5% EMR rate: InterestRate.new(0.05, :monthly)
12% APR compounded monthly InterestRate.new(0.12, :apr, :compounded => :monthly)
Custom Period 5% Compounded every third of the year InterestRate.new(0.05, 1/3.0)
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/valuation/interest_rate.rb', line 49 def initialize(rate, type=:yearly, = {}) if type.class.to_s == 'Float' @rate = convert_period(1/type, rate) elsif type.to_sym == :apr = {:compounded => :yearly}.merge() factor = @@periods[[:compounded]] @rate = (1 + (rate*factor.to_f))**(1/factor.to_f) - 1 else @rate = convert_period(1/@@periods[type], rate) end end |
Instance Attribute Details
#rate ⇒ Object
Returns the value of attribute rate.
5 6 7 |
# File 'lib/valuation/interest_rate.rb', line 5 def rate @rate end |
Class Method Details
.add_time_periods(periods) ⇒ Object
Add a unique time period. If you need different time periods that are not provided by default
All time periods are measured to a base time period of 1 year so to add an hourly time period
InterestRate.add_time_periods(:hourly => (1/(365*24))) This time period will now be available, in the init method. It will also add a instance method hourly
30 31 32 33 34 35 36 37 |
# File 'lib/valuation/interest_rate.rb', line 30 def self.add_time_periods(periods) @@periods.merge(periods) periods.each_pair do |period, n| define_method "#{period}" do convert_period n end end end |
Instance Method Details
#continuous(t) ⇒ Object
The continous interest rate for a given time period t
62 63 64 |
# File 'lib/valuation/interest_rate.rb', line 62 def continuous(t) E ** (rate * t) - 1 end |