Created
January 16, 2021 14:31
-
-
Save Houstonwp/6f411fe6a3ca276ae8fc563d8f4e5d90 to your computer and use it in GitHub Desktop.
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
{ | |
"metadata": { | |
"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.2-final" | |
}, | |
"orig_nbformat": 2, | |
"kernelspec": { | |
"name": "python38264bita04665306a3b44b794a2f52a43388ca8", | |
"display_name": "Python 3.8.2 64-bit" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"q = np.array([0.001,0.002,0.003,0.003,0.004,0.004,0.005,0.007,0.009,0.011])\n", | |
"w = np.array([0.05,0.07,0.08,0.10,0.14,0.20,0.20,0.20,0.10,0.04])\n", | |
"P = 100\n", | |
"S = 25000\n", | |
"r = 0.02" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def npv(q,w,P,S,r):\n", | |
" decrements = np.cumprod(1-q-w)\n", | |
" inforce = np.empty_like(decrements)\n", | |
" inforce[:1] = 1\n", | |
" inforce[1:] = decrements[:-1]\n", | |
" ncf = inforce * P - inforce * q * S\n", | |
" t = np.arange(np.size(q))\n", | |
" d = np.power(1/(1+r), t)\n", | |
" return np.sum(ncf * d)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"51.33132737013757" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 54 | |
} | |
], | |
"source": [ | |
"npv(q,w,P,S,r)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"21 µs ± 553 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit npv(q,w,P,S,r)" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment