Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

financial python library that has xirr and xnpv function?

numpy has irr and npv function, but I need xirr and xnpv function.

this link points out that xirr and xnpv will be coming soon. http://www.projectdirigible.com/documentation/spreadsheet-functions.html#coming-soon

Is there any python library that has those two functions? tks.

like image 953
Shuguang Yang Avatar asked Jan 19 '12 00:01

Shuguang Yang


People also ask

What's the difference between IRR and Xirr?

As we've explained, the key difference between IRR and XIRR is the way each formula handles cash flows. IRR doesn't take into account when the actual cash flow takes place, so it rolls them up into annual periods. By contrast, the XIRR formula considers the dates when the cash flow actually happens.

What is XNPV?

XNPV (Net Present Value of an Investment for Payments or Incomes at Irregular Intervals) Returns the net present value of an investment based on a discount rate and a set of future payments (negative values) and income (positive values). These payments or incomes do not need to occur at regular intervals.

How do you use the Xirr formula?

Procedure to calculate XIRR using excel Enter the redemption amount against the redemption date in Column B. You have XIRR (values, dates, [guess]). Use the formula =XIRR (B5:B15, A5:A17) * 100 and hit the enter button.


1 Answers

Here is one way to implement the two functions.

import scipy.optimize

def xnpv(rate, values, dates):
    '''Equivalent of Excel's XNPV function.

    >>> from datetime import date
    >>> dates = [date(2010, 12, 29), date(2012, 1, 25), date(2012, 3, 8)]
    >>> values = [-10000, 20, 10100]
    >>> xnpv(0.1, values, dates)
    -966.4345...
    '''
    if rate <= -1.0:
        return float('inf')
    d0 = dates[0]    # or min(dates)
    return sum([ vi / (1.0 + rate)**((di - d0).days / 365.0) for vi, di in zip(values, dates)])

def xirr(values, dates):
    '''Equivalent of Excel's XIRR function.

    >>> from datetime import date
    >>> dates = [date(2010, 12, 29), date(2012, 1, 25), date(2012, 3, 8)]
    >>> values = [-10000, 20, 10100]
    >>> xirr(values, dates)
    0.0100612...
    '''
    try:
        return scipy.optimize.newton(lambda r: xnpv(r, values, dates), 0.0)
    except RuntimeError:    # Failed to converge?
        return scipy.optimize.brentq(lambda r: xnpv(r, values, dates), -1.0, 1e10)
like image 184
KT. Avatar answered Sep 24 '22 02:09

KT.