ModelFlowIb 2.72__tar.gz → 2.73__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 (65) hide show
  1. modelflowib-2.73/PKG-INFO +156 -0
  2. modelflowib-2.73/README.md +113 -0
  3. modelflowib-2.73/license.md +10 -0
  4. {modelflowib-2.72 → modelflowib-2.73}/modelBLfunk.py +1 -1
  5. {modelflowib-2.72 → modelflowib-2.73}/modelclass.py +203 -19
  6. {modelflowib-2.72 → modelflowib-2.73}/modelconstruct_estimation.py +2872 -2829
  7. {modelflowib-2.72 → modelflowib-2.73}/modeldekom.py +15 -8
  8. {modelflowib-2.72 → modelflowib-2.73}/modelestimator_new.py +2612 -2607
  9. modelflowib-2.73/modelflowib.egg-info/PKG-INFO +156 -0
  10. {modelflowib-2.72/ModelFlowIb.egg-info → modelflowib-2.73/modelflowib.egg-info}/SOURCES.txt +8 -17
  11. {modelflowib-2.72/ModelFlowIb.egg-info → modelflowib-2.73/modelflowib.egg-info}/top_level.txt +1 -13
  12. modelflowib-2.73/modelgrabgdx.py +584 -0
  13. modelflowib-2.73/modelhtml.py +606 -0
  14. {modelflowib-2.72 → modelflowib-2.73}/modelmanipulation.py +4 -4
  15. {modelflowib-2.72 → modelflowib-2.73}/modelnewton.py +124 -35
  16. {modelflowib-2.72 → modelflowib-2.73}/modelreport.py +15 -11
  17. {modelflowib-2.72 → modelflowib-2.73}/modelwidget.py +13 -0
  18. modelflowib-2.73/pyproject.toml +81 -0
  19. modelflowib-2.72/ModelFlowIb.egg-info/PKG-INFO +0 -33
  20. modelflowib-2.72/PKG-INFO +0 -33
  21. modelflowib-2.72/model_doable.py +0 -40
  22. modelflowib-2.72/model_ifrs9.py +0 -88
  23. modelflowib-2.72/model_run_numba.py +0 -15
  24. modelflowib-2.72/modelclass2.py +0 -463
  25. modelflowib-2.72/modelfeedback.py +0 -243
  26. modelflowib-2.72/modelgrabgdx.py +0 -292
  27. modelflowib-2.72/modelpatternrev.py +0 -72
  28. modelflowib-2.72/modelsandbox.py +0 -76
  29. modelflowib-2.72/modelsandbox_Mixin.py +0 -1565
  30. modelflowib-2.72/modeltodo.py +0 -31
  31. modelflowib-2.72/modeluserfunk_old.py +0 -90
  32. modelflowib-2.72/modelwidget_input_old.py +0 -1541
  33. modelflowib-2.72/modelwidget_shinypy.py +0 -935
  34. modelflowib-2.72/pyproject.toml +0 -46
  35. {modelflowib-2.72 → modelflowib-2.73}/model_Excel.py +0 -0
  36. {modelflowib-2.72 → modelflowib-2.73}/model_cvx.py +0 -0
  37. {modelflowib-2.72 → modelflowib-2.73}/model_dynare.py +0 -0
  38. {modelflowib-2.72 → modelflowib-2.73}/model_financial_stability.py +0 -0
  39. {modelflowib-2.72 → modelflowib-2.73}/model_latex.py +0 -0
  40. {modelflowib-2.72 → modelflowib-2.73}/model_latex_class.py +0 -0
  41. {modelflowib-2.72 → modelflowib-2.73}/model_parquet_mixin.py +0 -0
  42. {modelflowib-2.72 → modelflowib-2.73}/modelconstruct.py +0 -0
  43. {modelflowib-2.72 → modelflowib-2.73}/modeldash.py +0 -0
  44. {modelflowib-2.72 → modelflowib-2.73}/modeldashboot.py +0 -0
  45. {modelflowib-2.72 → modelflowib-2.73}/modeldashsidebar.py +0 -0
  46. {modelflowib-2.72 → modelflowib-2.73}/modeldiff.py +0 -0
  47. {modelflowib-2.72 → modelflowib-2.73}/modeldisplay.py +0 -0
  48. {modelflowib-2.72 → modelflowib-2.73}/modelestimation.py +0 -0
  49. {modelflowib-2.72/ModelFlowIb.egg-info → modelflowib-2.73/modelflowib.egg-info}/dependency_links.txt +0 -0
  50. {modelflowib-2.72/ModelFlowIb.egg-info → modelflowib-2.73/modelflowib.egg-info}/requires.txt +0 -0
  51. {modelflowib-2.72 → modelflowib-2.73}/modelgrab.py +0 -0
  52. {modelflowib-2.72 → modelflowib-2.73}/modelgrabwf2.py +0 -0
  53. {modelflowib-2.72 → modelflowib-2.73}/modelhelp.py +0 -0
  54. {modelflowib-2.72 → modelflowib-2.73}/modelinvert.py +0 -0
  55. {modelflowib-2.72 → modelflowib-2.73}/modeljupyter.py +0 -0
  56. {modelflowib-2.72 → modelflowib-2.73}/modeljupytermagic.py +0 -0
  57. {modelflowib-2.72 → modelflowib-2.73}/modelmacrograb.py +0 -0
  58. {modelflowib-2.72 → modelflowib-2.73}/modelmf.py +0 -0
  59. {modelflowib-2.72 → modelflowib-2.73}/modelnet.py +0 -0
  60. {modelflowib-2.72 → modelflowib-2.73}/modelnormalize.py +0 -0
  61. {modelflowib-2.72 → modelflowib-2.73}/modelpattern.py +0 -0
  62. {modelflowib-2.72 → modelflowib-2.73}/modeluserfunk.py +0 -0
  63. {modelflowib-2.72 → modelflowib-2.73}/modelvis.py +0 -0
  64. {modelflowib-2.72 → modelflowib-2.73}/modelwidget_input.py +0 -0
  65. {modelflowib-2.72 → modelflowib-2.73}/setup.cfg +0 -0
