Skip to content

BUG: removed unnecesary warning from financial.pmt method #6073

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

Closed
wants to merge 3 commits into from

Conversation

githubmlai
Copy link

Previous implementation would cause divide by zero and hence warning since
"fact" variable was calculated for all rates (both zero and non-zero) but then
zero rates would be masked with a where equality.

Changed implementation to apply non-zero rates using "np.divide( numerator,
denominator, where=mask_of_zero_values)"and then apply zero rates with "np.copyto".

Also updated documentation to show exact formulas that are being calculated.

Done with help of Sebastian Berg in Austin,TX!

Fixes issue #5046

Previous implementation would cause divide by zero and hence warning since
"fact" variable was calculated for all rates (both zero and non-zero) but then
zero rates would be masked with a where equality.

Changed implementation to apply non-zero rates using "np.divide( numerator,
denominator, where=mask_of_zero_values)"and then apply zero rates with "np.copyto".

Also updated documentation to show exact formulas that are being calculated.

Done with help of Sebastian Berg in Austin,TX!

Fixes issue numpy#5046
return -(fv + pv*temp) / fact

# use different payment if rate == 0
zero_rate_mask = rate == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put parens around the == expression to make it easier to read: zero_rate_mask = (rate == 0)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok updated the code per your suggestion and ran "git pull --force"

On Mon, Jul 13, 2015 at 3:53 PM, Nathaniel J. Smith <
notifications@github.com> wrote:

In numpy/lib/financial.py
#6073 (comment):

@@ -208,12 +215,15 @@ def pmt(rate, nper, pv, fv=0, when='end'):
"""
when = _convert_when(when)
(rate, nper, pv, fv, when) = map(np.asarray, [rate, nper, pv, fv, when])

  • temp = (1 + rate)**nper
  • mask = (rate == 0.0)
  • np.copyto(rate, 1.0, where=mask)
  • z = np.zeros(np.broadcast(rate, nper, pv, fv, when).shape)
  • fact = np.where(mask != z, nper + z, (1 + rate_when)_(temp - 1)/rate + z)
  • return -(fv + pv*temp) / fact
  • use different payment if rate == 0

  • zero_rate_mask = rate == 0

Please put parens around the == expression to make it easier to read: zero_rate_mask
= (rate == 0)


Reply to this email directly or view it on GitHub
https://github.com/numpy/numpy/pull/6073/files#r34510377.

Previous implementation would cause divide by zero and hence warning since
"fact" variable was calculated for all rates (both zero and non-zero) but then
zero rates would be masked with a where equality.

Changed implementation to apply non-zero rates using "np.divide( numerator,
denominator, where=mask_of_zero_values)"and then apply zero rates with "np.copyto".

Also updated documentation to show exact formulas that are being calculated.

Done with help of Sebastian Berg in Austin,TX!

7/13/15 Added in parenthesis for reability per Nathaniel Smith comment e.g.
    zero_rate_mask = (rate == 0)

Fixes issue numpy#5046
payment = np.asarray(np.divide(-(fv + pv*compounded_rate)*rate,
(1 + rate*when)*(compounded_rate - 1),
where=~zero_rate_mask))
np.copyto(payment, -(fv + pv)/nper, where=zero_rate_mask)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Sorry, got interrupted half-way through reviewing :-))
I'd put spaces around most or all of these binary operators (e.g. (1 + rate * when) * (compounded_rate - 1)), though in this case it's more a matter of personal taste than rigid policy. (In general spaces around operators is PEP8's recommendation, but there's an escape clause for higher-priority operators that could apply here if one wanted.)

@njsmith
Copy link
Member

njsmith commented Jul 13, 2015

Also, is there a test for this function? If not it would be good to add one (esp. checking both rate = 0 and not).

Otherwise LGTM (= looks good to me).

@githubmlai
Copy link
Author

Ok!

Added in spaces around binary operators as suggested.

There are pmt test in test_financial.py but the tests did not try to catch
warnings.


def test_pmt(self):
res = np.pmt(0.08/12, 5_12, 15000)
tgt = -304.145914
assert_allclose(res, tgt)
# Test the edge case where rate == 0.0
res = np.pmt(0.0, 5_12, 15000)
tgt = -250.0
assert_allclose(res, tgt)
# Test the case where we use broadcast and
# the arguments passed in are arrays.
res = np.pmt([[0.0, 0.8],[0.3, 0.8]],[12, 3],[2000, 20000])
tgt = np.array([[-166.66667, -19311.258],[-626.90814, -19311.258]])
assert_allclose(res, tgt)

On Mon, Jul 13, 2015 at 5:56 PM, Nathaniel J. Smith <
notifications@github.com> wrote:

Also, is there a test for this function? If not it would be good to add
one (esp. checking both rate = 0 and not).

Otherwise LGTM (= looks good to me).


Reply to this email directly or view it on GitHub
#6073 (comment).

@charris
Copy link
Member

charris commented Jul 18, 2015

You should check that removing np.broadcast doesn't affect the shape of the result. The original where is a bit tricky dealing with that. Probably needs test(s) for various input shapes the check the resulting shape against the broadcast shape. That said, the function is probably over complicated to start with.

@homu
Copy link
Contributor

homu commented Sep 19, 2016

☔ The latest upstream changes (presumably #8050) made this pull request unmergeable. Please resolve the merge conflicts.

@seberg
Copy link
Member

seberg commented Sep 22, 2019

This was fixed long ago by a similar pr: gh-5577 (some of the doc changes could possible be moved, but I do not think it is worth keeping this open for). Thanks!

@seberg seberg closed this Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants