{ "cells": [ { "cell_type": "markdown", "id": "b7917d5a", "metadata": {}, "source": [ "### Read Multiple Result Map" ] }, { "cell_type": "code", "execution_count": 1, "id": "1065d162", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "id": "ed95e253", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Connecting engine server -> https://absbox.org/api/dev\n",
       "
\n" ], "text/plain": [ "Connecting engine server -> \u001b[4;94mhttps://absbox.org/api/dev\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/home/xiaoyu/repo/AbsBox/lib/python3.13/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'absbox.org'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings\n", " warnings.warn(\n" ] }, { "data": { "text/html": [ "
✅Connected, local lib:0.46.5, server:0.50.1\n",
       "
\n" ], "text/plain": [ "✅Connected, local li\u001b[1;92mb:0\u001b[0m.\u001b[1;36m46.5\u001b[0m, server:\u001b[1;36m0.50\u001b[0m.\u001b[1;36m1\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from absbox import API,EnginePath\n", "localAPI = API(EnginePath.DEV,check=False)\n", "\n", "\n", "from absbox.examples import test01,test02" ] }, { "cell_type": "markdown", "id": "574f0989", "metadata": {}, "source": [ "#### What is MultiResult ?\n", "\n", "`Multi Result` is just a map result return either by `runByScenarios()` or `runStructs()`" ] }, { "cell_type": "code", "execution_count": 3, "id": "55fde6c1", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/xiaoyu/repo/AbsBox/lib/python3.13/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'absbox.org'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings\n", " warnings.warn(\n" ] } ], "source": [ "rs1 = localAPI.runStructs({\"deal1\":test01,\"deal2\":test02}\n", " ,read=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "12f7a5bc", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/xiaoyu/repo/AbsBox/lib/python3.13/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'absbox.org'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings\n", " warnings.warn(\n" ] } ], "source": [ "p1 = (\"Pool\",(\"Mortgage\",None,{\"CPR\":0.01},None,None)\n", " ,None\n", " ,None)\n", "\n", "p2 = (\"Pool\",(\"Mortgage\",None,{\"CPR\":0.02},None,None)\n", " ,None\n", " ,None)\n", "\n", "\n", "rs2 = localAPI.runByScenarios(test01\n", " ,{\"CPR=1%\":p1,\"CPR=2%\":p2}\n", " ,runAssump = [\n", " (\"pricing\"\n", " ,{\"date\":\"2021-08-22\"\n", " ,\"curve\":[[\"2021-01-01\",0.025]\n", " ,[\"2024-08-01\",0.025]]})\n", " ]\n", " ,read=True)" ] }, { "cell_type": "markdown", "id": "75845521", "metadata": {}, "source": [ "User need to import candy functions and `lens` to build paths which points to the data field of interest" ] }, { "cell_type": "code", "execution_count": 5, "id": "15868ad0", "metadata": {}, "outputs": [], "source": [ "from absbox import readFlowsByScenarios,readMultiFlowsByScenarios,readFieldsByScenarios\n", "\n", "from lenses import lens" ] }, { "cell_type": "markdown", "id": "4fed59e3", "metadata": {}, "source": [ "#### readFlowsByScenarios()\n", "\n", "The read single cashflow from multiple scenarios and build a dataframe" ] }, { "cell_type": "code", "execution_count": 6, "id": "7f87818a", "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", "
deal1:balancedeal2:balance
date
2021-06-20543.90340.83
2021-07-20429.17224.94
2021-08-20313.86108.41
2021-09-20197.870.00
2021-10-2081.15NaN
\n", "
" ], "text/plain": [ " deal1:balance deal2:balance\n", "date \n", "2021-06-20 543.90 340.83\n", "2021-07-20 429.17 224.94\n", "2021-08-20 313.86 108.41\n", "2021-09-20 197.87 0.00\n", "2021-10-20 81.15 NaN" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readFlowsByScenarios(rs1, lens['bonds']['A1']['balance']).head()" ] }, { "cell_type": "code", "execution_count": 7, "id": "c1354bd6", "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", "
balancebalance
date
2021-06-20543.90340.83
2021-07-20429.17224.94
2021-08-20313.86108.41
2021-09-20197.870.00
2021-10-2081.15NaN
\n", "
" ], "text/plain": [ " balance balance\n", "date \n", "2021-06-20 543.90 340.83\n", "2021-07-20 429.17 224.94\n", "2021-08-20 313.86 108.41\n", "2021-09-20 197.87 0.00\n", "2021-10-20 81.15 NaN" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readFlowsByScenarios(rs1, lens['bonds']['A1']['balance'], fullName=False).head()" ] }, { "cell_type": "code", "execution_count": 8, "id": "aa1303fa", "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", "
CPR=1%:PrepaymentCPR=2%:Prepayment
Date
2021-04-013.476.98
2021-05-011.643.29
2021-06-011.603.22
2021-07-011.462.93
2021-08-011.422.85
\n", "
" ], "text/plain": [ " CPR=1%:Prepayment CPR=2%:Prepayment\n", "Date \n", "2021-04-01 3.47 6.98\n", "2021-05-01 1.64 3.29\n", "2021-06-01 1.60 3.22\n", "2021-07-01 1.46 2.93\n", "2021-08-01 1.42 2.85" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readFlowsByScenarios(rs2, lens['pool']['flow']['PoolConsol']['Prepayment']).head()" ] }, { "cell_type": "markdown", "id": "5ffecb9d", "metadata": {}, "source": [ "#### readMultiFlowsByScenarios\n", "\n", "User can have option to view `multiple columns` from a multi result and merge them into a multi-index dataframe" ] }, { "cell_type": "code", "execution_count": 9, "id": "f338d3b5", "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", "
ScenarioCPR=1%CPR=2%
FieldBalancePrepaymentBalancePrepayment
Date
2021-04-011989.693.471986.456.98
2021-05-011883.731.641879.093.29
2021-06-011777.201.601771.293.22
2021-07-011670.201.461663.262.93
2021-08-011562.631.421554.792.85
\n", "
" ], "text/plain": [ "Scenario CPR=1% CPR=2% \n", "Field Balance Prepayment Balance Prepayment\n", "Date \n", "2021-04-01 1989.69 3.47 1986.45 6.98\n", "2021-05-01 1883.73 1.64 1879.09 3.29\n", "2021-06-01 1777.20 1.60 1771.29 3.22\n", "2021-07-01 1670.20 1.46 1663.26 2.93\n", "2021-08-01 1562.63 1.42 1554.79 2.85" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readMultiFlowsByScenarios(rs2\n", " , (lens['pool']['flow']['PoolConsol'],[\"Balance\",'Prepayment'])).head()" ] }, { "cell_type": "markdown", "id": "006ac4cc", "metadata": {}, "source": [ "#### readFieldsByScenarios" ] }, { "cell_type": "markdown", "id": "3c125936", "metadata": {}, "source": [ "| since `0.28.15`\n", "\n", "`readFieldsByScenarios` is more flexible candy function to view the fields from multiple results\n", "\n", "Here is the dataframe to be view" ] }, { "cell_type": "code", "execution_count": 10, "id": "6da78f21", "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", "
pricingfaceWALdurationconvexityaccure interest
A1308.4030.8400.150.1500480.0313040.11
B1308.17130.8170.600.7246020.0000000.00
\n", "
" ], "text/plain": [ " pricing face WAL duration convexity accure interest\n", "A1 308.40 30.840 0.15 0.150048 0.031304 0.11\n", "B 1308.17 130.817 0.60 0.724602 0.000000 0.00" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs2['CPR=1%']['pricing']['summary']" ] }, { "cell_type": "markdown", "id": "21dea712", "metadata": {}, "source": [ "view by `Index`" ] }, { "cell_type": "code", "execution_count": 11, "id": "2f40a3dc", "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", "
CPR=1%CPR=2%
pricing308.400000300.610000
face30.84000030.061000
WAL0.1500000.140000
duration0.1500480.147078
convexity0.0313040.032289
accure interest0.1100000.110000
\n", "
" ], "text/plain": [ " CPR=1% CPR=2%\n", "pricing 308.400000 300.610000\n", "face 30.840000 30.061000\n", "WAL 0.150000 0.140000\n", "duration 0.150048 0.147078\n", "convexity 0.031304 0.032289\n", "accure interest 0.110000 0.110000" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readFieldsByScenarios(rs2, lens['pricing']['summary'], \"A1\")" ] }, { "cell_type": "markdown", "id": "592643c2", "metadata": {}, "source": [ "view by `Column`" ] }, { "cell_type": "code", "execution_count": 12, "id": "ce9a50a3", "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", "
CPR=1%CPR=2%
A1308.40300.61
B1308.171307.59
\n", "
" ], "text/plain": [ " CPR=1% CPR=2%\n", "A1 308.40 300.61\n", "B 1308.17 1307.59" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readFieldsByScenarios(rs2, lens['pricing']['summary'], \"pricing\", flip=True)" ] }, { "cell_type": "markdown", "id": "c37dee5b", "metadata": {}, "source": [ "view by `Column` ,which is a flow" ] }, { "cell_type": "code", "execution_count": 15, "id": "6be36411", "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", "
CPR=1%CPR=2%
Date
2021-04-013.476.98
2021-05-011.643.29
2021-06-011.63.22
2021-07-011.462.93
2021-08-011.422.85
2021-09-011.332.66
2021-10-011.22.39
2021-11-011.142.29
2021-12-011.022.03
2022-01-010.961.91
2022-02-010.861.72
2022-03-010.691.38
2022-04-010.671.34
2022-05-010.561.11
2022-06-010.480.96
2022-07-010.370.74
2022-08-010.290.58
2022-09-010.190.38
2022-10-010.090.18
\n", "
" ], "text/plain": [ " CPR=1% CPR=2%\n", "Date \n", "2021-04-01 3.47 6.98\n", "2021-05-01 1.64 3.29\n", "2021-06-01 1.6 3.22\n", "2021-07-01 1.46 2.93\n", "2021-08-01 1.42 2.85\n", "2021-09-01 1.33 2.66\n", "2021-10-01 1.2 2.39\n", "2021-11-01 1.14 2.29\n", "2021-12-01 1.02 2.03\n", "2022-01-01 0.96 1.91\n", "2022-02-01 0.86 1.72\n", "2022-03-01 0.69 1.38\n", "2022-04-01 0.67 1.34\n", "2022-05-01 0.56 1.11\n", "2022-06-01 0.48 0.96\n", "2022-07-01 0.37 0.74\n", "2022-08-01 0.29 0.58\n", "2022-09-01 0.19 0.38\n", "2022-10-01 0.09 0.18" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readFieldsByScenarios(rs2, lens['pool']['flow']['PoolConsol'], 'Prepayment', flip=True)" ] }, { "cell_type": "markdown", "id": "c8ddc995", "metadata": {}, "source": [ "view by `Index` which is a flow" ] }, { "cell_type": "code", "execution_count": 16, "id": "64149e19", "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", "
CPR=1%CPR=2%
Balance1345.931336.93
Principal107.4106.67
Interest9.689.62
Prepayment1.22.39
Default00
Recovery00
Loss00
WAC0.080.08
BorrowerNumNoneNone
PrepayPenaltyNoneNone
CumPrincipal841.95838.75
CumPrepay12.1224.32
CumDelinq00
CumDefault00
CumRecovery00
CumLoss00
\n", "
" ], "text/plain": [ " CPR=1% CPR=2%\n", "Balance 1345.93 1336.93\n", "Principal 107.4 106.67\n", "Interest 9.68 9.62\n", "Prepayment 1.2 2.39\n", "Default 0 0\n", "Recovery 0 0\n", "Loss 0 0\n", "WAC 0.08 0.08\n", "BorrowerNum None None\n", "PrepayPenalty None None\n", "CumPrincipal 841.95 838.75\n", "CumPrepay 12.12 24.32\n", "CumDelinq 0 0\n", "CumDefault 0 0\n", "CumRecovery 0 0\n", "CumLoss 0 0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readFieldsByScenarios(rs2, lens['pool']['flow']['PoolConsol'], '2021-10-01')" ] }, { "cell_type": "code", "execution_count": null, "id": "18c45f53-d5e3-4e8b-a55f-923410ccc40c", "metadata": {}, "outputs": [], "source": [] } ], "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.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }