{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Investment comparison calculator\n", "\n", "## Created by Nancy Aggarwal on Jul 12, 2020" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from scipy.optimize import minimize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pay regular premium, gain fixed interest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Frequency of Compounding interest = frequency of payments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Say one pays an amount $x$ regularly for $n$ time-intervals. Say one wants to get one's investement out after $N$ intervals ($N>=n$). Say the compound rate of interest (calculated at the same frequency) is $i$.\n", "\n", "Now, the amount after $n$ intervals is:\n", "\n", "$Y_n = x(1+i)\\sum_{j=0}^{n-1}(1+i)^j$\n", "\n", "Finally, the return after $N$ intervals is:\n", "\n", "$Z_N = Y_n*(1+i)^{N-n}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Frequency of compounding interest > frequency of payments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Say one pays an amount $x$ regularly for $n$ time-intervals. Say one wants to get one's investement out after $N$ intervals ($N>=n$). Say the compound rate of interest (calculated at the a frequency m) is $i$.\n", "\n", "Now, the amount after $n$ intervals is:\n", "\n", "$Y_n = x(1+i)^m\\sum_{j=0}^{n-1}(1+i)^{mj}$\n", "\n", "Finally, the return after $N$ intervals is:\n", "\n", "$Z_N = Y_n*(1+i)^{m(N-n)}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Implementation" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def calcTotalReturn(i,x,n,N,m):\n", " # n is years of premium in some time unit (say year or quarter)\n", " # N is years of maturity in the same time uint\n", " # i is interest in percent per that time unit\n", " # x is premium per that time unit\n", " # m is the number of times interest is compounded between two payments\n", "# print(\"interest = {}, premium = {}, years of premium = {}, years of maturity = {}\".format(i,x,n,N))\n", " i = i/100\n", " seriesarray = [(1+i)**(j*m) for j in range(n)]\n", " Yn = x*((1+i)**m)*sum(seriesarray)\n", " ZN = Yn*(1+i)**(m*(N-n))\n", " return ZN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example\n", "\n", "$8\\%$ yearly interest, premium of every 6 months at the rate of 55/yr for 16 years, interest compounded every month. Policy matured after 25 years." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "intervalfactor = 2 #(convert years to semesters)\n", "compoundingfactor = 6 #months in a semester\n", "\n", "IntervalsOfPremium = 16*intervalfactor\n", "IntervalsOfMaturity = 25*intervalfactor\n", "RateofInterest = 8/(intervalfactor*compoundingfactor) #percent\n", "IntervalPremium = 55/intervalfactor" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3722.659639968529" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calcTotalReturn(RateofInterest,IntervalPremium,IntervalsOfPremium,IntervalsOfMaturity,compoundingfactor)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Now back-calculate interest given final sum" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "FinalSum = 75*25 + 7.5e2 + 1e3" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3625.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FinalSum" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def err(i,tup):\n", " Model = calcTotalReturn(i[0],tup[0],tup[1],tup[2],tup[3])\n", " Meas = tup[4]\n", " error = abs((Model-Meas)/Meas)\n", "# print(i,Model,Meas,error)\n", " return error" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "minimizeResult=minimize(err,1,[IntervalPremium,IntervalsOfPremium,IntervalsOfMaturity,compoundingfactor,FinalSum],method=\"Powell\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "inferredInterest=minimizeResult.x*intervalfactor*compoundingfactor" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.858347053111679" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inferredInterest" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }