ModelFlowIb 2.53__tar.gz → 2.55__tar.gz

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.
Files changed (51) hide show
  1. {modelflowib-2.53 → modelflowib-2.55}/ModelFlowIb.egg-info/PKG-INFO +2 -2
  2. {modelflowib-2.53 → modelflowib-2.55}/PKG-INFO +2 -2
  3. {modelflowib-2.53 → modelflowib-2.55}/modelclass.py +57 -37
  4. {modelflowib-2.53 → modelflowib-2.55}/modelgrabwf2.py +74 -26
  5. {modelflowib-2.53 → modelflowib-2.55}/modelnormalize.py +5 -0
  6. {modelflowib-2.53 → modelflowib-2.55}/modelreport.py +5 -2
  7. {modelflowib-2.53 → modelflowib-2.55}/pyproject.toml +1 -1
  8. {modelflowib-2.53 → modelflowib-2.55}/ModelFlowIb.egg-info/SOURCES.txt +0 -0
  9. {modelflowib-2.53 → modelflowib-2.55}/ModelFlowIb.egg-info/dependency_links.txt +0 -0
  10. {modelflowib-2.53 → modelflowib-2.55}/ModelFlowIb.egg-info/requires.txt +0 -0
  11. {modelflowib-2.53 → modelflowib-2.55}/ModelFlowIb.egg-info/top_level.txt +0 -0
  12. {modelflowib-2.53 → modelflowib-2.55}/modelBLfunk.py +0 -0
  13. {modelflowib-2.53 → modelflowib-2.55}/model_Excel.py +0 -0
  14. {modelflowib-2.53 → modelflowib-2.55}/model_cvx.py +0 -0
  15. {modelflowib-2.53 → modelflowib-2.55}/model_doable.py +0 -0
  16. {modelflowib-2.53 → modelflowib-2.55}/model_dynare.py +0 -0
  17. {modelflowib-2.53 → modelflowib-2.55}/model_financial_stability.py +0 -0
  18. {modelflowib-2.53 → modelflowib-2.55}/model_ifrs9.py +0 -0
  19. {modelflowib-2.53 → modelflowib-2.55}/model_latex.py +0 -0
  20. {modelflowib-2.53 → modelflowib-2.55}/model_latex_class.py +0 -0
  21. {modelflowib-2.53 → modelflowib-2.55}/model_run_numba.py +0 -0
  22. {modelflowib-2.53 → modelflowib-2.55}/modelclass2.py +0 -0
  23. {modelflowib-2.53 → modelflowib-2.55}/modeldash.py +0 -0
  24. {modelflowib-2.53 → modelflowib-2.55}/modeldashboot.py +0 -0
  25. {modelflowib-2.53 → modelflowib-2.55}/modeldashsidebar.py +0 -0
  26. {modelflowib-2.53 → modelflowib-2.55}/modeldekom.py +0 -0
  27. {modelflowib-2.53 → modelflowib-2.55}/modeldiff.py +0 -0
  28. {modelflowib-2.53 → modelflowib-2.55}/modeldisplay.py +0 -0
  29. {modelflowib-2.53 → modelflowib-2.55}/modelfeedback.py +0 -0
  30. {modelflowib-2.53 → modelflowib-2.55}/modelgrab.py +0 -0
  31. {modelflowib-2.53 → modelflowib-2.55}/modelhelp.py +0 -0
  32. {modelflowib-2.53 → modelflowib-2.55}/modelinvert.py +0 -0
  33. {modelflowib-2.53 → modelflowib-2.55}/modeljupyter.py +0 -0
  34. {modelflowib-2.53 → modelflowib-2.55}/modeljupytermagic.py +0 -0
  35. {modelflowib-2.53 → modelflowib-2.55}/modelmacrograb.py +0 -0
  36. {modelflowib-2.53 → modelflowib-2.55}/modelmanipulation.py +0 -0
  37. {modelflowib-2.53 → modelflowib-2.55}/modelmf.py +0 -0
  38. {modelflowib-2.53 → modelflowib-2.55}/modelnet.py +0 -0
  39. {modelflowib-2.53 → modelflowib-2.55}/modelnewton.py +0 -0
  40. {modelflowib-2.53 → modelflowib-2.55}/modelpattern.py +0 -0
  41. {modelflowib-2.53 → modelflowib-2.55}/modelpatternrev.py +0 -0
  42. {modelflowib-2.53 → modelflowib-2.55}/modelsandbox.py +0 -0
  43. {modelflowib-2.53 → modelflowib-2.55}/modelsandbox_Mixin.py +0 -0
  44. {modelflowib-2.53 → modelflowib-2.55}/modeltodo.py +0 -0
  45. {modelflowib-2.53 → modelflowib-2.55}/modeluserfunk.py +0 -0
  46. {modelflowib-2.53 → modelflowib-2.55}/modeluserfunk_old.py +0 -0
  47. {modelflowib-2.53 → modelflowib-2.55}/modelvis.py +0 -0
  48. {modelflowib-2.53 → modelflowib-2.55}/modelwidget.py +0 -0
  49. {modelflowib-2.53 → modelflowib-2.55}/modelwidget_input.py +0 -0
  50. {modelflowib-2.53 → modelflowib-2.55}/modelwidget_shinypy.py +0 -0
  51. {modelflowib-2.53 → modelflowib-2.55}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: ModelFlowIb
