nowcastbox 0.1.0__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.
Files changed (151) hide show
  1. nowcastbox/__init__.py +268 -0
  2. nowcastbox/__main__.py +8 -0
  3. nowcastbox/__version__.py +3 -0
  4. nowcastbox/_logging.py +87 -0
  5. nowcastbox/api.py +557 -0
  6. nowcastbox/benchmarks/__init__.py +39 -0
  7. nowcastbox/benchmarks/_base.py +86 -0
  8. nowcastbox/benchmarks/_utils.py +275 -0
  9. nowcastbox/benchmarks/ar.py +226 -0
  10. nowcastbox/benchmarks/bridge.py +155 -0
  11. nowcastbox/benchmarks/mean.py +71 -0
  12. nowcastbox/benchmarks/midas.py +648 -0
  13. nowcastbox/benchmarks/random_walk.py +89 -0
  14. nowcastbox/benchmarks/sklearn_adapter.py +275 -0
  15. nowcastbox/cli/__init__.py +17 -0
  16. nowcastbox/cli/__main__.py +8 -0
  17. nowcastbox/cli/main.py +405 -0
  18. nowcastbox/core/__init__.py +96 -0
  19. nowcastbox/core/base.py +535 -0
  20. nowcastbox/core/data.py +1771 -0
  21. nowcastbox/core/exceptions.py +121 -0
  22. nowcastbox/core/formula.py +284 -0
  23. nowcastbox/core/frequency.py +937 -0
  24. nowcastbox/core/results.py +871 -0
  25. nowcastbox/data_sources/__init__.py +84 -0
  26. nowcastbox/data_sources/_http.py +261 -0
  27. nowcastbox/data_sources/_parsing.py +356 -0
  28. nowcastbox/data_sources/bcb.py +351 -0
  29. nowcastbox/data_sources/cache.py +492 -0
  30. nowcastbox/data_sources/exceptions.py +70 -0
  31. nowcastbox/data_sources/fred.py +277 -0
  32. nowcastbox/data_sources/ibge.py +463 -0
  33. nowcastbox/data_sources/ipea.py +321 -0
  34. nowcastbox/datasets/__init__.py +55 -0
  35. nowcastbox/datasets/_io.py +354 -0
  36. nowcastbox/datasets/_simulated.py +164 -0
  37. nowcastbox/datasets/data/brazil_gdp_releases.csv.gz +0 -0
  38. nowcastbox/datasets/data/brazil_nowcast.csv.gz +0 -0
  39. nowcastbox/datasets/data/brazil_vintages.csv.gz +0 -0
  40. nowcastbox/datasets/data/nyfed.csv.gz +0 -0
  41. nowcastbox/datasets/data/us_fred_md.csv.gz +0 -0
  42. nowcastbox/datasets/dataset.py +469 -0
  43. nowcastbox/datasets/licenses/NYFED-BSD-3-Clause.txt +36 -0
  44. nowcastbox/datasets/load.py +715 -0
  45. nowcastbox/datasets/metadata/brazil_calendar.yaml +99 -0
  46. nowcastbox/datasets/metadata/brazil_nowcast.yaml +1255 -0
  47. nowcastbox/datasets/metadata/brazil_vintages.yaml +78 -0
  48. nowcastbox/datasets/metadata/nyfed.yaml +452 -0
  49. nowcastbox/datasets/metadata/simulated_dfm.yaml +26 -0
  50. nowcastbox/datasets/metadata/us_fred_md.yaml +1594 -0
  51. nowcastbox/datasets/metadata/us_grs_like.yaml +62 -0
  52. nowcastbox/density/__init__.py +47 -0
  53. nowcastbox/density/bootstrap.py +718 -0
  54. nowcastbox/density/distribution.py +757 -0
  55. nowcastbox/density/predictive.py +317 -0
  56. nowcastbox/diagnostics/__init__.py +82 -0
  57. nowcastbox/diagnostics/_common.py +445 -0
  58. nowcastbox/diagnostics/contribution.py +423 -0
  59. nowcastbox/diagnostics/convergence.py +298 -0
  60. nowcastbox/diagnostics/data_quality.py +324 -0
  61. nowcastbox/diagnostics/report.py +653 -0
  62. nowcastbox/diagnostics/residuals.py +362 -0
  63. nowcastbox/diagnostics/stability.py +672 -0
  64. nowcastbox/evaluation/__init__.py +117 -0
  65. nowcastbox/evaluation/backtest.py +1445 -0
  66. nowcastbox/evaluation/metrics.py +386 -0
  67. nowcastbox/evaluation/scoring.py +1076 -0
  68. nowcastbox/evaluation/tests.py +900 -0
  69. nowcastbox/experiment/__init__.py +18 -0
  70. nowcastbox/experiment/experiment.py +813 -0
  71. nowcastbox/models/__init__.py +29 -0
  72. nowcastbox/models/_em_steps.py +1588 -0
  73. nowcastbox/models/_init_conditions.py +464 -0
  74. nowcastbox/models/_pca.py +314 -0
  75. nowcastbox/models/bridge.py +785 -0
  76. nowcastbox/models/em.py +1673 -0
  77. nowcastbox/models/long_run.py +257 -0
  78. nowcastbox/models/robust.py +941 -0
  79. nowcastbox/models/two_step.py +1183 -0
  80. nowcastbox/news/__init__.py +181 -0
  81. nowcastbox/news/_linear.py +158 -0
  82. nowcastbox/news/_model.py +763 -0
  83. nowcastbox/news/contributions.py +279 -0
  84. nowcastbox/news/decomposition.py +772 -0
  85. nowcastbox/news/plotting.py +299 -0
  86. nowcastbox/news/revisions.py +169 -0
  87. nowcastbox/news/tracker.py +476 -0
  88. nowcastbox/pipeline/__init__.py +103 -0
  89. nowcastbox/pipeline/data.py +513 -0
  90. nowcastbox/pipeline/examples.py +96 -0
  91. nowcastbox/pipeline/runner.py +713 -0
  92. nowcastbox/pipeline/snapshots.py +1171 -0
  93. nowcastbox/pipeline/spec.py +1712 -0
  94. nowcastbox/pipeline/templates/brazil_pib.yaml +33 -0
  95. nowcastbox/pipeline/templates/connectors.yaml +52 -0
  96. nowcastbox/pipeline/templates/csv.yaml +25 -0
  97. nowcastbox/pipeline/templates/simulated.yaml +26 -0
  98. nowcastbox/preprocessing/__init__.py +115 -0
  99. nowcastbox/preprocessing/_utils.py +288 -0
  100. nowcastbox/preprocessing/aggregation.py +1085 -0
  101. nowcastbox/preprocessing/missing.py +362 -0
  102. nowcastbox/preprocessing/outliers.py +329 -0
  103. nowcastbox/preprocessing/panel.py +432 -0
  104. nowcastbox/preprocessing/transforms.py +1189 -0
  105. nowcastbox/py.typed +0 -0
  106. nowcastbox/reports/__init__.py +13 -0
  107. nowcastbox/reports/html.py +596 -0
  108. nowcastbox/reports/templates/nowcast_report.html +115 -0
  109. nowcastbox/selection/__init__.py +69 -0
  110. nowcastbox/selection/_panel.py +302 -0
  111. nowcastbox/selection/_plot.py +145 -0
  112. nowcastbox/selection/bai_ng_factors.py +437 -0
  113. nowcastbox/selection/bai_ng_shocks.py +429 -0
  114. nowcastbox/selection/blocks.py +1155 -0
  115. nowcastbox/selection/targeted.py +896 -0
  116. nowcastbox/simulate/__init__.py +19 -0
  117. nowcastbox/simulate/dfm.py +282 -0
  118. nowcastbox/statespace/__init__.py +94 -0
  119. nowcastbox/statespace/_band_kernels.py +452 -0
  120. nowcastbox/statespace/_kernels.py +452 -0
  121. nowcastbox/statespace/collapse.py +227 -0
  122. nowcastbox/statespace/kalman.py +487 -0
  123. nowcastbox/statespace/representation.py +1032 -0
  124. nowcastbox/statespace/simulate.py +221 -0
  125. nowcastbox/statespace/smoother.py +254 -0
  126. nowcastbox/statespace/structure.py +354 -0
  127. nowcastbox/statespace/structured.py +1152 -0
  128. nowcastbox/utils.py +18 -0
  129. nowcastbox/vintages/__init__.py +29 -0
  130. nowcastbox/vintages/_utils.py +323 -0
  131. nowcastbox/vintages/calendar.py +981 -0
  132. nowcastbox/vintages/pseudo_real_time.py +393 -0
  133. nowcastbox/vintages/vintage_store.py +1221 -0
  134. nowcastbox/visualization/__init__.py +82 -0
  135. nowcastbox/visualization/_common.py +174 -0
  136. nowcastbox/visualization/data_flow.py +316 -0
  137. nowcastbox/visualization/diagnostics.py +104 -0
  138. nowcastbox/visualization/evaluation.py +188 -0
  139. nowcastbox/visualization/factors.py +564 -0
  140. nowcastbox/visualization/forecast.py +652 -0
  141. nowcastbox/visualization/news.py +680 -0
  142. nowcastbox/visualization/registry.py +166 -0
  143. nowcastbox/visualization/selection.py +216 -0
  144. nowcastbox/visualization/themes.py +431 -0
  145. nowcastbox-0.1.0.dist-info/METADATA +274 -0
  146. nowcastbox-0.1.0.dist-info/RECORD +151 -0
  147. nowcastbox-0.1.0.dist-info/WHEEL +5 -0
  148. nowcastbox-0.1.0.dist-info/entry_points.txt +2 -0
  149. nowcastbox-0.1.0.dist-info/licenses/LICENSE +21 -0
  150. nowcastbox-0.1.0.dist-info/licenses/nowcastbox/datasets/licenses/NYFED-BSD-3-Clause.txt +36 -0
  151. nowcastbox-0.1.0.dist-info/top_level.txt +1 -0
