dfm-python 0.2.9__py3-none-any.whl → 0.3.1__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 (36) hide show
  1. dfm_python/__init__.py +97 -22
  2. dfm_python/api/__init__.py +44 -0
  3. dfm_python/api/model_api.py +1148 -0
  4. dfm_python/api/session_api.py +402 -0
  5. dfm_python/config/__init__.py +35 -0
  6. dfm_python/{config_sources.py → config/io.py} +155 -122
  7. dfm_python/{config.py → config/schema.py} +38 -106
  8. dfm_python/core/__init__.py +125 -1
  9. dfm_python/core/diagnostics.py +61 -53
  10. dfm_python/core/em.py +77 -56
  11. dfm_python/{dfm.py → core/estimation.py} +32 -400
  12. dfm_python/core/helpers.py +315 -51
  13. dfm_python/core/results.py +293 -0
  14. dfm_python/core/{numeric.py → state_space.py} +630 -269
  15. dfm_python/{utils.py → core/structure.py} +74 -17
  16. dfm_python/core/time.py +775 -0
  17. dfm_python/dataloader/__init__.py +30 -0
  18. dfm_python/{data.py → dataloader/loader.py} +257 -53
  19. dfm_python/dataloader/preprocess.py +11 -0
  20. dfm_python/models/__init__.py +19 -0
  21. dfm_python/models/base.py +98 -0
  22. dfm_python/models/ddfm.py +978 -0
  23. dfm_python/models/dfm.py +213 -0
  24. dfm_python/nowcasting/__init__.py +13 -0
  25. dfm_python/nowcasting/nowcast.py +1497 -0
  26. dfm_python/utils/__init__.py +38 -0
  27. dfm_python/utils/convert.py +342 -0
  28. dfm_python/utils/loss.py +184 -0
  29. {dfm_python-0.2.9.dist-info → dfm_python-0.3.1.dist-info}/METADATA +591 -54
  30. dfm_python-0.3.1.dist-info/RECORD +32 -0
  31. {dfm_python-0.2.9.dist-info → dfm_python-0.3.1.dist-info}/WHEEL +1 -1
  32. dfm_python/api.py +0 -439
  33. dfm_python/kalman.py +0 -467
  34. dfm_python/news.py +0 -774
  35. dfm_python-0.2.9.dist-info/RECORD +0 -18
  36. {dfm_python-0.2.9.dist-info → dfm_python-0.3.1.dist-info}/licenses/LICENSE +0 -0
dfm_python/__init__.py CHANGED
@@ -7,6 +7,7 @@ This package implements a comprehensive Dynamic Factor Model framework with supp
7
7
  - Expectation-Maximization (EM) algorithm for parameter estimation
8
8
  - Kalman filtering and smoothing for factor extraction
9
9
  - News decomposition for nowcasting
10
+ - Deep Dynamic Factor Models (DDFM) with nonlinear encoders (optional, requires PyTorch)
10
11
 
11
12
  The package implements a clock-based approach to mixed-frequency DFMs, where all latent
12
13
  factors (global and block-level) are synchronized to a common "clock" frequency, typically
@@ -25,16 +26,23 @@ Key Features:
25
26
 
26
27
  Example (High-level API - Recommended):
27
28
  >>> import dfm_python as dfm
28
- >>> dfm.load_config('config/default.yaml')
29
- >>> dfm.load_data('data/sample_data.csv')
30
- >>> dfm.train(max_iter=1)
31
- >>> Xf, Zf = dfm.predict(horizon=6)
32
- >>> dfm.plot(kind='factor', factor_index=0, forecast_horizon=6, save_path='outputs/factor_forecast.png')
33
- >>> factors = dfm.get_result().Z # Access factors via result
29
+ >>> # Linear DFM
30
+ >>> model = dfm.DFM()
31
+ >>> model.load_config('config/default.yaml')
32
+ >>> model.load_data('data/sample_data.csv')
33
+ >>> model.train(max_iter=100)
34
+ >>> Xf, Zf = model.predict(horizon=6)
35
+ >>>
36
+ >>> # Or use DDFM (separate class)
37
+ >>> ddfm_model = dfm.DDFM(encoder_layers=[64, 32], num_factors=2)
38
+ >>> ddfm_model.load_config('config/default.yaml')
39
+ >>> ddfm_model.load_data('data/sample_data.csv')
40
+ >>> ddfm_model.train(epochs=100)
41
+ >>> Xf, Zf = ddfm_model.predict(horizon=6)
34
42
 
