本文介绍了具有xirr和xnpv功能的金融python库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy具有irr和npv功能,但是我需要xirr和xnpv功能.

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

此链接指出xirr和xnpv即将推出. http://www.projectdirigible.com/documentation/spreadsheet-functions.html #coming-soon

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

是否有具有这两个功能的python库? tks.

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

推荐答案

借助在网上找到的各种实现,我想出了一个python实现:

With the help of various implementations I found in the net, I came up with a python implementation:

def xirr(transactions):
    years = [(ta[0] - transactions[0][0]).days / 365.0 for ta in transactions]
    residual = 1
    step = 0.05
    guess = 0.05
    epsilon = 0.0001
    limit = 10000
    while abs(residual) > epsilon and limit > 0:
        limit -= 1
        residual = 0.0
        for i, ta in enumerate(transactions):
            residual += ta[1] / pow(guess, years[i])
        if abs(residual) > epsilon:
            if residual > 0:
                guess += step
            else:
                guess -= step
                step /= 2.0
    return guess-1

from datetime import date
tas = [ (date(2010, 12, 29), -10000),
    (date(2012, 1, 25), 20),
    (date(2012, 3, 8), 10100)]
print xirr(tas) #0.0100612640381

这篇关于具有xirr和xnpv功能的金融python库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 06:16