3
- Version: 2.53
3
+ Version: 2.55
4
4
  Summary: A tool to solve and manage dynamic economic models
5
5
  Author-email: Ib Hansen <your@email.com>
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: ModelFlowIb
3
- Version: 2.53
3
+ Version: 2.55
4
4
  Summary: A tool to solve and manage dynamic economic models
5
5
  Author-email: Ib Hansen <your@email.com>
6
6
  License: MIT
@@ -332,8 +332,8 @@ class BaseModel():
332
332
  self.endogene_true = self.endogene if self.normalized else {
333
333
  v for v in self.exogene if v+'___RES' in self.endogene}
334
334
 
335
-
336
- # dummy variables for variables for which to calculate adjustment variables
335
+ # self.check_endo_rhs()
336
+ # sdummy variables for variables for which to calculate adjustment variables
337
337
 
338
338
 
339
339
  self.fix_dummy = sorted(vx+'_D' for vx in self.endogene
@@ -1070,10 +1070,22 @@ class BaseModel():
1070
1070
  len(self.endogene))
1071
1071
  return '<\n'+out+'>'
1072
1072
 
1073
-
1074
- #
1075
-
1076
-
1073
+ def check_endo_rhs(self):
1074
+ #breakpoint()
1075
+ if self.normalized and not self.istopo:
1076
+ errors = []
1077
+ for i,endovar in enumerate(sorted(self.endogene)):
1078
+ if i >= 2000000000000000 :break
1079
+ this_term = self.allvar[endovar]['terms']
1080
+ ap = self.allvar[endovar]['assigpos']
1081
+ lhs = {t.var for t in this_term[:ap] if t.var}
1082
+ rhs_nolag = {t.var for t in this_term[ap:] if t.var and not t.lag}
1083
+ both = lhs & rhs_nolag
1084
+ if both:
1085
+ errors = errors + [f"{endovar} in: {self.allvar[endovar]['frml']}"]
1086
+ if errors:
1087
+ print('Potential problem in model, Left hand side unlagged on right hand')
1088
+ print(*errors,sep='\n')
1077
1089
  class Org_model_Mixin():
