dfm-python 0.5.51__py3-none-any.whl → 0.5.53__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 -24
- dfm_python/config/__init__.py +144 -28
- dfm_python/config/adapter.py +284 -448
- dfm_python/config/constants.py +362 -0
- dfm_python/config/schema/__init__.py +17 -0
- dfm_python/config/schema/block.py +64 -0
- dfm_python/config/schema/model.py +554 -0
- dfm_python/config/schema/results.py +297 -0
- dfm_python/config/types.py +299 -0
- dfm_python/datamodule/__init__.py +31 -0
- dfm_python/datamodule/base.py +612 -0
- dfm_python/{lightning → datamodule}/ddfm_dm.py +41 -167
- dfm_python/datamodule/dfm_dm.py +286 -0
- dfm_python/datamodule/kdfm_dm.py +179 -0
- dfm_python/dataset/__init__.py +60 -0
- dfm_python/{data → dataset}/dataloader.py +49 -47
- dfm_python/{data → dataset}/dataset.py +70 -66
- dfm_python/dataset/process.py +436 -0
- dfm_python/encoder/__init__.py +1 -3
- dfm_python/encoder/pca.py +94 -171
- dfm_python/encoder/{autoencoder.py → simple_encoder.py} +2 -1
- dfm_python/encoder/variational_encoder.py +0 -0
- dfm_python/functional/__init__.py +13 -0
- dfm_python/functional/dfm_block.py +418 -0
- dfm_python/functional/em.py +825 -0
- dfm_python/functional/irf.py +297 -0
- dfm_python/functional/krylov.py +124 -0
- dfm_python/logger/inference_logger.py +4 -5
- dfm_python/models/__init__.py +9 -3
- dfm_python/models/base.py +599 -602
- dfm_python/models/ddfm.py +474 -379
- dfm_python/models/dfm.py +934 -782
- dfm_python/models/kdfm.py +1735 -0
- dfm_python/numeric/builder.py +235 -0
- dfm_python/numeric/estimator.py +1075 -0
- dfm_python/numeric/stability.py +1183 -0
- dfm_python/numeric/tent.py +291 -0
- dfm_python/numeric/validator.py +652 -0
- dfm_python/ssm/__init__.py +11 -39
- dfm_python/ssm/arma.py +0 -0
- dfm_python/ssm/companion.py +982 -0
- dfm_python/ssm/kalman.py +363 -829
- dfm_python/ssm/structural.py +298 -0
- dfm_python/trainer/__init__.py +5 -77
- dfm_python/trainer/ddfm.py +472 -4
- dfm_python/trainer/dfm.py +6 -6
- dfm_python/trainer/kdfm.py +137 -0
- dfm_python/utils/__init__.py +251 -97
- dfm_python/utils/common.py +212 -0
- dfm_python/utils/errors.py +73 -0
- dfm_python/utils/helper.py +180 -0
- dfm_python/utils/metric.py +449 -0
- dfm_python/utils/misc.py +306 -0
- dfm_python/utils/tensor_utils.py +192 -0
- dfm_python/utils/validation.py +92 -0
- dfm_python-0.5.53.dist-info/METADATA +380 -0
- dfm_python-0.5.53.dist-info/RECORD +66 -0
- dfm_python/config/results.py +0 -405
- dfm_python/config/schema.py +0 -931
- dfm_python/config/utils.py +0 -547
- dfm_python/data/__init__.py +0 -30
- dfm_python/data/utils.py +0 -334
- dfm_python/lightning/__init__.py +0 -53
- dfm_python/lightning/dfm_dm.py +0 -379
- dfm_python/lightning/utils.py +0 -516
- dfm_python/models/utils.py +0 -470
- dfm_python/ssm/em.py +0 -1256
- dfm_python/ssm/utils.py +0 -430
- dfm_python/trainer/denoising.py +0 -490
- dfm_python/utils/data.py +0 -604
- dfm_python/utils/diagnostics.py +0 -577
- dfm_python/utils/helpers.py +0 -454
- dfm_python/utils/statespace.py +0 -1270
- dfm_python/utils/time.py +0 -1047
- dfm_python-0.5.51.dist-info/METADATA +0 -510
- dfm_python-0.5.51.dist-info/RECORD +0 -48
- {dfm_python-0.5.51.dist-info → dfm_python-0.5.53.dist-info}/WHEEL +0 -0
- {dfm_python-0.5.51.dist-info → dfm_python-0.5.53.dist-info}/licenses/LICENSE +0 -0
dfm_python/__init__.py
CHANGED
|
@@ -28,7 +28,7 @@ Example (Standard Lightning Pattern):
|
|
|
28
28
|
>>> import pandas as pd
|
|
29
29
|
>>>
|
|
30
30
|
>>> # Step 1: Load and preprocess data
|
|
31
|
-
>>> df = pd.read_csv('data/
|
|
31
|
+
>>> df = pd.read_csv('data/your_data.csv')
|
|
32
32
|
>>> df_processed = df[[col for col in df.columns if col != 'date']]
|
|
33
33
|
>>>
|
|
34
34
|
>>> # Step 2: Create DataModule
|
|
@@ -59,13 +59,13 @@ Example (Standard Lightning Pattern):
|
|
|
59
59
|
>>> trainer_ddfm.fit(ddfm_model, dm_ddfm)
|
|
60
60
|
>>> Xf, Zf = ddfm_model.predict(horizon=6)
|
|
61
61
|
|
|
62
|
-
Note: DFMConfig
|
|
62
|
+
Note: DFMConfig uses frequency dict to specify series (column names -> frequencies).
|
|
63
63
|
Users should use Hydra YAML configuration files instead.
|
|
64
64
|
|
|
65
65
|
For detailed documentation, see the README.md file and the tutorial notebooks/scripts.
|
|
66
66
|
"""
|
|
67
67
|
|
|
68
|
-
__version__ = "0.5.
|
|
68
|
+
__version__ = "0.5.53"
|
|
69
69
|
|
|
70
70
|
# ============================================================================
|
|
71
71
|
# PUBLIC API DEFINITION
|
|
@@ -75,7 +75,7 @@ __version__ = "0.5.5"
|
|
|
75
75
|
# Internal reorganization should not break these imports.
|
|
76
76
|
#
|
|
77
77
|
# Public API categories:
|
|
78
|
-
# 1. Configuration: DFMConfig,
|
|
78
|
+
# 1. Configuration: DFMConfig, config sources
|
|
79
79
|
# 2. High-level API: DFM, DDFM, module-level convenience functions
|
|
80
80
|
# 3. Core utilities: TimeIndex, diagnostics
|
|
81
81
|
# 4. Models: BaseFactorModel, DDFM (low-level)
|
|
@@ -84,29 +84,27 @@ __version__ = "0.5.5"
|
|
|
84
84
|
|
|
85
85
|
# Configuration (from config/ subpackage)
|
|
86
86
|
from .config import (
|
|
87
|
-
DEFAULT_BLOCK_NAME,
|
|
88
|
-
ConfigSource, YamlSource,
|
|
87
|
+
# DEFAULT_BLOCK_NAME, # Removed to avoid circular import - import directly from functional.dfm_block
|
|
88
|
+
ConfigSource, YamlSource,
|
|
89
89
|
make_config_source,
|
|
90
90
|
)
|
|
91
|
-
# Internal imports (
|
|
92
|
-
from .config import DFMConfig
|
|
91
|
+
# Internal imports (internal use only)
|
|
92
|
+
from .config import DFMConfig
|
|
93
93
|
|
|
94
94
|
# Results
|
|
95
|
-
from .config
|
|
95
|
+
from .config import DFMResult, DDFMResult, KDFMResult, BaseResult
|
|
96
96
|
|
|
97
97
|
# Utilities (from utils/ subpackage)
|
|
98
|
-
from .utils.
|
|
99
|
-
from .utils.time import calculate_rmse
|
|
98
|
+
from .utils.metric import calculate_rmse
|
|
100
99
|
|
|
101
|
-
#
|
|
100
|
+
# DataModule classes (includes both Lightning and custom implementations)
|
|
102
101
|
# Users can import these directly from dfm_python
|
|
103
|
-
from .
|
|
102
|
+
from .datamodule import (
|
|
104
103
|
DFMDataModule,
|
|
105
104
|
DDFMDataModule, # DDFM-specific DataModule
|
|
106
|
-
|
|
105
|
+
KDFMDataModule, # KDFM-specific DataModule
|
|
107
106
|
DDFMDataset, # DDFM Dataset class (usually not needed directly)
|
|
108
|
-
|
|
109
|
-
EMAlgorithm, # Module class
|
|
107
|
+
KDFMDataset, # KDFM Dataset class (usually not needed directly)
|
|
110
108
|
)
|
|
111
109
|
|
|
112
110
|
# Model implementations
|
|
@@ -116,18 +114,19 @@ from .models.dfm import DFM
|
|
|
116
114
|
# DDFM high-level API (PyTorch is mandatory)
|
|
117
115
|
from .models.ddfm import DDFM
|
|
118
116
|
|
|
117
|
+
# KDFM high-level API
|
|
118
|
+
from .models.kdfm import KDFM
|
|
119
|
+
|
|
119
120
|
__all__ = [
|
|
120
121
|
# Core classes
|
|
121
122
|
'DFM',
|
|
122
123
|
# Model base and implementations
|
|
123
124
|
'BaseFactorModel',
|
|
124
|
-
# Constants
|
|
125
|
-
'DEFAULT_BLOCK_NAME',
|
|
126
125
|
# Config sources
|
|
127
|
-
'ConfigSource', 'YamlSource',
|
|
126
|
+
'ConfigSource', 'YamlSource',
|
|
128
127
|
'make_config_source',
|
|
129
128
|
# Low-level API (functional interface - advanced usage)
|
|
130
|
-
'BaseResult', 'DFMResult', 'DDFMResult', '
|
|
129
|
+
'BaseResult', 'DFMResult', 'DDFMResult', 'KDFMResult', 'calculate_rmse',
|
|
131
130
|
]
|
|
132
131
|
|
|
133
132
|
# DDFM high-level API (PyTorch is mandatory)
|
|
@@ -135,22 +134,27 @@ __all__.extend([
|
|
|
135
134
|
'DDFM', # High-level API class
|
|
136
135
|
])
|
|
137
136
|
|
|
137
|
+
# KDFM high-level API
|
|
138
|
+
__all__.extend([
|
|
139
|
+
'KDFM', # High-level API class
|
|
140
|
+
])
|
|
141
|
+
|
|
138
142
|
# Lightning modules (mandatory dependency)
|
|
139
143
|
__all__.extend([
|
|
140
144
|
'DFMDataModule',
|
|
141
145
|
'DDFMDataModule', # DDFM-specific DataModule
|
|
142
|
-
'
|
|
146
|
+
'KDFMDataModule', # KDFM-specific DataModule
|
|
143
147
|
'DDFMDataset', # DDFM Dataset class (usually not needed directly)
|
|
144
|
-
'
|
|
145
|
-
'EMAlgorithm', # Module class
|
|
148
|
+
'KDFMDataset', # KDFM Dataset class (usually not needed directly)
|
|
146
149
|
])
|
|
147
150
|
|
|
148
151
|
# Trainer classes (mandatory dependency)
|
|
149
|
-
from .trainer import DFMTrainer, DDFMTrainer
|
|
152
|
+
from .trainer import DFMTrainer, DDFMTrainer, KDFMTrainer
|
|
150
153
|
|
|
151
154
|
__all__.extend([
|
|
152
155
|
'DFMTrainer',
|
|
153
156
|
'DDFMTrainer',
|
|
157
|
+
'KDFMTrainer',
|
|
154
158
|
])
|
|
155
159
|
|
|
156
160
|
|
dfm_python/config/__init__.py
CHANGED
|
@@ -1,60 +1,176 @@
|
|
|
1
1
|
"""Configuration subpackage for DFM.
|
|
2
2
|
|
|
3
3
|
This subpackage provides:
|
|
4
|
-
- Schema (DFMConfig,
|
|
4
|
+
- Schema (DFMConfig, etc.) in schema.py
|
|
5
5
|
- IO (ConfigSource, YamlSource, etc.) in adapter.py
|
|
6
|
+
|
|
7
|
+
Note: Series are specified via frequency dict mapping column names to frequencies.
|
|
6
8
|
"""
|
|
7
9
|
|
|
8
10
|
from .schema import (
|
|
9
|
-
BaseModelConfig, DFMConfig, DDFMConfig,
|
|
10
|
-
|
|
11
|
+
BaseModelConfig, DFMConfig, DDFMConfig, KDFMConfig,
|
|
12
|
+
BaseResult, DFMResult, DDFMResult, KDFMResult, FitParams,
|
|
13
|
+
)
|
|
14
|
+
# DEFAULT_BLOCK_NAME is imported lazily where needed to avoid circular imports
|
|
15
|
+
from .constants import (
|
|
16
|
+
DEFAULT_CONVERGENCE_THRESHOLD,
|
|
17
|
+
DEFAULT_TOLERANCE,
|
|
18
|
+
DEFAULT_MAX_ITER,
|
|
19
|
+
DEFAULT_MAX_EPOCHS,
|
|
20
|
+
DEFAULT_LEARNING_RATE,
|
|
21
|
+
DEFAULT_BATCH_SIZE,
|
|
22
|
+
DEFAULT_CLOCK_FREQUENCY,
|
|
23
|
+
MIN_EIGENVALUE,
|
|
24
|
+
MIN_STD,
|
|
11
25
|
)
|
|
12
|
-
from .
|
|
13
|
-
from .utils import validate_frequency, validate_transformation
|
|
26
|
+
from .schema.model import validate_frequency
|
|
14
27
|
from .adapter import (
|
|
15
28
|
ConfigSource,
|
|
16
29
|
YamlSource,
|
|
17
30
|
DictSource,
|
|
18
|
-
HydraSource,
|
|
19
|
-
MergedConfigSource,
|
|
20
31
|
make_config_source,
|
|
32
|
+
detect_config_type,
|
|
21
33
|
)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
# Re-export types for convenience
|
|
35
|
+
from .types import (
|
|
36
|
+
ArrayLike,
|
|
37
|
+
FloatArray,
|
|
38
|
+
IntArray,
|
|
39
|
+
BoolArray,
|
|
40
|
+
OptionalArray,
|
|
41
|
+
OptionalTensor,
|
|
42
|
+
OptionalArrayLike,
|
|
43
|
+
FactorState,
|
|
44
|
+
ObservationState,
|
|
45
|
+
ForecastResult,
|
|
46
|
+
CoefficientMatrix,
|
|
47
|
+
CovarianceMatrix,
|
|
48
|
+
IRFArray,
|
|
49
|
+
CompanionMatrix,
|
|
50
|
+
SeriesID,
|
|
51
|
+
Frequency,
|
|
52
|
+
DatasetName,
|
|
53
|
+
ModelName,
|
|
54
|
+
Batch,
|
|
55
|
+
Loss,
|
|
56
|
+
Optimizer,
|
|
57
|
+
ResultDict,
|
|
58
|
+
ForecastDict,
|
|
59
|
+
MetricsDict,
|
|
60
|
+
IRFResultDict,
|
|
61
|
+
CheckpointDict,
|
|
62
|
+
ConfigDict,
|
|
63
|
+
Device,
|
|
64
|
+
Shape,
|
|
65
|
+
Shape2D,
|
|
66
|
+
Shape3D,
|
|
67
|
+
Shape4D,
|
|
68
|
+
ForecastHorizon,
|
|
69
|
+
IRFHorizon,
|
|
70
|
+
LagOrder,
|
|
71
|
+
NumFactors,
|
|
72
|
+
NumVars,
|
|
73
|
+
PathLike,
|
|
74
|
+
ValidationIssue,
|
|
75
|
+
ValidationResult,
|
|
76
|
+
Tensor,
|
|
77
|
+
is_numpy_array,
|
|
78
|
+
is_torch_tensor,
|
|
79
|
+
is_array_like,
|
|
80
|
+
get_array_shape,
|
|
81
|
+
to_numpy,
|
|
82
|
+
to_tensor,
|
|
33
83
|
)
|
|
84
|
+
# Import lazily to avoid circular dependencies
|
|
85
|
+
try:
|
|
86
|
+
from ..numeric.builder import compute_idio_lengths
|
|
87
|
+
except ImportError:
|
|
88
|
+
compute_idio_lengths = None
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
from ..numeric.tent import get_tent_weights, get_agg_structure, group_by_freq
|
|
92
|
+
except ImportError:
|
|
93
|
+
get_tent_weights = None
|
|
94
|
+
get_agg_structure = None
|
|
95
|
+
group_by_freq = None
|
|
96
|
+
from .constants import FREQUENCY_HIERARCHY, PERIODS_PER_YEAR
|
|
97
|
+
|
|
98
|
+
# Simple utility function
|
|
99
|
+
def get_periods_per_year(frequency: str) -> int:
|
|
100
|
+
"""Get number of periods per year for a given frequency."""
|
|
101
|
+
return PERIODS_PER_YEAR.get(frequency, PERIODS_PER_YEAR.get(DEFAULT_CLOCK_FREQUENCY, 12))
|
|
34
102
|
|
|
35
103
|
__all__ = [
|
|
36
|
-
#
|
|
37
|
-
'BaseModelConfig', '
|
|
38
|
-
'DEFAULT_BLOCK_NAME',
|
|
104
|
+
# Base classes
|
|
105
|
+
'BaseModelConfig', 'BaseResult',
|
|
106
|
+
# 'DEFAULT_BLOCK_NAME', # Removed to avoid circular import - import directly from functional.dfm_block
|
|
107
|
+
# Model-specific configs (from schema.py)
|
|
108
|
+
'DFMConfig', 'DDFMConfig', 'KDFMConfig',
|
|
39
109
|
# Parameter overrides
|
|
40
110
|
'FitParams',
|
|
41
|
-
#
|
|
42
|
-
'
|
|
111
|
+
# Model-specific results (from results.py)
|
|
112
|
+
'DFMResult', 'DDFMResult', 'KDFMResult',
|
|
43
113
|
# Utilities
|
|
44
|
-
'validate_frequency',
|
|
114
|
+
'validate_frequency',
|
|
45
115
|
# IO
|
|
46
116
|
'ConfigSource', 'YamlSource', 'DictSource',
|
|
47
|
-
'
|
|
117
|
+
'make_config_source',
|
|
118
|
+
'detect_config_type',
|
|
48
119
|
# Frequency and aggregation utilities
|
|
49
120
|
'FREQUENCY_HIERARCHY',
|
|
50
121
|
'PERIODS_PER_YEAR',
|
|
51
122
|
'get_periods_per_year',
|
|
52
|
-
'get_annual_factor',
|
|
53
123
|
'compute_idio_lengths',
|
|
54
124
|
'get_tent_weights',
|
|
55
|
-
'generate_tent_weights',
|
|
56
|
-
'generate_R_mat',
|
|
57
125
|
'get_agg_structure',
|
|
58
126
|
'group_by_freq',
|
|
127
|
+
# Type definitions (from types.py)
|
|
128
|
+
'ArrayLike',
|
|
129
|
+
'FloatArray',
|
|
130
|
+
'IntArray',
|
|
131
|
+
'BoolArray',
|
|
132
|
+
'OptionalArray',
|
|
133
|
+
'OptionalTensor',
|
|
134
|
+
'OptionalArrayLike',
|
|
135
|
+
'FactorState',
|
|
136
|
+
'ObservationState',
|
|
137
|
+
'ForecastResult',
|
|
138
|
+
'CoefficientMatrix',
|
|
139
|
+
'CovarianceMatrix',
|
|
140
|
+
'IRFArray',
|
|
141
|
+
'CompanionMatrix',
|
|
142
|
+
'SeriesID',
|
|
143
|
+
'Frequency',
|
|
144
|
+
'DatasetName',
|
|
145
|
+
'ModelName',
|
|
146
|
+
'Batch',
|
|
147
|
+
'Loss',
|
|
148
|
+
'Optimizer',
|
|
149
|
+
'ResultDict',
|
|
150
|
+
'ForecastDict',
|
|
151
|
+
'MetricsDict',
|
|
152
|
+
'IRFResultDict',
|
|
153
|
+
'CheckpointDict',
|
|
154
|
+
'ConfigDict',
|
|
155
|
+
'Device',
|
|
156
|
+
'Shape',
|
|
157
|
+
'Shape2D',
|
|
158
|
+
'Shape3D',
|
|
159
|
+
'Shape4D',
|
|
160
|
+
'ForecastHorizon',
|
|
161
|
+
'IRFHorizon',
|
|
162
|
+
'LagOrder',
|
|
163
|
+
'NumFactors',
|
|
164
|
+
'NumVars',
|
|
165
|
+
'PathLike',
|
|
166
|
+
'ValidationIssue',
|
|
167
|
+
'ValidationResult',
|
|
168
|
+
'Tensor',
|
|
169
|
+
'is_numpy_array',
|
|
170
|
+
'is_torch_tensor',
|
|
171
|
+
'is_array_like',
|
|
172
|
+
'get_array_shape',
|
|
173
|
+
'to_numpy',
|
|
174
|
+
'to_tensor',
|
|
59
175
|
]
|
|
60
176
|
|