Skip to content

Instantly share code, notes, and snippets.

@sugatoray
Last active January 18, 2019 18:58
Show Gist options
  • Save sugatoray/68c08b09b6732e26bbbc9612e0e56f96 to your computer and use it in GitHub Desktop.
Save sugatoray/68c08b09b6732e26bbbc9612e0e56f96 to your computer and use it in GitHub Desktop.
Future_Value_and_Loan_Installments.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Future_Value_and_Loan_Installments.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/sugatoray/68c08b09b6732e26bbbc9612e0e56f96/future_value_and_loan_installments.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "Y-m69dwWgrOT",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# The concept of Future Value and Equal Installments\n",
"\n",
"When you deposit some money at a bank, and the bank gives you some interest for your money, you will find your deposited-value has increased (appreciated) after some time. Thus the value of your asset (the deposit) appreciates (rises) over time. On the other hand, let's say you purchase a car today, and try selling it after using it a few years, you will find that your car is valued less than the initial purchase value. This is known as depreciation of the asset (your car) owing to its perceived wear and tear. Depending on the type of asset under consideration, its value may appreciate or depreciate over time. This introduces the concept of Future Value (FV) and Present Value (PV) of an asset. \n",
"\n",
"\n",
"**Note:** \n",
"+ **Appreciation** of an asset means **positive** interest rate\n",
"+ **Depreciation** of an asset means **negative** interest rate \n",
"\n",
"In the following sections we will first learn to derive the formula for a geometric progression (GP) series. Any Future Value problem can be essentially broken down into a set of GP series summations.\n",
"\n",
"1. [Derivation of the Sum of a Geometric Progression (GP) Series](#scrollTo=M95jb8cdQ3yb&line=41&uniqifier=1)\n",
"1. [Future Value Calculation](#scrollTo=uihax7199LgQ&line=8&uniqifier=1)\n",
" 1. [Compound Interest](#scrollTo=zJ0WMQHFlQSX&line=8&uniqifier=1)\n",
" 1. [Future Value of Equal Installments per Payment Cycle](h#scrollTo=PveJNE73lTpx&line=27&uniqifier=1)\n",
"1. [Equal Installment Calculation for a Loan](#scrollTo=YLwopVCQS-hm&line=3&uniqifier=1)\n",
" 1. [Installment Calculation when deposited at the beginning of interest cycle](#scrollTo=wK4Q3VMVVFaY&line=5&uniqifier=1)\n",
" 1. [Installment Calculation when deposited at the end of interest cycle](#scrollTo=8FVnxOn4asLT&line=26&uniqifier=1)\n",
" 1. [Relationship between Installment amounts when paid at the beginning and at the end of the payment cycles](#scrollTo=NJxdic8lfYC8)\n",
"1. [Define Formulas to Calculate Future Value and Equal Installment of a Loan](#scrollTo=RM0wGPdmmXnV)\n",
"\n",
"\n",
"## Author: Sugato Ray\n",
"Disclaimer: You are free to use this information in any of your work. In case you use this, please cite.\n",
"\n",
"\n",
"### Useful Links\n",
"[How to use Markdown in Juputer Notebook in Colab](https://colab.research.google.com/drive/1brSHFOnPQTo0-9vQRC6oslZjAGRbzWuY#scrollTo=_aEuh4U93gAm&forceEdit=true&offline=true&sandboxMode=true)"
]
},
{
"metadata": {
"id": "M95jb8cdQ3yb",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 1. Derivation of the Sum of a Geometric Progression (GP) Series \n",
"\n",
"Let $S$ be the sum of a GP series, given by: \n",
"\n",
"<table>\n",
"<tr><td>\n",
"$$S = a + aR + aR^2 + aR^3 + \\cdots + aR^{N-3} + aR^{N-2} + aR^{N-1}$$\n",
"</td></tr>\n",
"</table>\n",
"\n",
"Multiply the above equation by $R$ on both sides:\n",
"\n",
"<table>\n",
"<tr><td>\n",
"$$R\\cdot S = aR + aR^2 + aR^3 + \\cdots + aR^{N-2} + aR^{N-1} + aR^{N}$$\n",
"</td></tr>\n",
"</table>\n",
"\n",
"Now, subtract the _second_ equation from the _first_ one: \n",
"\n",
"<table>\n",
"<tr><td>\n",
"$$S(1 - R) = a + (aR - aR) + (aR^2 - aR^2) + \\cdots + (aR^{N-2} - aR^{N-2}) + (aR^{N-1} - aR^{N-1}) - aR^{N}$$ \n",
"</td></tr>\n",
"</table>\n",
"<br>\n",
"<table>\n",
"<tr><td>\n",
"$$ \\Rightarrow S(1 - R) = a - aR^N = a(1 - R^N)$$\n",
"</td></tr>\n",
"</table>\n",
"\n",
"Thus, the formula for GP series summation is given by: \n",
"<br>\n",
"<table>\n",
"<tr><td>\n",
"$$S = \\frac{a(1 - R^N)}{1 - R} \\space ; \\space when \\space R<1$$\n",
"<br>\n",
"$$S = \\frac{a(R^N - 1)}{R - 1} \\space ; \\space when \\space R>1$$\n",
"</td></tr>\n",
"</table>"
]
},
{
"metadata": {
"id": "uihax7199LgQ",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 2. Future Value Calculation\n",
"\n",
"Let a = principal deposited today, at the beginning of the first interest cycle.\n",
"\n",
"$a$ = principal/deposit \n",
"$r$ = interest percentage for each interest cycle \n",
"$N$ = total number of interest cycles\n"
]
},
{
"metadata": {
"id": "zJ0WMQHFlQSX",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Compound Interest\n",
"The Future Value ($FV$) of a one-time deposit ($a$) over $N$ interest cycles with interest $r$, as a result of _**compund interest**_ is: \n",
"\n",
"$$FV = a(1+r)^{N} = aR^{N}; \\space where, R = 1+r$$\n",
"\n",
"The total compound interest ($CI$) is given by: \n",
"\n",
"$$CI = aR^N - a = a(R^N - 1)$$"
]
},
{
"metadata": {
"id": "PveJNE73lTpx",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Future Value of Equal Installments per Payment Cycle\n",
"\n",
"Let $a$ be the amount of installment deposited at the beginning of every interest cycle, with interest rate $r$ over a total number of $N$ interest cycles.\n",
"\n",
">| Interest Cycle | Installment | Total Interest Cycles till End | Future Value of Installment (FVI) |\n",
">|:-----:|:--------:|:--------:|:------:|\n",
">| $1$ | $a$ | $N$ | $aR^{N}$\n",
">| $2$ | $a$ | $N-1$ | $aR^{(N-1)}$\n",
">| $3$ | $a$ | $N-2$ | $aR^{(N-2)}$\n",
">| $4$ | $a$ | $N-3$ | $aR^{(N-3)}$\n",
">| $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$\n",
">| $j$ | $a$ | $N-(j-1)$ | $aR^{(N-j+1)}$\n",
">| $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$\n",
">| $N-1$ | $a$ | $2$ | $aR^{2}$\n",
">| $N$ | $a$ | $1$ | $aR^{1}$\n",
">|===============|==============|==========================|==========================\n",
">| **Total** | | | $FV$\n",
"\n",
"$$FV = aR(1+R+R^{2}+ \\cdots + R^{N-1}) = aR \\left( \\frac{R^{N}-1}{R-1} \\right); \\space where, R = 1+r$$\n",
"\n",
"\n",
"<table>\n",
"<tr><td>\n",
"$$ \\Rightarrow FV = aR \\left( \\frac{R^{N}-1}{R-1} \\right); \\space where, R = (1+r) > 1$$\n",
"</td></tr>\n",
"</table>\n"
]
},
{
"metadata": {
"id": "YLwopVCQS-hm",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 3. Equal Installment Calculation for a Loan\n",
"\n",
"Let $P$ be the amount of loan disbursed over a total of $N$ installment-payment cycles, with an interest rate ($r$) per each payment cycle. Additionally, let us assume that the equal installment of $a$, is deposited at the \n",
">(a) beginning of each interest cycle \n",
"(b) end of each interest cycle \n",
"\n"
]
},
{
"metadata": {
"id": "wK4Q3VMVVFaY",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Installment Calculation when deposited at the _beginning_ of interest cycle: for case (a)\n",
"\n",
"The installments (each of value $a$) are deposited at the **beginning** of each payment cycle. The following table shows how the total outstanding loan (TOL) is calculated at the end of each cycle.\n",
"\n",
">| Interest Cycle | Installment | Total Interest Cycles till End | Total Outstanding Loan (TOL) at the end of the cycle | Total Outstanding Loan in Simplified Format\n",
">|:-----:|:--------:|:--------:|:------:|:------:|\n",
">| $1$ | $a$ | $N$ | $TOL_{1} = (P - a)R$ | $PR - aR$ \n",
">| $2$ | $a$ | $N-1$ | $TOL_{2} = ((P - a)R - a)R$ | $PR^2 - aR(1 + R) $\n",
">| $3$ | $a$ | $N-2$ | $TOL_{3} = (((P - a)R - a)R - a)R$ | $PR^3 - aR(1 + R + R^2) $\n",
">| $4$ | $a$ | $N-3$ | $TOL_{4}$ | $PR^4 - aR(1 + R + R^2 + R^3) $\n",
">| $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$\n",
">| $j$ | $a$ | $N-(j-1)$ | $TOL_{j}$ | $PR^j - aR(1 + R + R^2 + \\cdots + R^j) $\n",
">| $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$\n",
">| $N$ | $a$ | $1$ | $TOL_{N}$ | $PR^{N} - aR(1 + R + R^2 + \\cdots + R^{N}) $\n",
"\n",
"Clearly, at the end of the interest cycles, the entire loan amount should be paid. Thus, the following identity must be true: \n",
"\n",
"$$PR^{N} - aR(1 + R + R^2 + \\cdots + R^{N}) = 0$$\n",
"\n",
"Solve this equation to get the intended value of the equal installment $a$: \n",
"\n",
"<table>\n",
"<tr><td>\n",
"$$a_{begin} = \\frac{PR^{N}}{R(1 + R + R^2 + \\cdots + R^{N})} = P \\left( \\frac{R^{(N-1)}(R - 1)}{R^{N} - 1} \\right)$$ \n",
"</td></tr> \n",
"</table>"
]
},
{
"metadata": {
"id": "8FVnxOn4asLT",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Installment Calculation when deposited at the _END_ of interest cycle: for case (b)\n",
"\n",
"The installments (each of value $a$) are deposited at the **end** of each payment cycle. The following table shows how the total outstanding loan (TOL) is calculated at the end of each cycle.\n",
"\n",
">| Interest Cycle | Installment | Total Interest Cycles till End | Total Outstanding Loan (TOL) at the end of the cycle | Total Outstanding Loan in Simplified Format\n",
">|:-----:|:--------:|:--------:|:------:|:------:|\n",
">| $1$ | $a$ | $N$ | $TOL_{1} = PR - a$ | $PR - a$ \n",
">| $2$ | $a$ | $N-1$ | $TOL_{2} = (PR - a)R - a$ | $PR^2 - a(1 + R) $\n",
">| $3$ | $a$ | $N-2$ | $TOL_{3} = aR^{(N-2)}$ | $PR^3 - a(1 + R + R^2) $\n",
">| $4$ | $a$ | $N-3$ | $TOL_{4}$ | $PR^4 - a(1 + R + R^2 + R^3) $\n",
">| $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$\n",
">| $j$ | $a$ | $N-(j-1)$ | $TOL_{j}$ | $PR^j - a(1 + R + R^2 + \\cdots + R^j) $\n",
">| $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$ | $\\vdots$\n",
">| $N$ | $a$ | $1$ | $TOL_{N}$ | $PR^{N} - a(1 + R + R^2 + \\cdots + R^{N}) $\n",
"\n",
"Clearly, at the end of the interest cycles, the entire loan amount should be paid. Thus, the following identity must be true: \n",
"\n",
"$$PR^{N} - a(1 + R + R^2 + \\cdots + R^{N}) = 0$$\n",
"\n",
"Solve this equation to get the intended value of the equal installment $a$: \n",
"\n",
"<table>\n",
"<tr><td>\n",
"$$a_{end} = \\frac{PR^{N}}{(1 + R + R^2 + \\cdots + R^{N})} = P \\left( \\frac{R^{N}(R - 1)}{R^{N} - 1} \\right)$$ \n",
"</td></tr> \n",
"</table>"
]
},
{
"metadata": {
"id": "NJxdic8lfYC8",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Relationship between Installment amounts when paid at the beginning and at the end of the payment cycles\n",
"\n",
"\n",
"<table>\n",
"<tr><td>\n",
"$$a_{end} = a_{begin}R = P \\left( \\frac{R^{N}(R - 1)}{R^{N} - 1} \\right)$$ \n",
"</td></tr> \n",
"</table>"
]
},
{
"metadata": {
"id": "RM0wGPdmmXnV",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 4. Define Formulas to Calculate Future Value and Equal Installment of a Loan\n",
"\n",
"Here we will import some necessary packages for future use and define some custom functions for evaluating future value and loan-installment."
]
},
{
"metadata": {
"id": "8w7sSa0c-J0b",
"colab_type": "code",
"cellView": "form",
"colab": {}
},
"cell_type": "code",
"source": [
"#@title Import necessary packages\n",
"import numpy as np\n",
"import pandas as pd"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "e58Irg0fnMZM",
"colab_type": "code",
"cellView": "form",
"colab": {}
},
"cell_type": "code",
"source": [
"#@title User Defined Functions: `get_future_value()` and `get_equal_installment()`\n",
"def get_future_value(interest_rate, total_cycles, deposit_amount):\n",
" if interest_rate > 1:\n",
" interest_rate = interest_rate/100\n",
" R = 1 + interest_rate\n",
" N = total_cycles\n",
" if R==1:\n",
" FV = deposit_amount\n",
" else:\n",
" FV = deposit_amount*R*(R**N - 1)/(R - 1)\n",
" \n",
" total_interest = FV - deposit_amount\n",
" \n",
" return (FV, total_interest)\n",
"\n",
"\n",
"def get_equal_installment(interest_rate, total_cycles, loan_amount, paid_at_cycle_end = True):\n",
" # for installments paid at the beginning of the cycles, set \n",
" # paid_at_cycle_end = False\n",
" if interest_rate > 1:\n",
" interest_rate = interest_rate/100\n",
" R = 1 + interest_rate\n",
" N = total_cycles\n",
" if R==1:\n",
" installment_amount = loan_amount/N\n",
" else:\n",
" installment_amount = loan_amount*(R**N)*(R - 1)/(R**N - 1)\n",
" if not paid_at_cycle_end:\n",
" installment_amount = installment_amount/R\n",
" \n",
" return installment_amount "
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "S6KtmTWruVI6",
"colab_type": "code",
"cellView": "form",
"outputId": "36c0e376-0b20-4b77-e334-dced6e7ed21e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 211
}
},
"cell_type": "code",
"source": [
"#@title Calculate Future Value and Total Interest { run: \"auto\", vertical-output: true }\n",
"user_annual_interest_rate_percentage = 12 #@param {type:\"number\"}\n",
"user_total_cycles = 60 #@param {type:\"integer\"}\n",
"user_deposit_amount = 100 #@param {type:\"number\"}\n",
"\n",
"annual_interest_rate = user_annual_interest_rate_percentage/100\n",
"interest_rate = annual_interest_rate/12\n",
"total_cycles = user_total_cycles\n",
"deposit_amount = user_deposit_amount\n",
"\n",
"FV, total_interest = get_future_value(interest_rate, total_cycles, deposit_amount)\n",
"\n",
"print(\"\\n\" + \"USER INPUT: \\n\")\n",
"print(\"\\t\" + \"Annual Interest Rate: {} %\".format(np.round(annual_interest_rate*100,2)))\n",
"print(\"\\t\" + \"Total Cycles (months): {}\".format(total_cycles))\n",
"print(\"\\t\" + \"Deposit Amount: ${}\".format(np.round(deposit_amount,2)))\n",
"print(\"\\n\" + \"FUNCTION OUTPUT: \\n\")\n",
"print(\"\\t\" + \"Future Value: ${}\".format(np.round(FV, 2)))\n",
"print(\"\\t\" + \"Total Interest: ${}\".format(np.round(total_interest, 2)))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"USER INPUT: \n",
"\n",
"\tAnnual Interest Rate: 12.0 %\n",
"\tTotal Cycles (months): 60\n",
"\tDeposit Amount: $100\n",
"\n",
"FUNCTION OUTPUT: \n",
"\n",
"\tFuture Value: $8248.64\n",
"\tTotal Interest: $8148.64\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "4qp6JSpQtl8n",
"colab_type": "code",
"cellView": "form",
"outputId": "54293f74-a8e7-46de-9515-60dfbad9692e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 211
}
},
"cell_type": "code",
"source": [
"#@title Calculate Installment Amount (if paid at the beginning or at the end of each cycle) { run: \"auto\", vertical-output: true }\n",
"user_annual_loan_interest_rate_percentage = 2.2 #@param {type:\"number\"}\n",
"user_total_loan_payment_cycles = 60 #@param {type:\"number\"}\n",
"user_loan_amount = 8000 #@param {type:\"number\"}\n",
"\n",
"\n",
"annual_interest_rate = user_annual_loan_interest_rate_percentage/100\n",
"interest_rate = annual_interest_rate/12\n",
"if user_total_loan_payment_cycles > 0:\n",
" total_cycles = user_total_loan_payment_cycles\n",
"else:\n",
" total_cycles = 60\n",
"if user_loan_amount > 0:\n",
" loan_amount = user_loan_amount\n",
"else:\n",
" loan_amount = 8000\n",
"\n",
"installment_amount_end = get_equal_installment(interest_rate, total_cycles, loan_amount, True)\n",
"installment_amount_begin = get_equal_installment(interest_rate, total_cycles, loan_amount, False)\n",
"\n",
"[installment_amount_end, installment_amount_begin]\n",
"\n",
"print(\"\\n\" + \"USER INPUT: \\n\")\n",
"print(\"\\t\" + \"Annual Interest Rate: {} %\".format(np.round(annual_interest_rate*100,2)))\n",
"print(\"\\t\" + \"Total Cycles (months): {}\".format(total_cycles))\n",
"print(\"\\t\" + \"Loan Amount: ${}\".format(np.round(loan_amount,2)))\n",
"print(\"\\n\" + \"FUNCTION OUTPUT: Installment Value\\n\")\n",
"print(\"\\t\" + \"If paid at the end of each cycle, \\t\\t\\t\" + \"a_end: ${}\".format(np.round(installment_amount_end, 2)))\n",
"print(\"\\t\" + \"If paid at the beginning of each cycle, \\t\\t\" + \"a_begin: ${}\".format(np.round(installment_amount_begin, 2)))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"USER INPUT: \n",
"\n",
"\tAnnual Interest Rate: 2.2 %\n",
"\tTotal Cycles (months): 60\n",
"\tLoan Amount: $8000\n",
"\n",
"FUNCTION OUTPUT: Installment Value\n",
"\n",
"\tIf paid at the end of each cycle, \t\t\ta_end: $140.92\n",
"\tIf paid at the beginning of each cycle, \t\ta_begin: $140.67\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment