Created
August 19, 2022 17:37
-
-
Save jmhummel/c8efb18c8c687e415893b55b395b7914 to your computer and use it in GitHub Desktop.
Riddler express 2022-08-19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "c4fdd441", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from sympy.solvers import solve\n", | |
"from sympy import *" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a8185c7f", | |
"metadata": {}, | |
"source": [ | |
"`r` is the interest rate (as a percentage) we are solving for" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"id": "90b7bc90", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"r = Symbol('r')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d970d768", | |
"metadata": {}, | |
"source": [ | |
"We substitute `72/r` for `t` in the exponential growth equation. On the right side, we have 2, so we subtract it (SymPy supposes all equations are equaled to 0)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"id": "290f5161", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\left(\\frac{r}{100} + 1\\right)^{\\frac{72}{r}} - 2$" | |
], | |
"text/plain": [ | |
"(r/100 + 1)**(72/r) - 2" | |
] | |
}, | |
"execution_count": 26, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"eq = (1+r/100)**(72/r)-2\n", | |
"eq" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0c66d352", | |
"metadata": {}, | |
"source": [ | |
"Solving yields a list containing a single solution" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"id": "acf6d1c8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[-100 - 72*LambertW(-25*2**(11/18)*log(2)/72, -1)/log(2)]\n" | |
] | |
} | |
], | |
"source": [ | |
"solutions = solve(eq, r)\n", | |
"print(solutions)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "64460f55", | |
"metadata": {}, | |
"source": [ | |
"Because our variable `r` is found in both the base and power of the equation, our algerbraic solution is quite messy, containing the [Lambert W function](https://en.wikipedia.org/wiki/Lambert_W_function)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "67aa9adb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle -100 - \\frac{72 W_{-1}\\left(- \\frac{25 \\cdot 2^{\\frac{11}{18}} \\log{\\left(2 \\right)}}{72}\\right)}{\\log{\\left(2 \\right)}}$" | |
], | |
"text/plain": [ | |
"-100 - 72*LambertW(-25*2**(11/18)*log(2)/72, -1)/log(2)" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"solution = solutions[0]\n", | |
"solution" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e6b33463", | |
"metadata": {}, | |
"source": [ | |
"Let's evalutate the numerical solution for the interest rate" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "11b5c1e8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 7.84687145301538$" | |
], | |
"text/plain": [ | |
"7.84687145301538" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"interest_rate = solution.evalf()\n", | |
"interest_rate" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a6cbd55f", | |
"metadata": {}, | |
"source": [ | |
"Substituting it into the \"Rule of 72\" approximation gets us the approximate doubling time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "070bbbf1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 9.17563138775925$" | |
], | |
"text/plain": [ | |
"9.17563138775925" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"rule_72_doubling_time = 72 / interest_rate\n", | |
"rule_72_doubling_time" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "62b691d5", | |
"metadata": {}, | |
"source": [ | |
"Lastly, we check our answer using the interest rate, and doubling time from above. We show it result in 2.0, and therefore, is correct!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "c77b1ede", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 2.0$" | |
], | |
"text/plain": [ | |
"2.00000000000000" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(1 + interest_rate/100)**rule_72_doubling_time" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.8.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment