Skip to content

Instantly share code, notes, and snippets.

@wrighter
Last active October 1, 2019 12:55
Show Gist options
  • Save wrighter/73c0ada3827daff4892eeedcc0c0cc3c to your computer and use it in GitHub Desktop.
Save wrighter/73c0ada3827daff4892eeedcc0c0cc3c to your computer and use it in GitHub Desktop.
Present Value
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"556.8374181775592"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def present_value(fv, i_rate, n_periods):\n",
" return fv / (1 + i_rate) ** n_periods\n",
"\n",
"present_value(1000, 0.05, 12)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame([(1000, 0.05, 12), (1000, 0.07, 12), (1000, 0.09, 12), (500, 0.02, 24)],\n",
" columns=['fv', 'i_rate', 'n_periods'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>fv</th>\n",
" <th>i_rate</th>\n",
" <th>n_periods</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>1000</td>\n",
" <td>0.05</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>1000</td>\n",
" <td>0.07</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>1000</td>\n",
" <td>0.09</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>500</td>\n",
" <td>0.02</td>\n",
" <td>24</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" fv i_rate n_periods\n",
"0 1000 0.05 12\n",
"1 1000 0.07 12\n",
"2 1000 0.09 12\n",
"3 500 0.02 24"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"for (index, row) in df.iterrows():\n",
" df.loc[index, 'pv'] = present_value(row.fv, row.i_rate, row.n_periods)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"df['pv2'] = df.apply(lambda r: present_value(r['fv'], r['i_rate'], r['n_periods']), axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"df['pv3'] = df['fv']/(1 + df['i_rate']) ** df['n_periods']"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>fv</th>\n",
" <th>i_rate</th>\n",
" <th>n_periods</th>\n",
" <th>pv</th>\n",
" <th>pv2</th>\n",
" <th>pv3</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>1000</td>\n",
" <td>0.05</td>\n",
" <td>12</td>\n",
" <td>556.837418</td>\n",
" <td>556.837418</td>\n",
" <td>556.837418</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>1000</td>\n",
" <td>0.07</td>\n",
" <td>12</td>\n",
" <td>444.011959</td>\n",
" <td>444.011959</td>\n",
" <td>444.011959</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>1000</td>\n",
" <td>0.09</td>\n",
" <td>12</td>\n",
" <td>355.534725</td>\n",
" <td>355.534725</td>\n",
" <td>355.534725</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>500</td>\n",
" <td>0.02</td>\n",
" <td>24</td>\n",
" <td>310.860744</td>\n",
" <td>310.860744</td>\n",
" <td>310.860744</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" fv i_rate n_periods pv pv2 pv3\n",
"0 1000 0.05 12 556.837418 556.837418 556.837418\n",
"1 1000 0.07 12 444.011959 444.011959 444.011959\n",
"2 1000 0.09 12 355.534725 355.534725 355.534725\n",
"3 500 0.02 24 310.860744 310.860744 310.860744"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.18 ms ± 58.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit df.apply(lambda r: present_value(r['fv'], r['i_rate'], r['n_periods']), axis=1) "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"493 µs ± 45.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit df['fv']/(1 + df['i_rate']) ** df['n_periods'] "
]
},
{
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment