ModelFlowIb 1.46__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ModelFlowIb-1.46.dist-info/METADATA +151 -0
- ModelFlowIb-1.46.dist-info/RECORD +48 -0
- ModelFlowIb-1.46.dist-info/WHEEL +5 -0
- ModelFlowIb-1.46.dist-info/top_level.txt +44 -0
- data/ken_base.pcim +1 -0
- modelBLfunk.py +70 -0
- model_Excel.py +334 -0
- model_cvx.py +132 -0
- model_doable.py +40 -0
- model_dynare.py +164 -0
- model_financial_stability.py +88 -0
- model_ifrs9.py +88 -0
- model_latex.py +505 -0
- model_latex_class.py +478 -0
- model_latex_classpy.py +401 -0
- model_run_numba.py +15 -0
- modelclass.py +7057 -0
- modelclass2.py +463 -0
- modeldash.py +265 -0
- modeldashboot.py +202 -0
- modeldashsidebar.py +442 -0
- modeldekom.py +518 -0
- modeldiff.py +561 -0
- modelgrab.py +318 -0
- modelgrabwf2.py +554 -0
- modelhelp.py +253 -0
- modelinvert.py +211 -0
- modeljupyter.py +821 -0
- modeljupytermagic.py +267 -0
- modelmacrograb.py +98 -0
- modelmanipulation.py +1273 -0
- modelmf.py +265 -0
- modelnet.py +114 -0
- modelnewton.py +819 -0
- modelnormalize.py +348 -0
- modelpattern.py +253 -0
- modelpatternrev.py +72 -0
- modelsandbox.py +76 -0
- modelsandbox_Mixin.py +1565 -0
- modeltodo.py +31 -0
- modeluserfunk.py +97 -0
- modeluserfunk_old.py +90 -0
- modelvis.py +677 -0
- modelwidget.py +610 -0
- modelwidget_input.py +961 -0
- modelwidget_new.py +1005 -0
- modelwidget_new_new.py +1238 -0
- modelwidget_shinypy.py +935 -0
modeldekom.py
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Module for making attribution analysis of a model.
|
|
4
|
+
|
|
5
|
+
The main function is attribution
|
|
6
|
+
|
|
7
|
+
Created on Wed May 31 08:50:51 2017
|
|
8
|
+
|
|
9
|
+
@author: hanseni
|
|
10
|
+
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
import pandas as pd
|
|
15
|
+
import fnmatch
|
|
16
|
+
import matplotlib.pyplot as plt
|
|
17
|
+
import matplotlib as mpl
|
|
18
|
+
import matplotlib.dates as mdates
|
|
19
|
+
|
|
20
|
+
import numpy
|
|
21
|
+
import ipywidgets as ip
|
|
22
|
+
import pdb
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
from modelhelp import cutout
|
|
27
|
+
import modelclass as mc
|
|
28
|
+
import modeldekom as mk
|
|
29
|
+
import modelvis as mv
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
idx= pd.IndexSlice
|
|
33
|
+
|
|
34
|
+
def attribution(model,experiments,start='',end='',save='',maxexp=10000,showtime=False,
|
|
35
|
+
summaryvar=['*']
|
|
36
|
+
,silent=False,msilent=True,type='level'):
|
|
37
|
+
""" Calculates an attribution analysis on a model
|
|
38
|
+
accepts a dictionary with experiments. the key is experiment name, the value is a list
|
|
39
|
+
of variables which has to be reset to the values in the baseline dataframe. """
|
|
40
|
+
summaryout = model.vlist(summaryvar)
|
|
41
|
+
adverseny = model.lastdf
|
|
42
|
+
base = model.basedf
|
|
43
|
+
if type == 'level':
|
|
44
|
+
adverse0=adverseny[summaryout].loc[start:end,:].copy()
|
|
45
|
+
elif type == 'growth':
|
|
46
|
+
adverse0=adverseny[summaryout].pct_change().loc[start:end,:].copy() * 100.
|
|
47
|
+
ret={}
|
|
48
|
+
modelsave = model.save # save the state of model.save
|
|
49
|
+
model.save = False # no need to save the experiments in each run
|
|
50
|
+
with model.timer('Total dekomp',showtime):
|
|
51
|
+
for i,(e,var) in enumerate(experiments.items()):
|
|
52
|
+
if i >= maxexp : break # when we are testing
|
|
53
|
+
oldvar=adverseny[var].copy()
|
|
54
|
+
if not silent:
|
|
55
|
+
print(i,'Experiment :',e,'\n','Touching: \n', var)
|
|
56
|
+
adverseny[var] = base[var]
|
|
57
|
+
tempdf = model(adverseny ,start,end,samedata=True,
|
|
58
|
+
silent=msilent)[summaryout]
|
|
59
|
+
adverseny[var] = oldvar
|
|
60
|
+
|
|
61
|
+
if type == 'level':
|
|
62
|
+
ret[e] = tempdf[summaryout].loc[start:end,:]
|
|
63
|
+
elif type == 'growth':
|
|
64
|
+
ret[e] = tempdf.pct_change().loc[start:end,:] * 100.
|
|
65
|
+
|
|
66
|
+
difret = {e : adverse0-ret[e] for e in ret}
|
|
67
|
+
|
|
68
|
+
df = pd.concat([difret[v] for v in difret],keys=difret.keys()).T
|
|
69
|
+
if save:
|
|
70
|
+
df.to_pickle('data\\' +save +r'.pc')
|
|
71
|
+
|
|
72
|
+
model.save = modelsave # restore the state of model.save
|
|
73
|
+
return df
|
|
74
|
+
|
|
75
|
+
def attribution_new(model,experiments,start='',end='',save='',maxexp=10000,showtime=False,
|
|
76
|
+
summaryvar=['*']
|
|
77
|
+
,silent=False,msilent=True,type='level'):
|
|
78
|
+
""" Calculates an attribution analysis on a model
|
|
79
|
+
accepts a dictionary with experiments. the key is experiment name, the value is a list
|
|
80
|
+
of variables which has to be reset to the values in the baseline dataframe. """
|
|
81
|
+
summaryout = model.vlist(summaryvar)
|
|
82
|
+
adverseny = model.lastdf
|
|
83
|
+
base = model.basedf
|
|
84
|
+
adverse0_level = adverseny[summaryout].loc[start:end,:].copy()
|
|
85
|
+
adverse0_growth = adverseny[summaryout].pct_change().loc[start:end,:].copy() * 100.
|
|
86
|
+
ret_level = {}
|
|
87
|
+
ret_growth = {}
|
|
88
|
+
modelsave = model.save # save the state of model.save
|
|
89
|
+
model.save = False # no need to save the experiments in each run
|
|
90
|
+
with model.timer('Total dekomp',showtime):
|
|
91
|
+
for i,(e,var) in enumerate(experiments.items()):
|
|
92
|
+
if i >= maxexp : break # when we are testing
|
|
93
|
+
oldvar=adverseny[var].copy()
|
|
94
|
+
if not silent:
|
|
95
|
+
print(i,'Experiment :',e,'\n','Touching: \n', var)
|
|
96
|
+
adverseny[var] = base[var]
|
|
97
|
+
tempdf = model(adverseny ,start,end,samedata=True,
|
|
98
|
+
silent=msilent)[summaryout]
|
|
99
|
+
adverseny[var] = oldvar
|
|
100
|
+
|
|
101
|
+
ret_level[e] = tempdf.loc[start:end,:]
|
|
102
|
+
ret_growth[e] = tempdf.pct_change().loc[start:end,:] * 100.
|
|
103
|
+
|
|
104
|
+
difret_level = {e : adverse0_level - ret_level[e] for e in ret_level}
|
|
105
|
+
difret_growth = {e : adverse0_growth - ret_growth[e] for e in ret_growth}
|
|
106
|
+
|
|
107
|
+
df_level = pd.concat([difret_level[v] for v in difret_level],keys=difret_level.keys()).T
|
|
108
|
+
df_growth = pd.concat([difret_growth[v] for v in difret_growth],keys=difret_growth.keys()).T
|
|
109
|
+
if save:
|
|
110
|
+
df_level.to_pickle('data\\' +save +r'_level.pc')
|
|
111
|
+
df_level.to_pickle('data\\' +save +r'_growth.pc')
|
|
112
|
+
|
|
113
|
+
model.save = modelsave # restore the state of model.save
|
|
114
|
+
return {'level':df_level, 'growth':df_growth}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def ilist(df,pat):
|
|
118
|
+
'''returns a list of variable in the model matching the pattern,
|
|
119
|
+
the pattern can be a list of patterns of a sting with patterns seperated by
|
|
120
|
+
blanks
|
|
121
|
+
|
|
122
|
+
This function operates on the index names of a dataframe. Relevant for attribution analysis
|
|
123
|
+
'''
|
|
124
|
+
if isinstance(pat,list):
|
|
125
|
+
upat=pat
|
|
126
|
+
else:
|
|
127
|
+
upat = [pat]
|
|
128
|
+
|
|
129
|
+
ipat = upat
|
|
130
|
+
out = [v for p in ipat for up in p.split() for v in sorted(fnmatch.filter(df.index,up.upper()))]
|
|
131
|
+
return out
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def GetAllImpact(impact,sumaryvar):
|
|
135
|
+
''' get all the impact from at impact dataframe'''
|
|
136
|
+
exo = list({v for v,t in impact.columns})
|
|
137
|
+
df = pd.concat([impact.loc[sumaryvar,c] for c in exo],axis=1)
|
|
138
|
+
df.columns = exo
|
|
139
|
+
return df
|
|
140
|
+
|
|
141
|
+
def GetSumImpact(impact,pat='PD__*'):
|
|
142
|
+
"""Gets the accumulated differences attributet to each impact group """
|
|
143
|
+
a = impact.loc[ilist(impact,pat),:].groupby(level=[0],axis=1).sum()
|
|
144
|
+
return a
|
|
145
|
+
|
|
146
|
+
def GetLastImpact(impact,pat='RCET1__*'):
|
|
147
|
+
"""Gets the last differences attributet to each impact group """
|
|
148
|
+
a = impact.loc[ilist(impact,pat),:].groupby(level=[0],axis=1).last()
|
|
149
|
+
return a
|
|
150
|
+
|
|
151
|
+
def GetAllImpact(impact,pat='RCET1__*'):
|
|
152
|
+
"""Gets the last differences attributet to each impact group """
|
|
153
|
+
a = impact.loc[ilist(impact,pat),:]
|
|
154
|
+
return a
|
|
155
|
+
|
|
156
|
+
def GetOneImpact(impact,pat='RCET1__*',per=''):
|
|
157
|
+
"""Gets differences attributet to each impact group in period:per """
|
|
158
|
+
a = impact.loc[ilist(impact,pat),idx[:,per]]
|
|
159
|
+
a.columns = [v[0] for v in a.columns]
|
|
160
|
+
return a
|
|
161
|
+
|
|
162
|
+
def AggImpact(impact):
|
|
163
|
+
""" Calculates the sum of impacts and place in the last column
|
|
164
|
+
|
|
165
|
+
This function is applied to the result iof a Get* function"""
|
|
166
|
+
asum= impact.sum(axis=1)
|
|
167
|
+
asum.name = '_Sum'
|
|
168
|
+
aout = pd.concat([impact,asum],axis=1)
|
|
169
|
+
return aout
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
class totdif():
|
|
173
|
+
''' Class to make modelvide attribution analysis
|
|
174
|
+
|
|
175
|
+
'''
|
|
176
|
+
|
|
177
|
+
def __init__(self, model,summaryvar='*',desdic={},experiments = None):
|
|
178
|
+
|
|
179
|
+
self.diffdf = model.exodif()
|
|
180
|
+
self.diffvar = self.diffdf.columns
|
|
181
|
+
if len(self.diffvar) == 0:
|
|
182
|
+
print('No variables to attribute to ')
|
|
183
|
+
self.go = False
|
|
184
|
+
self.typetext = 'Unknown'
|
|
185
|
+
|
|
186
|
+
else:
|
|
187
|
+
self.go = True
|
|
188
|
+
self.experiments = {v:v for v in self.diffvar} if experiments == None else experiments
|
|
189
|
+
self.model = model
|
|
190
|
+
self.start = self.model.current_per.tolist()[0]
|
|
191
|
+
self.end = self.model.current_per.tolist()[-1]
|
|
192
|
+
|
|
193
|
+
self.desdic = desdic
|
|
194
|
+
self.summaryvar = summaryvar
|
|
195
|
+
self.summaryout = model.vlist(self.summaryvar)
|
|
196
|
+
|
|
197
|
+
self.res = attribution_new(self.model,self.experiments,self.start,self.end,
|
|
198
|
+
summaryvar=self.summaryvar,showtime=1,silent=1,type=type)
|
|
199
|
+
|
|
200
|
+
def explain_last(self,pat='',top=0.9,title='',use='level',threshold=0.0):
|
|
201
|
+
'''
|
|
202
|
+
Explains last period
|
|
203
|
+
|
|
204
|
+
Args:
|
|
205
|
+
pat (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
206
|
+
top (TYPE, optional): DESCRIPTION. Defaults to 0.9.
|
|
207
|
+
title (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
208
|
+
use (TYPE, optional): DESCRIPTION. Defaults to 'level'.
|
|
209
|
+
threshold (TYPE, optional): DESCRIPTION. Defaults to 0.0.
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
fig (TYPE): DESCRIPTION.
|
|
213
|
+
|
|
214
|
+
'''
|
|
215
|
+
if self.go:
|
|
216
|
+
self.impact = mk.GetLastImpact(self.res[use],pat=pat).T
|
|
217
|
+
ntitle = f'Attribution last period, {use}' if title == '' else title
|
|
218
|
+
fig = mv.waterplot(self.impact,autosum=1,allsort=1,top=top,title= title,desdic=self.desdic,threshold=threshold)
|
|
219
|
+
return fig
|
|
220
|
+
|
|
221
|
+
def explain_sum(self,pat='',top=0.9,title='',use='level',threshold=0.0):
|
|
222
|
+
'''
|
|
223
|
+
Explains the sum
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
pat (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
228
|
+
top (TYPE, optional): DESCRIPTION. Defaults to 0.9.
|
|
229
|
+
title (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
230
|
+
use (TYPE, optional): DESCRIPTION. Defaults to 'level'.
|
|
231
|
+
threshold (TYPE, optional): DESCRIPTION. Defaults to 0.0.
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
fig (TYPE): DESCRIPTION.
|
|
235
|
+
|
|
236
|
+
'''
|
|
237
|
+
if self.go:
|
|
238
|
+
self.impact = mk.GetSumImpact(self.res[use],pat=pat).T
|
|
239
|
+
ntitle = f'Attribution, sum over all periods, {use}' if title == '' else title
|
|
240
|
+
fig = mv.waterplot(self.impact,autosum=1,allsort=1,top=top,title=ntitle,desdic=self.desdic,threshold=threshold )
|
|
241
|
+
return fig
|
|
242
|
+
|
|
243
|
+
def explain_per(self,pat='',per='',top=0.9,title='',use='level',threshold=0.0,ysize=5):
|
|
244
|
+
'''
|
|
245
|
+
Explains a periode
|
|
246
|
+
|
|
247
|
+
Args:
|
|
248
|
+
pat (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
249
|
+
per (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
250
|
+
top (TYPE, optional): DESCRIPTION. Defaults to 0.9.
|
|
251
|
+
title (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
252
|
+
use (TYPE, optional): DESCRIPTION. Defaults to 'level'.
|
|
253
|
+
threshold (TYPE, optional): DESCRIPTION. Defaults to 0.0.
|
|
254
|
+
ysize (TYPE, optional): DESCRIPTION. Defaults to 5.
|
|
255
|
+
|
|
256
|
+
Returns:
|
|
257
|
+
fig (TYPE): DESCRIPTION.
|
|
258
|
+
|
|
259
|
+
'''
|
|
260
|
+
if self.go:
|
|
261
|
+
tper = self.res[use].columns.get_level_values(1)[0] if per == '' else per
|
|
262
|
+
self.impact = mk.GetOneImpact(self.res[use],pat=pat,per=tper).T.rename(index=self.desdic)
|
|
263
|
+
t2per = str(tper.date()) if type(tper) == pd._libs.tslibs.timestamps.Timestamp else tper
|
|
264
|
+
ntitle = f'Attribution, {use}: {t2per}' if title == '' else title
|
|
265
|
+
fig = mv.waterplot(self.impact,autosum=1,allsort=1,top=top,title=ntitle,desdic=self.desdic ,threshold=threshold,ysize=ysize)
|
|
266
|
+
return fig
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def explain_allold(self,pat='',stacked=True,kind='bar',top=0.9,title='',use='level',
|
|
270
|
+
threshold=0.0,resample='',axvline=None):
|
|
271
|
+
if self.go:
|
|
272
|
+
years = mdates.YearLocator() # every year
|
|
273
|
+
months = mdates.MonthLocator() # every month
|
|
274
|
+
years_fmt = mdates.DateFormatter('%Y')
|
|
275
|
+
|
|
276
|
+
selected = GetAllImpact(self.res[use],pat)
|
|
277
|
+
grouped = selected.stack().groupby(level=[0])
|
|
278
|
+
fig, axis = plt.subplots(nrows=len(grouped),ncols=1,figsize=(10,5*len(grouped)),constrained_layout=False)
|
|
279
|
+
width = 0.5 # the width of the barsser
|
|
280
|
+
ntitle = f'Attribution, {use}' if title == '' else title
|
|
281
|
+
laxis = axis if isinstance(axis,numpy.ndarray) else [axis]
|
|
282
|
+
for j,((name,dfatt),ax) in enumerate(zip(grouped,laxis)):
|
|
283
|
+
dfatt.index = [i[1] for i in dfatt.index]
|
|
284
|
+
if resample=='':
|
|
285
|
+
tempdf=cutout(dfatt.T,threshold).T
|
|
286
|
+
else:
|
|
287
|
+
tempdf=cutout(dfatt.T,threshold).T.resample(resample).mean()
|
|
288
|
+
# pdb.set_trace()
|
|
289
|
+
tempdf.plot(ax=ax,kind=kind,stacked=stacked,title=self.desdic.get(name,name))
|
|
290
|
+
ax.set_ylabel(name,fontsize='x-large')
|
|
291
|
+
# ax.set_xticklabels(tempdf.index.tolist(), rotation = 45,fontsize='x-large')
|
|
292
|
+
## ax.xaxis.set_minor_locator(plt.NullLocator())
|
|
293
|
+
## ax.tick_params(axis='x', labelleft=True)
|
|
294
|
+
# ax.xaxis.set_major_locator(years)
|
|
295
|
+
# ax.xaxis_date()
|
|
296
|
+
# ax.xaxis.set_major_formatter(years_fmt)
|
|
297
|
+
# ax.xaxis.set_minor_locator(months)
|
|
298
|
+
# ax.tick_params(axis='x', labelrotation=45,right = True)
|
|
299
|
+
if type(axvline) != type(None): axis.axvline(axvline)
|
|
300
|
+
fig.suptitle(ntitle,fontsize=20)
|
|
301
|
+
if 1:
|
|
302
|
+
plt.tight_layout()
|
|
303
|
+
fig.subplots_adjust(top=top)
|
|
304
|
+
|
|
305
|
+
return fig
|
|
306
|
+
|
|
307
|
+
def explain_all(self,pat='',stacked=True,kind='bar',top=0.9,title='',use='level',
|
|
308
|
+
threshold=0.0,resample='',axvline=None):
|
|
309
|
+
'''
|
|
310
|
+
Explains all
|
|
311
|
+
|
|
312
|
+
Args:
|
|
313
|
+
pat (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
314
|
+
stacked (TYPE, optional): DESCRIPTION. Defaults to True.
|
|
315
|
+
kind (TYPE, optional): DESCRIPTION. Defaults to 'bar'.
|
|
316
|
+
top (TYPE, optional): DESCRIPTION. Defaults to 0.9.
|
|
317
|
+
title (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
318
|
+
use (TYPE, optional): DESCRIPTION. Defaults to 'level'.
|
|
319
|
+
threshold (TYPE, optional): DESCRIPTION. Defaults to 0.0.
|
|
320
|
+
resample (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
321
|
+
axvline (TYPE, optional): DESCRIPTION. Defaults to None.
|
|
322
|
+
|
|
323
|
+
Returns:
|
|
324
|
+
None.
|
|
325
|
+
|
|
326
|
+
'''
|
|
327
|
+
import warnings
|
|
328
|
+
if self.go:
|
|
329
|
+
years = mdates.YearLocator() # every year
|
|
330
|
+
months = mdates.MonthLocator() # every month
|
|
331
|
+
years_fmt = mdates.DateFormatter('%Y')
|
|
332
|
+
|
|
333
|
+
selected = GetAllImpact(self.res[use],pat)
|
|
334
|
+
grouped = selected.stack().groupby(level=[0])
|
|
335
|
+
fig, axis = plt.subplots(nrows=len(grouped),ncols=1,figsize=(10,5*len(grouped)),constrained_layout=False)
|
|
336
|
+
width = 0.5 # the width of the barsser
|
|
337
|
+
ntitle = f'Attribution, {use}' if title == '' else title
|
|
338
|
+
laxis = axis if isinstance(axis,numpy.ndarray) else [axis]
|
|
339
|
+
for j,((name,dfatt),ax) in enumerate(zip(grouped,laxis)):
|
|
340
|
+
dfatt.index = [i[1] for i in dfatt.index]
|
|
341
|
+
if resample=='':
|
|
342
|
+
tempdf=cutout(dfatt.T,threshold).T
|
|
343
|
+
else:
|
|
344
|
+
tempdf=cutout(dfatt.T,threshold).T.resample(resample).mean()
|
|
345
|
+
# pdb.set_trace()
|
|
346
|
+
selfstack = (kind == 'line' or kind == 'area') and stacked
|
|
347
|
+
tempdf = tempdf.rename(columns=self.desdic)
|
|
348
|
+
with warnings.catch_warnings():
|
|
349
|
+
warnings.simplefilter("ignore", category=UserWarning)
|
|
350
|
+
if selfstack:
|
|
351
|
+
df_neg, df_pos =tempdf.clip(upper=0), tempdf.clip(lower=0)
|
|
352
|
+
df_pos.plot(ax=ax,kind=kind,stacked=stacked,title=self.desdic.get(name,name))
|
|
353
|
+
ax.set_prop_cycle(None)
|
|
354
|
+
df_neg.plot(ax=ax,legend=False,kind=kind,stacked=stacked,title=self.desdic.get(name,name))
|
|
355
|
+
ax.set_ylim([df_neg.sum(axis=1).min(), df_pos.sum(axis=1).max()])
|
|
356
|
+
else:
|
|
357
|
+
tempdf.plot(ax=ax,kind=kind,stacked=stacked,title=self.desdic.get(name,name))
|
|
358
|
+
ax.xaxis.set_major_locator(plt.MaxNLocator(10))
|
|
359
|
+
ax.set_ylabel(name,fontsize='x-large')
|
|
360
|
+
# ax.set_xticklabels(tempdf.index.tolist(), rotation = 45,fontsize='x-large')
|
|
361
|
+
## ax.xaxis.set_minor_locator(plt.NullLocator())
|
|
362
|
+
## ax.tick_params(axis='x', labelleft=True)
|
|
363
|
+
# ax.xaxis.set_major_locator(years)
|
|
364
|
+
# ax.xaxis_date()
|
|
365
|
+
# ax.xaxis.set_major_formatter(years_fmt)
|
|
366
|
+
# ax.xaxis.set_minor_locator(months)
|
|
367
|
+
# ax.tick_params(axis='x', labelrotation=45,right = True)
|
|
368
|
+
if type(axvline) != type(None): axis.axvline(axvline)
|
|
369
|
+
fig.suptitle(ntitle,fontsize=20)
|
|
370
|
+
if 1:
|
|
371
|
+
plt.tight_layout()
|
|
372
|
+
fig.subplots_adjust(top=top)
|
|
373
|
+
|
|
374
|
+
return fig
|
|
375
|
+
|
|
376
|
+
#
|
|
377
|
+
def totexplain(self,pat='*',vtype='all',stacked=True,kind='bar',per='',top=0.9,title=''
|
|
378
|
+
,use='level',threshold=0.0,ysize=10,**kwargs):
|
|
379
|
+
'''
|
|
380
|
+
Wrapper for different explanations
|
|
381
|
+
- :any:`explain_last`
|
|
382
|
+
- :any:`explain_per`
|
|
383
|
+
- :any:`explain_sum`
|
|
384
|
+
- :any:`explain_all`
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
Args:
|
|
388
|
+
pat (TYPE, optional): DESCRIPTION. Defaults to '*'.
|
|
389
|
+
vtype (per|all|last|sum, optional): what data to attribute. Defaults to 'all'.
|
|
390
|
+
stacked (TYPE, optional): DESCRIPTION. Defaults to True.
|
|
391
|
+
kind (TYPE, optional): DESCRIPTION. Defaults to 'bar'.
|
|
392
|
+
per (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
393
|
+
top (TYPE, optional): DESCRIPTION. Defaults to 0.9.
|
|
394
|
+
title (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
395
|
+
use (TYPE, optional): DESCRIPTION. Defaults to 'level'.
|
|
396
|
+
threshold (TYPE, optional): DESCRIPTION. Defaults to 0.0.
|
|
397
|
+
ysize (TYPE, optional): DESCRIPTION. Defaults to 10.
|
|
398
|
+
**kwargs (TYPE): DESCRIPTION.
|
|
399
|
+
|
|
400
|
+
Returns:
|
|
401
|
+
fig (TYPE): DESCRIPTION.
|
|
402
|
+
|
|
403
|
+
'''
|
|
404
|
+
if vtype.upper() == 'PER' :
|
|
405
|
+
fig = self.explain_per(pat=pat,per=per,top=top,use=use,title=title,threshold=threshold,ysize=ysize)
|
|
406
|
+
|
|
407
|
+
elif vtype.upper() == 'LAST' :
|
|
408
|
+
fig = self.explain_last(pat=pat,top=top,use=use,title=title,threshold=threshold)
|
|
409
|
+
|
|
410
|
+
elif vtype.upper() == 'SUM' :
|
|
411
|
+
fig = self.explain_sum(pat=pat,top=top,use=use,title=title,threshold=threshold)
|
|
412
|
+
|
|
413
|
+
else:
|
|
414
|
+
fig = self.explain_all(pat=pat,stacked=stacked,kind=kind,top=top,use=use,title=title,threshold=threshold)
|
|
415
|
+
return fig
|
|
416
|
+
|
|
417
|
+
# def get_att_gui(self,var='FY',spat = '*',desdic={},use='level'):
|
|
418
|
+
# '''Creates a jupyter ipywidget to display model level
|
|
419
|
+
# attributions '''
|
|
420
|
+
# def show_all2(Variable,Periode,Save,Use):
|
|
421
|
+
# global fig1,fig2
|
|
422
|
+
# fig1 = self.totexplain(pat=Variable,top=0.87,use=Use)
|
|
423
|
+
# fig2 = self.totexplain(pat=Variable,vtype='per',per = Periode,top=0.85,use=Use)
|
|
424
|
+
# if Save:
|
|
425
|
+
# fig1.savefig(f'Attribution-{Variable}-{use}.pdf')
|
|
426
|
+
# fig2.savefig(f'Attribution-{Variable}-{Periode}-{use}.pdf')
|
|
427
|
+
# print(f'Attribution-{Variable}-{use}.pdf and Attribution-{Variable}-{Periode}-{use}.pdf aare saved' )
|
|
428
|
+
#
|
|
429
|
+
# show = ip.interactive(show_all2,
|
|
430
|
+
# Variable = ip.Dropdown(options = sorted(self.model.endogene),value=var),
|
|
431
|
+
# Periode = self.model.current_per,
|
|
432
|
+
# Use = ip.RadioButtons(options= ['level', 'growth'],description='Use'),
|
|
433
|
+
# Save = False,
|
|
434
|
+
# )
|
|
435
|
+
# return show
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
if __name__ == '__main__' :
|
|
439
|
+
#%%
|
|
440
|
+
# running withe the mtotal model
|
|
441
|
+
df2 = pd.DataFrame({'Z':[1., 22., 33,43] , 'TY':[10.,20.,30.,40.] ,'YD':[10.,20.,30.,40.]},index=[2017,2018,2019,2020])
|
|
442
|
+
df3 = pd.DataFrame({'Z':[1., 22., 33,43] , 'TY':[10.,40.,60.,10.] ,'YD':[10.,49.,36.,40.]},index=[2017,2018,2019,2020])
|
|
443
|
+
ftest = '''
|
|
444
|
+
FRMl <> ii = TY(-1)+c(-1)+Z*c(-1) $
|
|
445
|
+
frml <> c=0.8*yd+log(1) $
|
|
446
|
+
frml <> d = c +2*ii(-1) $
|
|
447
|
+
frml <> c2=0.8*yd+log(1) $
|
|
448
|
+
frml <> d2 = c + 42*ii $
|
|
449
|
+
frml <> c3=0.8*yd+log(1) $
|
|
450
|
+
frml <> d3 = c +ii $
|
|
451
|
+
'''
|
|
452
|
+
|
|
453
|
+
m2=mc.model(ftest,straight=True,modelname='m2 testmodel')
|
|
454
|
+
df2=mc.insertModelVar(df2,m2)
|
|
455
|
+
df3=mc.insertModelVar(df3,m2)
|
|
456
|
+
z1 = m2(df2)
|
|
457
|
+
z2 = m2(df3)
|
|
458
|
+
ccc = m2.totexplain(pat='D2',per=2019,vtype='all',top=0.8)
|
|
459
|
+
ccc = m2.totexplain('D2',vtype='last',top=0.8)
|
|
460
|
+
ccc = m2.totexplain('D2',vtype='per',top=0.8)
|
|
461
|
+
#%%
|
|
462
|
+
ddd = totdif(m2)
|
|
463
|
+
eee = totdif(m2)
|
|
464
|
+
ddd.totexplain('D2',vtype='all',top=0.8,use='growth');
|
|
465
|
+
eee.totexplain('D2',vtype='all',top=0.8);
|
|
466
|
+
|
|
467
|
+
if False and ( not 'mtotal' in locals() ) :
|
|
468
|
+
# get the model
|
|
469
|
+
with open(r"models\mtotal.fru", "r") as text_file:
|
|
470
|
+
ftotal = text_file.read()
|
|
471
|
+
|
|
472
|
+
#get the data
|
|
473
|
+
base0 = pd.read_pickle(r'data\base0.pc')
|
|
474
|
+
base = pd.read_pickle(r'data\base.pc')
|
|
475
|
+
adve0 = pd.read_pickle(r'data\adve0.pc')
|
|
476
|
+
#%%
|
|
477
|
+
mtotal = mc.model(ftotal)
|
|
478
|
+
|
|
479
|
+
# prune(mtotal,base)
|
|
480
|
+
#%%
|
|
481
|
+
baseny = mtotal(base0 ,'2016q1','2018q4',samedata=False)
|
|
482
|
+
adverseny = mtotal(adve0 ,'2016q1','2018q4',samedata=True)
|
|
483
|
+
#%%
|
|
484
|
+
diff = mtotal.exodif() # exogeneous variables which are different between baseny and adverseny
|
|
485
|
+
#%%
|
|
486
|
+
assert 1==2 # just for stopping in test situations
|
|
487
|
+
#%%
|
|
488
|
+
adverseny = mtotal(adve0 ,'2016q1','2018q4',samedata=True) # to makew sure we have the right adverse.
|
|
489
|
+
countries = {c.split('__')[2] for c in diff.columns} # list of countries
|
|
490
|
+
countryexperiments = {e: [c for c in diff.columns if ('__'+e+'__') in c] for e in countries } # dic of experiments
|
|
491
|
+
assert len(diff.columns) == sum([len(c) for c in countryexperiments.values()]) , 'Not all exogeneous chocks variables are accountet for'
|
|
492
|
+
countryimpact = attribution(mtotal,countryexperiments,save='countryimpactxx',maxexp=30000,showtime = 1)
|
|
493
|
+
#%%
|
|
494
|
+
adverseny = mtotal(adve0 ,'2016q1','2018q4',samedata=True)
|
|
495
|
+
vartypes = {c.split('__') [1] for c in diff.columns}
|
|
496
|
+
vartypeexperiments = {e: [c for c in diff.columns if ('__'+e+'__') in c] for e in vartypes }
|
|
497
|
+
assert len(diff.columns) == sum([len(c) for c in vartypeexperiments.values()]) , 'Not all exogeneous chocks variables are accountet for'
|
|
498
|
+
vartypeimpact = attribution(mtotal,vartypeexperiments,save='vartypeimpactxx',maxexp=3000,showtime=1)
|
|
499
|
+
##%%
|
|
500
|
+
#adverseny = mtotal(adve0 ,'2016q1','2018q4',samedata=True)
|
|
501
|
+
#allexo = {c[7:14] for c in diff.columns}
|
|
502
|
+
#allexoexperiments = {e: [c for c in diff.columns if ('__'+e+'__') in c] for e in allexo }
|
|
503
|
+
#allexoimpact = attribution(mtotal,allexoexperiments,base,adverseny,save='allexoimpact',maxexp=2000)
|
|
504
|
+
#%% test of upddf
|
|
505
|
+
if 0:
|
|
506
|
+
baseny = mtotal(base0 ,'2016q1','2018q4',samedata=False)
|
|
507
|
+
adverseny = mtotal(adve0 ,'2016q1','2018q4',samedata=True)
|
|
508
|
+
#%%
|
|
509
|
+
e = 'EE'
|
|
510
|
+
var = countryexperiments['EE']
|
|
511
|
+
vardiff = diff[var]
|
|
512
|
+
temp = adverseny[var].copy()
|
|
513
|
+
adverseny[var] = baseny[var]
|
|
514
|
+
temp2 = adverseny[var].copy()
|
|
515
|
+
_ = mc.upddf(adverseny,temp)
|
|
516
|
+
adverseny[var] = temp
|
|
517
|
+
temp3 = adverseny[var].copy()
|
|
518
|
+
|