1078
1090
  ''' This mixin enrich the Basemodel class with different utility methods.
1079
1091
 
@@ -4927,14 +4939,14 @@ class Display_Mixin():
4927
4939
  return
4928
4940
  allvars = list({c for k, df in self.keep_solutions.items()
4929
4941
  for c in df.columns})
4930
- vars = self.vlist_names(allvars, pat)
4942
+ _vars = self.vlist_names(allvars, pat)
4931
4943
  res = {}
4932
4944
  with self.set_smpl(start, end) as a, self.set_smpl_relative(start_ofset, end_ofset):
4933
4945
  # print(f'{self.current_per[-1]=}')
4934
4946
  for solver, solution in self.keep_solutions.items():
4935
- outv = pd.concat([solution.loc[self.current_per, v] for v in vars ], axis=1)
4947
+ outv = pd.concat([solution.loc[self.current_per, v] for v in _vars ], axis=1)
4936
4948
  if trans:
4937
- outv.columns = [self.var_description[v] for v in vars]
4949
+ outv.columns = [self.var_description[v] for v in _vars]
4938
4950
  outv.columns.names = ['Variable']
4939
4951
  res[solver] = outv
4940
4952
  return res
@@ -4961,10 +4973,12 @@ class Display_Mixin():
4961
4973
  return
4962
4974
  allvars = list({c for k, df in self.keep_solutions.items()
4963
4975
  for c in df.columns})
4964
- vars = self.vlist_names(allvars, pat)
4976
+ _vars = self.vlist_names(allvars, pat)
4977
+ # print(f'xx {pat=} {vars=} {allvars=}')
4978
+
4965
4979
  res = {}
4966
4980
  with self.set_smpl(start, end) as a, self.set_smpl_relative(start_ofset, end_ofset):
4967
- for v in vars:
4981
+ for v in _vars:
4968
4982
  # print(f'{v=}')
4969
4983
  outv = pd.concat([solution.loc[self.current_per, v]
4970
4984
  for solver, solution in self.keep_solutions.items()], axis=1)
@@ -5005,7 +5019,7 @@ class Display_Mixin():
5005
5019
  dfs = self.keep_var_dict(pat, start, end, start_ofset, end_ofset)
5006
5020
 
5007
5021
  if showtype == 'growth':
5008
- dfs = {v: vdf.pct_change()*100. for v, vdf in dfs.items()}
5022
+ dfs = {v: vdf.astype('float').pct_change()*100. for v, vdf in dfs.items()}
5009
5023
 
5010
5024
  elif showtype == 'change':
5011
5025
  dfs = {v: vdf.diff() for v, vdf in dfs.items()}
@@ -5059,6 +5073,7 @@ class Display_Mixin():
5059
5073
 
5060
5074
  """
5061
5075
  # breakpoint()
5076
+ # print(f'{pat=}')
5062
5077
  if showtype == 'growth':
5063
5078
  start_ofset = -1
5064
5079
  else:
@@ -5069,9 +5084,10 @@ class Display_Mixin():
5069
5084
  else:
5070
5085
  dfs = self.keep_var_dict(pat, start, end, start_ofset, trans=False)
5071
5086
 
5072
-
5087
+ # print(f'{dfs=}')
5088
+
5073
5089
  if showtype == 'growth':
5074
- dfs = {v: vdf.pct_change()*100. for v, vdf in dfs.items()}
5090
+ dfs = {v: vdf.astype('float').pct_change()*100. for v, vdf in dfs.items()}
5075
5091
 
5076
5092
  elif showtype == 'qoq_ar':
5077
5093
  dfs = {v: ((1.+vdf.pct_change())**4.-1.)*100. for v, vdf in dfs.items()}
@@ -6089,6 +6105,7 @@ class Json_Mixin():
6089
6105
  mmodel.current_per = current_per
6090
6106
  mmodel.basedf = lastdf.copy()
6091
6107
  return mmodel, lastdf
6108
+ mmodel.check_endo_rhs
6092
6109
 
6093
6110
  class Zip_Mixin():
6094
6111
  '''This experimental class zips a dumped file '''
@@ -8749,8 +8766,8 @@ if __name__ == '__main__':
8749
8766
  #%% test
8750
8767
  smallmodel = '''
8751
8768
  frml <> a = c + b $
8752
- frml <> d1 = x + 3 * a(-1)+ c **2 +a $
8753
- frml <> d3 = x + 3 * a(-1)+c **3 $
8769
+ frml <> d1 = x + 3 * a(-1)+ c **2 +a+ d3 $
8770
+ frml <> d3 = x + 3 * a(-1)+c **3 +d1 + d3 $
8754
8771
  Frml <> xx = 0.5 * c +a$
8755
8772
  frml <CALC_ADJUST> a_a = a-(c+b)$