nowcastbox/__init__.py ADDED
@@ -0,0 +1,268 @@
1
+ """NowcastBox: nowcasting with dynamic factor models in Python.
2
+
3
+ The most used objects are available at the top level (``nb.MixedFreqDFM``,
4
+ ``nb.prepare_panel``, ``nb.select_factors``, ``nb.pseudo_real_time``, ``nb.nowcast``,
5
+ ``nb.news_decomposition``, ``nb.PseudoRealTimeBacktest``, ``nb.nowcast_distribution``,
6
+ ``nb.run_diagnostics``, ``nb.NowcastReport``, ``nb.load_brazil_nowcast``,
7
+ ``nb.select_blocks``, ``nb.run_pipeline``...); the subpackages (``nb.preprocessing``,
8
+ ``nb.statespace``, ``nb.models``, ``nb.selection``, ``nb.vintages``,
9
+ ``nb.data_sources``, ``nb.datasets``, ``nb.news``, ``nb.density``, ``nb.evaluation``,
10
+ ``nb.benchmarks``, ``nb.diagnostics``, ``nb.visualization``, ``nb.reports``,
11
+ ``nb.experiment``, ``nb.pipeline``, ``nb.cli``, ``nb.simulate``) hold the complete API.
12
+
13
+ Production use (innovation I10): ``nb.run_pipeline("nowcast.yaml")`` or the
14
+ ``nowcastbox run nowcast.yaml`` command line, with versioned snapshots
15
+ (``nb.SnapshotStore``).
16
+
17
+ Fitted results also expose the wave-2 tools as methods: ``res.news(old, new, period)``,
18
+ ``res.nowcast_tracker(...)``, ``res.level_contributions()``, ``res.distribution()``,
19
+ ``res.diagnostics()`` and ``res.plot(kind)``.
20
+
21
+ Examples
22
+ --------
23
+ >>> import nowcastbox as nb
24
+ >>> isinstance(nb.__version__, str)
25
+ True
26
+ >>> from nowcastbox.models.two_step import simulate_two_step_example
27
+ >>> data = simulate_two_step_example(random_state=0)
28
+ >>> res = nb.TwoStepDFM(n_factors=1).fit(data, target="gdp")
29
+ >>> list(res.nowcast.columns)[:3]
30
+ ['observed', 'in_sample', 'out_of_sample']
31
+ """
32
+
33
+ from nowcastbox import (
34
+ benchmarks,
35
+ cli,
36
+ core,
37
+ data_sources,
38
+ datasets,
39
+ density,
40
+ diagnostics,
41
+ evaluation,
42
+ experiment,
43
+ models,
44
+ news,
45
+ pipeline,
46
+ preprocessing,
47
+ reports,
48
+ selection,
49
+ simulate,
50
+ statespace,
51
+ utils,
52
+ vintages,
53
+ visualization,
54
+ )
55
+ from nowcastbox.__version__ import __version__
56
+ from nowcastbox.api import nowcast
57
+ from nowcastbox.core import (
58
+ AggregationType,
59
+ BaseBenchmark,
60
+ BaseNowcaster,
61
+ ConvergenceWarning,
62
+ DataQualityWarning,
63
+ FactorResults,
64
+ FormulaError,
65
+ Frequency,
66
+ MixedFrequencyData,
67
+ ModelNotFittedError,
68
+ NowcastBoxError,
69
+ NowcastBoxWarning,
70
+ NowcastDataError,
71
+ NowcastResults,
72
+ SeriesCategory,
73
+ SeriesMetadata,
74
+ as_mixed_frequency_data,
75
+ parse_formula,
76
+ )
77
+ from nowcastbox.datasets import (
78
+ Dataset,
79
+ list_datasets,
80
+ load_brazil_calendar,
81
+ load_brazil_nowcast,
82
+ load_brazil_vintages,
83
+ load_dataset,
84
+ load_nyfed,
85
+ load_simulated_dfm,
86
+ load_us_fred_md,
87
+ load_us_grs_like,
88
+ )
89
+ from nowcastbox.density import NowcastDistribution, nowcast_distribution
90
+ from nowcastbox.diagnostics import DiagnosticsReport, run_diagnostics
91
+ from nowcastbox.evaluation import BacktestResults, PseudoRealTimeBacktest, scoring
92
+ from nowcastbox.experiment import NowcastExperiment
93
+ from nowcastbox.models import (
94
+ BridgeEquation,
95
+ BridgeResults,
96
+ MixedFreqDFM,
97
+ MixedFreqDFMResults,
98
+ TwoStepDFM,
99
+ TwoStepResults,
100
+ )
101
+ from nowcastbox.news import (
102
+ LevelContributions,
103
+ NewsResults,
104
+ NowcastTracker,
105
+ level_contributions,
106
+ news_decomposition,
107
+ nowcast_tracker,
108
+ )
109
+ from nowcastbox.pipeline import NowcastSpec, PipelineRun, SnapshotStore, run_pipeline
110
+ from nowcastbox.preprocessing import (
111
+ CalendarAggregation,
112
+ Compose,
113
+ Diff,
114
+ Identity,
115
+ Log,
116
+ PctChange,
117
+ Scale,
118
+ TemporalAggregation,
119
+ Transform,
120
+ apply_transforms,
121
+ calendar_aggregation,
122
+ invert_transforms,
123
+ month_to_quarter,
124
+ prepare_panel,
125
+ quarter_to_month,
126
+ )
127
+ from nowcastbox.reports import NowcastReport
128
+ from nowcastbox.selection import (
129
+ FactorSelectionResult,
130
+ SelectionPath,
131
+ ShockSelectionResult,
132
+ ValidationSettings,
133
+ hard_threshold,
134
+ select_blocks,
135
+ select_factors,
136
+ select_shocks,
137
+ select_targeted_predictors,
138
+ select_variables,
139
+ soft_threshold,
140
+ )
141
+ from nowcastbox.statespace import (
142
+ StateSpace,
143
+ kalman_filter,
144
+ kalman_smoother,
145
+ loglikelihood,
146
+ simulate_state_space,
147
+ )
148
+ from nowcastbox.vintages import (
149
+ ReleaseCalendar,
150
+ Vintage,
151
+ VintageStore,
152
+ generate_vintages,
153
+ pseudo_real_time,
154
+ )
155
+
156
+ __all__ = [
157
+ "AggregationType",
158
+ "BacktestResults",
159
+ "BaseBenchmark",
160
+ "BaseNowcaster",
161
+ "BridgeEquation",
162
+ "BridgeResults",
163
+ "CalendarAggregation",
164
+ "Compose",
165
+ "ConvergenceWarning",
166
+ "DataQualityWarning",
167
+ "Dataset",
168
+ "DiagnosticsReport",
169
+ "Diff",
170
+ "FactorResults",
171
+ "FactorSelectionResult",
172
+ "FormulaError",
173
+ "Frequency",
174
+ "Identity",
175
+ "LevelContributions",
176
+ "Log",
177
+ "MixedFreqDFM",
178
+ "MixedFreqDFMResults",
179
+ "MixedFrequencyData",
180
+ "ModelNotFittedError",
181
+ "NewsResults",
182
+ "NowcastBoxError",
183
+ "NowcastBoxWarning",
184
+ "NowcastDataError",
185
+ "NowcastDistribution",
186
+ "NowcastExperiment",
187
+ "NowcastReport",
188
+ "NowcastResults",
189
+ "NowcastSpec",
190
+ "NowcastTracker",
191
+ "PctChange",
192
+ "PipelineRun",
193
+ "PseudoRealTimeBacktest",
194
+ "ReleaseCalendar",
195
+ "Scale",
196
+ "SelectionPath",
197
+ "SeriesCategory",
198
+ "SeriesMetadata",
199
+ "ShockSelectionResult",
200
+ "SnapshotStore",
201
+ "StateSpace",
202
+ "TemporalAggregation",
203
+ "Transform",
204
+ "TwoStepDFM",
205
+ "TwoStepResults",
206
+ "ValidationSettings",
207
+ "Vintage",
208
+ "VintageStore",
209
+ "__version__",
210
+ "apply_transforms",
211
+ "as_mixed_frequency_data",
212
+ "benchmarks",
213
+ "calendar_aggregation",
214
+ "cli",
215
+ "core",
216
+ "data_sources",
217
+ "datasets",
218
+ "density",
219
+ "diagnostics",
220
+ "evaluation",
221
+ "experiment",
222
+ "generate_vintages",
223
+ "hard_threshold",
224
+ "invert_transforms",
225
+ "kalman_filter",
226
+ "kalman_smoother",
227
+ "level_contributions",
228
+ "list_datasets",
229
+ "load_brazil_calendar",
230
+ "load_brazil_nowcast",
231
+ "load_brazil_vintages",
232
+ "load_dataset",
233
+ "load_nyfed",
234
+ "load_simulated_dfm",
235
+ "load_us_fred_md",
236
+ "load_us_grs_like",
237
+ "loglikelihood",
238
+ "models",
239
+ "month_to_quarter",
240
+ "news",
241
+ "news_decomposition",
242
+ "nowcast",
243
+ "nowcast_distribution",
244
+ "nowcast_tracker",
245
+ "parse_formula",
246
+ "pipeline",
247
+ "prepare_panel",
248
+ "preprocessing",
249
+ "pseudo_real_time",
250
+ "quarter_to_month",
251
+ "reports",
252
+ "run_diagnostics",
253
+ "run_pipeline",
254
+ "scoring",
255
+ "select_blocks",
256
+ "select_factors",
257
+ "select_shocks",
258
+ "select_targeted_predictors",
259
+ "select_variables",
260
+ "selection",
261
+ "simulate",
262
+ "simulate_state_space",
263
+ "soft_threshold",
264
+ "statespace",
265
+ "utils",
266
+ "vintages",
267
+ "visualization",
268
+ ]
nowcastbox/__main__.py ADDED
@@ -0,0 +1,8 @@
1
+ """``python -m nowcastbox``: run the command-line interface."""
2
+
3
+ import sys
4
+
5
+ from nowcastbox.cli.main import main
6
+
7
+ if __name__ == "__main__": # pragma: no cover
8
+ sys.exit(main())
@@ -0,0 +1,3 @@
1
+ """Version information for nowcastbox."""
2
+
3
+ __version__ = "0.1.0"
nowcastbox/_logging.py ADDED
@@ -0,0 +1,87 @@
1
+ """Package-wide logging helpers.
2
+
3
+ All modules obtain their logger through :func:`get_logger`, so that every logger is a
4
+ child of the ``"nowcastbox"`` logger. A :class:`logging.NullHandler` is attached to the
5
+ root package logger (library best practice): nothing is printed unless the user
6
+ configures logging or calls :func:`set_log_level`.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import logging
12
+
13
+ __all__ = ["LOGGER_NAME", "get_logger", "set_log_level"]
14
+
15
+ LOGGER_NAME = "nowcastbox"
16
+
17
+ _package_logger = logging.getLogger(LOGGER_NAME)
18
+ _package_logger.addHandler(logging.NullHandler())
19
+
20
+
21
+ def get_logger(name: str | None = None) -> logging.Logger:
22
+ """Return a logger in the ``nowcastbox`` hierarchy.
23
+
24
+ Parameters
25
+ ----------
26
+ name : str, optional
27
+ Module name, typically ``__name__``. Names that do not start with
28
+ ``"nowcastbox"`` are prefixed with it. ``None`` returns the package logger.
29
+
30
+ Returns
31
+ -------
32
+ logging.Logger
33
+ The requested logger.
34
+
35
+ Examples
36
+ --------
37
+ >>> from nowcastbox._logging import get_logger
38
+ >>> get_logger("nowcastbox.models.em").name
39
+ 'nowcastbox.models.em'
40
+ >>> get_logger("custom").name
41
+ 'nowcastbox.custom'
42
+ """
43
+ if name is None or name == LOGGER_NAME:
44
+ return _package_logger
45
+ if not name.startswith(LOGGER_NAME + "."):
46
+ name = f"{LOGGER_NAME}.{name}"
47
+ return logging.getLogger(name)
48
+
49
+
50
+ def set_log_level(level: int | str, *, add_stream_handler: bool = True) -> None:
51
+ """Set the verbosity of the package logger.
52
+
53
+ Parameters
54
+ ----------
55
+ level : int or str
56
+ Logging level, e.g. ``logging.INFO`` or ``"DEBUG"``.
57
+ add_stream_handler : bool, default True
58
+ Attach a :class:`logging.StreamHandler` to the package logger if it has none,
59
+ so that messages are actually shown.
60
+
61
+ Raises
62
+ ------
63
+ ValueError
64
+ If ``level`` is a string that is not a valid logging level name.
65
+
66
+ Examples
67
+ --------
68
+ >>> import logging
69
+ >>> from nowcastbox._logging import set_log_level, get_logger
70
+ >>> set_log_level("WARNING", add_stream_handler=False)
71
+ >>> get_logger().level == logging.WARNING
72
+ True
73
+ """
74
+ if isinstance(level, str):
75
+ resolved = logging.getLevelName(level.upper())
76
+ if not isinstance(resolved, int):
77
+ raise ValueError(f"Unknown logging level {level!r}.")
78
+ level = resolved
79
+ _package_logger.setLevel(level)
80
+ has_stream = any(
81
+ isinstance(h, logging.StreamHandler) and not isinstance(h, logging.NullHandler)
82
+ for h in _package_logger.handlers
83
+ )
84
+ if add_stream_handler and not has_stream:
85
+ handler = logging.StreamHandler()
86
+ handler.setFormatter(logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s"))
87
+ _package_logger.addHandler(handler)