dfm-python 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.
- dfm_python/__init__.py +28 -0
- dfm_python/config.py +435 -0
- dfm_python/data_loader.py +500 -0
- dfm_python/dfm.py +2356 -0
- dfm_python/kalman.py +403 -0
- dfm_python/news.py +557 -0
- dfm_python-0.1.0.dist-info/METADATA +271 -0
- dfm_python-0.1.0.dist-info/RECORD +9 -0
- dfm_python-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dfm-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Dynamic Factor Model (DFM) estimation and nowcasting in Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/yourusername/dfm-python
|
|
6
|
+
Project-URL: Documentation, https://github.com/yourusername/dfm-python#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/yourusername/dfm-python
|
|
8
|
+
Project-URL: Issues, https://github.com/yourusername/dfm-python/issues
|
|
9
|
+
Author: DFM Python Contributors
|
|
10
|
+
License: MIT
|
|
11
|
+
Keywords: dfm,dynamic-factor-model,econometrics,forecasting,nowcasting,time-series
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Requires-Dist: numpy>=1.24.0
|
|
20
|
+
Requires-Dist: pandas>=2.0.0
|
|
21
|
+
Requires-Dist: scipy>=1.10.0
|
|
22
|
+
Provides-Extra: all
|
|
23
|
+
Requires-Dist: httpx>=0.24.0; extra == 'all'
|
|
24
|
+
Requires-Dist: hydra-core>=1.3.0; extra == 'all'
|
|
25
|
+
Requires-Dist: omegaconf>=2.3.0; extra == 'all'
|
|
26
|
+
Requires-Dist: psycopg2-binary>=2.9.0; extra == 'all'
|
|
27
|
+
Requires-Dist: pydantic-settings>=2.11.0; extra == 'all'
|
|
28
|
+
Requires-Dist: pytest>=7.0.0; extra == 'all'
|
|
29
|
+
Requires-Dist: supabase>=2.0.0; extra == 'all'
|
|
30
|
+
Provides-Extra: database
|
|
31
|
+
Requires-Dist: httpx>=0.24.0; extra == 'database'
|
|
32
|
+
Requires-Dist: psycopg2-binary>=2.9.0; extra == 'database'
|
|
33
|
+
Requires-Dist: pydantic-settings>=2.11.0; extra == 'database'
|
|
34
|
+
Requires-Dist: supabase>=2.0.0; extra == 'database'
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
37
|
+
Provides-Extra: hydra
|
|
38
|
+
Requires-Dist: hydra-core>=1.3.0; extra == 'hydra'
|
|
39
|
+
Requires-Dist: omegaconf>=2.3.0; extra == 'hydra'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# dfm-python
|
|
43
|
+
|
|
44
|
+
A generic Python implementation of Dynamic Factor Models (DFM) for nowcasting and forecasting.
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
- **Generic DFM Estimation**: EM algorithm for estimating dynamic factor models
|
|
49
|
+
- **Mixed-Frequency Data**: Handle data with different frequencies (daily, monthly, quarterly)
|
|
50
|
+
- **Missing Data Handling**: Robust handling of missing observations
|
|
51
|
+
- **News Decomposition**: Compare forecasts between vintages and attribute changes
|
|
52
|
+
- **Flexible Configuration**: Support for YAML and CSV configuration files
|
|
53
|
+
- **Optional Hydra Integration**: Use Hydra for experiment management (optional dependency)
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install dfm-python
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Optional Dependencies
|
|
62
|
+
|
|
63
|
+
For Hydra configuration support:
|
|
64
|
+
```bash
|
|
65
|
+
pip install dfm-python[hydra]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
For database integration (application-specific):
|
|
69
|
+
```bash
|
|
70
|
+
pip install dfm-python[database]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Install all optional dependencies:
|
|
74
|
+
```bash
|
|
75
|
+
pip install dfm-python[all]
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Quick Start
|
|
79
|
+
|
|
80
|
+
### Basic Usage
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from dfm_python import load_config, load_data, dfm
|
|
84
|
+
import pandas as pd
|
|
85
|
+
|
|
86
|
+
# Load configuration from YAML or CSV
|
|
87
|
+
config = load_config('config.yaml')
|
|
88
|
+
|
|
89
|
+
# Load data from CSV file
|
|
90
|
+
X, Time, Z = load_data('data.csv', config, sample_start=pd.Timestamp('2000-01-01'))
|
|
91
|
+
|
|
92
|
+
# Estimate DFM
|
|
93
|
+
Res = dfm(X, config, threshold=1e-4)
|
|
94
|
+
|
|
95
|
+
# Access results
|
|
96
|
+
factors = Res.Z # Extracted factors
|
|
97
|
+
loadings = Res.C # Factor loadings
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Configuration
|
|
101
|
+
|
|
102
|
+
The DFM module uses a configuration object (`DFMConfig`) that defines:
|
|
103
|
+
|
|
104
|
+
- **Series**: Each time series with frequency, transformation, and block membership
|
|
105
|
+
- **Blocks**: Factor blocks with number of factors per block
|
|
106
|
+
- **Estimation Parameters**: EM algorithm settings (threshold, max_iter, etc.)
|
|
107
|
+
|
|
108
|
+
#### YAML Configuration Example
|
|
109
|
+
|
|
110
|
+
```yaml
|
|
111
|
+
# config.yaml
|
|
112
|
+
model:
|
|
113
|
+
series:
|
|
114
|
+
series_1:
|
|
115
|
+
frequency: "m"
|
|
116
|
+
transformation: "pch"
|
|
117
|
+
blocks: [Global]
|
|
118
|
+
series_2:
|
|
119
|
+
frequency: "q"
|
|
120
|
+
transformation: "pca"
|
|
121
|
+
blocks: [Global, Investment]
|
|
122
|
+
|
|
123
|
+
blocks:
|
|
124
|
+
Global:
|
|
125
|
+
factors: 1
|
|
126
|
+
Investment:
|
|
127
|
+
factors: 1
|
|
128
|
+
|
|
129
|
+
dfm:
|
|
130
|
+
ar_lag: 1
|
|
131
|
+
threshold: 1e-5
|
|
132
|
+
max_iter: 5000
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### CSV Configuration Example
|
|
136
|
+
|
|
137
|
+
```csv
|
|
138
|
+
SeriesID,Frequency,Transformation,Block_Global,Block_Investment
|
|
139
|
+
series_1,m,pch,1,0
|
|
140
|
+
series_2,q,pca,1,1
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Data Format
|
|
144
|
+
|
|
145
|
+
CSV data files should have:
|
|
146
|
+
- First column: Date (YYYY-MM-DD format)
|
|
147
|
+
- Subsequent columns: One per series (matching SeriesID from config)
|
|
148
|
+
- Column names: Match SeriesID from configuration
|
|
149
|
+
|
|
150
|
+
Example:
|
|
151
|
+
```csv
|
|
152
|
+
Date,series_1,series_2
|
|
153
|
+
2000-01-01,100.0,50.0
|
|
154
|
+
2000-02-01,101.0,51.0
|
|
155
|
+
...
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Core Components
|
|
159
|
+
|
|
160
|
+
### DFMConfig
|
|
161
|
+
|
|
162
|
+
Configuration dataclass that defines the model structure:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from dfm_python import DFMConfig, SeriesConfig
|
|
166
|
+
|
|
167
|
+
config = DFMConfig(
|
|
168
|
+
series=[
|
|
169
|
+
SeriesConfig(
|
|
170
|
+
series_id="gdp",
|
|
171
|
+
frequency="q",
|
|
172
|
+
transformation="pca",
|
|
173
|
+
blocks=[1, 0] # Binary: loads on block 0 (Global)
|
|
174
|
+
)
|
|
175
|
+
],
|
|
176
|
+
block_names=["Global", "Investment"],
|
|
177
|
+
factors_per_block=[1, 1],
|
|
178
|
+
ar_lag=1,
|
|
179
|
+
threshold=1e-5,
|
|
180
|
+
max_iter=5000
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### DFM Estimation
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from dfm_python import dfm
|
|
188
|
+
|
|
189
|
+
# Estimate DFM model
|
|
190
|
+
result = dfm(X, config, threshold=1e-4, max_iter=1000)
|
|
191
|
+
|
|
192
|
+
# Access results
|
|
193
|
+
factors = result.Z # Factor estimates (T x r)
|
|
194
|
+
loadings = result.C # Factor loadings (N x r)
|
|
195
|
+
transition = result.A # Transition matrix
|
|
196
|
+
covariance = result.Q # Factor covariance
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### News Decomposition
|
|
200
|
+
|
|
201
|
+
Compare forecasts between two vintages:
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
from dfm_python import update_nowcast
|
|
205
|
+
|
|
206
|
+
# Compare old vs new vintage
|
|
207
|
+
news_df, forecast_df = update_nowcast(
|
|
208
|
+
X_old, X_new, Time, config, result,
|
|
209
|
+
series="gdp",
|
|
210
|
+
vintage_old="2024-01-01",
|
|
211
|
+
vintage_new="2024-02-01"
|
|
212
|
+
)
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## API Reference
|
|
216
|
+
|
|
217
|
+
### Main Functions
|
|
218
|
+
|
|
219
|
+
- `load_config(file)`: Load configuration from YAML or CSV
|
|
220
|
+
- `load_data(file, config)`: Load and transform data from CSV
|
|
221
|
+
- `dfm(X, config)`: Estimate DFM model using EM algorithm
|
|
222
|
+
- `update_nowcast(...)`: Perform news decomposition between vintages
|
|
223
|
+
|
|
224
|
+
### Configuration
|
|
225
|
+
|
|
226
|
+
- `DFMConfig`: Main configuration class
|
|
227
|
+
- `SeriesConfig`: Individual series configuration
|
|
228
|
+
|
|
229
|
+
### Results
|
|
230
|
+
|
|
231
|
+
- `DFMResult`: Result object with factors, loadings, and model parameters
|
|
232
|
+
|
|
233
|
+
## Architecture
|
|
234
|
+
|
|
235
|
+
The DFM module is designed to be **generic and application-agnostic**:
|
|
236
|
+
|
|
237
|
+
- **Core Module** (`dfm_python`): Generic DFM estimation logic
|
|
238
|
+
- **Adapters** (application-specific): Database integration, API clients, etc.
|
|
239
|
+
|
|
240
|
+
This separation allows the core module to be used in any context while keeping application-specific code separate.
|
|
241
|
+
|
|
242
|
+
## Requirements
|
|
243
|
+
|
|
244
|
+
- Python >= 3.12
|
|
245
|
+
- numpy >= 1.24.0
|
|
246
|
+
- pandas >= 2.0.0
|
|
247
|
+
- scipy >= 1.10.0
|
|
248
|
+
|
|
249
|
+
## License
|
|
250
|
+
|
|
251
|
+
MIT License
|
|
252
|
+
|
|
253
|
+
## Contributing
|
|
254
|
+
|
|
255
|
+
Contributions are welcome! Please ensure that:
|
|
256
|
+
- Core module remains generic (no application-specific code)
|
|
257
|
+
- All optional dependencies are properly handled
|
|
258
|
+
- Tests pass for the core functionality
|
|
259
|
+
|
|
260
|
+
## Citation
|
|
261
|
+
|
|
262
|
+
If you use this package in your research, please cite:
|
|
263
|
+
|
|
264
|
+
```bibtex
|
|
265
|
+
@software{dfm-python,
|
|
266
|
+
title = {dfm-python: Dynamic Factor Models for Nowcasting},
|
|
267
|
+
author = {DFM Python Contributors},
|
|
268
|
+
year = {2024},
|
|
269
|
+
url = {https://github.com/yourusername/dfm-python}
|
|
270
|
+
}
|
|
271
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
dfm_python/__init__.py,sha256=_74eT10hQse0xb22IsiTNKLe7daY_0YbKHjMkdhO3U4,961
|
|
2
|
+
dfm_python/config.py,sha256=dBhBO-ckKlzm0DPImOhdP9eDo1m0Nfh2vnanPUGMVfs,19005
|
|
3
|
+
dfm_python/data_loader.py,sha256=M0NytBWjtwWYIreD6Gsztt99XIn9hYQe2hVI8L5YZT8,18290
|
|
4
|
+
dfm_python/dfm.py,sha256=1zJCz5DberMnsdVq43rhoSYK5nI4ERADNEJEOlpjnUY,106078
|
|
5
|
+
dfm_python/kalman.py,sha256=RS9obW1eT7Q6L5NeXk9vB4ZYZbg96d0yYm4gk6NaO1M,14712
|
|
6
|
+
dfm_python/news.py,sha256=x55URP3VrrOxcmNq0eHl9X63ReqMMPOQ3Bj0oiN3-ew,21893
|
|
7
|
+
dfm_python-0.1.0.dist-info/METADATA,sha256=_ACla-WRfk8_DnPZA8YrxIH7XI_Xmyvr_gDH2KwH3Lw,6939
|
|
8
|
+
dfm_python-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
9
|
+
dfm_python-0.1.0.dist-info/RECORD,,
|