{ "cells": [ { "cell_type": "markdown", "id": "723dea80-1f1f-4da3-b600-aa9588446e7d", "metadata": {}, "source": [ "### IRR Example\n", "\n", "In `absbox`, user can calculate irr of a tranche or (multiple tranches) for scenarios of:\n", "\n", "* Holding to maturity\n", "* Holding and sell at point of future day with a pricing assumption\n", "* Buy a tranche at point of future day with a pricing assumption and hold to maturity" ] }, { "cell_type": "markdown", "id": "6a5c410a", "metadata": {}, "source": [ "#### Init" ] }, { "cell_type": "code", "execution_count": null, "id": "a791a17b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Connecting engine server -> http://localhost:8081\n",
       "
\n" ], "text/plain": [ "\u001b[1;35mConnecting engine server -> \u001b[0m\u001b[1;4;35mhttp://localhost:8081\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Connected, local lib:0.28.7, server:0.42.10\n",
       "
\n" ], "text/plain": [ "✅\u001b[1;32mConnected, local li\u001b[0m\u001b[1;32mb:0\u001b[0m\u001b[1;32m.\u001b[0m\u001b[1;32m28.7\u001b[0m\u001b[1;32m, server:\u001b[0m\u001b[1;32m0.42\u001b[0m\u001b[1;32m.\u001b[0m\u001b[1;32m10\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from absbox import examples,API,EnginePath\n", "\n", "\n", "localAPI = API(EnginePath.DEV, lang='english', check=False)" ] }, { "cell_type": "code", "execution_count": 65, "id": "b24530a1-8456-4eb2-8a96-5a7b04dc849d", "metadata": {}, "outputs": [], "source": [ "from absbox import Generic\n", "\n", "Irr01 = Generic(\n", " \"IRR Case\"\n", " ,{\"cutoff\":\"2021-03-01\",\"closing\":\"2021-04-01\",\"firstPay\":\"2021-06-20\"\n", " ,\"payFreq\":[\"DayOfMonth\",20],\"poolFreq\":\"MonthFirst\",\"stated\":\"2030-01-01\"}\n", " ,{'assets':[[\"Mortgage\"\n", " ,{\"originBalance\":2200,\"originRate\":[\"fix\",0.045],\"originTerm\":20\n", " ,\"freq\":\"Monthly\",\"type\":\"Level\",\"originDate\":\"2021-02-01\"}\n", " ,{\"currentBalance\":2200\n", " ,\"currentRate\":0.08\n", " ,\"remainTerm\":20\n", " ,\"status\":\"current\"}]]}\n", " ,((\"acc01\",{\"balance\":0}),)\n", " ,((\"A1\",{\"balance\":1000\n", " ,\"rate\":0.07\n", " ,\"originBalance\":1000\n", " ,\"originRate\":0.07\n", " ,\"startDate\":\"2021-04-01\"\n", " ,\"rateType\":{\"Fixed\":0.08}\n", " ,\"bondType\":{\"Sequential\":None}})\n", " ,(\"B\",{\"balance\":1000\n", " ,\"rate\":0.0\n", " ,\"originBalance\":1000\n", " ,\"originRate\":0.07\n", " ,\"startDate\":\"2021-04-01\"\n", " ,\"rateType\":{\"Fixed\":0.00}\n", " ,\"bondType\":{\"Equity\":None}\n", " }))\n", " ,tuple()\n", " ,{\"amortizing\":[\n", " [\"accrueAndPayInt\",\"acc01\",[\"A1\"]]\n", " ,[\"payPrin\",\"acc01\",[\"A1\"]]\n", " ,[\"payPrin\",\"acc01\",[\"B\"]]\n", " ,[\"payIntResidual\",\"acc01\",\"B\"]\n", " ]}\n", " ,[[\"CollectedInterest\",\"acc01\"]\n", " ,[\"CollectedPrincipal\",\"acc01\"]\n", " ,[\"CollectedPrepayment\",\"acc01\"]\n", " ,[\"CollectedRecoveries\",\"acc01\"]]\n", " ,None\n", " ,None\n", " ,None\n", " ,None\n", " ,(\"PreClosing\",\"Amortizing\")\n", " )\n" ] }, { "cell_type": "markdown", "id": "af9dedfe", "metadata": {}, "source": [ "#### Holding bond to maturity" ] }, { "cell_type": "code", "execution_count": 66, "id": "e03297f6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Warning Message from server:\n",
       "\n",
       "
\n" ], "text/plain": [ "Warning Message from server:\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "r0 = localAPI.run(Irr01\n", " ,poolAssump=(\"Pool\",(\"Mortgage\",{\"CDRPadding\":[0.01,0.02]},{\"CPR\":0.02},{\"Rate\":0.1,\"Lag\":5},None)\n", " ,None\n", " ,None)\n", " ,runAssump = [(\"pricing\"\n", " ,{\"IRR\":\n", " {\"B\":(\"holding\",[(\"2021-04-01\",-500)],500)\n", " }\n", " }\n", " )\n", " ]\n", " ,read=True\n", " )" ] }, { "cell_type": "markdown", "id": "2b73c5d2-2f27-4c48-8a02-c4a8734c50e6", "metadata": {}, "source": [ "Get IRR" ] }, { "cell_type": "code", "execution_count": 69, "id": "4ba17daf-ce98-4a88-abdb-286fd163c281", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.264238" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r0['pricing']['summary'].loc[\"B\"].IRR" ] }, { "cell_type": "markdown", "id": "8159eec0-8cfc-483a-95b6-525b1027a45b", "metadata": {}, "source": [ "Get breakdown of the bond cashflow of holding" ] }, { "cell_type": "code", "execution_count": 27, "id": "3163b313", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
balanceinterestprincipalratecashintDueintOverIntfactormemo
date
2021-04-010.000.000.000-500.0000NaN
2021-06-20500.000.000.0000.00001.00000<PayYield:B>
2021-07-20500.000.000.0000.00001.00000<PayYield:B>
2021-08-20500.000.000.0000.00001.00000<PayYield:B>
2021-09-20500.000.000.0000.00001.00000<PayYield:B>
2021-10-20500.000.000.0000.00001.00000<PayYield:B>
2021-11-20476.250.0023.74023.74000.95251[<PayPrin:B>, <PayYield:B>]
2021-12-20418.080.0058.17058.17000.83616[<PayPrin:B>, <PayYield:B>]
2022-01-20360.160.0057.92057.92000.72032[<PayPrin:B>, <PayYield:B>]
2022-02-20302.540.0057.62057.62000.60508[<PayPrin:B>, <PayYield:B>]
2022-03-20245.270.0057.26057.26000.49055[<PayPrin:B>, <PayYield:B>]
2022-04-20188.230.0057.04057.04000.37647[<PayPrin:B>, <PayYield:B>]
2022-05-20131.500.0056.73056.73000.26300[<PayPrin:B>, <PayYield:B>]
2022-06-2075.040.0056.45056.45000.15009[<PayPrin:B>, <PayYield:B>]
2022-07-2018.890.0056.15056.15000.03778[<PayPrin:B>, <PayYield:B>]
2022-08-200.0036.9818.89055.87000.00000[<PayPrin:B>, <PayYield:B>]
2022-09-200.0055.590.00055.59000.00000<PayYield:B>
2022-10-200.0055.300.00055.30000.00000<PayYield:B>
2022-11-200.000.040.0000.04000.00000<PayYield:B>
2022-12-200.000.030.0000.03000.00000<PayYield:B>
2023-01-200.000.020.0000.02000.00000<PayYield:B>
2023-02-200.000.010.0000.01000.00000<PayYield:B>
2023-03-200.000.000.0000.00000.00000<PayYield:B>
\n", "
" ], "text/plain": [ " balance interest principal rate cash intDue intOverInt \\\n", "date \n", "2021-04-01 0.00 0.00 0.00 0 -500.00 0 0 \n", "2021-06-20 500.00 0.00 0.00 0 0.00 0 0 \n", "2021-07-20 500.00 0.00 0.00 0 0.00 0 0 \n", "2021-08-20 500.00 0.00 0.00 0 0.00 0 0 \n", "2021-09-20 500.00 0.00 0.00 0 0.00 0 0 \n", "2021-10-20 500.00 0.00 0.00 0 0.00 0 0 \n", "2021-11-20 476.25 0.00 23.74 0 23.74 0 0 \n", "2021-12-20 418.08 0.00 58.17 0 58.17 0 0 \n", "2022-01-20 360.16 0.00 57.92 0 57.92 0 0 \n", "2022-02-20 302.54 0.00 57.62 0 57.62 0 0 \n", "2022-03-20 245.27 0.00 57.26 0 57.26 0 0 \n", "2022-04-20 188.23 0.00 57.04 0 57.04 0 0 \n", "2022-05-20 131.50 0.00 56.73 0 56.73 0 0 \n", "2022-06-20 75.04 0.00 56.45 0 56.45 0 0 \n", "2022-07-20 18.89 0.00 56.15 0 56.15 0 0 \n", "2022-08-20 0.00 36.98 18.89 0 55.87 0 0 \n", "2022-09-20 0.00 55.59 0.00 0 55.59 0 0 \n", "2022-10-20 0.00 55.30 0.00 0 55.30 0 0 \n", "2022-11-20 0.00 0.04 0.00 0 0.04 0 0 \n", "2022-12-20 0.00 0.03 0.00 0 0.03 0 0 \n", "2023-01-20 0.00 0.02 0.00 0 0.02 0 0 \n", "2023-02-20 0.00 0.01 0.00 0 0.01 0 0 \n", "2023-03-20 0.00 0.00 0.00 0 0.00 0 0 \n", "\n", " factor memo \n", "date \n", "2021-04-01 NaN \n", "2021-06-20 1.00000 \n", "2021-07-20 1.00000 \n", "2021-08-20 1.00000 \n", "2021-09-20 1.00000 \n", "2021-10-20 1.00000 \n", "2021-11-20 0.95251 [, ] \n", "2021-12-20 0.83616 [, ] \n", "2022-01-20 0.72032 [, ] \n", "2022-02-20 0.60508 [, ] \n", "2022-03-20 0.49055 [, ] \n", "2022-04-20 0.37647 [, ] \n", "2022-05-20 0.26300 [, ] \n", "2022-06-20 0.15009 [, ] \n", "2022-07-20 0.03778 [, ] \n", "2022-08-20 0.00000 [, ] \n", "2022-09-20 0.00000 \n", "2022-10-20 0.00000 \n", "2022-11-20 0.00000 \n", "2022-12-20 0.00000 \n", "2023-01-20 0.00000 \n", "2023-02-20 0.00000 \n", "2023-03-20 0.00000 " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r0['pricing']['breakdown']['B']" ] }, { "cell_type": "markdown", "id": "11c6e53b-18c0-49ce-ae69-40b7c81d4df9", "metadata": {}, "source": [ "#### Sale the bond during the holding" ] }, { "cell_type": "code", "execution_count": 46, "id": "52d006f4-6b4d-4773-9c83-c6ab85d5c7cb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Warning Message from server:\n",
       "\n",
       "
\n" ], "text/plain": [ "Warning Message from server:\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "r1 = localAPI.run(Irr01\n", " ,poolAssump=(\"Pool\",(\"Mortgage\",{\"CDRPadding\":[0.01,0.02]},{\"CPR\":0.02},{\"Rate\":0.1,\"Lag\":5},None)\n", " ,None\n", " ,None)\n", " ,runAssump = [(\"pricing\",{\"IRR\":\n", " {\"A1\":(\"holding\",[(\"2021-04-01\",-500)],500,\"2021-08-19\",(\"byFactor\",1.0))}\n", " }\n", " )]\n", " ,read=True)" ] }, { "cell_type": "code", "execution_count": 47, "id": "c78dd59c-195a-48a2-91ce-62a85f8a2f28", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IRR
A10.07196
\n", "
" ], "text/plain": [ " IRR\n", "A1 0.07196" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r1['pricing']['summary']" ] }, { "cell_type": "code", "execution_count": 30, "id": "8dfacf8e-042a-4e40-acb1-6e084c6235d5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
balanceinterestprincipalratecashintDueintOverIntfactormemo
date
2021-04-010.000.000.000.00-500.0000NaN
2021-06-20266.977.67233.030.07240.70000.53394[<PayInt:A1>, <PayPrin:A1>]
2021-07-20209.041.5357.920.0759.46000.41809[<PayInt:A1>, <PayPrin:A1>]
2021-08-190.001.20209.040.00210.2400NaN
\n", "
" ], "text/plain": [ " balance interest principal rate cash intDue intOverInt \\\n", "date \n", "2021-04-01 0.00 0.00 0.00 0.00 -500.00 0 0 \n", "2021-06-20 266.97 7.67 233.03 0.07 240.70 0 0 \n", "2021-07-20 209.04 1.53 57.92 0.07 59.46 0 0 \n", "2021-08-19 0.00 1.20 209.04 0.00 210.24 0 0 \n", "\n", " factor memo \n", "date \n", "2021-04-01 NaN \n", "2021-06-20 0.53394 [, ] \n", "2021-07-20 0.41809 [, ] \n", "2021-08-19 NaN " ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r1['pricing']['breakdown']['A1']" ] }, { "cell_type": "markdown", "id": "3e1a0d9c-59eb-4c12-9c5e-7b29a5d232f8", "metadata": {}, "source": [ "#### Buy a bond in future" ] }, { "cell_type": "code", "execution_count": 41, "id": "0ce024ed-6fef-4c9d-ac5f-42f0a4d908dc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Warning Message from server:\n",
       "\n",
       "
\n" ], "text/plain": [ "Warning Message from server:\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "r3 = localAPI.run(Irr01\n", " ,poolAssump=(\"Pool\",(\"Mortgage\",{\"CDRPadding\":[0.01,0.02]},{\"CPR\":0.02},{\"Rate\":0.1,\"Lag\":5},None)\n", " ,None\n", " ,None)\n", " ,runAssump = [(\"pricing\",{\"IRR\":\n", " {\"A1\":(\"buy\",\"2021-08-01\",(\"byFactor\",0.99),(\"byCash\",200))}\n", " }\n", " )]\n", " ,read=True)" ] }, { "cell_type": "code", "execution_count": 42, "id": "ffdfbf73-a917-491f-b44a-167937233b4b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IRR
A10.12248
\n", "
" ], "text/plain": [ " IRR\n", "A1 0.12248" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r3['pricing']['summary']" ] }, { "cell_type": "code", "execution_count": 43, "id": "7089b006-d043-4024-8715-731fb0fc6f67", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
balanceinterestprincipalratecashintDueintOverIntfactormemo
date
2021-08-010.00-0.96-200.000.00-200.9600NaN
2021-08-20145.911.1956.100.0757.30000.30198[<PayInt:A1>, <PayPrin:A1>]
2021-09-2089.660.8656.250.0757.11000.18557[<PayInt:A1>, <PayPrin:A1>]
2021-10-2033.390.5156.270.0756.78000.06911[<PayInt:A1>, <PayPrin:A1>]
2021-11-200.000.1933.390.0733.59000.00000[<PayInt:A1>, <PayPrin:A1>]
\n", "
" ], "text/plain": [ " balance interest principal rate cash intDue intOverInt \\\n", "date \n", "2021-08-01 0.00 -0.96 -200.00 0.00 -200.96 0 0 \n", "2021-08-20 145.91 1.19 56.10 0.07 57.30 0 0 \n", "2021-09-20 89.66 0.86 56.25 0.07 57.11 0 0 \n", "2021-10-20 33.39 0.51 56.27 0.07 56.78 0 0 \n", "2021-11-20 0.00 0.19 33.39 0.07 33.59 0 0 \n", "\n", " factor memo \n", "date \n", "2021-08-01 NaN \n", "2021-08-20 0.30198 [, ] \n", "2021-09-20 0.18557 [, ] \n", "2021-10-20 0.06911 [, ] \n", "2021-11-20 0.00000 [, ] " ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r3['pricing']['breakdown']['A1']" ] } ], "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }