Module: Xirr::Base
- Included in:
- Bisection, Brent, NewtonMethod, RtSafe, RtSafeC
- Defined in:
- lib/xirr/base.rb
Overview
Shared numerics for the solver classes: the net present value of a cashflow, its derivative, and the discounting timeline. Each solver includes this and is constructed with the Cashflow it works on.
Instance Attribute Summary collapse
-
#cf ⇒ Object
readonly
Returns the value of attribute cf.
Instance Method Summary collapse
- #initialize(cf) ⇒ Object
-
#periods_from_start(date) ⇒ Float
Periods (years, with the default period) from the first transaction to
date. -
#xnpv(rate) ⇒ Float
Net present value of the cashflow at
rate: Σ amount / (1 + rate)^t. -
#xnpv_derivative(rate) ⇒ Float
Derivative of #xnpv with respect to rate: Σ -t · amount / (1 + rate)^(t+1).
Instance Attribute Details
#cf ⇒ Object (readonly)
Returns the value of attribute cf.
8 9 10 |
# File 'lib/xirr/base.rb', line 8 def cf @cf end |
Instance Method Details
#initialize(cf) ⇒ Object
11 12 13 |
# File 'lib/xirr/base.rb', line 11 def initialize(cf) @cf = cf end |
#periods_from_start(date) ⇒ Float
Periods (years, with the default period) from the first transaction to date.
18 19 20 |
# File 'lib/xirr/base.rb', line 18 def periods_from_start(date) (date - cf.min_date) / cf.period end |
#xnpv(rate) ⇒ Float
Net present value of the cashflow at rate: Σ amount / (1 + rate)^t.
25 26 27 28 |
# File 'lib/xirr/base.rb', line 25 def xnpv(rate) r = rate.to_f flows.inject(0.0) { |sum, (t, amount)| sum + amount / (1 + r)**t } end |
#xnpv_derivative(rate) ⇒ Float
Derivative of #xnpv with respect to rate: Σ -t · amount / (1 + rate)^(t+1).
33 34 35 36 |
# File 'lib/xirr/base.rb', line 33 def xnpv_derivative(rate) r = rate.to_f flows.inject(0.0) { |sum, (t, amount)| sum + (-t * amount / (1 + r)**(t + 1)) } end |