Read Multiple Result Map

%load_ext autoreload
%autoreload 2
from absbox import API,EnginePath
localAPI = API(EnginePath.DEV,check=False)


from absbox.examples import test01,test02
Connecting engine server -> https://absbox.org/api/dev
/home/docs/checkouts/readthedocs.org/user_builds/absbox-doc/envs/stable/lib/python3.11/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
  warnings.warn(
✅Connected, local lib:0.52.3, server:0.52.3

What is MultiResult ?

Multi Result is just a map result return either by runByScenarios() or runStructs()

rs1 = localAPI.runStructs({"deal1":test01,"deal2":test02}
                         ,read=True)
/home/docs/checkouts/readthedocs.org/user_builds/absbox-doc/envs/stable/lib/python3.11/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
  warnings.warn(
p1 = ("Pool",("Mortgage",None,{"CPR":0.01},None,None)
                                ,None
                                ,None)

p2 = ("Pool",("Mortgage",None,{"CPR":0.02},None,None)
                                ,None
                                ,None)


rs2 = localAPI.runByScenarios(test01
                             ,{"CPR=1%":p1,"CPR=2%":p2}
                             ,runAssump = [
                                 ("pricing"
                                  ,{"date":"2021-08-22"
                                    ,"curve":[["2021-01-01",0.025]
                                             ,["2024-08-01",0.025]]})
                             ]
                             ,read=True)
/home/docs/checkouts/readthedocs.org/user_builds/absbox-doc/envs/stable/lib/python3.11/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
  warnings.warn(
---------------------------------------------------------------------------
AbsboxError                               Traceback (most recent call last)
Cell In[4], line 10
      1 p1 = ("Pool",("Mortgage",None,{"CPR":0.01},None,None)
      2                                 ,None
      3                                 ,None)
      5 p2 = ("Pool",("Mortgage",None,{"CPR":0.02},None,None)
      6                                 ,None
      7                                 ,None)
---> 10 rs2 = localAPI.runByScenarios(test01
     11                              ,{"CPR=1%":p1,"CPR=2%":p2}
     12                              ,runAssump = [
     13                                  ("pricing"
     14                                   ,{"date":"2021-08-22"
     15                                     ,"curve":[["2021-01-01",0.025]
     16                                              ,["2024-08-01",0.025]]})
     17                              ]
     18                              ,read=True)

File ~/checkouts/readthedocs.org/user_builds/absbox-doc/envs/stable/lib/python3.11/site-packages/absbox/client.py:410, in API.runByScenarios(self, deal, poolAssump, runAssump, read, showWarning, debug)
    408 if result is None or 'error' in result or "Left" in set(tz.concat([ _.keys() for _ in result.values()])):
    409     leftVal = { k:v['Left'] for k,v in result.items() if "Left" in v }
--> 410     raise AbsboxError(f"❌ Failed to get response from run: {leftVal}")
    412 result = tz.valmap(lambda x:x['Right'] ,result)
    414 rawWarnMsgByScen = {k: self._getWarningMsg(v[RunResp.LogResp.value],showWarning) for k, v in result.items()}

AbsboxError: ❌ Failed to get response from run: {'CPR=1%': 'pv4: cashflow date 2021-01-01 is before pricing date 2021-08-22', 'CPR=2%': 'pv4: cashflow date 2021-01-01 is before pricing date 2021-08-22'}

User need to import candy functions and lens to build paths which points to the data field of interest

from absbox import readFlowsByScenarios,readMultiFlowsByScenarios,readFieldsByScenarios

from lenses import lens

readFlowsByScenarios()

The read single cashflow from multiple scenarios and build a dataframe

readFlowsByScenarios(rs1, lens['bonds']['A1']['balance']).head()
deal1:balance deal2:balance
date
2021-06-20 543.90 340.83
2021-07-20 429.17 224.94
2021-08-20 313.86 108.41
2021-09-20 197.87 0.00
2021-10-20 81.15 NaN
readFlowsByScenarios(rs1, lens['bonds']['A1']['balance'], fullName=False).head()
balance balance
date
2021-06-20 543.90 340.83
2021-07-20 429.17 224.94
2021-08-20 313.86 108.41
2021-09-20 197.87 0.00
2021-10-20 81.15 NaN
readFlowsByScenarios(rs2, lens['pool']['flow']['PoolConsol']['Prepayment']).head()
CPR=1%:Prepayment CPR=2%:Prepayment
Date
2021-04-01 3.47 6.98
2021-05-01 1.64 3.29
2021-06-01 1.60 3.22
2021-07-01 1.46 2.93
2021-08-01 1.42 2.85

readMultiFlowsByScenarios

User can have option to view multiple columns from a multi result and merge them into a multi-index dataframe

readMultiFlowsByScenarios(rs2
                          , (lens['pool']['flow']['PoolConsol'],["Balance",'Prepayment'])).head()
Scenario CPR=1% CPR=2%
Field Balance Prepayment Balance Prepayment
Date
2021-04-01 1989.69 3.47 1986.45 6.98
2021-05-01 1883.73 1.64 1879.09 3.29
2021-06-01 1777.20 1.60 1771.29 3.22
2021-07-01 1670.20 1.46 1663.26 2.93
2021-08-01 1562.63 1.42 1554.79 2.85

readFieldsByScenarios

| since 0.28.15

readFieldsByScenarios is more flexible candy function to view the fields from multiple results

Here is the dataframe to be view

rs2['CPR=1%']['pricing']['summary']
pricing face WAL duration convexity accure interest
A1 308.40 30.840 0.15 0.150048 0.031304 0.11
B 1308.17 130.817 0.60 0.724602 0.000000 0.00

view by Index

readFieldsByScenarios(rs2, lens['pricing']['summary'], "A1")
CPR=1% CPR=2%
pricing 308.400000 300.610000
face 30.840000 30.061000
WAL 0.150000 0.140000
duration 0.150048 0.147078
convexity 0.031304 0.032289
accure interest 0.110000 0.110000

view by Column

readFieldsByScenarios(rs2, lens['pricing']['summary'], "pricing", flip=True)
CPR=1% CPR=2%
A1 308.40 300.61
B 1308.17 1307.59

view by Column ,which is a flow

readFieldsByScenarios(rs2, lens['pool']['flow']['PoolConsol'], 'Prepayment', flip=True)
CPR=1% CPR=2%
Date
2021-04-01 3.47 6.98
2021-05-01 1.64 3.29
2021-06-01 1.6 3.22
2021-07-01 1.46 2.93
2021-08-01 1.42 2.85
2021-09-01 1.33 2.66
2021-10-01 1.2 2.39
2021-11-01 1.14 2.29
2021-12-01 1.02 2.03
2022-01-01 0.96 1.91
2022-02-01 0.86 1.72
2022-03-01 0.69 1.38
2022-04-01 0.67 1.34
2022-05-01 0.56 1.11
2022-06-01 0.48 0.96
2022-07-01 0.37 0.74
2022-08-01 0.29 0.58
2022-09-01 0.19 0.38
2022-10-01 0.09 0.18

view by Index which is a flow

readFieldsByScenarios(rs2, lens['pool']['flow']['PoolConsol'], '2021-10-01')
CPR=1% CPR=2%
Balance 1345.93 1336.93
Principal 107.4 106.67
Interest 9.68 9.62
Prepayment 1.2 2.39
Default 0 0
Recovery 0 0
Loss 0 0
WAC 0.08 0.08
BorrowerNum None None
PrepayPenalty None None
CumPrincipal 841.95 838.75
CumPrepay 12.12 24.32
CumDelinq 0 0
CumDefault 0 0
CumRecovery 0 0
CumLoss 0 0