numpy_financial.pmt() in Python (original) (raw)
Last Updated : 19 Mar, 2025
**numpy_financial.pmt() function in Python is part of the numpy-financial library and is used for calculating the payment amount required for a loan or an investment, assuming that payments are constant and the interest rate remains unchanged throughout the term. This function is particularly useful for financial calculations involving loans, mortgages, or other types of payments spread over a series of periods.
To use the numpy_financial.pmt() function in Python, you need to install the numpy-financial library, which is an extension of NumPy for financial calculations. You can install the numpy-financial library using pip (Python’s package manager):
pip install numpy-financial
**Example:
Let’s calculate the monthly payment required for a $10,000 loan over 5 years with an annual interest rate of 6%.
Python `
import numpy_financial as npf
Given values
rate = 0.06 / 12
nper = 12 * 5
pv = 10000
Calculate monthly payment
payment = npf.pmt(rate, nper, pv)
print(f"Monthly payment: {payment:.2f}")
`
**Output
Monthly payment: -193.33
**Explanation:
- **rate = 0.06 / 12: Converts the annual interest rate (6%) to a monthly interest rate.
- **nper = 12 * 5: Calculates the total number of periods (12 months per year for 5 years).
- **pv = 10000: The loan amount is 10,000.
Understanding the Formula
The numpy_financial.pmt() function solves the following equation for the payment (pmt):
[Tex]f v + p v \times (1 + rate)^{nper} + pmt \times \frac{(1 + rate \times when)}{rate} \times \left( (1 + rate)^{nper} – 1 \right) = 0[/Tex]
Syntax
numpy_financial.pmt(rate, nper, pv, fv=0, when=’end’)
Parameters
- **rate: The interest rate for each period.
- **nper: The total number of periods (e.g., months or years).
- **pv: The present value (the loan amount or investment).
- **fv: The future value, or the desired loan balance after the last payment (default is 0).
- **when : Specifies whether the payment is made at the ‘end’ (default) or ‘begin’ of each period.
Return Value
The function returns the payment amount for each period.
Example of numpy_financial.pmt()
**1. Investment Payment Calculation
Suppose we want to make monthly payments into an account with a future value of $50,000 after 10 years, assuming an annual interest rate of 4%.
Python `
import numpy_financial as npf
Parameters
rate = 0.04 / 12
nper = 10 * 12
fv = 50000
Calculate the monthly payment
payment = npf.pmt(rate, nper, 0, fv)
print(f"Monthly Payment: ${payment:.2f}")
`
**Output
Monthly Payment: $330.09
**Explanation:
- The interest rate is converted to a monthly rate by dividing the annual rate (4%) by 12.
- The number of payments is calculated by multiplying 10 years by 12 months.
- The npf.pmt() function calculates the amount you need to pay monthly to achieve a future value of $50,000.
**2. Mortgage Payment Calculation
In this example, we’ll calculate the monthly mortgage payment for a house loan. You borrow $250,000 for 30 years at an annual interest rate of 3.5%. We will calculate the monthly payment using numpy_financial.pmt().
Java `
import numpy_financial as npf
Parameters
rate = 0.035 / 12 # Monthly interest rate (annual rate divided by 12) nper = 30 * 12 # Total number of payments (30 years with monthly payments) pv = 250000 # Present value (loan amount)
Calculate the monthly mortgage payment
payment = npf.pmt(rate, nper, pv)
print(f"Monthly Mortgage Payment: ${payment:.2f}")
`
**Output
Monthly Mortgage Payment: $1122.61
**Explanation:
- The rate is calculated by dividing the annual interest rate 3.5% by 12 to get the monthly interest rate.
- The nper is calculated by multiplying 30 years by 12 months.
- The npf.pmt() function is used to calculate the monthly payment for the mortgage loan.
When to Use numpy_financial.pmt()
- **Loan Payments: When you need to calculate the monthly payment for a loan or mortgage with a fixed interest rate and a fixed term.
- **Investment Withdrawals: When calculating how much you can withdraw from an investment at regular intervals (like monthly) to deplete the entire amount by the end of a fixed term.
- **Annuities: Useful for calculating annuity payments when the principal amount is withdrawn periodically over a set term.