Skip to content

Commit

Permalink
Updated car loan to have shorter variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
mGalarnyk committed Dec 7, 2018
1 parent 5db8951 commit 6edf45a
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 185 deletions.
161 changes: 67 additions & 94 deletions Finance/.ipynb_checkpoints/car_loans-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,29 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# California sales tax 7.5%\n",
"P = 31115 * (1.075)\n",
"\n",
"r = 0.0702 / 12\n",
"n = 60"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"662.638532377825"
"662.64"
]
},
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"P = 31115 * (1.075)\n",
"r = 0.0702 / 12\n",
"n = 60\n",
"numerator = (r *((1 + r)**(n)) )\n",
"denominator = ((1 + r)**(n)) - 1\n",
"\n",
"emi = P * (numerator / denominator)\n",
"emi"
"np.round(emi,2)"
]
},
{
Expand All @@ -80,37 +67,35 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"687.23"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Price + Tax + Emissions + Registration + Plate + Documentation\n",
"P = 31115 + (32615 * 0.0975) + 50 + 200 + 65 + 80\n",
"\n",
"r = 0.0702 / 12\n",
"n = 60"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"n = 60\n",
"numerator = (r *((1 + r)**(n)) )\n",
"denominator = ((1 + r)**(n)) - 1\n",
"\n",
"emi = P * (numerator / denominator)\n",
"emi = np.round(emi, 2)"
"np.round(emi,2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"metadata": {
"collapsed": false
},
Expand All @@ -121,7 +106,7 @@
"'The Monthly Payment with fees included is 24.59 higher'"
]
},
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -155,7 +140,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"metadata": {
"collapsed": false
},
Expand All @@ -166,7 +151,7 @@
"202.93628062500002"
]
},
"execution_count": 7,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -188,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 6,
"metadata": {
"collapsed": false
},
Expand All @@ -199,7 +184,7 @@
"34205.6725"
]
},
"execution_count": 8,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -217,7 +202,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 7,
"metadata": {
"collapsed": false
},
Expand All @@ -229,46 +214,34 @@
"term = 60\n",
"P = 34689.96\n",
"\n",
"def calc_interest(P, emi, interest_rate = 0.0702):\n",
" \n",
" # Assuming no fractional interst on a loan \n",
" interest_paid = np.floor( ((interest_rate / 12) * P ) * 100) / 100\n",
" \n",
" principal_paid = np.round(emi - interest_paid, 2)\n",
" \n",
"def calc_interest(P,emi,interest_rate = 0.0702):\n",
" interest_paid = np.floor(((interest_rate/12)*P)*100)/100\n",
" principal_paid = np.round(emi-interest_paid, 2)\n",
" new_balance = np.round(P - principal_paid,2)\n",
" \n",
" return(emi, interest_paid, principal_paid, new_balance)\n",
"\n",
"payment_list = []\n",
"for month in range(1, term + 1):\n",
"\n",
" emi, interest_paid, principal_paid, new_balance = calc_interest(P, emi)\n",
"\n",
" payment_list.append([month, P, emi, interest_paid, principal_paid, new_balance])\n",
"\n",
" P = np.round(new_balance,2)"
"for n in range(1, term + 1):\n",
" emi,i_paid,p_paid,new_p = calc_interest(P, emi)\n",
" payment_list.append([n, P, emi, i_paid, p_paid, new_p])\n",
" P = np.round(new_p,2)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"payment_table = pd.DataFrame(payment_list, columns = ['Month',\n",
" 'Starting Balance',\n",
" 'Repayment',\n",
" 'Interest Paid',\n",
" 'Principal Paid',\n",
" 'New Balance'])"
"c_names = ['Month','Starting Balance','Repayment','Interest Paid','Principal Paid','New Balance']\n",
"payment_table = pd.DataFrame(payment_list, columns = c_names)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 9,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -307,7 +280,7 @@
" <th>0</th>\n",
" <td>1</td>\n",
" <td>34689.96</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>202.93</td>\n",
" <td>484.30</td>\n",
" <td>34205.66</td>\n",
Expand All @@ -316,7 +289,7 @@
" <th>1</th>\n",
" <td>2</td>\n",
" <td>34205.66</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>200.10</td>\n",
" <td>487.13</td>\n",
" <td>33718.53</td>\n",
Expand All @@ -325,7 +298,7 @@
" <th>2</th>\n",
" <td>3</td>\n",
" <td>33718.53</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>197.25</td>\n",
" <td>489.98</td>\n",
" <td>33228.55</td>\n",
Expand All @@ -334,7 +307,7 @@
" <th>3</th>\n",
" <td>4</td>\n",
" <td>33228.55</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>194.38</td>\n",
" <td>492.85</td>\n",
" <td>32735.70</td>\n",
Expand All @@ -343,7 +316,7 @@
" <th>4</th>\n",
" <td>5</td>\n",
" <td>32735.70</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>191.50</td>\n",
" <td>495.73</td>\n",
" <td>32239.97</td>\n",
Expand All @@ -352,7 +325,7 @@
" <th>5</th>\n",
" <td>6</td>\n",
" <td>32239.97</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>188.60</td>\n",
" <td>498.63</td>\n",
" <td>31741.34</td>\n",
Expand All @@ -361,7 +334,7 @@
" <th>6</th>\n",
" <td>7</td>\n",
" <td>31741.34</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>185.68</td>\n",
" <td>501.55</td>\n",
" <td>31239.79</td>\n",
Expand All @@ -370,7 +343,7 @@
" <th>7</th>\n",
" <td>8</td>\n",
" <td>31239.79</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>182.75</td>\n",
" <td>504.48</td>\n",
" <td>30735.31</td>\n",
Expand All @@ -379,7 +352,7 @@
" <th>8</th>\n",
" <td>9</td>\n",
" <td>30735.31</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>179.80</td>\n",
" <td>507.43</td>\n",
" <td>30227.88</td>\n",
Expand All @@ -388,7 +361,7 @@
" <th>9</th>\n",
" <td>10</td>\n",
" <td>30227.88</td>\n",
" <td>687.23</td>\n",
" <td>687.230218</td>\n",
" <td>176.83</td>\n",
" <td>510.40</td>\n",
" <td>29717.48</td>\n",
Expand All @@ -398,17 +371,17 @@
"</div>"
],
"text/plain": [
" Month Starting Balance Repayment Interest Paid Principal Paid \\\n",
"0 1 34689.96 687.23 202.93 484.30 \n",
"1 2 34205.66 687.23 200.10 487.13 \n",
"2 3 33718.53 687.23 197.25 489.98 \n",
"3 4 33228.55 687.23 194.38 492.85 \n",
"4 5 32735.70 687.23 191.50 495.73 \n",
"5 6 32239.97 687.23 188.60 498.63 \n",
"6 7 31741.34 687.23 185.68 501.55 \n",
"7 8 31239.79 687.23 182.75 504.48 \n",
"8 9 30735.31 687.23 179.80 507.43 \n",
"9 10 30227.88 687.23 176.83 510.40 \n",
" Month Starting Balance Repayment Interest Paid Principal Paid \\\n",
"0 1 34689.96 687.230218 202.93 484.30 \n",
"1 2 34205.66 687.230218 200.10 487.13 \n",
"2 3 33718.53 687.230218 197.25 489.98 \n",
"3 4 33228.55 687.230218 194.38 492.85 \n",
"4 5 32735.70 687.230218 191.50 495.73 \n",
"5 6 32239.97 687.230218 188.60 498.63 \n",
"6 7 31741.34 687.230218 185.68 501.55 \n",
"7 8 31239.79 687.230218 182.75 504.48 \n",
"8 9 30735.31 687.230218 179.80 507.43 \n",
"9 10 30227.88 687.230218 176.83 510.40 \n",
"\n",
" New Balance \n",
"0 34205.66 \n",
Expand All @@ -423,7 +396,7 @@
"9 29717.48 "
]
},
"execution_count": 11,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -470,7 +443,7 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 13,
"metadata": {
"collapsed": false,
"scrolled": true
Expand Down Expand Up @@ -537,7 +510,7 @@
"P = 34689.96\n",
"term = 60\n",
"\n",
"def generate_loan_table(P, term, interest_rate = 0.0702):\n",
"def generate_loan_table(P, term, interest_rate=0.0702):\n",
"\n",
" def calc_emi(P, n, interest_rate):\n",
" r = interest_rate / 12\n",
Expand All @@ -557,9 +530,9 @@
" emi = calc_emi(P, term, interest_rate)\n",
" payment_list = []\n",
" \n",
" for month in range(1, term + 1):\n",
" emi, interest_paid, principal_paid, new_balance = calc_interest(P, emi, interest_rate)\n",
" payment_list.append([month, P, emi, interest_paid, principal_paid, new_balance])\n",
" for n in range(1, term + 1):\n",
" emi,i_paid,p_paid, new_p = calc_interest(P, emi, interest_rate)\n",
" payment_list.append([n, P, emi, i_paid, p_paid, new_p])\n",
" P = np.round(new_balance,2)\n",
" \n",
" payment_table = pd.DataFrame(payment_list, columns = ['Month',\n",
Expand Down
Loading

0 comments on commit 6edf45a

Please sign in to comment.