35
43
  Example (Low-level API - For advanced usage):
36
44
  >>> from dfm_python import DFM, DFMConfig, SeriesConfig
37
- >>> from dfm_python.data import load_data # Preferred import
45
+ >>> from dfm_python.dataloader import load_data # Preferred import
38
46
  >>> # Option 1: Load from YAML
39
47
  >>> config = load_config('config.yaml')
40
48
  >>> # Option 2: Create directly
@@ -50,27 +58,82 @@ Example (Low-level API - For advanced usage):
50
58
  For detailed documentation, see the README.md file and the tutorial notebooks/scripts.
51
59
  """
52
60
 
53
- __version__ = "0.2.9"
54
-
61
+ __version__ = "0.3.1"
62
+
63
+ # ============================================================================
64
+ # PUBLIC API DEFINITION
65
+ # ============================================================================
66
+ # This __init__.py is the single source of truth for the public API.
67
+ # All symbols exported here are considered stable public API.
68
+ # Internal reorganization should not break these imports.
69
+ #
70
+ # Public API categories:
71
+ # 1. Configuration: DFMConfig, SeriesConfig, BlockConfig, Params, config sources
72
+ # 2. High-level API: DFM, DDFM, module-level convenience functions
73
+ # 3. Core utilities: DFMCore, run_kf, TimeIndex, diagnostics
74
+ # 4. Models: BaseFactorModel, DFMLinear, DDFM (low-level)
75
+ # 5. Nowcasting: Nowcast, result classes, para_const
76
+ # 6. Data & Results: DFMResult, transform_data
77
+ # ============================================================================
78
+
79
+ # Configuration (from config/ subpackage)
55
80
  from .config import (
56
81
  DFMConfig, SeriesConfig, BlockConfig, Params, DEFAULT_GLOBAL_BLOCK_NAME,
57
- ConfigSource, YamlSource, DictSource, SpecCSVSource, HydraSource,
82
+ ConfigSource, YamlSource, DictSource, HydraSource,
58
83
  MergedConfigSource, make_config_source,
59
84
  )
60
- from .data import transform_data
61
- from .dfm import DFMResult
85
+
86
+ # Data utilities
87
+ from .dataloader import transform_data
88
+
89
+ # Results
90
+ from .core import DFMResult
91
+
92
+ # Core utilities (from core/ subpackage)
62
93
  from .core import calculate_rmse, diagnose_series, print_series_diagnosis
63
- from .dfm import DFMCore # Core DFM class from dfm.py
64
- from .kalman import run_kf, skf, fis, miss_data
65
- from .news import update_nowcast, news_dfm, para_const
94
+ from .core.state_space import run_kf, skf, fis, miss_data
95
+ # DFMCore is now an alias for DFMLinear (backward compatibility)
96
+ from .models.dfm import DFMLinear
97
+ DFMCore = DFMLinear
98
+
99
+ # Nowcasting (already in nowcasting/ subpackage)
100
+ from .nowcasting import (
101
+ Nowcast,
102
+ para_const,
103
+ NowcastResult,
104
+ NewsDecompResult,
105
+ BacktestResult,
106
+ )
66
107
 
67
- # Import high-level API (extends core DFM with convenience methods)
68
- # Import module-level convenience functions directly from api.py to avoid duplication
108
+ # High-level API (from api/ subpackage)
69
109
  from .api import (
70
110
  DFM, _dfm_instance, from_yaml, from_spec, from_spec_df, from_dict,
71
- load_config, load_data, train, predict, plot, reset
111
+ load_config, load_data, load_pickle, train, predict, plot, reset, create_model
72
112
  )
73
113
 
114
+ # Model implementations
115
+ from .models.base import BaseFactorModel
116
+ from .models.dfm import DFMLinear
117
+
118
+ # DDFM high-level API and low-level model (both optional, requires PyTorch)
119
+ try:
120
+ from .api import DDFM as DDFMAPI, _ddfm_instance
121
+ from .api import load_config_ddfm, load_data_ddfm, train_ddfm, predict_ddfm, plot_ddfm, reset_ddfm
122
+ from .models.ddfm import DDFM as DDFMModel
123
+ _has_ddfm = True
124
+ # Export high-level API as DDFM
125
+ DDFM = DDFMAPI
126
+ except ImportError:
127
+ _has_ddfm = False
128
+ DDFM = None # type: ignore
129
+ DDFMModel = None # type: ignore
130
+ load_config_ddfm = None # type: ignore
131
+ load_data_ddfm = None # type: ignore
132
+ train_ddfm = None # type: ignore
133
+ predict_ddfm = None # type: ignore
134
+ plot_ddfm = None # type: ignore
135
+ reset_ddfm = None # type: ignore
136
+
74
137
  # Expose properties as module-level attributes
75
138
  # Use property-like access via functions or direct attribute access
76
139
  # Since 'config' conflicts with the config module, we'll use a different approach
@@ -97,15 +160,19 @@ def get_original_data():
97
160
 
98
161
  __all__ = [
99
162
  # Core classes
100
- 'DFMConfig', 'SeriesConfig', 'BlockConfig', 'Params', 'DFM', 'DFMCore',
163
+ 'DFMConfig', 'SeriesConfig', 'BlockConfig', 'Params', 'DFM', 'DFMCore', 'Nowcast',
164
+ # Model base and implementations
165
+ 'BaseFactorModel', 'DFMLinear',
166
+ # Nowcast result classes
167
+ 'NowcastResult', 'NewsDecompResult', 'BacktestResult',
101
168
  # Constants
102
169
  'DEFAULT_GLOBAL_BLOCK_NAME',
103
170
  # Config sources
104
- 'ConfigSource', 'YamlSource', 'DictSource', 'SpecCSVSource', 'HydraSource',
171
+ 'ConfigSource', 'YamlSource', 'DictSource', 'HydraSource',
105
172
  'MergedConfigSource', 'make_config_source',
106
173
  # High-level API (module-level - recommended)
107
174
  'load_config',
108
- 'load_data', 'train', 'predict', 'plot', 'reset',
175
+ 'load_data', 'load_pickle', 'train', 'predict', 'plot', 'reset', 'create_model',
109
176
  'get_config', 'get_data', 'get_time', 'get_result', 'get_original_data',
110
177
  # Convenience constructors (cleaner API)
111
178
  'from_yaml', 'from_spec', 'from_spec_df', 'from_dict',
@@ -113,6 +180,14 @@ __all__ = [
113
180
  'transform_data',
114
181
  'DFMResult', 'calculate_rmse', 'diagnose_series', 'print_series_diagnosis',
115
182
  'run_kf', 'skf', 'fis', 'miss_data',
116
- 'update_nowcast', 'news_dfm', 'para_const',
183
+ 'para_const', # Internal utility, kept for backward compatibility
117
184
  ]
118
185
 
186
+ # Add DDFM high-level API and convenience functions if available
187
+ if _has_ddfm:
188
+ __all__.extend([
189
+ 'DDFM', # High-level API class
190
+ 'load_config_ddfm', 'load_data_ddfm', 'train_ddfm',
191
+ 'predict_ddfm', 'plot_ddfm', 'reset_ddfm'
192
+ ])
193
+
@@ -0,0 +1,44 @@
1
+ """High-level API subpackage for Dynamic Factor Models.
2
+
3
+ This subpackage provides:
4
+ - Model API classes (DFMBase, DFM, DDFM) in model_api.py
5
+ - Session API (singleton instances and module-level functions) in session_api.py
6
+ """
7
+
8
+ from .model_api import DFMBase, DFM, DDFM
9
+ from .session_api import (
10
+ _dfm_instance,
11
+ _ddfm_instance,
12
+ load_config,
13
+ load_data,
14
+ train,
15
+ predict,
16
+ plot,
17
+ load_pickle,
18
+ reset,
19
+ load_config_ddfm,
20
+ load_data_ddfm,
21
+ train_ddfm,
22
+ predict_ddfm,
23
+ plot_ddfm,
24
+ reset_ddfm,
25
+ create_model,
26
+ from_yaml,
27
+ from_spec,
28
+ from_spec_df,
29
+ from_dict,
30
+ )
31
+
32
+ __all__ = [
33
+ # High-level API classes
34
+ 'DFMBase', 'DFM', 'DDFM',
35
+ # Singleton instances (internal use)
36
+ '_dfm_instance', '_ddfm_instance',
37
+ # Module-level convenience functions (DFM)
38
+ 'load_config', 'load_data', 'load_pickle', 'train', 'predict', 'plot', 'reset',
39
+ # Module-level convenience functions (DDFM)
40
+ 'load_config_ddfm', 'load_data_ddfm', 'train_ddfm', 'predict_ddfm', 'plot_ddfm', 'reset_ddfm',
41
+ # Factory and convenience constructors
42
+ 'create_model', 'from_yaml', 'from_spec', 'from_spec_df', 'from_dict',
43
+ ]
44
+