8756
8773
  frml <CALC_ADJUST> b_a = a-(c+b)$'''
@@ -8758,24 +8775,27 @@ frml <CALC_ADJUST> b_a = a-(c+b)$'''
8758
8775
  'X': 'Eksport <æøåÆØÅ>;',
8759
8776
  'C': 'Forbrug'}
8760
8777
  mmodel = model(smallmodel, var_description=des, svg=1, browser=1)
8761
- df = pd.DataFrame(
8762
- {'X': [0.2, 0.2, 0.2], 'C': [2.0, 3., 5.],'R': [2., 1., 0.4],
8763
- 'PPP': [0.4, 0., 0.4]})
8764
- df2 = df.copy()
8765
- df2.loc[:,'C'] = [5, 10, 20]
8766
-
8767
- xx = mmodel(df)
8768
- yy = mmodel(df2)
8769
-
8770
- # zz = model.modelload(r'https://raw.githubusercontent.com/IbHansen/Modelflow2/master/Examples/ADAM/baseline.pcim')
8771
- mpak,baseline = model.modelload(r'pak.pcim',run=1,use_fbmin=True)
8772
- alternative = baseline.upd("<2020 2100> PAKGGREVCO2CER PAKGGREVCO2GER PAKGGREVCO2OER = 30")
8773
- result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=0) # simulates the model
8774
- diff_level, att_level, att_pct, diff_growth, att_growth = tup = mpak.dekomp('PAKNECONOTHRXN',lprint=False,start=2020,end=2027)
8775
-
8776
-
8777
- fb_nodes,dag_nodes = mpak.get_minimal_feedback_set(mpak.endograph)
8778
- print(f'Number of endogenous: {len(mpak.endogene)}, Number of variables in simultaneuous block {len(mpak.coreorder)}')
8779
- print(f'Number of variables in "minimum" feedback set: {len(fb_nodes)}')
8780
- print(f'Number of variables in "DAG" graph : {len(dag_nodes)}')
8781
- # mtest,bk = model.modelload(r'ibstestnozip')
8778
+ mmodel.check_endo_rhs()
8779
+ #%%
8780
+ if 1:
8781
+ df = pd.DataFrame(
8782
+ {'X': [0.2, 0.2, 0.2], 'C': [2.0, 3., 5.],'R': [2., 1., 0.4],
8783
+ 'PPP': [0.4, 0., 0.4]})
8784
+ df2 = df.copy()
8785
+ df2.loc[:,'C'] = [5, 10, 20]
8786
+
8787
+ xx = mmodel(df)
8788
+ yy = mmodel(df2)
8789
+
8790
+ # zz = model.modelload(r'https://raw.githubusercontent.com/IbHansen/Modelflow2/master/Examples/ADAM/baseline.pcim')
8791
+ mpak,baseline = model.modelload(r'pak.pcim',run=1,use_fbmin=True)
8792
+ alternative = baseline.upd("<2020 2100> PAKGGREVCO2CER PAKGGREVCO2GER PAKGGREVCO2OER = 30")
8793
+ result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=0) # simulates the model
8794
+ diff_level, att_level, att_pct, diff_growth, att_growth = tup = mpak.dekomp('PAKNECONOTHRXN',lprint=False,start=2020,end=2027)
8795
+
8796
+
8797
+ fb_nodes,dag_nodes = mpak.get_minimal_feedback_set(mpak.endograph)
8798
+ print(f'Number of endogenous: {len(mpak.endogene)}, Number of variables in simultaneuous block {len(mpak.coreorder)}')
8799
+ print(f'Number of variables in "minimum" feedback set: {len(fb_nodes)}')
8800
+ print(f'Number of variables in "DAG" graph : {len(dag_nodes)}')
8801
+ # mtest,bk = model.modelload(r'ibstestnozip')
@@ -223,33 +223,81 @@ def wf2_to_clean(wf2name,modelname='',save_file = False,freq='A'):
223
223
 
224
224
  @dataclass
225
225
  class GrabWfModel():
