-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TST: add PV tests for basic examples #85
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,13 +90,27 @@ def test_decimal_with_when(self): | |
|
||
|
||
class TestPV: | ||
def test_pv(self): | ||
assert_almost_equal(npf.pv(0.07, 20, 12000, 0), -127128.17, 2) | ||
|
||
def test_pv_decimal(self): | ||
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), | ||
Decimal('0')), | ||
Decimal('-127128.1709461939327295222005')) | ||
@pytest.mark.parametrize("rate, periods, payments, future_value, exp_result", [ | ||
(0.07, 20, 0, 12000, -3101.0280337664), | ||
(0.025, 4, 0, 1000, -905.9506447998), | ||
(-0.07, 56, 0, 1200000, -69845129.5182595000), | ||
(0.05, 20, 0, -12000, 4522.6737944760), | ||
(0.05, 356.5288568, 0, -25000.00, 0.0006971776), | ||
(0.05, 50, 0, 0, 0.0000000000), | ||
(0.05, 26, 500, 0, -7187.5926504975), | ||
]) | ||
@pytest.mark.parametrize('number_type', [float, Decimal]) | ||
def test_pv(self, rate, periods, payments, future_value, exp_result, number_type): | ||
result = npf.pv(rate, periods, payments, future_value) | ||
|
||
if exp_result is not numpy.nan: | ||
if number_type is Decimal: | ||
assert result == pytest.approx(exp_result, rel=0.2) | ||
Comment on lines
+107
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please prefer the routines in numpy.testing over those in pytest. In this case, we should not be testing that two values are approximately equal, but that they are exactly equal as, when we are using Decimal, we should be keeping arbitrary precision. |
||
else: | ||
assert_allclose(float(result), float( | ||
exp_result), atol=1e-10) | ||
Comment on lines
+110
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good. :) |
||
else: | ||
assert numpy.isnan(result) | ||
|
||
|
||
class TestRate: | ||
|
@@ -279,6 +293,7 @@ def test_mirr(self, values, finance_rate, reinvest_rate, expected): | |
) | ||
def test_mirr_decimal(self, number_type, args, expected): | ||
values = [number_type(v) for v in args['values']] | ||
|
||
result = npf.mirr( | ||
values, | ||
number_type(args['finance_rate']), | ||
|
@@ -721,7 +736,8 @@ def test_gh_39(self): | |
|
||
def test_gh_44(self): | ||
# "true" value as calculated by Google sheets | ||
cf = [-1678.87, 771.96, 1814.05, 3520.30, 3552.95, 3584.99, 4789.91, -1] | ||
cf = [-1678.87, 771.96, 1814.05, 3520.30, | ||
3552.95, 3584.99, 4789.91, -1] | ||
assert_almost_equal(npf.irr(cf), 1.00426, 4) | ||
|
||
def test_irr_no_real_solution_exception(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These values should be strings, not floats. A float stores an inexact representation of most numbers, the decimal representation of that number will not be the same as the floating point representation. For example see: