Future Values and Ipywidgets

Cristiane Silva
Analytics Vidhya
Published in
3 min readAug 21, 2020

--

How to use Ipywidgets to visualize future value with different interest rates.

There are some calculations that even being easy becoming better with a visualization of his terms. Moreover, the sooner we start investing the more gains we will have in the future. Of course, there are several other variables in the equation of “the game of saving resources for the future” but now two variables will be presented here: interest rate and time.

Future Values

The most basic principle of finance is: a dollar today is worth more than a dollar tomorrow. That means money has a time value.

If you invest $100 in a bank account that pays interest of rate 5% a year. In the first year, you will earn interest of 0.05 $100 $5 and the value of your investment will grow to $105:

And if we can “see” this?

Now the same equation above can be presented using python. We can use the future value formula or utilize the library Numpy Financial .

The numpy financial module contains a function future value, .fv(rate, nper, pmt, pv), which allows you to calculate the future value of an investment as before with a few simple parameters:

  • rate: the rate of return of the investment
  • nper: the lifespan of the investment
  • pmt: the (fixed) payment at the beginning or end of each period (which is 0 in our example)
  • pv: the present value of the investment

It is important to note that in this function call, you must pass a negative value into the pv parameter if it represents a negative cash flow (cash going out).

First, we must import the libraries:

# Importing the libraries
import numpy as np
import numpy_financial as npf
import matplotlib.pyplot as plt

Then, use Numpy’s .fv() function, calculate the future value of a $100 investment returning 5% per year for 2 years.

# Calculate investment
investment = npf.fv(rate=.05, nper=2, pmt=0, pv=-100)
print("$" + str(round(investment, 2)))
$110.25

The higher the interest rate, the faster your savings will grow

Next, you’ll see how plot different interest rates (0%, 5%, 10%, and 15%) with an investment of $100 in the same graph.

plt.figure(figsize=(10,8))
y = [npf.fv(rate=np.linspace(0,0.15,num=4), nper=i, pmt=0, pv=-100) for i in range(21)]

Using the function np.linspace(0, 0.15 , num=4) at rate allows plot 4 curves(num=4), in a range between 0 and 0.15.

plt.plot(y)plt.legend(["r = 0%", "r = 5%","r = 10%" , "r = 15%"])plt.ylabel('Future value of $100, dollars')
plt.xlabel('years')

As the rates are plotted from a function, to write the legend as an array is a way to present the four rates.

Figure 1 — Growth of an investment at different interest rates

Interact with Ipywidgets

Another way to see the impact of interest rate in your future value is by applying an interactive tool in your data. Ipywidgets is a library that uses interface (UI) controls for exploring code and data interactively.

import ipywidgets as widgets
from IPython.display import display
%matplotlib inline
def show_fv(rate):
plt.figure(figsize=(10,8))
y = [npf.fv(rate, nper=i, pmt=0, pv=-100) for i in range(21)]
plt.plot(y)plt.ylabel('Future value of $100, dollars')
plt.xlabel('years')

controls = widgets.interactive(show_fv,rate=(0, .20, .01))
display(controls)

The result is the graph interactive below:

Figure 2 — The graph shows different Future values according to the interest rates

Figure 2 presents the output of the code using the library Ipywidgets. This is a way to use this tool and know at the time the influence of a variable in your results.

--

--

Cristiane Silva
Analytics Vidhya

Engineer, MBA in Finance & Investment, and Data Scientist who contributes code to the community. linkedin.com/in/ssilvacris/