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
modelmf.py
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
This is a module for extending pandas dataframes with the modelflow toolbox
|
|
5
|
+
|
|
6
|
+
Created on Sat March 2019
|
|
7
|
+
|
|
8
|
+
@author: hanseni
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import pandas as pd
|
|
12
|
+
from collections import namedtuple
|
|
13
|
+
import inspect
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
from modelclass import model
|
|
18
|
+
import modelvis as mv
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
if not hasattr(pd.DataFrame,'mf') or 0:
|
|
22
|
+
@pd.api.extensions.register_dataframe_accessor("mf")
|
|
23
|
+
class mf():
|
|
24
|
+
'''A class to extend Pandas Dataframes with ModelFlow functionalities
|
|
25
|
+
|
|
26
|
+
Not to be used on its own
|
|
27
|
+
|
|
28
|
+
'''
|
|
29
|
+
|
|
30
|
+
def __init__(self, pandas_obj):
|
|
31
|
+
# self._validate(pandas_obj)
|
|
32
|
+
self._obj = pandas_obj
|
|
33
|
+
self.modelopt = {}
|
|
34
|
+
self.solveopt = {}
|
|
35
|
+
self.eq = ''
|
|
36
|
+
# print(self._obj)
|
|
37
|
+
|
|
38
|
+
def _setmodel(self,m):
|
|
39
|
+
''' a function to be prepared'''
|
|
40
|
+
self.model = m
|
|
41
|
+
|
|
42
|
+
def copy(self):
|
|
43
|
+
''' copy a modelflow extended dataframe, so it remember its model and options'''
|
|
44
|
+
|
|
45
|
+
res = self._obj.copy()
|
|
46
|
+
|
|
47
|
+
res.mf._setmodel(self.model)
|
|
48
|
+
res.mf.modelopt = self.modelopt
|
|
49
|
+
res.mf.solveopt = self.solveopt
|
|
50
|
+
res.mf.eq = self.eq
|
|
51
|
+
return res
|
|
52
|
+
|
|
53
|
+
def solve(self,start='',slut='',**kwargs):
|
|
54
|
+
'''Solves a model'''
|
|
55
|
+
this = self._obj
|
|
56
|
+
# print(kwargs)
|
|
57
|
+
self.solveopt = {**self.solveopt,**kwargs,**{'start':start,'slut':slut}}
|
|
58
|
+
# print(self.solveopt)
|
|
59
|
+
# print(self.solveopt)
|
|
60
|
+
res = self.model(this,**self.solveopt)
|
|
61
|
+
res.mf._setmodel(self.model)
|
|
62
|
+
res.mf.modelopt = self.modelopt
|
|
63
|
+
res.mf.solveopt = self.solveopt
|
|
64
|
+
return res
|
|
65
|
+
|
|
66
|
+
def makemodel(self,eq,**kwargs):
|
|
67
|
+
'''Makes a model from equations'''
|
|
68
|
+
self.modelopt = {**self.modelopt,**kwargs}
|
|
69
|
+
self.eq = eq
|
|
70
|
+
self.model = model.from_eq(self.eq,**kwargs)
|
|
71
|
+
return self.copy()
|
|
72
|
+
|
|
73
|
+
def __call__(self,eq='',**kwargs):
|
|
74
|
+
'''
|
|
75
|
+
returns a model instace
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
eq (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
79
|
+
**kwargs (TYPE): DESCRIPTION.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
model: a instance .
|
|
83
|
+
|
|
84
|
+
'''
|
|
85
|
+
if self.eq is eq:
|
|
86
|
+
return self
|
|
87
|
+
else:
|
|
88
|
+
res = self.makemodel(eq,**kwargs)
|
|
89
|
+
return res.mf
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def __getattr__(self, name):
|
|
93
|
+
try:
|
|
94
|
+
# print('dddddd',name)
|
|
95
|
+
return self.model.__getattribute__(name)
|
|
96
|
+
except:
|
|
97
|
+
pass
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
# print('dddddd',name)
|
|
101
|
+
return self.model.__getattribute__(name.upper())
|
|
102
|
+
except:
|
|
103
|
+
pass
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
return mv.varvis(self.model,name.upper())
|
|
107
|
+
except:
|
|
108
|
+
print(f'Error calling this attribute: {name}')
|
|
109
|
+
raise AttributeError
|
|
110
|
+
pass
|
|
111
|
+
|
|
112
|
+
def __getitem__(self, name):
|
|
113
|
+
'''Retrieves __getitem__ from model instance'''
|
|
114
|
+
a=self.model.__getitem__(name)
|
|
115
|
+
return a
|
|
116
|
+
|
|
117
|
+
def __dir__(self):
|
|
118
|
+
''' to help tab-completion '''
|
|
119
|
+
res = list(self.__dict__.keys())
|
|
120
|
+
return res
|
|
121
|
+
|
|
122
|
+
@pd.api.extensions.register_dataframe_accessor("mfcalc")
|
|
123
|
+
class mfcalc():
|
|
124
|
+
'''
|
|
125
|
+
Used to carry out calculation specified as equations
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
eq (TYPE): Equations one on each line. can be started with <start end> to control calculation sample .
|
|
129
|
+
start (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
130
|
+
slut (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
131
|
+
showeq (TYPE, optional): If True the equations will be printed. Defaults to False.
|
|
132
|
+
**kwargs (TYPE): Here all solve options can be provided.
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
Dataframe.
|
|
136
|
+
|
|
137
|
+
'''
|
|
138
|
+
def __init__(self, pandas_obj):
|
|
139
|
+
# self._validate(pandas_obj)
|
|
140
|
+
self._obj = pandas_obj
|
|
141
|
+
# print(self._obj)
|
|
142
|
+
|
|
143
|
+
def __call__(self,eq,start='',slut='',showeq=False, **kwargs):
|
|
144
|
+
'''
|
|
145
|
+
This call performs the calculation
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
eq (TYPE): Equations one on each line. can be started with <start end> to control calculation sample .
|
|
149
|
+
start (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
150
|
+
slut (TYPE, optional): DESCRIPTION. Defaults to ''.
|
|
151
|
+
showeq (TYPE, optional): If True the equations will be printed. Defaults to False.
|
|
152
|
+
**kwargs (TYPE): Here all solve options can be provided.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
Dataframe.
|
|
156
|
+
|
|
157
|
+
'''
|
|
158
|
+
|
|
159
|
+
# print({**kwargs,**{'start':start,'slut':slut}})
|
|
160
|
+
if (l0:=eq.strip()).startswith('<'):
|
|
161
|
+
timesplit = l0.split('>',1)
|
|
162
|
+
time_options = timesplit[0].replace('<','').replace(',',' ').replace(':',' ').replace('/',' ').split()
|
|
163
|
+
|
|
164
|
+
if len(time_options)==1:
|
|
165
|
+
start = time_options[0]
|
|
166
|
+
slut = time_options[0]
|
|
167
|
+
elif len(time_options)==2:
|
|
168
|
+
start,slut = time_options
|
|
169
|
+
else:
|
|
170
|
+
raise Exception(f'To many times \nOffending:"{l0}"')
|
|
171
|
+
xeq = timesplit[1]
|
|
172
|
+
else:
|
|
173
|
+
xeq=eq
|
|
174
|
+
|
|
175
|
+
res = self._obj.mf(xeq,**kwargs).solve(**{**kwargs,**{'start':start,'slut':slut,'silent':True}})
|
|
176
|
+
# print('jddd')
|
|
177
|
+
if showeq:
|
|
178
|
+
print(self._obj.mf.equations)
|
|
179
|
+
return res
|
|
180
|
+
|
|
181
|
+
@pd.api.extensions.register_dataframe_accessor("mfupdate")
|
|
182
|
+
class mfupdate():
|
|
183
|
+
'''Extend a dataframe to update with values from another dataframe'''
|
|
184
|
+
def __init__(self, pandas_obj):
|
|
185
|
+
# self._validate(pandas_obj)
|
|
186
|
+
self._obj = pandas_obj
|
|
187
|
+
# print(self._obj)
|
|
188
|
+
|
|
189
|
+
def second(self,df,safe=True):
|
|
190
|
+
this = self._obj.copy(deep=True)
|
|
191
|
+
col=df.columns
|
|
192
|
+
index = df.index
|
|
193
|
+
if safe: # if dooing many experiments, we dont want this to pappen
|
|
194
|
+
assert 0 == len(set(index) - set(this.index)), 'Index in update not in dataframe'
|
|
195
|
+
assert 0 == len(set(col) - set(this.columns)),'Column in update not in dataframe'
|
|
196
|
+
this.loc[index,col] = df.loc[index,col]
|
|
197
|
+
return this
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def __call__(self,df):
|
|
201
|
+
return self.second(df)
|
|
202
|
+
|
|
203
|
+
@pd.api.extensions.register_dataframe_accessor("ibloc")
|
|
204
|
+
class ibloc():
|
|
205
|
+
'''Extend a dataframe with a slice method which accepot wildcards in column selection.
|
|
206
|
+
|
|
207
|
+
The method just juse the method vlist from modelclass.model class '''
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def __init__(self, pandas_obj):
|
|
211
|
+
# self._validate(pandas_obj)
|
|
212
|
+
self._obj = pandas_obj
|
|
213
|
+
# print(self._obj)
|
|
214
|
+
|
|
215
|
+
def __getitem__(self, select):
|
|
216
|
+
m = model()
|
|
217
|
+
m.lastdf = self._obj
|
|
218
|
+
# print(select)
|
|
219
|
+
vars = m.vlist(select)
|
|
220
|
+
return self._obj.loc[:,vars]
|
|
221
|
+
|
|
222
|
+
def __call__(self):
|
|
223
|
+
''' Not in use'''
|
|
224
|
+
print('hello from ibloc')
|
|
225
|
+
return
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def f(a):
|
|
229
|
+
return 42
|
|
230
|
+
|
|
231
|
+
if __name__ == '__main__':
|
|
232
|
+
#this is for testing
|
|
233
|
+
df = pd.DataFrame({'Z': [1., 2., 3,4] , 'TY':[10.,20.,30.,40.] ,'YD':[10.,20.,30.,40.]},index= [2017,2018,2019,2020])
|
|
234
|
+
df1 = pd.DataFrame({'Z': [1., 2., 3,4] , 'TY':[10.,20.,30.,40.] ,'YD':[10.,20.,30.,40.]},index= [2017,2018,2019,2020])
|
|
235
|
+
df2 = pd.DataFrame({'Z':[1., 22., 33,43] , 'TY':[10.,20.,30.,40.] ,'YD':[10.,20.,30.,40.]},index=[2017,2018,2019,2020])
|
|
236
|
+
df3 = pd.DataFrame({'Z':[1., 22., 33,43] , 'TY':[10.,20.,30.,40.] ,'YD':[10.,20.,30.,40.]},index=[2017,2018,2019,2020])
|
|
237
|
+
df4 = pd.DataFrame({'Z':[ 223., 333] , 'TY':[203.,303.] },index=[2018,2019])
|
|
238
|
+
ftest = '''
|
|
239
|
+
ii = x+z
|
|
240
|
+
c=0.8*yd
|
|
241
|
+
i = ii+iy
|
|
242
|
+
x = f(2)
|
|
243
|
+
y = c + i + x+ i(-1)
|
|
244
|
+
yX = 0.02
|
|
245
|
+
dogplace = y *4 '''
|
|
246
|
+
assert 1==1
|
|
247
|
+
#%%
|
|
248
|
+
d22=(m2:=model.from_eq(ftest,funks=[f]))(df4)
|
|
249
|
+
aa = df.mf(ftest,funks=[f]).solve()
|
|
250
|
+
aa.mf.solve()
|
|
251
|
+
aa.mf.drawmodel()
|
|
252
|
+
aa.mf.drawendo()
|
|
253
|
+
# aa.mf.YX.draw()
|
|
254
|
+
aa.mf.istopo
|
|
255
|
+
aa.mf['*']
|
|
256
|
+
df2.mfcalc(ftest,funks=[f],showeq=1)
|
|
257
|
+
df2.mf.Y.draw(all=True,pdf=0)
|
|
258
|
+
#%%
|
|
259
|
+
df4 = pd.DataFrame({'Z':[ 223., 333] , 'YD':[203.,303.] },index=[2018,2019])
|
|
260
|
+
x = df2.mfupdate(df4)
|
|
261
|
+
#%% test graph
|
|
262
|
+
import networkx as nx
|
|
263
|
+
zz = df2.mf.totgraph
|
|
264
|
+
nx.draw(zz)
|
|
265
|
+
|
modelnet.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Wed Oct 15 14:30:44 2014
|
|
4
|
+
|
|
5
|
+
Displays an adjencacy matrix
|
|
6
|
+
.
|
|
7
|
+
@author: ibh
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import print_function
|
|
12
|
+
import networkx as nx
|
|
13
|
+
from itertools import groupby, chain
|
|
14
|
+
from collections import OrderedDict,Counter
|
|
15
|
+
import numpy as np
|
|
16
|
+
from matplotlib import pyplot, patches
|
|
17
|
+
import matplotlib.pyplot as plt
|
|
18
|
+
import matplotlib as mpl
|
|
19
|
+
import pandas as pd
|
|
20
|
+
import seaborn as sns
|
|
21
|
+
from matplotlib import colors
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def draw_adjacency_matrix(G, node_order=None, partitions=None,type=False,title='Structure',size=(10,10)):
|
|
25
|
+
"""
|
|
26
|
+
- G is a netorkx graph
|
|
27
|
+
- node_order (optional) is a list of nodes, where each node in G
|
|
28
|
+
appears exactly once
|
|
29
|
+
- partitions is a list of node lists, where each node in G appears
|
|
30
|
+
in exactly one node list
|
|
31
|
+
|
|
32
|
+
- type is a list of saying "simultaneous" or something else, has to have same length as partitions
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
df = nx.to_pandas_adjacency(G,nodelist=node_order).T
|
|
36
|
+
|
|
37
|
+
fig, ax = plt.subplots(figsize=size)
|
|
38
|
+
plt.title(title,fontsize=20)
|
|
39
|
+
cmap1 = colors.ListedColormap(['white','blue'])
|
|
40
|
+
norm1 = colors.BoundaryNorm([0.,0.3,30], cmap1.N)
|
|
41
|
+
sns.heatmap(df,ax=ax,cbar=False,square=True,cmap=cmap1,norm=norm1)
|
|
42
|
+
plt.xlabel('This variable is input to', fontsize=18)
|
|
43
|
+
plt.ylabel('The formular for this variable', fontsize=18)
|
|
44
|
+
|
|
45
|
+
color = 'blue'
|
|
46
|
+
current_idx = 0
|
|
47
|
+
if partitions:
|
|
48
|
+
for i,module in enumerate(partitions):
|
|
49
|
+
if type:
|
|
50
|
+
if len(type[i]) and type[i].startswith('Simultaneous'):
|
|
51
|
+
ax.add_patch(patches.Rectangle((current_idx, current_idx),
|
|
52
|
+
len(module), # Width
|
|
53
|
+
len(module), # Height
|
|
54
|
+
facecolor=[1.,0,0,0.5]))
|
|
55
|
+
elif len(type[i]) and type[i] == 'Recursiv':
|
|
56
|
+
ax.add_patch(patches.Polygon([(current_idx, current_idx),
|
|
57
|
+
(current_idx, current_idx+len(module)),
|
|
58
|
+
(current_idx+len(module), current_idx+len(module))],
|
|
59
|
+
facecolor=[0,1,0,0.5]))
|
|
60
|
+
elif len(type[i]) and type[i] == 'Endogeneous':
|
|
61
|
+
ax.add_patch(patches.Rectangle((current_idx, current_idx),
|
|
62
|
+
len(module), # Width
|
|
63
|
+
len(module), # Height
|
|
64
|
+
facecolor=[1.,0,0,0.5]))
|
|
65
|
+
elif len(type[i]) and type[i] == 'Exogeneous':
|
|
66
|
+
ax.add_patch(patches.Rectangle(( current_idx,0),
|
|
67
|
+
len(module), # Width
|
|
68
|
+
current_idx, # Height
|
|
69
|
+
facecolor=[1.,1,0,0.5]))
|
|
70
|
+
else:
|
|
71
|
+
ax.add_patch(patches.Rectangle((current_idx, current_idx),
|
|
72
|
+
len(module), # Width
|
|
73
|
+
len(module), # Height
|
|
74
|
+
facecolor=[1.,0,0,0.5]))
|
|
75
|
+
current_idx += len(module)
|
|
76
|
+
if 'Recursiv' in type or 'Simultaneous' in type :
|
|
77
|
+
firkant = patches.Rectangle((0,0),1,1,facecolor=[1.,0,0,1], label='Feedback block')
|
|
78
|
+
trekant = patches.Polygon([(0, 0),(0, 1),(1, 3)],facecolor=[0,1,0,1], label='No feedback block')
|
|
79
|
+
ax.legend(handles=[firkant,trekant],loc=1,fontsize=18)
|
|
80
|
+
|
|
81
|
+
if 'Endogeneous' in type or 'Exogeneous' in type :
|
|
82
|
+
firkant = patches.Rectangle((0,0),1,1,facecolor=[1.,0,0,1], label='Endogeneous variables')
|
|
83
|
+
trekant = patches.Polygon([(0, 0),(0, 1),(1, 3)],facecolor=[1,1,0,1], label='Exogeneous variables')
|
|
84
|
+
ax.legend(handles=[firkant,trekant],loc=3,fontsize=18)
|
|
85
|
+
return fig
|
|
86
|
+
|
|
87
|
+
def drawendoexo(model,size=(6.,6.)):
|
|
88
|
+
''' Draw dependency including exogeneous. Used for illustrating for small models'''
|
|
89
|
+
fig = draw_adjacency_matrix(model.totgraph_nolag,
|
|
90
|
+
list(chain(model.endogene,model.exogene)),[model.endogene,model.exogene],
|
|
91
|
+
['Endogeneous','Exogeneous'], title = 'Dependencies' ,size=size)
|
|
92
|
+
return fig
|
|
93
|
+
|
|
94
|
+
if __name__ == '__main__' :
|
|
95
|
+
#%%
|
|
96
|
+
ftest = ''' FRMl <> y = c + i + x $
|
|
97
|
+
FRMl <> yd = 0.6 * y + y0 $
|
|
98
|
+
FRMl <> c= 0.6 * yd + 0.2 *yd(-1) $
|
|
99
|
+
FRMl <> i = I0 $
|
|
100
|
+
FRMl <> ii = i *2 $
|
|
101
|
+
FRMl <> x = 2 $
|
|
102
|
+
frml <> dog = y $'''
|
|
103
|
+
import modelclass as mc
|
|
104
|
+
mtest = mc.model(ftest)
|
|
105
|
+
draw_adjacency_matrix(mtest.totgraph_nolag)
|
|
106
|
+
plt.show()
|
|
107
|
+
draw_adjacency_matrix(mtest.endograph,mtest.strongorder,mtest.strongblock,mtest.strongtype)
|
|
108
|
+
plt.show()
|
|
109
|
+
#%%
|
|
110
|
+
mtest.draw('Y')
|
|
111
|
+
mtest.drawmodel()
|
|
112
|
+
#%%
|
|
113
|
+
|
|
114
|
+
drawendoexo(mtest)
|