NumPy Financial Module Deep Dive

Gaurav Kumar
5 min readFeb 4, 2024

--

When it comes to financial calculations and data manipulation in Python, the NumPy library stands out as a powerhouse. Within the realm of NumPy, the NumPy financial module serves as a valuable tool set for anyone dealing with financial mathematics, from simple interest calculations to complex investment analysis. In this deep dive, we'll explore the capabilities and features of the NumPy financial module, highlighting its utility in various financial scenarios.

NumPy financial:

NumPy is a popular library for numerical computing in Python. It provides support for arrays and matrices, along with a wide range of mathematical functions. While NumPy itself is not explicitly focused on finance, it forms the foundation for various domain-specific libraries like numpy_financial. This module extends NumPy's capabilities by offering financial functions commonly used in banking, investment, and other financial sectors.

Installation

Before we dive into the functionality of NumPy financial, it's essential to ensure you have it installed. You can do this using pip:

pip install numpy-financial

Once installed, you can import it into your Python scripts or Jupyter notebooks as follows:

import numpy_financial as npf

The recommended alias is npf. For example,

import numpy_financial as npf
npf.irr([-750500, 45000, 150700, 700000, 220000, 307000])

Output:
0.21287538856102084

So, let’s understand about “irr”
We use IRR to calculate the annual rate of return of an investment.

IRR is a useful tool for comparing different investments and deciding which one is the best option. It is also a good way to measure the profitability of an investment over time.

Key Functions

1. Time Value of Money (TVM) Functions

TVM functions are fundamental in finance, and NumPy financial provides an array of TVM functions, such as:

What is the use of npf.fv, npf.pv, npf.nper, npf.pmt?

npf.fv (Future Value): You use this to determine how much money an investment will grow to in the future, helping you plan for savings or investments.

npf.pv (Present Value): It helps you figure out the current worth of a future cash flow, which is crucial for evaluating investments, loans, and other financial choices.

npf.nper (Number of Periods): This tells you how long it will take to reach a financial goal or pay off a loan, aiding in setting realistic timelines.

npf.pmt (Payment): It calculates the regular payment needed to reach a financial goal, such as paying off a loan or saving for a future expense, assisting in budgeting and planning.
npf.fv(rate, nper, pmt, pv): Calculate the future value of an investment.
npf.pv(rate, nper, pmt, fv): Determine the present value of an investment.
npf.nper(rate, pmt, pv, fv): Calculate the number of payment periods needed.
npf.pmt(rate, nper, pv, fv): Determine the periodic payment amount.
import numpy_financial as npf

# Define variables
rate = 0.05 # Interest rate (5%)
nper = 10 # Number of periods
pmt = -1000 # Periodic payment (negative for outgoing cash flow)
pv = 0 # Present value or initial investment

# Calculate Future Value (FV)
fv = npf.fv(rate, nper, pmt, pv)
print(f"Future Value (FV): ${fv:.2f}")

# Calculate Present Value (PV)
pv = npf.pv(rate, nper, pmt, fv)
print(f"Present Value (PV): ${pv:.2f}")

# Calculate Number of Payment Periods (NPER)
nper = npf.nper(rate, pmt, pv, fv)
print(f"Number of Payment Periods (NPER): {nper:.2f} periods")

# Calculate Periodic Payment (PMT)
pmt = npf.pmt(rate, nper, pv, fv)
print(f"Periodic Payment (PMT): ${pmt:.2f}")

Output:
Future Value (FV): $12577.89
Present Value (PV): $-0.00
Number of Payment Periods (NPER): 10.00 periods
Periodic Payment (PMT): $-1000.00

These functions simplify complex financial calculations, such as loan amortization, retirement planning, and investment growth projections.

2. Net Present Value (NPV) and Internal Rate of Return (IRR)

npf.npv(rate, values): Calculate the Net Present Value of a series of cash flows.
npf.irr(values): Determine the Internal Rate of Return of a series of cash flows.
import numpy_financial as npf

# Define the cash flows (negative values for investments, positive for returns)
cash_flows = [-1000, 300, 300, 300, 300]

# Define the discount rate (rate of return)
discount_rate = 0.1 # 10%

