def test_irr(self):
v = [-150000, 15000, 25000, 35000, 45000, 60000]
assert_almost_equal(np.irr(v), 0.0524, 2)
v = [-100, 0, 0, 74]
assert_almost_equal(np.irr(v), -0.0955, 2)
v = [-100, 39, 59, 55, 20]
assert_almost_equal(np.irr(v), 0.28095, 2)
v = [-100, 100, 0, -7]
assert_almost_equal(np.irr(v), -0.0833, 2)
v = [-100, 100, 0, 7]
assert_almost_equal(np.irr(v), 0.06206, 2)
v = [-5, 10.5, 1, -8, 1]
assert_almost_equal(np.irr(v), 0.0886, 2)
# Test that if there is no solution then np.irr returns nan
# Fixes gh-6744
v = [-1, -2, -3]
assert_equal(np.irr(v), np.nan)
python类irr()的实例源码
def test_irr(self):
v = [-150000, 15000, 25000, 35000, 45000, 60000]
assert_almost_equal(np.irr(v), 0.0524, 2)
v = [-100, 0, 0, 74]
assert_almost_equal(np.irr(v), -0.0955, 2)
v = [-100, 39, 59, 55, 20]
assert_almost_equal(np.irr(v), 0.28095, 2)
v = [-100, 100, 0, -7]
assert_almost_equal(np.irr(v), -0.0833, 2)
v = [-100, 100, 0, 7]
assert_almost_equal(np.irr(v), 0.06206, 2)
v = [-5, 10.5, 1, -8, 1]
assert_almost_equal(np.irr(v), 0.0886, 2)
def test_irr(self):
v = [-150000, 15000, 25000, 35000, 45000, 60000]
assert_almost_equal(np.irr(v), 0.0524, 2)
v = [-100, 0, 0, 74]
assert_almost_equal(np.irr(v), -0.0955, 2)
v = [-100, 39, 59, 55, 20]
assert_almost_equal(np.irr(v), 0.28095, 2)
v = [-100, 100, 0, -7]
assert_almost_equal(np.irr(v), -0.0833, 2)
v = [-100, 100, 0, 7]
assert_almost_equal(np.irr(v), 0.06206, 2)
v = [-5, 10.5, 1, -8, 1]
assert_almost_equal(np.irr(v), 0.0886, 2)
def test_irr(self):
v = [-150000, 15000, 25000, 35000, 45000, 60000]
assert_almost_equal(np.irr(v), 0.0524, 2)
v = [-100, 0, 0, 74]
assert_almost_equal(np.irr(v), -0.0955, 2)
v = [-100, 39, 59, 55, 20]
assert_almost_equal(np.irr(v), 0.28095, 2)
v = [-100, 100, 0, -7]
assert_almost_equal(np.irr(v), -0.0833, 2)
v = [-100, 100, 0, 7]
assert_almost_equal(np.irr(v), 0.06206, 2)
v = [-5, 10.5, 1, -8, 1]
assert_almost_equal(np.irr(v), 0.0886, 2)
def test_irr(self):
v = [-150000, 15000, 25000, 35000, 45000, 60000]
assert_almost_equal(np.irr(v), 0.0524, 2)
v = [-100, 0, 0, 74]
assert_almost_equal(np.irr(v), -0.0955, 2)
v = [-100, 39, 59, 55, 20]
assert_almost_equal(np.irr(v), 0.28095, 2)
v = [-100, 100, 0, -7]
assert_almost_equal(np.irr(v), -0.0833, 2)
v = [-100, 100, 0, 7]
assert_almost_equal(np.irr(v), 0.06206, 2)
v = [-5, 10.5, 1, -8, 1]
assert_almost_equal(np.irr(v), 0.0886, 2)
def run_many(case):
@functools.wraps(case)
def wrapped():
for test in range(1000):
d, r = case()
assert irr.irr(d) == pytest.approx(r)
return wrapped
def test_performance():
us_times = []
np_times = []
ns = [10, 20, 50, 100]
for n in ns:
k = 100
sums = [0.0, 0.0]
for j in range(k):
r = math.exp(random.gauss(0, 1.0 / n)) - 1
x = random.gauss(0, 1)
d = [x] + [0.0] * (n-2) + [-x * (1+r)**(n-1)]
results = []
for i, f in enumerate([irr.irr, numpy.irr]):
t0 = time.time()
results.append(f(d))
sums[i] += time.time() - t0
if not numpy.isnan(results[1]):
assert results[0] == pytest.approx(results[1])
for times, sum in zip([us_times, np_times], sums):
times.append(sum/k)
try:
from matplotlib import pyplot
import seaborn
except ImportError:
return
pyplot.plot(ns, us_times, label='Our library')
pyplot.plot(ns, np_times, label='Numpy')
pyplot.xlabel('n')
pyplot.ylabel('time(s)')
pyplot.yscale('log')
pyplot.savefig('plot.png')
def test_irr(self):
v = [-150000, 15000, 25000, 35000, 45000, 60000]
assert_almost_equal(np.irr(v), 0.0524, 2)
v = [-100, 0, 0, 74]
assert_almost_equal(np.irr(v), -0.0955, 2)
v = [-100, 39, 59, 55, 20]
assert_almost_equal(np.irr(v), 0.28095, 2)
v = [-100, 100, 0, -7]
assert_almost_equal(np.irr(v), -0.0833, 2)
v = [-100, 100, 0, 7]
assert_almost_equal(np.irr(v), 0.06206, 2)
v = [-5, 10.5, 1, -8, 1]
assert_almost_equal(np.irr(v), 0.0886, 2)
def npv(rate, values):
"""
Returns the NPV (Net Present Value) of a cash flow series.
Parameters
----------
rate : scalar
The discount rate.
values : array_like, shape(M, )
The values of the time series of cash flows. The (fixed) time
interval between cash flow "events" must be the same as that for
which `rate` is given (i.e., if `rate` is per year, then precisely
a year is understood to elapse between each cash flow event). By
convention, investments or "deposits" are negative, income or
"withdrawals" are positive; `values` must begin with the initial
investment, thus `values[0]` will typically be negative.
Returns
-------
out : float
The NPV of the input cash flow series `values` at the discount
`rate`.
Notes
-----
Returns the result of: [G]_
.. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}}
References
----------
.. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
Addison-Wesley, 2003, pg. 346.
Examples
--------
>>> np.npv(0.281,[-100, 39, 59, 55, 20])
-0.0084785916384548798
(Compare with the Example given for numpy.lib.financial.irr)
"""
values = np.asarray(values)
return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)
def npv(rate, values):
"""
Returns the NPV (Net Present Value) of a cash flow series.
Parameters
----------
rate : scalar
The discount rate.
values : array_like, shape(M, )
The values of the time series of cash flows. The (fixed) time
interval between cash flow "events" must be the same as that for
which `rate` is given (i.e., if `rate` is per year, then precisely
a year is understood to elapse between each cash flow event). By
convention, investments or "deposits" are negative, income or
"withdrawals" are positive; `values` must begin with the initial
investment, thus `values[0]` will typically be negative.
Returns
-------
out : float
The NPV of the input cash flow series `values` at the discount
`rate`.
Notes
-----
Returns the result of: [G]_
.. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}}
References
----------
.. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
Addison-Wesley, 2003, pg. 346.
Examples
--------
>>> np.npv(0.281,[-100, 39, 59, 55, 20])
-0.0084785916384548798
(Compare with the Example given for numpy.lib.financial.irr)
"""
values = np.asarray(values)
return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)
def npv(rate, values):
"""
Returns the NPV (Net Present Value) of a cash flow series.
Parameters
----------
rate : scalar
The discount rate.
values : array_like, shape(M, )
The values of the time series of cash flows. The (fixed) time
interval between cash flow "events" must be the same as that for
which `rate` is given (i.e., if `rate` is per year, then precisely
a year is understood to elapse between each cash flow event). By
convention, investments or "deposits" are negative, income or
"withdrawals" are positive; `values` must begin with the initial
investment, thus `values[0]` will typically be negative.
Returns
-------
out : float
The NPV of the input cash flow series `values` at the discount
`rate`.
Notes
-----
Returns the result of: [G]_
.. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}}
References
----------
.. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
Addison-Wesley, 2003, pg. 346.
Examples
--------
>>> np.npv(0.281,[-100, 39, 59, 55, 20])
-0.0084785916384548798
(Compare with the Example given for numpy.lib.financial.irr)
"""
values = np.asarray(values)
return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)
def npv(rate, values):
"""
Returns the NPV (Net Present Value) of a cash flow series.
Parameters
----------
rate : scalar
The discount rate.
values : array_like, shape(M, )
The values of the time series of cash flows. The (fixed) time
interval between cash flow "events" must be the same as that for
which `rate` is given (i.e., if `rate` is per year, then precisely
a year is understood to elapse between each cash flow event). By
convention, investments or "deposits" are negative, income or
"withdrawals" are positive; `values` must begin with the initial
investment, thus `values[0]` will typically be negative.
Returns
-------
out : float
The NPV of the input cash flow series `values` at the discount
`rate`.
Notes
-----
Returns the result of: [G]_
.. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}}
References
----------
.. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
Addison-Wesley, 2003, pg. 346.
Examples
--------
>>> np.npv(0.281,[-100, 39, 59, 55, 20])
-0.0084785916384548798
(Compare with the Example given for numpy.lib.financial.irr)
"""
values = np.asarray(values)
return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)
def npv(rate, values):
"""
Returns the NPV (Net Present Value) of a cash flow series.
Parameters
----------
rate : scalar
The discount rate.
values : array_like, shape(M, )
The values of the time series of cash flows. The (fixed) time
interval between cash flow "events" must be the same as that for
which `rate` is given (i.e., if `rate` is per year, then precisely
a year is understood to elapse between each cash flow event). By
convention, investments or "deposits" are negative, income or
"withdrawals" are positive; `values` must begin with the initial
investment, thus `values[0]` will typically be negative.
Returns
-------
out : float
The NPV of the input cash flow series `values` at the discount
`rate`.
Notes
-----
Returns the result of: [G]_
.. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}}
References
----------
.. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
Addison-Wesley, 2003, pg. 346.
Examples
--------
>>> np.npv(0.281,[-100, 39, 59, 55, 20])
-0.0084785916384548798
(Compare with the Example given for numpy.lib.financial.irr)
"""
values = np.asarray(values)
return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)
def npv(rate, values):
"""
Returns the NPV (Net Present Value) of a cash flow series.
Parameters
----------
rate : scalar
The discount rate.
values : array_like, shape(M, )
The values of the time series of cash flows. The (fixed) time
interval between cash flow "events" must be the same as that for
which `rate` is given (i.e., if `rate` is per year, then precisely
a year is understood to elapse between each cash flow event). By
convention, investments or "deposits" are negative, income or
"withdrawals" are positive; `values` must begin with the initial
investment, thus `values[0]` will typically be negative.
Returns
-------
out : float
The NPV of the input cash flow series `values` at the discount
`rate`.
Notes
-----
Returns the result of: [G]_
.. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}}
References
----------
.. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
Addison-Wesley, 2003, pg. 346.
Examples
--------
>>> np.npv(0.281,[-100, 39, 59, 55, 20])
-0.0084785916384548798
(Compare with the Example given for numpy.lib.financial.irr)
"""
values = np.asarray(values)
return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)