Basic Financial Calculations using Python
This article was published as a part of the Data Science Blogathon
Python has become immensely popular these days across many fields like web development, machine learning, data analytics, finance, deep learning, scientific calculations, and so on.
Python is used in so many fields these days that knowing how to write programs in Python is very essential. These days, Python is also an important tool in the financial world. One can easily complete many financial calculations using Python. The simple nature of python and easy-to-learn functions and code structure makes it easy for anyone with basic programming and math knowledge to start with python.
(Image Source: https://www.pexels.com/photo/blue-and-yellow-graph-on-stock-market-monitor-159888/)
Python has been around for a long time and recently, it has gained high popularity. The code is very concise in Python and simple to write. So, people working in Finance, Business, or Analytics need not focus on the technicalities of programming that much and focus on the implementation. Python has a lot of libraries that help in various types of mathematical computations. All these make Python the ideal programming language for financial calculations.
Let us implement some of the basic financial calculations in Python.
Simple interest is something which everyone has studied in school. It is used to calculate the interest accrued on a loan or a savings deposit. Suppose, you deposit some amount in your bank, and want to calculate what will be the amount your bank will finally give you after adding interest, we can calculate that using simple interest.
To calculate the simple interest we need three things, the principal amount, rate of interest, and time period. For the implementation here, we denote the principal as ‘p’, rate as ‘r’, and time as ‘t’.
The function is implemented in the following way. A point to be noted is that here we are considering yearly interest, so the time and rate are taken yearly.
The final amount and simple interest are printed as required.
The money earned from simple interest does not have compounding.
In the case of compound interest, the principle of compounding is used. The compound interest formula calculates the interest earned on the amount where interest from the previous investment cycle is also added as principal. As the interest earned is reinvested, the investment will earn more money due to compounding.
The interest on the original balance would only count as simple interest, but as the additional amount is also added, we get CI.
The code implementation is simple. The point to be noted here is that the rate is yearly and time is also taken in years.
#compound interest def compound_interest(p,r,t): print('Amount: ', p) print("Rate of Interest (Per Annum)", r) print("Time (In Years): ",t) a= p*((1+r/100)**t) ci= a-p print("Final Amount: ", a) print("Compound Interest: ", ci)
Now, calling the function.
The output is as follows. The code executes without any problems.
The output is as expected.
We all have heard about inflation in some way or the other. Be it our mother complaining about how the prices of groceries and vegetables are increasing, or be it the fact that bus and taxi fares have increased. Inflation reduces purchasing power. The same 100 INR we have today was more valuable 10 years ago, and similarly, the same 100 INR will be less valuable 10 years from now. Inflation reduces the purchasing power of money.
To calculate the net purchasing of an amount, we need the inflation rate and the time period. The rates are taken yearly here and the time is also yearly. Calculation of purchasing power can help us compare it against the investment to see if we are gaining money on the investment.
The code is simple to implement.
# Purchasing power def pur_power(p,r,t): print("Initial Amount: ",p) print("Annual Inflation Rate: ",r) print("Time in years:", t) a= p* ((100/(100+r))**t) print("Final amount after",t,"years of inflation: ", a)
Now, we call the function with data.
So, we can see that by assuming a 5% inflation rate, 10,000 INR after 10 years will be around 6100 in today’s terms.
Compound Annual Growth Rate
We often invest in mutual funds and other bonds, where the interest rates vary with time. And people usually invest in them for a long time. The usual investment in bonds and mutual funds is for a period of 5 years or more. But, as the rates vary, the calculation of interest rates can be difficult. The compound annual growth rate formula helps in such cases to calculate the annual growth rate.
To calculate CAGR, we need the initial amount, final amount, and the time period in years. The code implementation is simple. CAGR calculation helps us in calculating the performance of a fund or bond in comparison to other investment options.
The code is implemented as shown.
#compound annual growth rate def CAGR(p,a, t ): print("Initial value of money: ",p) print('Final value of money: ',a) print("Time in years: ", t) t_inv=1/t cagr_rate= (((a/p)**t_inv) -1)*100 print("Compound Annual Growth Rate (CAGR): ", cagr_rate)
Now, we call the function.
So, we can see that by providing the initial and final value of money and the time, we get the CAGR rate.
We all have heard about loans and EMIs etc. People take loans for various reasons, like education, house construction, purchase of car, property etc. The payments on loans are usually made on a monthly basis.
EMI stands for Equated Monthly Installment. It is the fixed amount to be paid by the borrower to the lender each month to complete the repayment of the loan. The benefit for EMI for borrowers is that they will be knowing exactly how much they have to pay back to the lender each month. The lender can be your bank or any other financial company.
The EMI is dependent on various factors like the principal borrowed, rate of interest, tenure of loan, etc. Let us look at the implementation of the calculation of monthly EMI. EMIs are adjusted in such a way that the borrower is able to pay back the amount per month without any delay or problem.
The code is easy and simple to implement.
#monthly EMI calculation def EMI(p,r,n): print("Total Loan Amount: ",p) print("Rate of Interest: ",r) print("Number of installments: ",n) r_mon= r/(12*100) emi = p * r_mon * ((1+r_mon)**n)/((1+r_mon)**n - 1) print("EMI for",n,"months: ",emi )
Now, we call the function.
The monthly EMI is calculated.
Double Time Calculation
The doubling time formula is used in finance to calculate how much time it will require to double our investment based on the interest rate. It is a very important metric to determine if an investment is worth the effort and time. The doubling time formula takes only the interest rate as an input. The logarithmic function is used for the calculation. Often, many bonds and funds are invested till they are doubled. The funds/ bonds have a CAGR rate associated with them. With the rate, we can calculate the doubling time. The code is easy and simple.
#doubling time calculation def double(r): print("Rate of Interest: ",r) t= math.log(2)/math.log(1+ (r/100)) print("Time taken in years to double money at",r,"percent PA: ", t)
Let us call the function.
So, we can see that for an 8% interest rate, money will be doubled in roughly 9 years.
The weighted average is used to calculate the average of a set of numbers with each having its different weights. The ratio of the importance of each value is called its weight. All weights should add up to be 1 or 100%.
The concept of the weighted average is used in mathematical calculations a lot. In finance, it has many applications. One of the most important applications is the weighted average cost of capital.
Let us implement the WA calculation. The rates taken are the various items. The rates can be assumed to be rates of interest of various investments a person holds. The ratio of each investment principal is in the ratio list. The WA formula gives the net rate of interest.
The code is as follows.
ratio=[0.20, 0.25, 0.35,0.10, 0.10] rates=[7.5, 8.5, 8, 5, 6] def weighted_average(ratio,rates): wa=0 for i in range(len(ratio)): wa= wa+ ratio[i]*rates[i] print("Weighted Average returns: ",wa)
Now, we call the function.
So, we get the output.
To check the code, follow this link.
Analytics | Content Creation
Connect with me on Linkedin.
My other articles on Analytics Vidhya: Link.