Module: Finfast

Defined in:
lib/finfast.rb,
lib/finfast/version.rb,
ext/finfast/finfast.c

Constant Summary collapse

DAYS_PER_YEAR =
365.0
VERSION_MAJOR =
0
VERSION_MINOR =
0
VERSION_PATCH =
4
VERSION =
[VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH].join('.')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fv(i, n, amt) ⇒ Object

Future value of a lump amount.

Params:

i

The interest rate per period

n

The number of periods

amt

The amount in today’s terms (present value)



42
43
44
# File 'lib/finfast.rb', line 42

def fv(i, n, amt)
  return  ((1 + i) ** n) * amt
end

.fvan(i, n, pmt) ⇒ Object

Future value of an annuity.

Params:

i

The interest rate per period

n

The number of periods

pmt

The payment made in each period



31
32
33
# File 'lib/finfast.rb', line 31

def fvan(i, n, pmt)
  return pmt * (((1 + i) ** n) - 1) / i 
end

.ipmt(i, per, n, pv) ⇒ Object

The interest payment for a given period in an investment.

Params:

i

The interest rate per period

n

The number of periods

per

The period for which to calculate the interest payment

pv

The present value, or principal



66
67
68
69
# File 'lib/finfast.rb', line 66

def ipmt(i, per, n, pv)
  pmt = pmt(i, n, pv)
  return ipmtp(i, per, pv, pmt)
end

.ipmtp(i, per, pv, pmt) ⇒ Object

The interest payment for a loan, given that you are making a fixed payment different from the one dictated by the terms of the loan.

Params:

i

The interest rate per period

per

The period for which to calculate the interest payment

pv

The present value, or principal

pmt

The amount of the fixed payment



80
81
82
83
84
85
# File 'lib/finfast.rb', line 80

def ipmtp(i, per, pv, pmt)
  fv_orig = fv(i, per - 1, pv)       # FV at the beginning of the period
  fvan_pmts = fvan(i, per - 1, pmt)  # FV of what we've been paying
  balance_in_period = fv_orig - fvan_pmts # FV of what's left
  return balance_in_period * i
end

.ipmts(i, pers, n, pv) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/finfast.rb', line 87

def ipmts(i, pers, n, pv)
  result = []
  pers.each do |p|
    result.push(ipmt(i, p, n, pv))
  end
  return result
end

.newtonObject

.pmt(i, n, pv) ⇒ Object

Calculates the payment for a loan based on constant payments and a constant interest rate.

Params:

i

The interest rate per period

n

The number of periods

pv

The present value, or principal



54
55
56
# File 'lib/finfast.rb', line 54

def pmt(i, n, pv)
  return pv  /  ((1 - (1 / (1 + i) ** n )) / i)
end

.xirr(transactions, guess = 0.0, tolerance = 1e-6, iterations = 100) ⇒ Object

Calculates internal rate of return on a series of irregular cash flows, similar to the OpenOffice.org XIRR function described here: wiki.openoffice.org/wiki/Documentation/How_Tos/Calc%3a_XIRR_function

Params:

transactions

A hash of d, pmt: p

guess

An initial guess for the algorithm.



16
17
18
19
20
21
22
# File 'lib/finfast.rb', line 16

def xirr(transactions, guess=0.0, tolerance=1e-6, iterations=100)
  date_array = transactions.keys
  pmt_array = transactions.values
  d_0 = date_array.min
  exp_array = date_array.map { |d| exp(d, d_0) }
  newton(pmt_array, exp_array, guess, tolerance, iterations)
end

Instance Method Details

#date_difference_months(d1, d2) ⇒ Object



111
112
113
# File 'lib/finfast.rb', line 111

def date_difference_months(d1, d2)
  return d1.year * 12 - d2.year * 12 + d1.month - d2.month
end

#ipmtsp(i, pers, pv, pmt) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/finfast.rb', line 95

def ipmtsp(i, pers, pv, pmt)
  result = []
  pers.each do |p|
    result.push(ipmtp(i, p, pv, pmt))
  end
  return result
end

#newtonObject



9
10
# File 'ext/finfast/finfast.c', line 9

static VALUE method_newton(VALUE self, VALUE pmts, VALUE exps, 
VALUE guess, VALUE tolerance, VALUE max_iter);

#pv_stream(i, stream) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/finfast.rb', line 103

def pv_stream(i, stream)
  result = 0
  stream.each_with_index do |x, p|
    result += x / (1 + i) ** (p + 1)
  end
  return result
end