• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / NumPy / 5 Useful Personal Finance Functions from NumPy Financial

5 Useful Personal Finance Functions from NumPy Financial

February 22, 2020 by cmdlinetips

Until recently, NumPy in Python had simple, but useful financial functions. These NumPy Financial functions are extremely handy for many personal finance questions, like estimating how much interest you have to pay on a loan and how your money grows with a monthly investing plan with a certain interest rate. Recently, the financial functions are deprecated and now the financial functions are in a standalone NumPy project/package called numpy-financial.

In this post we will go through some common personal finance questions and see how 4 functions from NumPy Financial can be useful. With these financial functions you don’t have to run to finance calculate websites to get guesstimate of simple finance questions in an easily reproducible manner.

Even though your current NumPy version might have these financial functions, it is a good idea to install NumPy Financial first..

# install numpy-financial
pip install numpy-financial

Let us load Numpy Financial as npf. We have also imported NumPy.

import numpy as np
import numpy_financial as npf

1. How to Compute Future Value with npf.fv

Let us say you are saving 200$ dollars per month for your kids college education and the average interest rate for the type of investment is 5%. And you are interested in the future value of the educational fund after 10 years?

NumPy Financial’s future value function fv() can get you that.

The key inputs for npf.fv() are
* Rate of interest as decimal (not per cent) per period (0.05/12)
* Number of compounding periods in months( 10*12)
* Payment (as negative numbers as the money is going out (-200)

interest_rate = 0.05/12
n_periods = 10*12
payment_per_month = -200
present_value = -200

We have each of the input needed as variables stored in Python and we are ready to compute the future value of educational funding.

future_value = npf.fv(interest_rate, n_periods, payment_per_month, present_value)
print(round(future_value))
31386.0

Thus, saving $200 a month at 5% annual interest we get $31,386 after 10 years.

2. How To Compute the number of load payments?: npf.nper()

Let us consider another common financial situation that NumPy Financial function can help. Let us say you have a car loan of $20,000 with annual interest of 4%. And you want to pay $400 per month towards the loan and want to know the total number of payments you have to make. Basically, you are interested in the number of years it will take to pay off the car loan. NumPy Financial’s nper() function can help you with that.

The key inputs for npf.nper are
* Rate of loan interest as decimal (not per cent) per period: 0.05/12
* Payment per month -400
* Loan amount $20000
Let us define the variables needed for computing the number of payments needed to pay off the load.

interest_rate=0.04/12
payment_per_month=-400
loan_amount=20000
npf.nper(0.04/12, -400, 20000)
array(54.78757726)

Now we can compute the number of periodic payments with nper().

n_payments = npf.nper(interest_rate, payment_per_month, loan_amount)
print(n_payments)
54.79

If we have a 20000 load with 4% interest, it will take about 55 months to pay off the loan if we pay $400 per month.

3. How To Compute Your Monthly Payment for a Mortgage loan?

Let us say, you are interested in a home mortgage loan and want to know how much monthly payment you need to make to pay off $400,000 loan in 30 years at an annual interest rate of 8%? We can use Numpy financial’s pmt() function to compute payment against loan principal plus interest.

interest_rate=0.06/12
mortgage_amount=400000
n_periods = 30*12
m_payment = npf.pmt(interest_rate, n_periods, mortgage_amount)
print(round(m_payment))
-2398.0

With NumPy Financial’s npmt() function, we found that we need 2398.0 monthly as payment against loan principal plus interest for the mortgage loan.

print(round(m_payment))

4. How To Compute the interest portion of a payment?

The monthly payment against a loan has two parts. One is the interest portion of the loan and the other is actual payment that goes towards the loan, i.e. payment against loan principal. Both these change at different rate over the loan period. And it is really interesting to see the relationship. NumPy Financial has two functions to tease apart the interest and payment against loan principal.

Let us first compute the interest portion of a payment with ipmt() function in NumPy Financial. Let us list the total number of periods as a list.

periods=np.arange(30*12) + 1

We can use ipmt() function in NumPy financial to compute the interest portion of the mortgage.

interest_per_month=npf.ipmt(interest_rate, periods, 30*12, mortgage_amount)
interest_per_month[0:10]
array([-398.2 , -400.19, -402.19, -404.21, -406.23, -408.26, -410.3 ,
       -412.35, -414.41, -416.48])

This gives us how much we pay as interest for the load we took.

5. How To Compute the payment against loan principal.

Let us compute how much money we pay goest to cover the principal amount of the load. We can use ppmt() function to compute payment against loan principal.

principal_per_month= npf.ppmt(interest_rate, periods, n_periods, principal)
principal_per_month[0:10]
array([-2398.2, -2398.2, -2398.2, -2398.2, -2398.2, -2398.2, -2398.2,
       -2398.2, -2398.2, -2398.2])

We can gain a better understanding of the mortgage loan by looking at the relationship between payment against loan principal and interest portion of our payment. Let us create a Pandas dataframe using the variables we computed. And make a simple line plot with Pandas to see the relationship.

mortgage_df = pd.DataFrame({"principal":principal_per_month,
                           "interest":interest_per_month,
                           "monthly_p":interest_per_month +payment_per_month})
mortgage_df.plot()
plt.xlabel("Payment", size=16)
plt.ylabel("Payment Monthly", size=16)

This shows the classing Principal and Interest payment for a mortgage and how they change over the payments. We can see that, during initial period we pay more in interest and only after about 200 payments we end up paying more towards the principal.

Principal and Interest Payment Relationship for a Mortgage
Principal and Interest Payment Relationship for a Mortgage

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Implement Pandas Groupby operation with NumPy? NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities10 Basic Arithmetic Operations with NumPy array Default Thumbnail9 Basic Linear Algebra Operations with NumPy Default Thumbnail3 Basic Commands to Manipulate NumPy 2d-arrays

Filed Under: NumPy, NumPy Financial Functions Tagged With: NumPy Financial, Python

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version