ipywidgets
How to use Ipywidgets to visualize future value with different interest rates.
如何使用Ipywidgets可视化不同利率下的未来价值。
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:
如果您在银行帐户中投资100美元,该银行帐户的年利率为5%。 在第一年,您将获得0.05美元的利息$ 100 $ 5,您的投资价值将增加到$ 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 .
现在,可以使用python呈现上述相同的方程式。 我们可以使用未来价值公式或使用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:
numpy金融模块包含一个功能终值.fv(rate,nper,pmt,pv) ,它允许您像以前一样通过一些简单的参数来计算投资的终值:
rate: the rate of return of the investment
rate :投资回报率
nper: the lifespan of the investment
nper :投资寿命
pmt: the (fixed) payment at the beginning or end of each period (which is 0 in our example)
pmt :每个期间开始或结束时的(固定)付款(在我们的示例中为0)
pv: the present value of the investment
pv :投资的现值
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).
重要的是要注意,在此函数调用中,如果它表示负现金流量(现金支出),则必须将负值传递到pv参数中。
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.
然后,使用Numpy的.fv()函数,计算100美元的投资在2年内每年返回5%的终值。
# 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.
接下来,您将看到如何在同一张图中绘制以100美元投资的不同利率(0%,5%,10%和15%)。
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.
以速率使用函数np.linspace(0,0.15,num = 4)允许绘制4条曲线(num = 4),范围在0到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.
由于比率是从函数中绘制的,因此将图例写为数组是表示这四个比率的一种方法。
与Ipywidgets互动 (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.
查看利率对未来价值的影响的另一种方法是在数据中应用交互式工具。 Ipywidgets是一个使用界面(UI)控件以交互方式浏览代码和数据的库。
import ipywidgets as widgets
from IPython.display import display
%matplotlib inlinedef show_fv(rate):
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 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.
图2展示了使用库Ipywidgets的代码输出。 这是使用此工具并了解变量在结果中的影响的一种方式。
翻译自: https://medium.com/analytics-vidhya/future-values-and-ipywidgets-ce45e4d6a076
ipywidgets