ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

lab_124.ipynb

(10315B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {},
      6    "source": [
      7     "# Present Value of Liabilities and Funding Ratio\n",
      8     "\n",
      9     "In this lab session, we'll examine how to discount future liabilities to compute the present value of future liabilities, and measure the funding ratio.\n",
     10     "\n",
     11     "The funding ratio is the ratio of the current value of assets to the present value of the liabilities.\n",
     12     "\n",
     13     "In order to compute the present value, we need to discount the amount of the liability based on the relevant interest rate derived from the yield curve.\n",
     14     "\n",
     15     "For simplicity, we'll assume that the yield curve is flat, and so the interest rate is the same for all horizons.\n",
     16     "\n",
     17     "The present value of a set of liabilities $L$ where each liability $L_i$ is due at time $t_i$ is give by:\n",
     18     "\n",
     19     "$$ PV(L) = \\sum_{i=1}^{k} B(t_i) L_i$$\n",
     20     "\n",
     21     "where $B(t_i)$ is the price of a pure discount bond that pays 1 dollar at time $t_i$\n",
     22     "\n",
     23     "If we assume the yield curve is flat and the annual rate of interest is $r$ then $B(t)$ is given by\n",
     24     "\n",
     25     "$$B(t) = \\frac{1}{(1+r)^t}$$\n",
     26     "\n"
     27    ]
     28   },
     29   {
     30    "cell_type": "code",
     31    "execution_count": 2,
     32    "metadata": {},
     33    "outputs": [
     34     {
     35      "name": "stderr",
     36      "output_type": "stream",
     37      "text": [
     38       "/home/user/trading/coursera/portfolio-construction-01/nb/edhec_risk_kit_124.py:34: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
     39       "  if filetype is \"returns\":\n",
     40       "/home/user/trading/coursera/portfolio-construction-01/nb/edhec_risk_kit_124.py:37: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
     41       "  elif filetype is \"nfirms\":\n",
     42       "/home/user/trading/coursera/portfolio-construction-01/nb/edhec_risk_kit_124.py:40: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
     43       "  elif filetype is \"size\":\n"
     44      ]
     45     }
     46    ],
     47    "source": [
     48     "import numpy as np\n",
     49     "import pandas as pd\n",
     50     "import edhec_risk_kit_124 as erk\n",
     51     "\n",
     52     "%load_ext autoreload\n",
     53     "%autoreload 2"
     54    ]
     55   },
     56   {
     57    "cell_type": "code",
     58    "execution_count": 3,
     59    "metadata": {},
     60    "outputs": [],
     61    "source": [
     62     "def discount(t, r):\n",
     63     "    \"\"\"\n",
     64     "    Compute the price of a pure discount bond that pays $1 at time t where t is in years and r is the annual interest rate\n",
     65     "    \"\"\"\n",
     66     "    return (1+r)**(-t)\n"
     67    ]
     68   },
     69   {
     70    "cell_type": "code",
     71    "execution_count": 4,
     72    "metadata": {},
     73    "outputs": [
     74     {
     75      "data": {
     76       "text/plain": [
     77        "0.7440939148967249"
     78       ]
     79      },
     80      "execution_count": 4,
     81      "metadata": {},
     82      "output_type": "execute_result"
     83     }
     84    ],
     85    "source": [
     86     "b = discount(10, .03)\n",
     87     "b"
     88    ]
     89   },
     90   {
     91    "cell_type": "markdown",
     92    "metadata": {},
     93    "source": [
     94     "You can verify that if you buy that bond today, and hold it for 10 years at an interest rate of 3 percent per year, we will get paid \\$1"
     95    ]
     96   },
     97   {
     98    "cell_type": "code",
     99    "execution_count": 5,
    100    "metadata": {},
    101    "outputs": [
    102     {
    103      "data": {
    104       "text/plain": [
    105        "1.0"
    106       ]
    107      },
    108      "execution_count": 5,
    109      "metadata": {},
    110      "output_type": "execute_result"
    111     }
    112    ],
    113    "source": [
    114     "b*(1.03**10)"
    115    ]
    116   },
    117   {
    118    "cell_type": "code",
    119    "execution_count": 6,
    120    "metadata": {},
    121    "outputs": [],
    122    "source": [
    123     "def pv(l, r):\n",
    124     "    \"\"\"\n",
    125     "    Compute the present value of a list of liabilities given by the time (as an index) and amounts\n",
    126     "    \"\"\"\n",
    127     "    dates = l.index\n",
    128     "    discounts = discount(dates, r)\n",
    129     "    return (discounts*l).sum()"
    130    ]
    131   },
    132   {
    133    "cell_type": "markdown",
    134    "metadata": {},
    135    "source": [
    136     "Assume that you have 4 liabilities, of 1, 1.5, 2, and 2.5M dollars. Assume the first of these are 3 years away and the subsequent ones are spaced out 6 months apart, i.e. at time 3, 3.5, 4 and 4.5 years from now. Let's compute the present value of the liabilities based on an interest rate of 3% per year.\n",
    137     "\n",
    138     "In an individual investment context, you can think oif liabilities as Goals, such as saving for Life Events such as a down payment for a house, college expenses for your children, or retirement income. In each of these cases, we have a requirement of a cash flow at some point in the future ... anytime you have a future cash requirement, you can think of it as a liability."
    139    ]
    140   },
    141   {
    142    "cell_type": "code",
    143    "execution_count": 7,
    144    "metadata": {},
    145    "outputs": [],
    146    "source": [
    147     "liabilities = pd.Series(data=[1, 1.5, 2, 2.5], index=[3, 3.5, 4, 4.5])"
    148    ]
    149   },
    150   {
    151    "cell_type": "code",
    152    "execution_count": 8,
    153    "metadata": {},
    154    "outputs": [
    155     {
    156      "data": {
    157       "text/plain": [
    158        "6.233320315080045"
    159       ]
    160      },
    161      "execution_count": 8,
    162      "metadata": {},
    163      "output_type": "execute_result"
    164     }
    165    ],
    166    "source": [
    167     "pv(liabilities, 0.03)"
    168    ]
    169   },
    170   {
    171    "cell_type": "markdown",
    172    "metadata": {},
    173    "source": [
    174     "We can now compute the funding ratio, based on current asset values:"
    175    ]
    176   },
    177   {
    178    "cell_type": "code",
    179    "execution_count": 9,
    180    "metadata": {},
    181    "outputs": [],
    182    "source": [
    183     "def funding_ratio(assets, liabilities, r):\n",
    184     "    \"\"\"\n",
    185     "    Computes the funding ratio of a series of liabilities, based on an interest rate and current value of assets\n",
    186     "    \"\"\"\n",
    187     "    return assets/pv(liabilities, r)\n"
    188    ]
    189   },
    190   {
    191    "cell_type": "code",
    192    "execution_count": 10,
    193    "metadata": {},
    194    "outputs": [
    195     {
    196      "data": {
    197       "text/plain": [
    198        "0.8021407126958777"
    199       ]
    200      },
    201      "execution_count": 10,
    202      "metadata": {},
    203      "output_type": "execute_result"
    204     }
    205    ],
    206    "source": [
    207     "funding_ratio(5, liabilities, 0.03)"
    208    ]
    209   },
    210   {
    211    "cell_type": "markdown",
    212    "metadata": {},
    213    "source": [
    214     "Now assume interest rates go down to 2% ... let's recompute the funding ratio:"
    215    ]
    216   },
    217   {
    218    "cell_type": "code",
    219    "execution_count": 11,
    220    "metadata": {},
    221    "outputs": [
    222     {
    223      "data": {
    224       "text/plain": [
    225        "0.7720304366941648"
    226       ]
    227      },
    228      "execution_count": 11,
    229      "metadata": {},
    230      "output_type": "execute_result"
    231     }
    232    ],
    233    "source": [
    234     "funding_ratio(5, liabilities, 0.02)"
    235    ]
    236   },
    237   {
    238    "cell_type": "markdown",
    239    "metadata": {},
    240    "source": [
    241     "We can examine the effect of interest rates on funding ratio:\n",
    242     "\n",
    243     "Recall that our liabilities are:"
    244    ]
    245   },
    246   {
    247    "cell_type": "code",
    248    "execution_count": 12,
    249    "metadata": {},
    250    "outputs": [
    251     {
    252      "data": {
    253       "text/plain": [
    254        "3.0    1.0\n",
    255        "3.5    1.5\n",
    256        "4.0    2.0\n",
    257        "4.5    2.5\n",
    258        "dtype: float64"
    259       ]
    260      },
    261      "execution_count": 12,
    262      "metadata": {},
    263      "output_type": "execute_result"
    264     }
    265    ],
    266    "source": [
    267     "liabilities"
    268    ]
    269   },
    270   {
    271    "cell_type": "code",
    272    "execution_count": 13,
    273    "metadata": {},
    274    "outputs": [
    275     {
    276      "data": {
    277       "application/vnd.jupyter.widget-view+json": {
    278        "model_id": "79ce1fd3718f41d6a777bb034ffe57eb",
    279        "version_major": 2,
    280        "version_minor": 0
    281       },
    282       "text/plain": [
    283        "interactive(children=(IntSlider(value=5, description='assets', max=10, min=1), FloatSlider(value=0.1, descript…"
    284       ]
    285      },
    286      "metadata": {},
    287      "output_type": "display_data"
    288     }
    289    ],
    290    "source": [
    291     "import ipywidgets as widgets\n",
    292     "from IPython.display import display\n",
    293     "%matplotlib inline\n",
    294     "\n",
    295     "def show_funding_ratio(assets, r):\n",
    296     "    fr = funding_ratio(assets, liabilities, r)\n",
    297     "    print(f'{fr*100:.2f}%')\n",
    298     "    \n",
    299     "controls = widgets.interactive(show_funding_ratio,\n",
    300     "                                   assets=widgets.IntSlider(min=1, max=10, step=1, value=5),\n",
    301     "                                   r=(0, .20, .01)\n",
    302     ")\n",
    303     "display(controls)"
    304    ]
    305   },
    306   {
    307    "cell_type": "markdown",
    308    "metadata": {},
    309    "source": [
    310     "As the illustration above shows, even if your assets do not go down in value, cash can be a risky asset if you think about the funding ratio rather than the asset value. Even though cash is a \"safe asset\" in the sense that the asset value does not go down, cash can be a very risky asset because the value of the liabilities goes up when interest rates go down. Therefore, if you think about your savings in terms of funding ratio (i.e. how much money do you have compared to what you need) then cash is a risky asset and can result in a decline in your funding ratio.\n",
    311     "\n",
    312     "We'll investigate this and solutions to this in the next session, but for now, add the `discount`, `pv`, and `funding_ratio` functions to the `edhec_risk_kit.py` file.\n",
    313     "\n",
    314     "```python\n",
    315     "def discount(t, r):\n",
    316     "    \"\"\"\n",
    317     "    Compute the price of a pure discount bond that pays a dollar at time t where t is in years and r is the annual interest rate\n",
    318     "    \"\"\"\n",
    319     "    return (1+r)**(-t)\n",
    320     "\n",
    321     "def pv(l, r):\n",
    322     "    \"\"\"\n",
    323     "    Compute the present value of a list of liabilities given by the time (as an index) and amounts\n",
    324     "    \"\"\"\n",
    325     "    dates = l.index\n",
    326     "    discounts = discount(dates, r)\n",
    327     "    return (discounts*l).sum()\n",
    328     "\n",
    329     "def funding_ratio(assets, liabilities, r):\n",
    330     "    \"\"\"\n",
    331     "    Computes the funding ratio of a series of liabilities, based on an interest rate and current value of assets\n",
    332     "    \"\"\"\n",
    333     "    return assets/pv(liabilities, r)\n",
    334     "```\n"
    335    ]
    336   },
    337   {
    338    "cell_type": "code",
    339    "execution_count": null,
    340    "metadata": {},
    341    "outputs": [],
    342    "source": []
    343   }
    344  ],
    345  "metadata": {
    346   "kernelspec": {
    347    "display_name": "Python 3",
    348    "language": "python",
    349    "name": "python3"
    350   },
    351   "language_info": {
    352    "codemirror_mode": {
    353     "name": "ipython",
    354     "version": 3
    355    },
    356    "file_extension": ".py",
    357    "mimetype": "text/x-python",
    358    "name": "python",
    359    "nbconvert_exporter": "python",
    360    "pygments_lexer": "ipython3",
    361    "version": "3.8.8"
    362   }
    363  },
    364  "nbformat": 4,
    365  "nbformat_minor": 2
    366 }