<jupyter_start><jupyter_text>This notebook shows retrieve a model and data from an model dump file. The model is then simulated and the results are displayed.   Why specify a model in Latex? Sometime the **implementation** of a model in software don't match the **specification** of the model in the text in which the model is presented. It can be a chalange to make sure that the specification is updated in order to reflect changes made in the implementation. By extracting the model from a Latex script which describes and specify the model a one can always be sure that simulations reflect the model as described in the paper. Also the author is forced to make a complete specification of the model, else it won't run.   The Economic Credit Loss model This jupyter notebook is inspired by IMF working paper (WP/20/111) The Expected Credit Loss Modeling from a Top-Down Stress Testing Perspective by <NAME>, <NAME>, <NAME>, <NAME>. The working paper and the associated material is located https://www.imf.org/en/Publications/WP/Issues/2020/07/03/Expected-Credit-Loss-Modeling-from-a-Top-Down-Stress-Testing-Perspective-49545from the abstract of the paper: > The objective of this paper is to present an integrated tool suite for IFRS 9- and CECL compatibleestimation in top-down solvency stress tests. The tool suite serves as anillustration for institutions wishing to include accounting-based approaches for credit riskmodeling in top-down stress tests.This is a jupyter notebook build with the purpose of illustrating the use of ModelFlow. The purpose is testing so take very much care.    Import libraries<jupyter_code>%load_ext autoreload
%autoreload 2
import pandas as pd
from IPython.core.display import HTML,Markdown,Latex
from ipywidgets import interact,Dropdown,Checkbox



from modelclass import model
import modeljupytermagic
import modeljupyter as mj


# some useful stuf
model.widescreen()
pd.set_option('display.max_rows', None, 'display.max_columns', 10, 'precision', 2)
sortdf = lambda df: df[sorted([c for c in df.columns])]<jupyter_output><empty_output><jupyter_text>Fetch the precooked model and baseline The model and data has been specified in another Jupyter notebook: [file/Model for economic credit loss with Z score transition nynamic - setup]<jupyter_code>ecl,baseline  = model.modelload('ecl.pcim')<jupyter_output><empty_output><jupyter_text>The modelspecification - in latexThis is the rendering of the latex input, which has specified the model. So the model is extracted from the Latex script, which produces the output below<jupyter_code>Markdown(ecl.equations_latex)<jupyter_output><empty_output><jupyter_text>Input values for the adverse scenario<jupyter_code>%%dataframe projection_adverse show
LOAN_GROWTH	M_S1	M_S2	WRO_S3	Z
0	1.00e-02	0.04	0.03	0.06	0.00
1	-1.00e-02	0.04	0.03	0.06	-0.65
2	-8.00e-03	0.04	0.03	0.06	-0.84
3	-6.00e-03	0.04	0.03	0.06	-0.99
4	-4.00e-03	0.04	0.03	0.06	-0.69
5	-2.00e-03	0.04	0.03	0.06	-0.39
6	-0.00e+00	0.04	0.03	0.06	-0.24<jupyter_output><empty_output><jupyter_text>Create an adverse Dataframe<jupyter_code>adverse = baseline.copy()
adverse.update(projection_adverse)<jupyter_output><empty_output><jupyter_text>Run ModelFor the Baseline and the adverse scenario<jupyter_code>base_result  = ecl(baseline,keep='Baseline')
adverse_result = ecl(adverse,keep = 'Adverse')<jupyter_output>Will start calculating: testmodel
2022  solved
2023  solved
2024  solved
2025  solved
2026  solved
2027  solved
testmodel calculated 
Will start calculating: testmodel
2022  solved
2023  solved
2024  solved
2025  solved
2026  solved
2027  solved
testmodel calculated<jupyter_text>Inspect ResultsThe ecl instance of the model class has a number of methods and properties wich allows the analysis and display of results.   The difference in exogeneous variables (the input variable)<jupyter_code>ecl.exodif()<jupyter_output><empty_output><jupyter_text>Loan growth<jupyter_code>with ecl.set_smpl('2021','2027'):
    ecl.keep_plot('loan_total',showtype='growth',legend=0);<jupyter_output><empty_output><jupyter_text>Transition probabilities<jupyter_code>ecl.keep_plot('tr_*   ',showtype='level',legend=False);<jupyter_output><empty_output><jupyter_text>We can look at the structure of the model<jupyter_code>ecl.drawmodel(HR=0)<jupyter_output>Error in generation graph picture:
Error: graph\A_model_graph.gv: syntax error in line 1 near 'Digraph'

Perhaps it is already open - then close the application
Error in generation graph picture:
Error: graph\A_model_graph.gv: syntax error in line 1 near 'Digraph'

Perhaps it is already open - then close the application<jupyter_text>Or just one variable and trace the variable preceding it And look at their values<jupyter_code>with ecl.set_smpl(2021,2027):
    ecl.loan_s3.tracepre(all=1,HR=0,up=0)<jupyter_output><empty_output><jupyter_text>Or use an interactive inspector to find find the attribution to individual<jupyter_code>@interact
def explain(Variable=Dropdown(options = sorted(ecl.endogene),value='LOAN_TOTAL'),
            Levels=[0,1,2,3]):
    mj.get_frml_latex(ecl,Variable)
    _ = ecl.explain(Variable,up=Levels,svg=1,dec=2,size=(10,10),des=1)<jupyter_output><empty_output><jupyter_text>What exogenous input drives Probability of Default (point in time - PD_PIT) We want to find out how much of the difference in the results from the baseline and adverse which can be explained by the different exogenous variables. This requires shocking each exogenous variable each period, simulating the model for each experiment and save the results. All done by calling one method:<jupyter_code>ecl.totexplain('PD_PIT');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07735882457072048, 'fraction_numerical': 0.04160028323597097, 'mean_word_length': 5.790865384615385, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 56, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '38839', 'license': 'X11', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1783, 'n_words': 623, 'path': 'Examples/Economic Credit Loss/ECL Z - just run.ipynb', 'repo_name': 'IbHansen/modelflow2', 'size': '5685'}