From 54084d7f0e4c8f65d00a03e238fe009c5f7baece Mon Sep 17 00:00:00 2001 From: Xiaoyu Date: Fri, 16 Aug 2024 03:18:19 +0800 Subject: [PATCH] add readFieldsByScenarios --- docs/source/nbsample/viewMultiResult.ipynb | 562 +++++++++++++++++---- 1 file changed, 477 insertions(+), 85 deletions(-) diff --git a/docs/source/nbsample/viewMultiResult.ipynb b/docs/source/nbsample/viewMultiResult.ipynb index 4d9b5b3..ec9deb6 100644 --- a/docs/source/nbsample/viewMultiResult.ipynb +++ b/docs/source/nbsample/viewMultiResult.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "1dc782b5", + "id": "b7917d5a", "metadata": {}, "source": [ "### Read Multiple Result Map" @@ -10,8 +10,19 @@ }, { "cell_type": "code", - "execution_count": 24, - "id": "d4e28fd6", + "execution_count": 1, + "id": "1065d162", + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ed95e253", "metadata": {}, "outputs": [ { @@ -30,11 +41,11 @@ { "data": { "text/html": [ - "
Connected, local lib:0.26.4, server:0.27.10\n",
+       "
Connected, local lib:0.28.7, server:0.28.18\n",
        "
\n" ], "text/plain": [ - "✅\u001b[1;32mConnected, local li\u001b[0m\u001b[1;32mb:0\u001b[0m\u001b[1;32m.\u001b[0m\u001b[1;32m26.4\u001b[0m\u001b[1;32m, server:\u001b[0m\u001b[1;32m0.27\u001b[0m\u001b[1;32m.\u001b[0m\u001b[1;32m10\u001b[0m\n" + "✅\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.28\u001b[0m\u001b[1;32m.\u001b[0m\u001b[1;32m18\u001b[0m\n" ] }, "metadata": {}, @@ -51,7 +62,7 @@ }, { "cell_type": "markdown", - "id": "3d8d0bc9", + "id": "574f0989", "metadata": {}, "source": [ "#### What is MultiResult ?\n", @@ -61,8 +72,8 @@ }, { "cell_type": "code", - "execution_count": 25, - "id": "f00c390f", + "execution_count": 4, + "id": "55fde6c1", "metadata": {}, "outputs": [], "source": [ @@ -72,8 +83,8 @@ }, { "cell_type": "code", - "execution_count": 26, - "id": "a9309656", + "execution_count": 13, + "id": "12f7a5bc", "metadata": {}, "outputs": [], "source": [ @@ -88,12 +99,18 @@ "\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": "c5cadf11", + "id": "4fed59e3", "metadata": {}, "source": [ "#### readFlowsByScenarios()" @@ -101,18 +118,18 @@ }, { "cell_type": "code", - "execution_count": 27, - "id": "e0cb4ef4", + "execution_count": 6, + "id": "15868ad0", "metadata": {}, "outputs": [], "source": [ - "from absbox import readFlowsByScenarios" + "from absbox import readFlowsByScenarios,readMultiFlowsByScenarios,readFieldsByScenarios" ] }, { "cell_type": "code", - "execution_count": 28, - "id": "ee782639", + "execution_count": 7, + "id": "7f87818a", "metadata": {}, "outputs": [ { @@ -185,7 +202,7 @@ "2021-10-20 81.15 NaN" ] }, - "execution_count": 28, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -198,8 +215,8 @@ }, { "cell_type": "code", - "execution_count": 29, - "id": "883c9c90", + "execution_count": 8, + "id": "c1354bd6", "metadata": {}, "outputs": [ { @@ -272,7 +289,7 @@ "2021-10-20 81.15 NaN" ] }, - "execution_count": 29, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -283,8 +300,8 @@ }, { "cell_type": "code", - "execution_count": 30, - "id": "9aa11367", + "execution_count": 9, + "id": "aa1303fa", "metadata": {}, "outputs": [ { @@ -357,7 +374,7 @@ "2021-08-01 1.42 2.85" ] }, - "execution_count": 30, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -368,18 +385,18 @@ }, { "cell_type": "markdown", - "id": "d4c80a14", + "id": "5ffecb9d", "metadata": {}, "source": [ - "#### readMultiFlowsByScenarios()\n", + "#### 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": null, - "id": "aa95d299", + "execution_count": 10, + "id": "f338d3b5", "metadata": {}, "outputs": [ { @@ -461,102 +478,336 @@ " 1554.79\n", " 2.85\n", " \n", + " \n", + "\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": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "readMultiFlowsByScenarios(rs2\n", + " , (lens['pool']['flow'],[\"Balance\",'Prepayment'])).head()" + ] + }, + { + "cell_type": "markdown", + "id": "006ac4cc", + "metadata": {}, + "source": [ + "#### readFieldsByScenarios" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6da78f21", + "metadata": {}, + "outputs": [ + { + "ename": "", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31mRunning cells with 'Python 3.12.3' requires the ipykernel package.\n", + "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", + "\u001b[1;31mCommand: '/opt/homebrew/bin/python3 -m pip install ipykernel -U --user --force-reinstall'" + ] + } + ], + "source": [ + "rs2['CPR=1%']['pricing']['summary']" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "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.40300.610
face30.8430.061
WAL0.150.140
duration0.140.140
convexity0.160.160
accure interest0.110.110
\n", + "
" + ], + "text/plain": [ + " CPR=1% CPR=2%\n", + "pricing 308.40 300.610\n", + "face 30.84 30.061\n", + "WAL 0.15 0.140\n", + "duration 0.14 0.140\n", + "convexity 0.16 0.160\n", + "accure interest 0.11 0.110" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "readFieldsByScenarios(rs2, lens['pricing']['summary'], \"A1\")" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "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": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "readFieldsByScenarios(rs2, lens['pricing']['summary'], \"pricing\", flip=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "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", " \n", " \n", - " \n", " \n", - " \n", " \n", " \n", " \n", " \n", - " \n", " \n", - " \n", " \n", " \n", " \n", " \n", - " \n", " \n", - " \n", " \n", " \n", " \n", " \n", - " \n", " \n", - " \n", " \n", " \n", " \n", @@ -564,46 +815,187 @@ "" ], "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\n", - "2021-09-01 1454.53 1.33 1445.99 2.66\n", - "2021-10-01 1345.93 1.20 1336.93 2.39\n", - "2021-11-01 1236.77 1.14 1227.44 2.29\n", - "2021-12-01 1127.10 1.02 1117.67 2.03\n", - "2022-01-01 1016.86 0.96 1007.49 1.91\n", - "2022-02-01 906.09 0.86 896.97 1.72\n", - "2022-03-01 794.84 0.69 786.23 1.38\n", - "2022-04-01 682.97 0.67 674.99 1.34\n", - "2022-05-01 570.55 0.56 563.43 1.11\n", - "2022-06-01 457.57 0.48 451.46 0.96\n", - "2022-07-01 344.03 0.37 339.16 0.74\n", - "2022-08-01 229.92 0.29 226.47 0.58\n", - "2022-09-01 115.25 0.19 113.42 0.38\n", - "2022-10-01 0.00 0.09 0.00 0.18" + " 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": 53, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "from absbox import readMultiFlowsByScenarios\n", - "\n", - "readMultiFlowsByScenarios(rs2\n", - " , (lens['pool']['flow'],[\"Balance\",'Prepayment'])).head()" + "readFieldsByScenarios(rs2, lens['pool']['flow'], 'Prepayment', flip=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "64149e19", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\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-011454.531.331445.992.66
2021-10-011345.931.201336.931.22.39
2021-11-011236.771.141227.442.29
2021-12-011127.101.021117.672.03
2022-01-011016.860.961007.491.91
2022-02-01906.090.86896.971.72
2022-03-01794.840.69786.231.38
2022-04-01682.970.67674.991.34
2022-05-01570.550.56563.431.11
2022-06-01457.570.48451.460.96
2022-07-01344.030.37339.160.74
2022-08-01229.920.29226.470.58
2022-09-01115.250.19113.420.38
2022-10-010.000.090.000.18
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \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": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "readFieldsByScenarios(rs2, lens['pool']['flow'], '2021-10-01')" ] }, { "cell_type": "code", "execution_count": null, - "id": "ae0613fd", + "id": "322f8971", "metadata": {}, "outputs": [], "source": [] @@ -625,7 +1017,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.3" } }, "nbformat": 4,