@@ -0,0 +1,156 @@
1
+ Metadata-Version: 2.4
2
+ Name: modelflowib
3
+ Version: 2.73
4
+ Summary: A tool to solve and manage dynamic economic and financial models
5
+ Author-email: Ib Hansen <Ib.Hansen.Iv@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/IbHansen/ModelFlow2
8
+ Project-URL: Repository, https://github.com/IbHansen/ModelFlow2
9
+ Keywords: economics,dynamic models,BLL,numba,modelling
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.9
13
+ Description-Content-Type: text/markdown
14
+ License-File: license.md
15
+ Requires-Dist: pandas
16
+ Requires-Dist: matplotlib
17
+ Requires-Dist: seaborn
18
+ Requires-Dist: sympy
19
+ Requires-Dist: numpy
20
+ Requires-Dist: networkx
21
+ Requires-Dist: tqdm
22
+ Requires-Dist: scipy
23
+ Requires-Dist: numexpr
24
+ Requires-Dist: bottleneck
25
+ Requires-Dist: XlsxWriter
26
+ Requires-Dist: pyarrow
27
+ Requires-Dist: tabulate
28
+ Requires-Dist: openpyxl
29
+ Requires-Dist: xarray
30
+ Requires-Dist: cvxopt
31
+ Requires-Dist: numba
32
+ Requires-Dist: dash
33
+ Requires-Dist: dash-bootstrap-components
34
+ Requires-Dist: dash-interactive-graphviz
35
+ Requires-Dist: lmfit
36
+ Requires-Dist: jupyter
37
+ Requires-Dist: ipywidgets
38
+ Requires-Dist: ipydatagrid
39
+ Provides-Extra: shiny
40
+ Requires-Dist: shiny; extra == "shiny"
41
+ Requires-Dist: shinywidgets; extra == "shiny"
42
+ Dynamic: license-file
43
+
44
+ # ModelFlow
45
+ A Python toolkit to manage models
46
+
47
+ The easy way to start is here [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/IbHansen/modelflow/master).
48
+
49
+ This will start **ModelFlow** as an online Jupyter notebook. Select one of the notebooks (files with the extension .ipynb). Some of the
50
+ notebooks are ModelFlow related. Other implement different model
51
+
52
+ - FRBUS will start the FRB/US model from the Federal reserve board.
53
+ - Q-JEM will start the Quarterly Japanese Economic Model from Bank of Japan
54
+ - ADAM will start the Anual Danish Aggregate Model for the Danish Statistical agency.
55
+ - Solow will implement a simple textybook Solow model.
56
+
57
+ You can also look at the python files of the system. Located at the **modelflow/** folder
58
+
59
+ Alternative look at the *getting started* section later in this file. It explains how to run ModelFlow localy.
60
+
61
+ The **Pandas** library is a great library to handle all kinds of datamanipulation and transformations.
62
+
63
+ However when it comes to models which contains lags or models which requires solving simultanous equations, Pandas is not quite helpful.
64
+
65
+ ModelFlow extends Pandas to handle a range of such models. And they can be large.
66
+
67
+ It requires you to specify the *model* specified as equations (the **business logic**) and place the *data* in a Pandas **DataFrame**. ModelFlow allows the model to meet the data and return the result as a new DataFrame.
68
+
69
+ A number of **analytical tools for model and result analytic** helps to understand the model and its results.
70
+
71
+ The user can **extend and modify the tools** to her or his needs.
72
+
73
+ **Onboarding models and combining models from different sources**. Creating a Macro prudential model often entails recycling several models specified in different ways: Excel, Latex, Dynare, Python or other languages. Python's ecosystem makes it possible to transform many different models into ModelFlow models or to wrap them into functions which can be called from ModelFlow models.
74
+
75
+ **Models can be specified in a high level Business logic language (a Domain Specific language)**. This allows the formulation of a model in a concise and expressive language which is close to the economic of the model. The user can concentrate on the economic or financial content - not the coding of the solution. The code for solving the model is generated by the tool. Then you can *solve the
76
+ simultaneous* (or *non-simultaneous* model) in an efficient way.
77
+
78
+ ## Introduction
79
+
80
+ **ModelFlow is written in Python**. Python comes "batteries included" and is
81
+ the basis of a very rich ecosystem, which consists of a wide array of
82
+ libraries. ModelFlow is just another library. It supplements the existing
83
+ libraries regarding modeling language and solving and allows the use of
84
+ Python as a model management framework.
85
+
86
+ **Data handling and wrangling is done in the Pandas library**. This
87
+ library is the Swiss army knife of data science in Python. It can import and export data to most systems and it is very powerful in manipulating and transforming data.
88
+ The core
89
+ element of Pandas is the *Dataframe*. A Dataframe is a two-dimensional
90
+ tabular data structure. Each *column* consists of cells of the same type
91
+ -- it can be a number, a string, a matrix or another Python data object.This includes matrices and other dataframes. Each *row is indexed.* The index can basically be any type of variable
92
+ including dates, which is especially relevant for economic and financial models.
93
+
94
+ **ModelFlow gives the user tools for more than solving models**. This
95
+ includes:
96
+
97
+ - *Visualization* and comparison of results
98
+
99
+ - *Integration* of models from different sources
100
+
101
+ - *Analyze the logical structure of a model*. By applying graph theory,
102
+ ModelFlow can find data lineage, find a suitable calculating sequence and trace
103
+ causes of changes through the calculations.
104
+
105
+ - *Inverting* the model to calculating the necessary instruments to
106
+ achieve a desired target.
107
+
108
+ - Calculating the *attributions* from input to the results of a model.
109
+
110
+ - Calculating the *attribution* from input to the result of each
111
+ formula.
112
+
113
+ - Finding and calculating partial *derivatives* of formulas
114
+
115
+ - *Integrating user defined python functions* in the Business logic
116
+ language (like optimization, calculating risk weights or to make a matrices consistent with the RAS algorithm )
117
+
118
+ - *Wrap matlab* models so they can be used in the Business logic
119
+ language.
120
+
121
+ - *Speed up* solving using "Just in time compilation"
122
+
123
+ - Analyze the model structure through tools from graph theory
124
+
125
+ - Handle *large models.* 1,000,000 formulas is not a problem.
126
+
127
+ - Integrate model management in Jupyter notebooks for *agile and user
128
+ friendly model use*
129
+
130
+
131
+ **The core code of ModelFlow is small and
132
+ documented.** Thus it can easily be modified and expanded to the specific need of the user. *ModelFlow is a toolset*. It can handle models, which conform to the tools.
133
+
134
+ If you need a feature or have a model which can't be handled in ModelFlow,
135
+ you are encouraged to improve ModelFlow. Please share the
136
+ improvement, other users may have the same need, or can be inspired by
137
+ your work.
138
+
139
+ Also bear in mind that ModelFlow is experimental. It is provided ”as is”, without any representation or warranty of any kind either express or implied.
140
+
141
+ ## Getting started
142
+
143
+ You need Python 3.7+ with asssociated libraries. The easy way is to install Anaconda Python is to download the distribution from [https://www.anaconda.com/distribution](https://www.anaconda.com/distribution)
144
+
145
+ Now you can install ModelFlow by running a command window from the Anaconda prompt and execute theese commands
146
+ ```
147
+ conda install modelflow -c ibh -c defaults -c conda-forge
148
+ ```
149
+ You will find the anaconda prompt by searching "anaconda" in the start menu search field
150
+
151
+ Try out one of the workbooks. to do this you:
152
+ ```
153
+ click on the Adaconda prompth in the Anaconda folder
154
+ cd <The location you have downloaded the workbooks>
155
+ jupyter notebook
156
+ ```
@@ -0,0 +1,113 @@
1
+ # ModelFlow
2
+ A Python toolkit to manage models
3
+
4
+ The easy way to start is here [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/IbHansen/modelflow/master).
5
+
6
+ This will start **ModelFlow** as an online Jupyter notebook. Select one of the notebooks (files with the extension .ipynb). Some of the
7
+ notebooks are ModelFlow related. Other implement different model
8
+
9
+ - FRBUS will start the FRB/US model from the Federal reserve board.
10
+ - Q-JEM will start the Quarterly Japanese Economic Model from Bank of Japan
11
+ - ADAM will start the Anual Danish Aggregate Model for the Danish Statistical agency.
12
+ - Solow will implement a simple textybook Solow model.
13
+
14
+ You can also look at the python files of the system. Located at the **modelflow/** folder
15
+
16
+ Alternative look at the *getting started* section later in this file. It explains how to run ModelFlow localy.
17
+
18
+ The **Pandas** library is a great library to handle all kinds of datamanipulation and transformations.
19
+
20
+ However when it comes to models which contains lags or models which requires solving simultanous equations, Pandas is not quite helpful.
21
+
22
+ ModelFlow extends Pandas to handle a range of such models. And they can be large.
23
+
24
+ It requires you to specify the *model* specified as equations (the **business logic**) and place the *data* in a Pandas **DataFrame**. ModelFlow allows the model to meet the data and return the result as a new DataFrame.
25
+
26
+ A number of **analytical tools for model and result analytic** helps to understand the model and its results.
27
+
28
+ The user can **extend and modify the tools** to her or his needs.
29
+
30
+ **Onboarding models and combining models from different sources**. Creating a Macro prudential model often entails recycling several models specified in different ways: Excel, Latex, Dynare, Python or other languages. Python's ecosystem makes it possible to transform many different models into ModelFlow models or to wrap them into functions which can be called from ModelFlow models.
31
+
32
+ **Models can be specified in a high level Business logic language (a Domain Specific language)**. This allows the formulation of a model in a concise and expressive language which is close to the economic of the model. The user can concentrate on the economic or financial content - not the coding of the solution. The code for solving the model is generated by the tool. Then you can *solve the
33
+ simultaneous* (or *non-simultaneous* model) in an efficient way.
34
+
35
+ ## Introduction
36
+
37
+ **ModelFlow is written in Python**. Python comes "batteries included" and is
38
+ the basis of a very rich ecosystem, which consists of a wide array of
39
+ libraries. ModelFlow is just another library. It supplements the existing
40
+ libraries regarding modeling language and solving and allows the use of
41
+ Python as a model management framework.
42
+
43
+ **Data handling and wrangling is done in the Pandas library**. This
44
+ library is the Swiss army knife of data science in Python. It can import and export data to most systems and it is very powerful in manipulating and transforming data.
45
+ The core
46
+ element of Pandas is the *Dataframe*. A Dataframe is a two-dimensional
47
+ tabular data structure. Each *column* consists of cells of the same type
48
+ -- it can be a number, a string, a matrix or another Python data object.This includes matrices and other dataframes. Each *row is indexed.* The index can basically be any type of variable
49
+ including dates, which is especially relevant for economic and financial models.
50
+
51
+ **ModelFlow gives the user tools for more than solving models**. This
52
+ includes:
53
+
54
+ - *Visualization* and comparison of results
55
+
56
+ - *Integration* of models from different sources
57
+
58
+ - *Analyze the logical structure of a model*. By applying graph theory,
59
+ ModelFlow can find data lineage, find a suitable calculating sequence and trace
60
+ causes of changes through the calculations.
61
+
62
+ - *Inverting* the model to calculating the necessary instruments to
63
+ achieve a desired target.
64
+
65
+ - Calculating the *attributions* from input to the results of a model.
66
+
67
+ - Calculating the *attribution* from input to the result of each
68
+ formula.
69
+
70
+ - Finding and calculating partial *derivatives* of formulas
71
+
72
+ - *Integrating user defined python functions* in the Business logic
73
+ language (like optimization, calculating risk weights or to make a matrices consistent with the RAS algorithm )
74
+
75
+ - *Wrap matlab* models so they can be used in the Business logic
76
+ language.
77
+
78
+ - *Speed up* solving using "Just in time compilation"
79
+
80
+ - Analyze the model structure through tools from graph theory
81
+
82
+ - Handle *large models.* 1,000,000 formulas is not a problem.
83
+
84
+ - Integrate model management in Jupyter notebooks for *agile and user
85
+ friendly model use*
86
+
87
+
88
+ **The core code of ModelFlow is small and
89
+ documented.** Thus it can easily be modified and expanded to the specific need of the user. *ModelFlow is a toolset*. It can handle models, which conform to the tools.
90
+
91
+ If you need a feature or have a model which can't be handled in ModelFlow,
92
+ you are encouraged to improve ModelFlow. Please share the
93
+ improvement, other users may have the same need, or can be inspired by
94
+ your work.
95
+
96
+ Also bear in mind that ModelFlow is experimental. It is provided ”as is”, without any representation or warranty of any kind either express or implied.
97
+
98
+ ## Getting started
99
+
100
+ You need Python 3.7+ with asssociated libraries. The easy way is to install Anaconda Python is to download the distribution from [https://www.anaconda.com/distribution](https://www.anaconda.com/distribution)
101
+
102
+ Now you can install ModelFlow by running a command window from the Anaconda prompt and execute theese commands
103
+ ```
104
+ conda install modelflow -c ibh -c defaults -c conda-forge
105
+ ```
106
+ You will find the anaconda prompt by searching "anaconda" in the start menu search field
107
+
108
+ Try out one of the workbooks. to do this you:
109
+ ```
110
+ click on the Adaconda prompth in the Anaconda folder
111
+ cd <The location you have downloaded the workbooks>
112
+ jupyter notebook
113
+ ```
@@ -0,0 +1,10 @@
1
+ Copyright © 2019 Danmarks Nationalbank and ECB
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Soft-ware"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+
8
+ Except as contained in this notice, the name of Danmarks Nationalbank and ECB shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Danmarks Nationalbank and ECB.
9
+
10
+ Based on MIT License.
@@ -7,7 +7,7 @@ Created on Fri Mar 2 17:01:49 2018
7
7
  Functions placed here are included in the Pyfs business language
8
8
 
9
9
  """
10
- from math import exp, log, sqrt, tanh, erf
10
+ from math import exp, log, sqrt, tanh, erf,isclose
11
11
  from numpy import transpose , array
12
12
  from scipy.stats import norm,lognorm
13
13
  from scipy.stats import gamma
@@ -49,6 +49,7 @@ from collections import defaultdict, namedtuple
49
49
  from itertools import groupby, chain, zip_longest,accumulate
50
50
  import re
51
51
  import pandas as pd
52
+ from pandas.api.types import is_numeric_dtype
52
53
  import sys
53
54
  import networkx as nx
54
55
  import fnmatch
@@ -104,6 +105,7 @@ import modelvis as mv
104
105
  import modelpattern as pt
105
106
  from modelnet import draw_adjacency_matrix
106
107
  from modelnewton import newton_diff
108
+ from modelsolver_ng import Solver_ng_Mixin
107
109
  import modeljupyter as mj
108
110
  from modelhelp import cutout, update_var
109
111
  from modelnormalize import normal
@@ -146,7 +148,7 @@ class BaseModel():
146
148
  tabcomplete=True, previousbase=False, use_preorder=True, normalized=True,safeorder= False,
147
149
  var_description={}, model_description = '',
148
150
  var_groups = {}, reports = {}, equations_latex='',
149
- eviews_dict = {},use_fbmin= False,
151
+ eviews_dict = {},use_fbmin= True,
150
152
  substitution = {},
151
153
  **kwargs):
152
154
  ''' initialize a model'''
@@ -1405,11 +1407,17 @@ class Org_model_Mixin():
1405
1407
 
1406
1408
  def __getattr__(self, name):
1407
1409
  '''To execute the . operator
1408
-
1409
-
1410
-
1410
+
1411
+
1412
+
1411
1413
  '''
1412
-
1414
+ # Guard against infinite recursion: __getattr__ is only called when normal
1415
+ # lookup fails. Dunder/internal names and a not-yet-initialised (or empty)
1416
+ # model lack 'allvar_set'; reading it via self.allvar_set would re-enter
1417
+ # __getattr__ endlessly, so resolve it from __dict__ directly.
1418
+ if name.startswith('__') and name.endswith('__'):
1419
+ raise AttributeError(name)
1420
+
1413
1421
  if name.startswith('model_var_'):
1414
1422
  wishfor= name[len('model_var_'):]
1415
1423
  return(self.var_with_frmlname(wishfor))
@@ -1418,10 +1426,11 @@ class Org_model_Mixin():
1418
1426
  return(self.frml_with_frmlname(wishfor))
1419
1427
 
1420
1428
 
1421
- else:
1422
- if name.upper() in self.allvar_set:
1429
+ else:
1430
+ allvar_set = self.__dict__.get('allvar_set')
1431
+ if allvar_set is not None and name.upper() in allvar_set:
1423
1432
  return mv.varvis(model=self, var=name.upper())
1424
- else:
1433
+ else:
1425
1434
  raise AttributeError(f'The specification:"{name}" did not match a method, property or variable name')
1426
1435
 
1427
1436
  def __dir__(self):
@@ -4778,10 +4787,10 @@ class Display_Mixin():
4778
4787
  .set_sticky(axis='index')\
4779
4788
  .set_table_attributes('class="table"')
4780
4789
 
4781
- if any(df.dtypes == 'object'):
4782
- result = result
4783
- else:
4790
+ if all(is_numeric_dtype(dt) for dt in df.dtypes):
4784
4791
  result = result.format('{:,.'+xdec+'f}'+xpct)
4792
+ else:
4793
+ result = result
4785
4794
 
4786
4795
  if use_tooltip:
4787
4796
  try:
@@ -4843,10 +4852,14 @@ class Display_Mixin():
4843
4852
 
4844
4853
  result = df.style.set_table_styles(styles)
4845
4854
 
4846
- if any(df.dtypes == 'object'):
4847
- result = result
4848
- else:
4855
+ # Only apply the numeric format when every column is numeric.
4856
+ # NB: pandas >=3.0 stores strings as the 'str' dtype (not 'object'),
4857
+ # so a "not object" test wrongly treats pre-formatted string tables
4858
+ # (df_str) as numeric and crashes formatting them. Test for numeric.
4859
+ if all(is_numeric_dtype(dt) for dt in df.dtypes):
4849
4860
  result = result.format('{:,.'+xdec+'f}'+xpct+' ')
4861
+ else:
4862
+ result = result
4850
4863
 
4851
4864
  if use_tooltip:
4852
4865
  try:
@@ -6460,6 +6473,8 @@ class Solver_Mixin():
6460
6473
 
6461
6474
  solver : str, optional
6462
6475
  Specifies the solver to be used. The default solver is chosen based on the model's properties.
6476
+ A next-generation (modelsolver_ng) solver can be selected by passing its
6477
+ ``<name>_ng`` name, e.g. ``solver='newton_ng'`` or ``solver='sim_ng'``.
6463
6478
 
6464
6479
  silent : bool, optional
6465
6480
  If True, the solver runs silently without printing output to the console. Default is True.
@@ -6537,7 +6552,7 @@ class Solver_Mixin():
6537
6552
  solver = newkwargs.get('solver', solverguess)
6538
6553
  silent = newkwargs.get('silent', True)
6539
6554
  self.model_solver = getattr(self, solver)
6540
-
6555
+
6541
6556
  # print(f'solver:{solver},solverkwargs:{newkwargs}')
6542
6557
  # breakpoint()
6543
6558
  outdf = self.model_solver(*args, **newkwargs)
@@ -6638,7 +6653,9 @@ class Solver_Mixin():
6638
6653
  # breakpoint()
6639
6654
  # if we import from a cache, we assume that the dataframe is in the same order
6640
6655
  if transpile_reset or not hasattr(self, f'pro_{jitname}'):
6641
- jitfilename= f'modelsource/{jitname}_jitsolver.py'.replace(' ','_')
6656
+ # sanitize: the model name can contain characters that
6657
+ # are illegal in a module name / file path (e.g. 'FRB/US')
6658
+ jitfilename= f"modelsource/{re.sub(r'[^0-9a-zA-Z_]', '_', jitname)}_jitsolver.py"
6642
6659
  jitfile = Path(jitfilename)
6643
6660
  jitfile.parent.mkdir(parents=True, exist_ok=True)
6644
6661
  if not silent:
@@ -9345,7 +9362,7 @@ class Report_Mixin:
9345
9362
 
9346
9363
  from model_parquet_mixin import Parquet_Mixin
9347
9364
 
9348
- class model(Parquet_Mixin, Zip_Mixin, Json_Mixin, Model_help_Mixin, Solver_Mixin, Display_Mixin, Graph_Draw_Mixin, Graph_Mixin,
9365
+ class model(Parquet_Mixin, Zip_Mixin, Json_Mixin, Model_help_Mixin, Solver_Mixin, Solver_ng_Mixin, Display_Mixin, Graph_Draw_Mixin, Graph_Mixin,
9349
9366
  Dekomp_Mixin, Org_model_Mixin, BaseModel, Description_Mixin, Excel_Mixin, Dash_Mixin, Modify_Mixin,
9350
9367
  Fix_Mixin,Stability_Mixin,Report_Mixin):
9351
9368
  '''This is the main model definition'''
@@ -9633,8 +9650,7 @@ frml <CALC_ADJUST> b_a = a-(c+b)$'''
9633
9650
  # zz = model.modelload(r'https://raw.githubusercontent.com/IbHansen/Modelflow2/master/Examples/ADAM/baseline.pcim')
9634
9651
  mpak,baseline = model.modelload(r'pak.pcim',run=1,use_fbmin=True)
9635
9652
  alternative = baseline.upd("<2020 2100> PAKGGREVCO2CER PAKGGREVCO2GER PAKGGREVCO2OER = 30")
9636
- result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=0) # simulates the model
9637
- diff_level, att_level, att_pct, diff_growth, att_growth = tup = mpak.dekomp('PAKNECONOTHRXN',lprint=False,start=2020,end=2027)
9653
+ result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=0,solver='sim',nonlin=6) # simulates the model
9638
9654
 
9639
9655
 
9640
9656
  fb_nodes,dag_nodes = mpak.get_minimal_feedback_set(mpak.endograph)
@@ -9642,3 +9658,171 @@ frml <CALC_ADJUST> b_a = a-(c+b)$'''
9642
9658
  print(f'Number of variables in "minimum" feedback set: {len(fb_nodes)}')
9643
9659
  print(f'Number of variables in "DAG" graph : {len(dag_nodes)}')
9644
9660
  # mtest,bk = model.modelload(r'ibstestnozip')
9661
+
9662
+ #%% test next-generation (ng) solvers against the legacy solvers
9663
+ if 1:
9664
+ ngmodel = model(smallmodel)
9665
+ ngdf = pd.DataFrame(
9666
+ {'X': [0.2, 0.2, 0.2], 'C': [2.0, 3., 5.], 'R': [2., 1., 0.4],
9667
+ 'PPP': [0.4, 0., 0.4]})
9668
+
9669
+ # Gauss-Seidel: legacy .sim vs new .sim_ng
9670
+ old_sim = ngmodel.sim(ngdf, silent=1)
9671
+ new_sim = ngmodel.sim_ng(ngdf, silent=1)
9672
+ d_sim = float((old_sim - new_sim).abs().max().max())
9673
+ print(f'sim vs sim_ng max abs diff: {d_sim:.3e}')
9674
+
9675
+ # 1-D stuffed Gauss-Seidel: legacy .sim1d vs new .sim1d_ng
9676
+ old_sim1d = ngmodel.sim1d(ngdf, silent=1)
9677
+ new_sim1d = ngmodel.sim1d_ng(ngdf, silent=1)
9678
+ d_sim1d = float((old_sim1d - new_sim1d).abs().max().max())
9679
+ print(f'sim1d vs sim1d_ng max abs diff: {d_sim1d:.3e}')
9680
+
9681
+ # Newton needs a well-posed simultaneous model: the smallmodel above is
9682
+ # singular for D3 (D3 = ... + D3 -> zero on the Jacobian diagonal once the
9683
+ # -I term is applied), so use a small non-degenerate linear one here.
9684
+ # No recursive tail on purpose: per-period Newton solves only the
9685
+ # simultaneous core (endovar = coreorder), so a recursive var would be left
9686
+ # unsolved and legitimately disagree with the stacked solver (all endo).
9687
+ goodmodel = model('frml <> a = 0.5*b + x $ '
9688
+ 'frml <> b = 0.3*a + c $ ')
9689
+ gdf = pd.DataFrame({'X': [1.0, 2., 3.], 'C': [2.0, 2., 2.]})
9690
+
9691
+ # unified per-period Newton: legacy .newton_implicit vs new .newton_ng
9692
+ old_newton = goodmodel.newton_implicit(gdf, silent=1)
9693
+ new_newton = goodmodel.newton_ng(gdf, silent=1)
9694
+ d_newton = float((old_newton - new_newton).abs().max().max())
9695
+ print(f'newton_implicit vs newton_ng max abs diff: {d_newton:.3e}')
9696
+
9697
+ # unified stacked Newton: legacy .newtonstack_implicit vs new .newtonstack_ng
9698
+ old_stack = goodmodel.newtonstack_implicit(gdf, silent=1)
9699
+ new_stack = goodmodel.newtonstack_ng(gdf, silent=1)
9700
+ d_stack = float((old_stack - new_stack).abs().max().max())
9701
+ print(f'newtonstack_implicit vs newtonstack_ng max abs diff: {d_stack:.3e}')
9702
+
9703
+ # 'newtonstack_implicit_ng' is an alias for 'newtonstack_ng'
9704
+ d_alias = float((goodmodel.newtonstack_implicit_ng(gdf, silent=1)
9705
+ - new_stack).abs().max().max())
9706
+
9707
+ # per-period and stacked Newton must reach the same (well-posed) solution
9708
+ d_cross = float((new_newton - new_stack).abs().max().max())
9709
+ print(f'newton_ng vs newtonstack_ng max abs diff: {d_cross:.3e}')
9710
+
9711
+ # single-pass DAG evaluator: legacy .xgenr vs new .xgenr_ng. (This toy
9712
+ # model is actually simultaneous, so xgenr's single sweep is not a real
9713
+ # solution -- but both implementations must produce the SAME sweep.)
9714
+ old_xgenr = ngmodel.xgenr(ngdf, silent=1)
9715
+ new_xgenr = ngmodel.xgenr_ng(ngdf, silent=1)
9716
+ d_xgenr = float((old_xgenr - new_xgenr).abs().max().max())
9717
+ print(f'xgenr vs xgenr_ng max abs diff: {d_xgenr:.3e}')
9718
+
9719
+ # dispatcher form (well-posed model)
9720
+ disp = goodmodel.solve_ng(gdf, solver='newton', silent=1)
9721
+ d_disp = float((disp - new_newton).abs().max().max())
9722
+ print(f'solve_ng(newton) matches newton_ng : {d_disp < 1e-9}')
9723
+
9724
+ # __call__ can select an ng solver via its '<name>_ng' name, with the
9725
+ # unchanged getattr(self, solver) dispatch. The surrounding __call__
9726
+ # behaviour is identical, so solver='sim' and solver='sim_ng' must match.
9727
+ call_classic = ngmodel(ngdf, solver='sim', silent=1, reset_options=True)
9728
+ call_ng = ngmodel(ngdf, solver='sim_ng', silent=1, reset_options=True)
9729
+ d_call = float((call_classic - call_ng).abs().max().max())
9730
+ print(f"__call__(solver='sim') vs (solver='sim_ng') max abs diff: {d_call:.3e}")
9731
+
9732
+ # opt-in residual recording: keep_residual=True populates model.ng_residual
9733
+ # (Gauss builds a no-jit res model only then; default solves skip it).
9734
+ goodmodel.sim_ng(gdf, silent=1, keep_residual=True)
9735
+ r_gauss = float(goodmodel.ng_residual.abs().max().max())
9736
+ goodmodel.newton_ng(gdf, silent=1, keep_residual=True)
9737
+ r_newton = float(goodmodel.ng_residual.abs().max().max())
9738
+ print(f'keep_residual: sim_ng max|F(y)-y| {r_gauss:.3e}, newton_ng {r_newton:.3e}')
9739
+
9740
+ assert (d_sim < 1e-6 and d_sim1d < 1e-6 and d_xgenr < 1e-6
9741
+ and d_newton < 1e-6 and d_stack < 1e-6 and d_alias < 1e-12
9742
+ and d_cross < 1e-6 and d_call < 1e-6
9743
+ and r_gauss < 1e-4 and r_newton < 1e-4), \
9744
+ 'ng solver diverges from legacy'
9745
+ print('ng solver test: OK')
9746
+
9747
+ #%% experiment: solve the PAK carbon-tax scenario with each ng solver and
9748
+ # check every solver reaches the same solution as the default solve.
9749
+ if 1:
9750
+ mpak,baseline = model.modelload(r'pak.pcim',run=1,use_fbmin=True,ljit=False)
9751
+ # The reference is the default solve computed above:
9752
+ # result = mpak(alternative, 2020, 2100, keep='Carbon tax nominal 30')
9753
+ # Excluded: 'res' (an internal residual-codegen flavour, not a stand-alone
9754
+ # solver) and 'xgenr' (a single topological sweep -- only valid for a DAG,
9755
+ # not this simultaneous model).
9756
+ # ('newtonstack_implicit' is an alias of 'newtonstack' -- same solver.)
9757
+ ng_experiment = ['sim', 'sim1d', 'newton', 'newtonstack','newton_fbmin','newtonstack_fbmin']
9758
+
9759
+ # The Newton-family solvers refresh the Jacobian periodically (nonlin) for
9760
+ # this nonlinear model; Gauss-Seidel needs no options. Any converged solver
9761
+ # must reach the same fixed point as the default solve.
9762
+ newton_opts = dict(nonlin=6)
9763
+ solver_opts = {
9764
+ 'sim': {}, 'sim1d': {},
9765
+ 'newton': newton_opts, 'newtonstack': newton_opts,
9766
+ 'newton_fbmin': newton_opts, 'newtonstack_fbmin': newton_opts
9767
+ }
9768
+
9769
+ endo = sorted(mpak.endogene)
9770
+ ref = result[endo] # default (auto-guessed) solution, endo columns
9771
+
9772
+ print('\nPAK carbon-tax scenario -- ng solver experiment (vs default solve):')
9773
+ print(f'{"solver":26s}{"max |diff|":>16s}{"max rel diff":>16s} result')
9774
+ diffs = [] # converged but disagrees -> real problem
9775
+ failed = [] # raised / did not converge -> solver limitation
9776
+ for sname in ng_experiment:
9777
+ try:
9778
+ r = mpak(alternative, 2020, 2100, solver=f'{sname}_ng',
9779
+ silent=1, reset_options=True,ljit=False,
9780
+ keep=f'Carbon tax 30 [{sname}_ng]',
9781
+ **solver_opts[sname])
9782
+ except Exception as e:
9783
+ print(f'{sname + "_ng":26s}{"solve raised":>32s} {type(e).__name__}')
9784
+ failed.append(f'{sname} ({type(e).__name__})')
9785
+ continue
9786
+ cur = r[endo]
9787
+ absdiff = (cur - ref).abs()
9788
+ maxabs = float(absdiff.max().max())
9789
+ relerr = float((absdiff / (ref.abs() + 1e-8)).max().max())
9790
+ same = np.allclose(cur.values, ref.values, rtol=1e-2, atol=1e-2)
9791
+ print(f'{sname + "_ng":26s}{maxabs:16.3e}{relerr:16.3e} '
9792
+ f'{"same" if same else "DIFFERENT"}')
9793
+ if not same:
9794
+ diffs.append(f'{sname} (max rel diff {relerr:.2e})')
9795
+
9796
+ # Report only -- this is a real-world demonstration, not a unit test.
9797
+ # Gauss-Seidel (sim/sim1d) must match the default solve; the Newton family
9798
+ # should match too now that per-period Newton also solves the recursive
9799
+ # prolog/epilog blocks. Only Gauss-Seidel mismatches are asserted.
9800
+ gs_problems = [d for d in diffs if d.split()[0] in ('sim', 'sim1d')]
9801
+ if failed:
9802
+ print(f'PAK solver experiment: did not converge / raised: {failed}')
9803
+ if diffs:
9804
+ print(f'PAK solver experiment: differ from default solve: {diffs}')
9805
+ if not failed and not diffs:
9806
+ print('PAK solver experiment: all ng solvers match the default solve')
9807
+ assert not gs_problems, \
9808
+ f'Gauss-Seidel ng solver differs from default solve: {gs_problems}'
9809
+ #%%
9810
+ with mpak.timer('load') :
9811
+ mpak,baseline = model.modelload(r'pak.pcim',run=1,use_fbmin=True,ljit=False,solver='sim1d')
9812
+ with mpak.timer('update'):
9813
+ alternative = baseline.upd("<2020 2100> PAKGGREVCO2CER PAKGGREVCO2GER PAKGGREVCO2OER = 30")
9814
+ #%%
9815
+ with mpak.timer('newton_ng'):
9816
+ result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=0,solver='newton_ng',nonlin=20,max_iterations=100,ljit=True,jacobian='fd') # simulates the model
9817
+ with mpak.timer('newton_ng jit'):
9818
+ result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=1,solver='newton_fbmin_ng',max_iterations=100,ljit=True,jacobian='fd') # simulates the model
9819
+ with mpak.timer('sim'):
9820
+ result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=1,solver='sim_ng',nonlin=23,ljit=False) # simulates the model
9821
+ with mpak.timer('sim jit'):
9822
+ result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=1,solver='sim_ng',nonlin=23,ljit=True) # simulates the model
9823
+ #%%
9824
+ with mpak.timer('newtonstack_fbmin_ng'):
9825
+ # reset_options: __call__ options are sticky, so jacobian='fd' from the
9826
+ # newton_fbmin cells above would otherwise leak in (a fd build here is
9827
+ # n_fb_stacked+1 full-span sweeps -- minutes of silence)
9828
+ result = mpak(alternative,2020,2100,keep='Carbon tax nominal 30',silent=1,solver='newtonstack_fbmin_ng',nonlin=5,max_iterations=100,ljit=False,reset_options=True) # simulates the model