macroforecast 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.
- macroforecast/__init__.py +85 -0
- macroforecast/cli.py +293 -0
- macroforecast/config.py +604 -0
- macroforecast/data/__init__.py +30 -0
- macroforecast/data/_base.py +390 -0
- macroforecast/data/fred_md.py +160 -0
- macroforecast/data/fred_qd.py +144 -0
- macroforecast/data/fred_sd.py +184 -0
- macroforecast/data/merge.py +283 -0
- macroforecast/data/schema.py +468 -0
- macroforecast/data/specs/__init__.py +0 -0
- macroforecast/data/specs/fred_md.json +144 -0
- macroforecast/data/specs/fred_qd.json +50 -0
- macroforecast/data/specs/fred_sd.json +31 -0
- macroforecast/data/vintages.py +174 -0
- macroforecast/evaluation/__init__.py +60 -0
- macroforecast/evaluation/combination.py +222 -0
- macroforecast/evaluation/cw.py +167 -0
- macroforecast/evaluation/decomposition.py +188 -0
- macroforecast/evaluation/dm.py +156 -0
- macroforecast/evaluation/gw.py +209 -0
- macroforecast/evaluation/horserace.py +441 -0
- macroforecast/evaluation/mcs.py +223 -0
- macroforecast/evaluation/metrics.py +78 -0
- macroforecast/evaluation/regime.py +236 -0
- macroforecast/interpretation/__init__.py +49 -0
- macroforecast/interpretation/dual.py +248 -0
- macroforecast/interpretation/marginal.py +634 -0
- macroforecast/interpretation/pbsv.py +259 -0
- macroforecast/interpretation/variable_importance.py +248 -0
- macroforecast/mcp/__init__.py +5 -0
- macroforecast/mcp/config.py +173 -0
- macroforecast/mcp/indexer.py +314 -0
- macroforecast/mcp/ingest_blog.py +105 -0
- macroforecast/mcp/ingest_pdf.py +83 -0
- macroforecast/mcp/server.py +245 -0
- macroforecast/pipeline/__init__.py +83 -0
- macroforecast/pipeline/components.py +176 -0
- macroforecast/pipeline/estimator.py +133 -0
- macroforecast/pipeline/experiment.py +693 -0
- macroforecast/pipeline/features.py +489 -0
- macroforecast/pipeline/horserace.py +169 -0
- macroforecast/pipeline/models.py +957 -0
- macroforecast/pipeline/r_models.py +506 -0
- macroforecast/pipeline/results.py +373 -0
- macroforecast/preprocessing/__init__.py +54 -0
- macroforecast/preprocessing/missing.py +200 -0
- macroforecast/preprocessing/panel.py +506 -0
- macroforecast/preprocessing/transforms.py +398 -0
- macroforecast/replication/__init__.py +8 -0
- macroforecast/replication/clss2021.py +340 -0
- macroforecast/utils/__init__.py +24 -0
- macroforecast/utils/cache.py +199 -0
- macroforecast/utils/latex.py +310 -0
- macroforecast/utils/registry.py +301 -0
- macroforecast/viz/__init__.py +19 -0
- macroforecast/viz/plots.py +966 -0
- macroforecast-0.1.0.dist-info/METADATA +156 -0
- macroforecast-0.1.0.dist-info/RECORD +62 -0
- macroforecast-0.1.0.dist-info/WHEEL +4 -0
- macroforecast-0.1.0.dist-info/entry_points.txt +2 -0
- macroforecast-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""macroforecast: Decomposing ML Forecast Gains in Macroeconomic Forecasting.
|
|
2
|
+
|
|
3
|
+
Modules
|
|
4
|
+
-------
|
|
5
|
+
* ``macroforecast.data`` — FRED-MD/QD/SD loaders + MacroFrame
|
|
6
|
+
* ``macroforecast.preprocessing`` — tcode transforms, MARX/MAF, panel preprocessing
|
|
7
|
+
* ``macroforecast.pipeline`` — ForecastExperiment, models, features
|
|
8
|
+
* ``macroforecast.evaluation`` — MSFE, MCS, DM, CW statistical tests
|
|
9
|
+
* ``macroforecast.interpretation`` — dual weights, PBSV, variable importance
|
|
10
|
+
* ``macroforecast.viz`` — visualization
|
|
11
|
+
* ``macroforecast.utils`` — registry, LaTeX export, cache
|
|
12
|
+
* ``macroforecast.replication`` — paper-specific helpers (CLSS 2021 etc.)
|
|
13
|
+
|
|
14
|
+
Quick start::
|
|
15
|
+
|
|
16
|
+
from macroforecast import load_fred_md, ForecastExperiment
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
__version__ = "0.1.0"
|
|
20
|
+
|
|
21
|
+
from macroforecast.data import (
|
|
22
|
+
MacroFrame,
|
|
23
|
+
MacroFrameMetadata,
|
|
24
|
+
MergeResult,
|
|
25
|
+
RealTimePanel,
|
|
26
|
+
VariableMetadata,
|
|
27
|
+
list_available_vintages,
|
|
28
|
+
load_fred_md,
|
|
29
|
+
load_fred_qd,
|
|
30
|
+
load_fred_sd,
|
|
31
|
+
load_vintage_panel,
|
|
32
|
+
merge_macro_frames,
|
|
33
|
+
)
|
|
34
|
+
from macroforecast.pipeline import (
|
|
35
|
+
FeatureSpec,
|
|
36
|
+
ForecastExperiment,
|
|
37
|
+
ForecastRecord,
|
|
38
|
+
ModelSpec,
|
|
39
|
+
ResultSet,
|
|
40
|
+
)
|
|
41
|
+
from macroforecast.preprocessing import (
|
|
42
|
+
TransformCode,
|
|
43
|
+
apply_hamilton_filter,
|
|
44
|
+
apply_maf,
|
|
45
|
+
apply_marx,
|
|
46
|
+
apply_pca,
|
|
47
|
+
apply_tcode,
|
|
48
|
+
apply_tcodes,
|
|
49
|
+
apply_x_factors,
|
|
50
|
+
classify_missing,
|
|
51
|
+
handle_missing,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
__all__ = [
|
|
55
|
+
"__version__",
|
|
56
|
+
# data
|
|
57
|
+
"load_fred_md",
|
|
58
|
+
"load_fred_qd",
|
|
59
|
+
"load_fred_sd",
|
|
60
|
+
"MacroFrame",
|
|
61
|
+
"MacroFrameMetadata",
|
|
62
|
+
"VariableMetadata",
|
|
63
|
+
"list_available_vintages",
|
|
64
|
+
"load_vintage_panel",
|
|
65
|
+
"RealTimePanel",
|
|
66
|
+
"merge_macro_frames",
|
|
67
|
+
"MergeResult",
|
|
68
|
+
# preprocessing
|
|
69
|
+
"TransformCode",
|
|
70
|
+
"apply_tcode",
|
|
71
|
+
"apply_tcodes",
|
|
72
|
+
"apply_marx",
|
|
73
|
+
"apply_maf",
|
|
74
|
+
"apply_x_factors",
|
|
75
|
+
"apply_pca",
|
|
76
|
+
"apply_hamilton_filter",
|
|
77
|
+
"classify_missing",
|
|
78
|
+
"handle_missing",
|
|
79
|
+
# pipeline
|
|
80
|
+
"ForecastExperiment",
|
|
81
|
+
"ModelSpec",
|
|
82
|
+
"FeatureSpec",
|
|
83
|
+
"ResultSet",
|
|
84
|
+
"ForecastRecord",
|
|
85
|
+
]
|
macroforecast/cli.py
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
"""macroforecast command-line interface.
|
|
2
|
+
|
|
3
|
+
Usage
|
|
4
|
+
-----
|
|
5
|
+
macroforecast --help
|
|
6
|
+
macroforecast run experiment.yaml
|
|
7
|
+
macroforecast init [--output experiment.yaml]
|
|
8
|
+
macroforecast info experiment.yaml
|
|
9
|
+
|
|
10
|
+
Commands
|
|
11
|
+
--------
|
|
12
|
+
run Execute a forecast experiment from a YAML config file.
|
|
13
|
+
init Write a default YAML config template to disk.
|
|
14
|
+
info Print a summary of the resolved config without running.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import argparse
|
|
20
|
+
import logging
|
|
21
|
+
import sys
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger("macroforecast")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# ---------------------------------------------------------------------------
|
|
28
|
+
# Logging setup
|
|
29
|
+
# ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _setup_logging(verbose: bool = False) -> None:
|
|
33
|
+
level = logging.DEBUG if verbose else logging.INFO
|
|
34
|
+
logging.basicConfig(
|
|
35
|
+
format="%(asctime)s %(levelname)-8s %(name)s — %(message)s",
|
|
36
|
+
datefmt="%H:%M:%S",
|
|
37
|
+
level=level,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# ---------------------------------------------------------------------------
|
|
42
|
+
# run
|
|
43
|
+
# ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _cmd_run(args: argparse.Namespace) -> int:
|
|
47
|
+
"""Execute a forecast experiment defined in a YAML config file."""
|
|
48
|
+
from macroforecast.config import load_config
|
|
49
|
+
from macroforecast.pipeline.experiment import ForecastExperiment
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
cfg = load_config(args.config)
|
|
53
|
+
except (FileNotFoundError, ValueError) as exc:
|
|
54
|
+
logger.error("Config error: %s", exc)
|
|
55
|
+
return 1
|
|
56
|
+
|
|
57
|
+
logger.info("Experiment: %s", cfg.experiment_id)
|
|
58
|
+
logger.info(
|
|
59
|
+
"Dataset: %s (target=%s, vintage=%s)",
|
|
60
|
+
cfg.data.dataset,
|
|
61
|
+
cfg.data.target,
|
|
62
|
+
cfg.data.vintage or "current",
|
|
63
|
+
)
|
|
64
|
+
logger.info("Models: %d configured", len(cfg.model_specs))
|
|
65
|
+
logger.info("Horizons: %s", cfg.horizons)
|
|
66
|
+
logger.info("Window: %s", cfg.window.value)
|
|
67
|
+
logger.info("OOS range: %s → %s", cfg.oos_start or "auto", cfg.oos_end or "end")
|
|
68
|
+
|
|
69
|
+
# Load data
|
|
70
|
+
try:
|
|
71
|
+
panel, target = _load_data(cfg)
|
|
72
|
+
except Exception as exc:
|
|
73
|
+
logger.error("Data loading failed: %s", exc)
|
|
74
|
+
return 1
|
|
75
|
+
|
|
76
|
+
output_dir = cfg.output_dir / cfg.experiment_id
|
|
77
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
78
|
+
|
|
79
|
+
exp = ForecastExperiment(
|
|
80
|
+
panel=panel,
|
|
81
|
+
target=target,
|
|
82
|
+
horizons=cfg.horizons,
|
|
83
|
+
model_specs=cfg.model_specs,
|
|
84
|
+
feature_spec=cfg.feature_spec,
|
|
85
|
+
window=cfg.window,
|
|
86
|
+
rolling_size=cfg.rolling_size,
|
|
87
|
+
oos_start=cfg.oos_start,
|
|
88
|
+
oos_end=cfg.oos_end,
|
|
89
|
+
n_jobs=cfg.n_jobs,
|
|
90
|
+
experiment_id=cfg.experiment_id,
|
|
91
|
+
output_dir=output_dir,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
logger.info("Running experiment...")
|
|
95
|
+
rs = exp.run()
|
|
96
|
+
|
|
97
|
+
logger.info("Done. %d forecast records.", len(rs))
|
|
98
|
+
|
|
99
|
+
if args.summary:
|
|
100
|
+
_print_summary(rs)
|
|
101
|
+
|
|
102
|
+
return 0
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _load_data(cfg):
|
|
106
|
+
"""Load dataset and extract panel + target from MacroFrame."""
|
|
107
|
+
from macroforecast.data import load_fred_md, load_fred_qd
|
|
108
|
+
|
|
109
|
+
dataset = cfg.data.dataset.lower().replace("-", "_")
|
|
110
|
+
cache_dir = Path(cfg.data.cache_dir).expanduser() if cfg.data.cache_dir else None
|
|
111
|
+
|
|
112
|
+
if dataset == "fred_md":
|
|
113
|
+
mf = load_fred_md(vintage=cfg.data.vintage, cache_dir=cache_dir)
|
|
114
|
+
elif dataset == "fred_qd":
|
|
115
|
+
mf = load_fred_qd(vintage=cfg.data.vintage, cache_dir=cache_dir)
|
|
116
|
+
else:
|
|
117
|
+
raise ValueError(
|
|
118
|
+
f"Dataset '{cfg.data.dataset}' not supported via CLI. "
|
|
119
|
+
"Use fred_md or fred_qd. For fred_sd, load programmatically."
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# Apply stationarity transforms if needed
|
|
123
|
+
if not mf.metadata.is_transformed:
|
|
124
|
+
mf = mf.transform()
|
|
125
|
+
|
|
126
|
+
df = mf.data
|
|
127
|
+
|
|
128
|
+
if cfg.data.target not in df.columns:
|
|
129
|
+
raise ValueError(
|
|
130
|
+
f"Target '{cfg.data.target}' not found in dataset. "
|
|
131
|
+
f"Available columns: {list(df.columns[:10])} ..."
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
target = df[cfg.data.target]
|
|
135
|
+
panel = df.drop(columns=[cfg.data.target])
|
|
136
|
+
|
|
137
|
+
# Drop columns with all NaN
|
|
138
|
+
panel = panel.dropna(axis=1, how="all")
|
|
139
|
+
|
|
140
|
+
# Restrict to rows where target is observed
|
|
141
|
+
mask = target.notna()
|
|
142
|
+
panel = panel.loc[mask]
|
|
143
|
+
target = target.loc[mask]
|
|
144
|
+
|
|
145
|
+
return panel, target
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _print_summary(rs) -> None:
|
|
149
|
+
"""Print MSFE table to stdout."""
|
|
150
|
+
try:
|
|
151
|
+
summary = rs.msfe_by_model()
|
|
152
|
+
if summary.empty:
|
|
153
|
+
print("No results to summarise.")
|
|
154
|
+
return
|
|
155
|
+
print("\n--- MSFE Summary ---")
|
|
156
|
+
print(summary.to_string(index=False))
|
|
157
|
+
except Exception:
|
|
158
|
+
pass
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# ---------------------------------------------------------------------------
|
|
162
|
+
# init
|
|
163
|
+
# ---------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _cmd_init(args: argparse.Namespace) -> int:
|
|
167
|
+
"""Write the default YAML config template."""
|
|
168
|
+
from macroforecast.config import DEFAULT_CONFIG_YAML
|
|
169
|
+
|
|
170
|
+
out_path = Path(args.output)
|
|
171
|
+
if out_path.exists() and not args.force:
|
|
172
|
+
logger.error("File already exists: %s. Use --force to overwrite.", out_path)
|
|
173
|
+
return 1
|
|
174
|
+
|
|
175
|
+
out_path.write_text(DEFAULT_CONFIG_YAML)
|
|
176
|
+
logger.info("Config template written to: %s", out_path)
|
|
177
|
+
return 0
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# ---------------------------------------------------------------------------
|
|
181
|
+
# info
|
|
182
|
+
# ---------------------------------------------------------------------------
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _cmd_info(args: argparse.Namespace) -> int:
|
|
186
|
+
"""Print a resolved config summary without running."""
|
|
187
|
+
from macroforecast.config import load_config
|
|
188
|
+
|
|
189
|
+
try:
|
|
190
|
+
cfg = load_config(args.config)
|
|
191
|
+
except (FileNotFoundError, ValueError) as exc:
|
|
192
|
+
logger.error("Config error: %s", exc)
|
|
193
|
+
return 1
|
|
194
|
+
|
|
195
|
+
print(f"Experiment ID: {cfg.experiment_id}")
|
|
196
|
+
print(f"Output dir: {cfg.output_dir}")
|
|
197
|
+
print(f"Dataset: {cfg.data.dataset}")
|
|
198
|
+
print(f"Target: {cfg.data.target}")
|
|
199
|
+
print(f"Vintage: {cfg.data.vintage or 'current'}")
|
|
200
|
+
print(f"Horizons: {cfg.horizons}")
|
|
201
|
+
print(f"Window: {cfg.window.value}")
|
|
202
|
+
print(f"OOS start: {cfg.oos_start or 'auto'}")
|
|
203
|
+
print(f"OOS end: {cfg.oos_end or 'auto'}")
|
|
204
|
+
print(f"n_jobs: {cfg.n_jobs}")
|
|
205
|
+
print(f"Models ({len(cfg.model_specs)}):")
|
|
206
|
+
for spec in cfg.model_specs:
|
|
207
|
+
print(f" - {spec.model_id}")
|
|
208
|
+
print("Features:")
|
|
209
|
+
fs = cfg.feature_spec
|
|
210
|
+
print(
|
|
211
|
+
f" factor_type={fs.factor_type!r}, n_factors={fs.n_factors}, "
|
|
212
|
+
f"n_lags={fs.n_lags}, lookback={fs.lookback}"
|
|
213
|
+
)
|
|
214
|
+
return 0
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
# ---------------------------------------------------------------------------
|
|
218
|
+
# Argument parser
|
|
219
|
+
# ---------------------------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
223
|
+
parser = argparse.ArgumentParser(
|
|
224
|
+
prog="macroforecast",
|
|
225
|
+
description="macroforecast — Decomposing ML Forecast Gains",
|
|
226
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
227
|
+
)
|
|
228
|
+
parser.add_argument(
|
|
229
|
+
"-v", "--verbose", action="store_true", help="Enable DEBUG logging."
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
subparsers = parser.add_subparsers(dest="command", metavar="COMMAND")
|
|
233
|
+
subparsers.required = True
|
|
234
|
+
|
|
235
|
+
# run
|
|
236
|
+
run_parser = subparsers.add_parser(
|
|
237
|
+
"run", help="Run a forecast experiment from a YAML config."
|
|
238
|
+
)
|
|
239
|
+
run_parser.add_argument(
|
|
240
|
+
"config", metavar="CONFIG.yaml", help="Path to the YAML experiment config file."
|
|
241
|
+
)
|
|
242
|
+
run_parser.add_argument(
|
|
243
|
+
"--summary", action="store_true", help="Print MSFE summary table after the run."
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
# init
|
|
247
|
+
init_parser = subparsers.add_parser(
|
|
248
|
+
"init", help="Write a default YAML config template."
|
|
249
|
+
)
|
|
250
|
+
init_parser.add_argument(
|
|
251
|
+
"--output",
|
|
252
|
+
"-o",
|
|
253
|
+
default="experiment.yaml",
|
|
254
|
+
help="Output file path (default: experiment.yaml).",
|
|
255
|
+
)
|
|
256
|
+
init_parser.add_argument(
|
|
257
|
+
"--force", action="store_true", help="Overwrite existing file."
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
# info
|
|
261
|
+
info_parser = subparsers.add_parser("info", help="Print a resolved config summary.")
|
|
262
|
+
info_parser.add_argument(
|
|
263
|
+
"config", metavar="CONFIG.yaml", help="Path to the YAML config file."
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
return parser
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
# ---------------------------------------------------------------------------
|
|
270
|
+
# Entry point
|
|
271
|
+
# ---------------------------------------------------------------------------
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def main(argv: list[str] | None = None) -> int:
|
|
275
|
+
parser = _build_parser()
|
|
276
|
+
args = parser.parse_args(argv)
|
|
277
|
+
_setup_logging(verbose=args.verbose)
|
|
278
|
+
|
|
279
|
+
dispatch = {
|
|
280
|
+
"run": _cmd_run,
|
|
281
|
+
"init": _cmd_init,
|
|
282
|
+
"info": _cmd_info,
|
|
283
|
+
}
|
|
284
|
+
handler = dispatch.get(args.command)
|
|
285
|
+
if handler is None:
|
|
286
|
+
parser.print_help()
|
|
287
|
+
return 1
|
|
288
|
+
|
|
289
|
+
return handler(args)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
if __name__ == "__main__":
|
|
293
|
+
sys.exit(main())
|