226
- '''This class takes a world bank model specification, variable data and variable description
227
- and transform it to ModelFlow business language
228
-
229
- args:
230
- filename : any = '' #wf1 name
231
- modelname : any = ''
232
- freq : str = 'A'
233
- eviews_run_lines : list =field(default_factory=list)
234
- model_all_about : dict = field(default_factory=dict)
235
- start : any = None # start of testing if not overruled by mfmsa
236
- end : any = None # end of testing if not overruled by mfmsa
237
- country_trans : any = lambda x:x[:] # function which transform model specification
238
- country_df_trans : any = lambda x:x # function which transforms initial dataframe
239
- make_fitted : bool = False # if True, a clean equation for fittet variables is created
240
- fit_start : any = 2000 # start of fittet model
241
- fit_end : any = None # end of fittet model unless overruled by mfmsa
242
- do_add_factor_calc : bool = True # calculate the add factors
243
- test_frml : str ='' # a testmodel as string if used no wf processing
244
- disable_progress : bool = False # Disable progress bar
245
- save_file : bool = False # save information to file
246
- model_description : str = '' # model description
247
- cty : str = '' # country ISO code like PAK, default to 3 first letter in modelname
248
- cty_name : str = '' # country name like Pakistan, default to standard mapping in ISO standard
249
- var_groups : dict = field(default_factory=dict,init=False) # var_groups
250
- extra_var_descriptions : dict = field(default_factory=dict) # extra variable descriptions
226
+ """
227
+ This class takes a World Bank model specification, variable data, and variable description,
228
+ and transforms it into ModelFlow business language.
251
229
 
252
- '''
230
+ Args:
231
+ filename : any = '' # wf1 name
232
+ modelname : any = ''
233
+ freq : str = 'A'
234
+ eviews_run_lines : list = field(default_factory=list)
235
+ model_all_about : dict = field(default_factory=dict)
236
+ start : any = None # start of testing if not overruled by mfmsa
237
+ end : any = None # end of testing if not overruled by mfmsa
238
+ country_trans : any = lambda x:x[:] # function which transforms model specification
239
+ country_df_trans : any = lambda x:x # function which transforms initial dataframe
240
+ make_fitted : bool = False # if True, a clean equation for fitted variables is created
241
+ fit_start : any = 2000 # start of fitted model
242
+ fit_end : any = None # end of fitted model unless overruled by mfmsa
243
+ do_add_factor_calc : bool = True # calculate the add factors
244
+ test_frml : str = '' # a test model as string; if used, no wf processing
245
+ disable_progress : bool = False # disable progress bar
246
+ save_file : bool = False # save information to file
247
+ model_description : str = '' # model description
248
+ cty : str = '' # country ISO code like PAK, defaults to first 3 letters of modelname
249
+ cty_name : str = '' # country name like Pakistan, default via ISO mapping
250
+ var_groups : dict = field(default_factory=dict, init=False) # variable groups
251
+ extra_var_descriptions : dict = field(default_factory=dict) # extra variable descriptions
252
+
253
+ Methods:
254
+ __post_init__():
255
+ Initialize and process the model, transform equations, perform syntax checks,
256
+ and set up the model and data for simulation and analysis.
257
+
258
+ print_frml(pat='*', eviews=''):
259
+ Print model equations matching a wildcard pattern and optional EViews content filter.
260
+
261
+ trans_eviews(rawmodel):
262
+ (staticmethod) Convert EViews-style equations into ModelFlow syntax.
263
+
264
+ var_description (property):
265
+ Return enriched variable descriptions combining WF1 file, WB defaults, and extras.
266
+
267
+ wb_default_descriptions (property):
268
+ Load default World Bank variable descriptions from an online repository.
269
+
270
+ mfmsa_options (property):
271
+ Return the raw MFMSA options XML string.
272
+
273
+ mfmsa_options_dict (property):
274
+ Parse and return MFMSA options as a nested dictionary.
275
+
276
+ mfmsa_start_end (property):
277
+ Retrieve start and end periods from MFMSA metadata.
278
+
279
+ mfmsa_country (property):
280
+ Retrieve the country ISO code from MFMSA metadata.
281
+
282
+ mfmsa_quasiIdentities (property):
283
+ Return a set of quasi-identity variable names from MFMSA.
284
+
285
+ var_groups_default (property):
286
+ Provide default variable groupings for World Bank models.
287
+
288
+ dfmodel (property):
289
+ Return the transformed and enriched dataframe for model calculation.
290
+
291
+ __call__():
292
+ Return the processed model object and base input dataframe.
293
+
294
+ test_model(start=None, end=None, maxvar=1_000_000, maxerr=100, tol=0.0001,
295
+ showall=False, showinput=False, nofit=True):
296
+ Compare model calculations against input data to validate model fidelity.
297
+
298
+ iso_countries (property):
299
+ Return a dictionary mapping ISO country codes to country names.
300
+ """
253
301
 
254
302
  filename : any = '' #wf1 name
255
303
  modelname : any = ''
@@ -167,12 +167,17 @@ def preprocess(udtryk,funks=[]):
167
167
  while pct_match := funk_in('PCT_GROWTH',udtryk_up):
168
168
  forpc,pcudtryk,efterpc=funk_find_arg(pct_match,udtryk_up)
169
169
  udtryk_up=f'{forpc} (100 * ( ({pcudtryk}) / ({lagone(pcudtryk,funks=funks)}) -1)) {efterpc}'
170
+
171
+ while pct_match := funk_in('PCY',udtryk_up):
172
+ forpc,pcudtryk,efterpc=funk_find_arg(pct_match,udtryk_up)
173
+ udtryk_up=f'{forpc} (100 * ( ({pcudtryk}) / ({lagone(pcudtryk,funks=funks,laglead=-4)}) -1)) {efterpc}'
170
174
 
171
175
  while diff_match := funk_in('DIFF' , udtryk_up):
172
176
  fordif,difudtryk_up,efterdif=funk_find_arg(diff_match,udtryk_up)
173
177
  udtryk_up=fordif+'(('+difudtryk_up+')-('+lagone(difudtryk_up+'',funks=funks)+'))'+efterdif
174
178
 
175
179
  while diff_match := funk_in('D' , udtryk_up):
180
+ print(f'{udtryk_up} on D')
176
181
  fordif,difudtryk_up,efterdif=funk_find_arg(diff_match,udtryk_up)
177
182
  difudtryk_up = difudtryk_up.replace(' ','').replace(',0,1','') if difudtryk_up.endswith(',0,1') else difudtryk_up
178
183
  udtryk_up=fordif+'(('+difudtryk_up+')-('+lagone(difudtryk_up+'',funks=funks)+'))'+efterdif
@@ -613,10 +613,12 @@ class DisplayDef:
613
613
 
614
614
  # Combine options using the existing __add__ method of DisplaySpec
615
615
  new_spec = self.spec + other.spec
616
-
616
+ # print(f'{self.spec=}')
617
+ # print(f'{new_spec=}')
617
618
  # Merge names if they differ, separated by a comma
618
619
  new_name = self.name if self.name == other.name else f"{self.name}_{other.name}"
619
-
620
+ # print(f'{new_name=}')
621
+
620
622
  # Create a new DisplayDef with the combined specifications
621
623
  return self.__class__(mmodel=self.mmodel, spec=new_spec, name=new_name)
622
624
 
@@ -1271,6 +1273,7 @@ class DisplayKeepFigDef(DisplayDef):
1271
1273
  # textdf = pd.DataFrame(float('nan'), index=self.mmodel.current_per, columns=[line.centertext]).T
1272
1274
  outlist = []
1273
1275
  else:
1276
+ # print(f'{line.pat=}')
1274
1277
  locallinedfdict = self.mmodel.keep_get_plotdict_new(
1275
1278
  pat=line.pat,
1276
1279
  showtype=line.showtype,
@@ -7,7 +7,7 @@ where = ["."]
7
7
 
8
8
  [project]
9
9
  name = "ModelFlowIb"
10
- version = "2.53"
10
+ version = "2.55"
11
11
  description = "A tool to solve and manage dynamic economic models"
12
12
  authors = [{name = "Ib Hansen", email = "your@email.com"}]
13
13
  license = { text = "MIT" }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes