Getting Started
Before diving in, ensure you have NumPy installed. If not, use pip:
pip install numpy
Import the necessary module:
import numpy_financial as npf
Core Financial Functions
Let’s examine some fundamental functions:
1. fv(rate, nper, pmt, pv, when='end')
: Future Value
Calculates the future value of an investment.
rate
: The periodic interest rate.nper
: The total number of payment periods.pmt
: The payment made each period. Can be negative (investment) or positive (loan).pv
: The present value (initial investment).when
: Specifies whether payments are made at the beginning (‘begin’) or end (‘end’) of each period (default is ‘end’).
= 0.05
rate = 10
nper = 0 # No additional payments
pmt = -1000 # Initial investment (negative because it's an outflow)
pv = npf.fv(rate, nper, pmt, pv)
fv print(f"Future Value: {fv:.2f}")
2. pv(rate, nper, pmt, fv, when='end')
: Present Value
Calculates the present value of a future amount.
= 0.07
rate = 5
nper = 0
pmt = 2000
fv = npf.pv(rate, nper, pmt, fv)
pv print(f"Present Value: {pv:.2f}")
3. npv(rate, values)
: Net Present Value
Calculates the net present value of a series of cash flows.
= 0.1
rate = [-1000, 200, 300, 400, 500]
values = npf.npv(rate, values)
npv print(f"Net Present Value: {npv:.2f}")
4. irr(values)
: Internal Rate of Return
Calculates the internal rate of return of a series of cash flows.
= npf.irr(values)
irr print(f"Internal Rate of Return: {irr:.2f}")
5. mirr(values, finance_rate, reinvest_rate)
: Modified Internal Rate of Return
Calculates the modified internal rate of return, considering different financing and reinvestment rates.
= 0.05
finance_rate = 0.08
reinvest_rate = npf.mirr(values, finance_rate, reinvest_rate)
mirr print(f"Modified Internal Rate of Return: {mirr:.2f}")
6. pmt(rate, nper, pv, fv=0, when='end')
: Payment
Calculates the periodic payment for a loan or investment.
= 0.06 / 12 # Monthly interest rate
rate = 5 * 12 # Total number of payments
nper = 20000 # Loan amount
pv = npf.pmt(rate, nper, pv)
pmt print(f"Monthly Payment: {pmt:.2f}")
7. ppmt(rate, per, nper, pv, fv=0, when='end')
: Principal Payment
Calculates the principal payment portion of a loan payment at a specific period.
= 1
per = npf.ppmt(rate, per, nper, pv)
principal_payment print(f"Principal Payment in the First Month: {principal_payment:.2f}")
8. ipmt(rate, per, nper, pv, fv=0, when='end')
: Interest Payment
Calculates the interest payment portion of a loan payment at a specific period.
= npf.ipmt(rate, per, nper, pv)
interest_payment print(f"Interest Payment in the First Month: {interest_payment:.2f}")
These examples demonstrate the basic usage of NumPy’s financial functions. Remember to adjust the inputs according to your specific financial scenario. For more complex financial modeling, consider using more specialized libraries.