# Calculate NPV
npv = npf.npv(discount_rate, cash_flows)
print(f"Net Present Value (NPV): {npv:.2f}")

# Calculate IRR
irr = npf.irr(cash_flows)
print(f"Internal Rate of Return (IRR): {irr:.2%}")

Output:
Net Present Value (NPV): -49.04
Internal Rate of Return (IRR): 7.71%

These functions are vital for evaluating investment projects, helping you make informed decisions about the profitability of an investment.

3. Depreciation Calculations

What is “straight line depreciation” and “double declining balance depreciation” ? why we use?

Straight-line depreciation and double-declining balance depreciation are two methods of calculating depreciation for assets.

Straight-line depreciation is the simplest method, and it allocates an equal amount of depreciation expense to each year of the asset's useful life.
Double-declining balance depreciation depreciates an asset more rapidly in the early years of its useful life. It calculates the depreciation expense as a multiple of the straight-line depreciation expense.

We use these methods to allocate the cost of an asset over its useful life. This is done for financial reporting purposes, to match the expenses of using the asset with the revenues generated by the asset.
npf.sl(nper, cost, salvage): Calculate straight-line depreciation.
npf.ddb(nper, cost, salvage, period): Determine double declining balance depreciation.
def straight_line_depreciation(cost, salvage_value, useful_life):
annual_depreciation = (cost - salvage_value) / useful_life
return annual_depreciation

def double_declining_balance_depreciation(cost, salvage_value, useful_life, period):
depreciation_rate = 2 / useful_life
previous_period_depreciation = 0

for _ in range(period):
current_period_depreciation = (cost - previous_period_depreciation) * depreciation_rate
previous_period_depreciation += current_period_depreciation

return min(current_period_depreciation, cost - salvage_value)

# Example usage
cost = 10000 # Initial cost of the asset
salvage_value = 2000 # Estimated salvage value at the end of its useful life
useful_life = 5 # Number of years the asset will be used
period = 3 # The specific year for which you want to calculate depreciation

depreciation = double_declining_balance_depreciation(cost, salvage_value, useful_life, period)
print(f"Depreciation in year {period}: ${depreciation:.2f}")

depreciation = straight_line_depreciation(cost, salvage_value, useful_life)
print(f"Annual Depreciation: ${depreciation:.2f}")

Output:
Depreciation in year 3: $1440.00
Annual Depreciation: $1600.00

These functions are useful for asset depreciation and tax planning.

4. Rate of Return Calculations

npf.mirr(values, finance_rate, reinvest_rate): Calculate the Modified Internal Rate of Return.
npf.xirr(values, dates): Determine the Internal Rate of Return for irregular cash flows.
import numpy_financial as npf
from pyxirr import xirr
import pandas as pd
from datetime import date

# Cash flows for the investment or project
cash_flows = [-1000, 200, 250, 300, 350]
dates = [date(2019, 1, 1),date(2020, 1, 1), date(2021, 1, 1), date(2022, 1, 1), date(2023, 1, 1)]

# Finance rate (cost of capital)
finance_rate = 0.1

# Reinvestment rate (rate at which positive cash flows are reinvested)
reinvest_rate = 0.12

# feed columnar data
xirr(dates, cash_flows)

# feed tuples
xirr(zip(dates, cash_flows))

# Calculate the Modified Internal Rate of Return (MIRR)
mirr = npf.mirr(cash_flows, finance_rate, reinvest_rate)

# Calculate the Internal Rate of Return (XIRR)
xirr = xirr(pd.DataFrame({"dates": dates, "cash_flows": cash_flows}))

print(f"The Modified Internal Rate of Return (MIRR) is: {mirr:.2%}")

print(f"The Internal Rate of Return (XIRR) is: {xirr:.2%}")

Output:
The Modified Internal Rate of Return (MIRR) is: 6.38%
The Internal Rate of Return (XIRR) is: 3.58%

These functions help you assess investment performance more accurately, especially when dealing with non-standard cash flow patterns.

Most of these functions are in use, using the excel functions, which you can efficiently use to calculate all the values we have discussed above. Using python give you much more flexibility and control over parameters. You can use these functions as base for Optimizing a more complex problem.

Do give this library a try and let me know in case you face any issue. Happy Learning !!!

Want to get in touch, you can follow me here.

--

--