ModelFlowIb 2.93__tar.gz → 2.95__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.
- {modelflowib-2.93 → modelflowib-2.95}/PKG-INFO +1 -1
- {modelflowib-2.93 → modelflowib-2.95}/model_parquet_mixin.py +29 -4
- {modelflowib-2.93 → modelflowib-2.95}/modelclass.py +19 -5
- {modelflowib-2.93 → modelflowib-2.95}/modelconstruct_estimation.py +62 -1
- {modelflowib-2.93 → modelflowib-2.95}/modelestimator_new.py +95 -7
- modelflowib-2.95/modelestimator_r.py +1492 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelfactorize.py +240 -15
- {modelflowib-2.93 → modelflowib-2.95}/modelflowib.egg-info/PKG-INFO +1 -1
- {modelflowib-2.93 → modelflowib-2.95}/modelflowib.egg-info/SOURCES.txt +1 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelflowib.egg-info/top_level.txt +1 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelgrabgdx.py +42 -19
- {modelflowib-2.93 → modelflowib-2.95}/modelnewton.py +178 -14
- {modelflowib-2.93 → modelflowib-2.95}/modelreport.py +14 -10
- {modelflowib-2.93 → modelflowib-2.95}/modelsolver_ng.py +51 -22
- {modelflowib-2.93 → modelflowib-2.95}/pyproject.toml +3 -3
- {modelflowib-2.93 → modelflowib-2.95}/README.md +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/license.md +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelBLfunk.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/model_Excel.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/model_cvx.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/model_dynare.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/model_financial_stability.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/model_latex.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/model_latex_class.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelcausality.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelconstruct.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeldash.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeldashboot.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeldashsidebar.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeldekom.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeldiff.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeldisplay.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelestimation.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelflowib.egg-info/dependency_links.txt +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelflowib.egg-info/requires.txt +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelgrab.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelgrabexcel.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelgrabgekkobank.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelgrabwf2.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelhelp.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelhtml.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelinput_shiny.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelinvert.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeljupyter.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeljupytermagic.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelmacrograb.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelmanipulation.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelmf.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelnet.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelnormalize.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelode.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelpattern.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modeluserfunk.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelvis.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelwidget.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelwidget_core.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/modelwidget_input.py +0 -0
- {modelflowib-2.93 → modelflowib-2.95}/setup.cfg +0 -0
|
@@ -84,6 +84,24 @@ def _period_series_to_json(series):
|
|
|
84
84
|
return json.dumps(result_dict)
|
|
85
85
|
|
|
86
86
|
|
|
87
|
+
def storable_options(options):
|
|
88
|
+
"""The sticky solve options (oldkwargs) that can be written as JSON. A function with a ``spec`` dict (the
|
|
89
|
+
factorizers from modelfactorize.normal/pardiso) is stored as that dict. Others, such as a user function given
|
|
90
|
+
as factorizer=, are left out with a note: the loaded model then uses the default for them."""
|
|
91
|
+
keep, dropped = {}, []
|
|
92
|
+
for k, v in (options or {}).items():
|
|
93
|
+
if callable(v) and isinstance(getattr(v, 'spec', None), dict):
|
|
94
|
+
v = v.spec
|
|
95
|
+
try:
|
|
96
|
+
json.dumps(v)
|
|
97
|
+
keep[k] = v
|
|
98
|
+
except (TypeError, ValueError):
|
|
99
|
+
dropped.append(k)
|
|
100
|
+
if dropped:
|
|
101
|
+
print(f'modeldump: the solve option(s) {", ".join(dropped)} can not be saved and are left out')
|
|
102
|
+
return keep
|
|
103
|
+
|
|
104
|
+
|
|
87
105
|
def _df_to_feather_bytes(df):
|
|
88
106
|
"""Serialize a DataFrame to in-memory Feather/Arrow IPC bytes.
|
|
89
107
|
|
|
@@ -164,7 +182,8 @@ class Parquet_Mixin:
|
|
|
164
182
|
|
|
165
183
|
# ── dump ──────────────────────────────────────────────────────────────
|
|
166
184
|
|
|
167
|
-
def modeldump(self, file_path='', keep=False, large=False, compact=False, derivatives=False,
|
|
185
|
+
def modeldump(self, file_path='', keep=False, large=False, compact=False, derivatives=False, savebase=False,
|
|
186
|
+
**kwargs):
|
|
168
187
|
"""Dump model to disk.
|
|
169
188
|
|
|
170
189
|
Parameters
|
|
@@ -187,6 +206,11 @@ class Parquet_Mixin:
|
|
|
187
206
|
session takes them from the model instead of finding them again, which is
|
|
188
207
|
the slow part of its first solve for a large model. They are used only if
|
|
189
208
|
the equations and the solver options are the same.
|
|
209
|
+
savebase : bool
|
|
210
|
+
If True, store the basedf instead of the lastdf (for instance the baseline after an
|
|
211
|
+
experiment has been run). Only one dataframe is stored, so the file stays small; kept
|
|
212
|
+
solutions can be added with ``keep=True``. ``modelload`` gives it back as the dataframe
|
|
213
|
+
of the file, as always.
|
|
190
214
|
|
|
191
215
|
A model instance without a lastdf holds no dataframe, so the feather
|
|
192
216
|
format has nothing to be fast about. Such a model is always written as a
|
|
@@ -200,7 +224,8 @@ class Parquet_Mixin:
|
|
|
200
224
|
|
|
201
225
|
if not large or nodata:
|
|
202
226
|
# ── original JSON/gzip path ──────────────────────────────────
|
|
203
|
-
return super().modeldump(file_path=file_path, keep=keep, derivatives=derivatives,
|
|
227
|
+
return super().modeldump(file_path=file_path, keep=keep, derivatives=derivatives, savebase=savebase,
|
|
228
|
+
**kwargs)
|
|
204
229
|
|
|
205
230
|
# ── fast feather/zip path ────────────────────────────────────────
|
|
206
231
|
pathname = Path(file_path)
|
|
@@ -222,7 +247,7 @@ class Parquet_Mixin:
|
|
|
222
247
|
'format': 'feather_zip',
|
|
223
248
|
'frml': self.equations,
|
|
224
249
|
'modelname': self.name,
|
|
225
|
-
'oldkwargs': self.__dict__.get('oldkwargs', {}), # a model never solved has none
|
|
250
|
+
'oldkwargs': storable_options(self.__dict__.get('oldkwargs', {})), # a model never solved has none
|
|
226
251
|
'run_on_load': self.__dict__.get('run_on_load', False),
|
|
227
252
|
'straight': bool(self.__dict__.get('straight', False)), # loads the same way again
|
|
228
253
|
'diff_expressions': self.__dict__.get('diff_expressions', {}) if derivatives else {},
|
|
@@ -261,7 +286,7 @@ class Parquet_Mixin:
|
|
|
261
286
|
# current_per
|
|
262
287
|
zf.writestr('current_per.json', current_per_json)
|
|
263
288
|
# lastdf → feather
|
|
264
|
-
zf.writestr('lastdf.feather', _df_to_feather_bytes(_maybe_compact(self.
|
|
289
|
+
zf.writestr('lastdf.feather', _df_to_feather_bytes(_maybe_compact(self.dump_frame(savebase))))
|
|
265
290
|
# keep_solutions → one feather each, indexed by position
|
|
266
291
|
if keep:
|
|
267
292
|
for i, (name, df) in enumerate(self.keep_solutions.items()):
|
|
@@ -6865,11 +6865,24 @@ class Json_Mixin():
|
|
|
6865
6865
|
|
|
6866
6866
|
return pathname, dump_data
|
|
6867
6867
|
|
|
6868
|
-
def
|
|
6868
|
+
def dump_frame(self, savebase=False):
|
|
6869
|
+
'''The dataframe a dump stores: dump_df (normally the lastdf), or with savebase=True the basedf instead.
|
|
6870
|
+
Only one dataframe is stored, so the file stays small; kept solutions can be added with keep=True.'''
|
|
6871
|
+
if savebase:
|
|
6872
|
+
base = self.__dict__.get('basedf')
|
|
6873
|
+
if isinstance(base, pd.DataFrame):
|
|
6874
|
+
return base
|
|
6875
|
+
print('modeldump: savebase=True, but the model instance has no basedf - the lastdf is dumped')
|
|
6876
|
+
return self.dump_df
|
|
6877
|
+
|
|
6878
|
+
def modeldump_base(self, outfile='',keep=False,data=True,derivatives=False,savebase=False):
|
|
6869
6879
|
'''Dumps a model and its lastdf to a json file
|
|
6870
6880
|
|
|
6871
6881
|
if keep=True the model.keep_solutions will alse be dumped
|
|
6872
6882
|
|
|
6883
|
+
if savebase=True the basedf is dumped instead of the lastdf (for instance the baseline after an
|
|
6884
|
+
experiment has been run). modelload gives it back as the dataframe of the file, as always
|
|
6885
|
+
|
|
6873
6886
|
if derivatives=True the expressions for the derivatives, which a Newton solver has found
|
|
6874
6887
|
(model.diff_expressions), are dumped too. A Newton solver in a later session then takes them
|
|
6875
6888
|
from the model instead of finding them again - the slow part of its first solve for a large model
|
|
@@ -6925,14 +6938,15 @@ class Json_Mixin():
|
|
|
6925
6938
|
current_per_json = None
|
|
6926
6939
|
|
|
6927
6940
|
|
|
6941
|
+
from model_parquet_mixin import storable_options
|
|
6928
6942
|
dumpjson = {
|
|
6929
6943
|
'version': '1.10',
|
|
6930
6944
|
'has_data': dump_data,
|
|
6931
6945
|
'frml': self.equations,
|
|
6932
|
-
'lastdf': self.
|
|
6946
|
+
'lastdf': self.dump_frame(savebase).to_json(double_precision=15) if dump_data else None,
|
|
6933
6947
|
'current_per': current_per_json ,
|
|
6934
6948
|
'modelname': self.name,
|
|
6935
|
-
'oldkwargs': self.__dict__.get('oldkwargs',{}),
|
|
6949
|
+
'oldkwargs': storable_options(self.__dict__.get('oldkwargs',{})),
|
|
6936
6950
|
'run_on_load': self.__dict__.get('run_on_load', False),
|
|
6937
6951
|
'straight': bool(self.__dict__.get('straight', False)), # loads the same way again
|
|
6938
6952
|
'diff_expressions': self.__dict__.get('diff_expressions', {}) if derivatives else {},
|
|
@@ -7134,12 +7148,12 @@ class Json_Mixin():
|
|
|
7134
7148
|
|
|
7135
7149
|
class Zip_Mixin():
|
|
7136
7150
|
'''This experimental class zips a dumped file '''
|
|
7137
|
-
def modeldump(self, file_path='',keep=False,zip_file=True,data=True,derivatives=False):
|
|
7151
|
+
def modeldump(self, file_path='',keep=False,zip_file=True,data=True,derivatives=False,savebase=False):
|
|
7138
7152
|
pathname, dump_data = self._dump_pathname(file_path, self.has_data, data=data)
|
|
7139
7153
|
|
|
7140
7154
|
pathname.parent.mkdir(parents=True, exist_ok=True)
|
|
7141
7155
|
|
|
7142
|
-
json_string = self.modeldump_base(keep=keep,data=dump_data,derivatives=derivatives)
|
|
7156
|
+
json_string = self.modeldump_base(keep=keep,data=dump_data,derivatives=derivatives,savebase=savebase)
|
|
7143
7157
|
|
|
7144
7158
|
|
|
7145
7159
|
if zip_file:
|
|
@@ -1600,6 +1600,9 @@ BUILTIN_ESTIMATORS = {
|
|
|
1600
1600
|
'ols': 'Estimate_ols',
|
|
1601
1601
|
'nls_lmfit': 'Estimate_nls_lmfit',
|
|
1602
1602
|
'nls_eviews': 'Estimate_nls_eviews',
|
|
1603
|
+
# R as the estimation engine (modelestimator_r); method lm / nls by default
|
|
1604
|
+
'r_lm': 'Estimate_r_lm',
|
|
1605
|
+
'r_nls': 'Estimate_r_nls',
|
|
1603
1606
|
}
|
|
1604
1607
|
|
|
1605
1608
|
|
|
@@ -1650,6 +1653,11 @@ def _get_estimator_class(estimator_name, estimator_classes: Optional[dict] = Non
|
|
|
1650
1653
|
"Equations with <estimator=...> require modelestimator_new to be importable."
|
|
1651
1654
|
) from exc
|
|
1652
1655
|
|
|
1656
|
+
if class_names[name].startswith('Estimate_r_'):
|
|
1657
|
+
# The R backends live in their own module so modelestimator_new
|
|
1658
|
+
# does not depend on them.
|
|
1659
|
+
import modelestimator_r as me
|
|
1660
|
+
|
|
1653
1661
|
try:
|
|
1654
1662
|
return getattr(me, class_names[name])
|
|
1655
1663
|
except AttributeError as exc:
|
|
@@ -2366,7 +2374,8 @@ class Makemodel(BaseExplode):
|
|
|
2366
2374
|
self.markdown_model = (mfmod_list_to_codeblock(self.original_statements))
|
|
2367
2375
|
|
|
2368
2376
|
elif self.type_input == 'single':
|
|
2369
|
-
|
|
2377
|
+
# join the lines with a space, so a tag ending a line stays apart from the equation
|
|
2378
|
+
extracted_frml = ' '.join(line.strip() for line in self.original_statements.splitlines()).strip()
|
|
2370
2379
|
self.markdown_model = ''
|
|
2371
2380
|
|
|
2372
2381
|
else:
|
|
@@ -2685,6 +2694,46 @@ class Makemodel(BaseExplode):
|
|
|
2685
2694
|
cols[name] = ser
|
|
2686
2695
|
return pd.DataFrame(cols)
|
|
2687
2696
|
|
|
2697
|
+
def actual_fitted(self, level: bool = True):
|
|
2698
|
+
"""Actual, fitted and residuals of all estimated equations as one DataFrame.
|
|
2699
|
+
|
|
2700
|
+
The columns have two levels: the endogenous variable, and ``Actual``,
|
|
2701
|
+
``Fitted``, ``Residuals``. Take one equation with
|
|
2702
|
+
``mm.actual_fitted()['CON']``.
|
|
2703
|
+
|
|
2704
|
+
level : bool, default True
|
|
2705
|
+
True: the endogenous variable itself. When the left-hand side is
|
|
2706
|
+
``DLOG(CON)`` this is ``CON``, its one-step fitted value (the
|
|
2707
|
+
estimated equation solved for ``CON``, lagged values are the
|
|
2708
|
+
actual ones) and the residual in the units of ``CON``.
|
|
2709
|
+
False: the left-hand side as it was estimated (``DLOG(CON)``).
|
|
2710
|
+
|
|
2711
|
+
Each equation covers its own estimation sample, so rows outside it
|
|
2712
|
+
are NaN. An equation which can't be solved for its variable is left
|
|
2713
|
+
out with ``level=True``.
|
|
2714
|
+
|
|
2715
|
+
Per-equation access: ``rec['estimator_object'].af_level_df`` (or
|
|
2716
|
+
``.af_df`` and ``.residuals_df``) on the entries of
|
|
2717
|
+
:attr:`estimation_records`; ``modelestimator_new.plot_actual_vs_fitted
|
|
2718
|
+
(est, level=True)`` draws the chart of the report.
|
|
2719
|
+
"""
|
|
2720
|
+
import pandas as pd
|
|
2721
|
+
frames = {}
|
|
2722
|
+
for rec in self.estimation_records:
|
|
2723
|
+
est = rec.get('estimator_object')
|
|
2724
|
+
if not _is_estimator_backend_instance(est):
|
|
2725
|
+
continue
|
|
2726
|
+
frame = est.af_level_df if level else est.af_df.join(est.residuals_df)
|
|
2727
|
+
if frame is None:
|
|
2728
|
+
continue
|
|
2729
|
+
name = getattr(est, 'endo_var', '') or rec.get('frmlname', '')
|
|
2730
|
+
if level: # the variable is the outer column level
|
|
2731
|
+
frame = frame.rename(columns=lambda c: c.removeprefix(f'{est.endo_var}__'))
|
|
2732
|
+
frames[name] = frame
|
|
2733
|
+
if not frames:
|
|
2734
|
+
return pd.DataFrame()
|
|
2735
|
+
return pd.concat(frames, axis=1)
|
|
2736
|
+
|
|
2688
2737
|
@property
|
|
2689
2738
|
def markdown_with_estimation(self) -> str:
|
|
2690
2739
|
"""Original Markdown input with compact estimation tables inserted.
|
|
@@ -3085,6 +3134,18 @@ class Listmodels(BaseExplode):
|
|
|
3085
3134
|
return pd.DataFrame()
|
|
3086
3135
|
return pd.concat(frames, axis=1)
|
|
3087
3136
|
|
|
3137
|
+
def actual_fitted(self, level: bool = True):
|
|
3138
|
+
"""Actual, fitted and residuals of all member Makemodels, combined column-wise.
|
|
3139
|
+
|
|
3140
|
+
See :meth:`Makemodel.actual_fitted` for the layout.
|
|
3141
|
+
"""
|
|
3142
|
+
import pandas as pd
|
|
3143
|
+
frames = [mex.actual_fitted(level=level) for mex in self.makemodels]
|
|
3144
|
+
frames = [f for f in frames if not f.empty]
|
|
3145
|
+
if not frames:
|
|
3146
|
+
return pd.DataFrame()
|
|
3147
|
+
return pd.concat(frames, axis=1)
|
|
3148
|
+
|
|
3088
3149
|
@property
|
|
3089
3150
|
def markdown_with_estimation(self) -> str:
|
|
3090
3151
|
"""Concatenate member Makemodel markdown-with-estimation strings."""
|
|
@@ -1378,6 +1378,67 @@ class EstimatorBackend(Eq_parent, ABC):
|
|
|
1378
1378
|
.rename(columns=lambda s: s.lower().capitalize())
|
|
1379
1379
|
)
|
|
1380
1380
|
|
|
1381
|
+
# ---- A/F and residuals of the endogenous variable itself ---------------
|
|
1382
|
+
|
|
1383
|
+
@cached_property
|
|
1384
|
+
def lhs_is_transformed(self) -> bool:
|
|
1385
|
+
"""True when the left-hand side is a transformation of the endogenous
|
|
1386
|
+
variable (``DLOG(CON)``, ``DIFF(CON)``, ``LOG(CON/P)`` ...), so
|
|
1387
|
+
:attr:`af_df` is not in the units of the variable itself."""
|
|
1388
|
+
lhs = self.org_eq_clean.split("=", 1)[0]
|
|
1389
|
+
return "".join(lhs.split()).upper() != self.endo_var.upper()
|
|
1390
|
+
|
|
1391
|
+
@cached_property
|
|
1392
|
+
def af_level_df(self) -> Optional[pd.DataFrame]:
|
|
1393
|
+
"""Actual, fitted and residuals of the endogenous variable itself.
|
|
1394
|
+
|
|
1395
|
+
:attr:`af_df` and :attr:`residuals_df` are in the units of the
|
|
1396
|
+
left-hand side as it was estimated, e.g. ``DLOG(CON)``. Here the
|
|
1397
|
+
estimated equation is solved for the variable (``CON``):
|
|
1398
|
+
|
|
1399
|
+
* ``Actual`` the variable,
|
|
1400
|
+
* ``Fitted`` the normalized equation with the estimated
|
|
1401
|
+
coefficients, evaluated on the data. Lagged values of the variable
|
|
1402
|
+
are the actual ones, so this is the one-step (static) fit, not a
|
|
1403
|
+
dynamic simulation,
|
|
1404
|
+
* ``Residuals`` ``Actual - Fitted``, in the units of the variable.
|
|
1405
|
+
|
|
1406
|
+
The columns are prefixed with the variable name and ``__``
|
|
1407
|
+
(``CON__Actual``, ``CON__Fitted``, ``CON__Residuals``), so frames of
|
|
1408
|
+
several equations can be joined side by side.
|
|
1409
|
+
|
|
1410
|
+
When the left-hand side is the variable itself the frame holds the
|
|
1411
|
+
same numbers as :attr:`af_df` and :attr:`residuals_df`. ``None`` (and
|
|
1412
|
+
a message) when the equation can't be solved for the variable.
|
|
1413
|
+
"""
|
|
1414
|
+
prefix = f"{self.endo_var}__"
|
|
1415
|
+
if not self.lhs_is_transformed:
|
|
1416
|
+
return self.af_df.join(self.residuals_df).add_prefix(prefix)
|
|
1417
|
+
start, end = self.smpl
|
|
1418
|
+
try:
|
|
1419
|
+
normalized = nz.normal(
|
|
1420
|
+
self.org_eq_baked, add_add_factor=False
|
|
1421
|
+
).normalized
|
|
1422
|
+
lhs, rhs = normalized.split("=", 1)
|
|
1423
|
+
if lhs.strip().upper() != self.endo_var.upper():
|
|
1424
|
+
raise ValueError(f"normalized to {lhs.strip()!r}")
|
|
1425
|
+
res = self.eq_var_df.mfcalc(
|
|
1426
|
+
f"<{start},{end}> FITTED_LEVEL = {rhs}"
|
|
1427
|
+
)
|
|
1428
|
+
out = pd.DataFrame({
|
|
1429
|
+
"Actual": res.loc[start:end, self.endo_var],
|
|
1430
|
+
"Fitted": res.loc[start:end, "FITTED_LEVEL"],
|
|
1431
|
+
})
|
|
1432
|
+
except Exception as e:
|
|
1433
|
+
print(
|
|
1434
|
+
f"{type(self).__name__}({self.endo_var}): no actual/fitted "
|
|
1435
|
+
f"for the variable itself, the equation could not be solved "
|
|
1436
|
+
f"for it ({type(e).__name__}: {str(e)[:100]})"
|
|
1437
|
+
)
|
|
1438
|
+
return None
|
|
1439
|
+
out["Residuals"] = out["Actual"] - out["Fitted"]
|
|
1440
|
+
return out.add_prefix(prefix)
|
|
1441
|
+
|
|
1381
1442
|
@cached_property
|
|
1382
1443
|
def estimation_smpl(self) -> Tuple[Any, Any]:
|
|
1383
1444
|
"""The usable sample, derived from non-missing residuals.
|
|
@@ -2088,6 +2149,11 @@ class LSResult:
|
|
|
2088
2149
|
title_html = self._build_title_html()
|
|
2089
2150
|
summary_html = self.olsmodel._render_summary_html()
|
|
2090
2151
|
plot_html = self._build_plot_html(plot_format)
|
|
2152
|
+
# the same for the endogenous variable itself, when the left-hand
|
|
2153
|
+
# side is a transformation of it (DLOG(CON) -> CON)
|
|
2154
|
+
if (self.olsmodel.lhs_is_transformed
|
|
2155
|
+
and self.olsmodel.af_level_df is not None):
|
|
2156
|
+
plot_html += self._build_plot_html(plot_format, level=True)
|
|
2091
2157
|
return title_html + summary_html + plot_html
|
|
2092
2158
|
|
|
2093
2159
|
def show(self, plot_format: str = "svg") -> None:
|
|
@@ -2124,17 +2190,21 @@ class LSResult:
|
|
|
2124
2190
|
f"<pre><code>{normalized}</code></pre></p>"
|
|
2125
2191
|
)
|
|
2126
2192
|
|
|
2127
|
-
def _build_plot_html(self, plot_format: str) -> str:
|
|
2193
|
+
def _build_plot_html(self, plot_format: str, level: bool = False) -> str:
|
|
2128
2194
|
buf = BytesIO()
|
|
2129
2195
|
plot_actual_vs_fitted(
|
|
2130
2196
|
self.olsmodel, save_to=buf, format=plot_format,
|
|
2131
|
-
figsize=(8, 5), dpi=150,
|
|
2197
|
+
figsize=(8, 5), dpi=150, level=level,
|
|
2132
2198
|
)
|
|
2133
2199
|
buf.seek(0)
|
|
2134
2200
|
b64 = base64.b64encode(buf.read()).decode("utf-8")
|
|
2135
2201
|
mime = "svg+xml" if plot_format == "svg" else "png"
|
|
2202
|
+
heading = (
|
|
2203
|
+
f"Actual vs Fitted Plot: {self.olsmodel.endo_var} itself "
|
|
2204
|
+
"(one-step fit)" if level else "Actual vs Fitted Plot"
|
|
2205
|
+
)
|
|
2136
2206
|
return (
|
|
2137
|
-
"<h3>
|
|
2207
|
+
f"<h3>{heading}</h3>"
|
|
2138
2208
|
f'<img style="max-width:100%; height:auto;" '
|
|
2139
2209
|
f'src="data:image/{mime};base64,{b64}" />'
|
|
2140
2210
|
)
|
|
@@ -2440,28 +2510,46 @@ def plot_actual_vs_fitted(
|
|
|
2440
2510
|
format: str = "png",
|
|
2441
2511
|
figsize: Tuple[float, float] = (7, 3),
|
|
2442
2512
|
dpi: int = 150,
|
|
2513
|
+
level: bool = False,
|
|
2443
2514
|
):
|
|
2444
2515
|
"""Plot Actual vs Fitted (top panel) and Residuals (bottom panel).
|
|
2445
2516
|
|
|
2517
|
+
``level=False`` plots the left-hand side as it was estimated (``af_df``,
|
|
2518
|
+
``residuals_df``), ``level=True`` the endogenous variable itself
|
|
2519
|
+
(``af_level_df``): ``CON`` when the left-hand side is ``DLOG(CON)``.
|
|
2520
|
+
|
|
2446
2521
|
If ``save_to`` is a file-like object, the plot is saved there and the
|
|
2447
2522
|
figure is closed; otherwise it is shown interactively.
|
|
2448
2523
|
"""
|
|
2524
|
+
if level:
|
|
2525
|
+
if emodel.af_level_df is None:
|
|
2526
|
+
raise ValueError(
|
|
2527
|
+
f"{emodel.endo_var}: no actual/fitted for the variable itself"
|
|
2528
|
+
)
|
|
2529
|
+
prefix = f"{emodel.endo_var}__"
|
|
2530
|
+
af_df = residuals_df = emodel.af_level_df.rename(
|
|
2531
|
+
columns=lambda c: c.removeprefix(prefix))
|
|
2532
|
+
title = f"Actual vs Fitted: {emodel.endo_var} itself (one-step fit)"
|
|
2533
|
+
else:
|
|
2534
|
+
af_df, residuals_df = emodel.af_df, emodel.residuals_df
|
|
2535
|
+
title = f"Actual vs Fitted: {emodel.endo_var}"
|
|
2536
|
+
|
|
2449
2537
|
fig = plt.figure(figsize=figsize, dpi=dpi)
|
|
2450
2538
|
gs = GridSpec(3, 1, height_ratios=[2, 0.1, 1])
|
|
2451
2539
|
|
|
2452
2540
|
ax1 = fig.add_subplot(gs[0])
|
|
2453
2541
|
ax2 = fig.add_subplot(gs[2], sharex=ax1)
|
|
2454
2542
|
|
|
2455
|
-
ax1.plot(
|
|
2543
|
+
ax1.plot(af_df.index, af_df["Actual"],
|
|
2456
2544
|
label="Actual", linewidth=2)
|
|
2457
|
-
ax1.plot(
|
|
2545
|
+
ax1.plot(af_df.index, af_df["Fitted"],
|
|
2458
2546
|
label="Fitted", linestyle="--")
|
|
2459
|
-
ax1.set_title(
|
|
2547
|
+
ax1.set_title(title)
|
|
2460
2548
|
ax1.set_ylabel(emodel.endo_var)
|
|
2461
2549
|
ax1.grid(True)
|
|
2462
2550
|
ax1.legend()
|
|
2463
2551
|
|
|
2464
|
-
ax2.plot(
|
|
2552
|
+
ax2.plot(residuals_df.index, residuals_df["Residuals"],
|
|
2465
2553
|
color="gray")
|
|
2466
2554
|
ax2.axhline(0, color="red", linestyle="--", linewidth=1)
|
|
2467
2555
|
ax2.set_title("Residuals")
|