dfm-python 0.5.52__py3-none-any.whl → 0.5.54__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 (80) hide show
  1. dfm_python/__init__.py +30 -26
  2. dfm_python/config/__init__.py +144 -28
  3. dfm_python/config/adapter.py +300 -448
  4. dfm_python/config/constants.py +448 -0
  5. dfm_python/config/schema/__init__.py +17 -0
  6. dfm_python/config/schema/block.py +64 -0
  7. dfm_python/config/schema/model.py +620 -0
  8. dfm_python/config/schema/results.py +397 -0
  9. dfm_python/config/types.py +314 -0
  10. dfm_python/datamodule/__init__.py +31 -0
  11. dfm_python/datamodule/base.py +618 -0
  12. dfm_python/datamodule/ddfm_dm.py +276 -0
  13. dfm_python/datamodule/dfm_dm.py +313 -0
  14. dfm_python/datamodule/kdfm_dm.py +180 -0
  15. dfm_python/dataset/__init__.py +60 -0
  16. dfm_python/{data → dataset}/dataloader.py +49 -47
  17. dfm_python/{data → dataset}/dataset.py +70 -66
  18. dfm_python/dataset/process.py +435 -0
  19. dfm_python/decoder/mlp.py +12 -5
  20. dfm_python/encoder/__init__.py +1 -3
  21. dfm_python/encoder/pca.py +95 -174
  22. dfm_python/encoder/{autoencoder.py → simple_encoder.py} +81 -37
  23. dfm_python/encoder/variational_encoder.py +0 -0
  24. dfm_python/functional/__init__.py +13 -0
  25. dfm_python/functional/dfm_block.py +419 -0
  26. dfm_python/functional/em.py +958 -0
  27. dfm_python/functional/irf.py +305 -0
  28. dfm_python/functional/krylov.py +124 -0
  29. dfm_python/logger/inference_logger.py +4 -5
  30. dfm_python/models/__init__.py +9 -3
  31. dfm_python/models/base.py +403 -647
  32. dfm_python/models/ddfm.py +594 -496
  33. dfm_python/models/dfm.py +1088 -841
  34. dfm_python/models/kdfm.py +1855 -0
  35. dfm_python/numeric/builder.py +191 -0
  36. dfm_python/numeric/estimator.py +849 -0
  37. dfm_python/numeric/stability.py +1239 -0
  38. dfm_python/numeric/tent.py +296 -0
  39. dfm_python/numeric/validator.py +699 -0
  40. dfm_python/ssm/__init__.py +11 -39
  41. dfm_python/ssm/arma.py +0 -0
  42. dfm_python/ssm/companion.py +995 -0
  43. dfm_python/ssm/kalman.py +365 -829
  44. dfm_python/ssm/structural.py +298 -0
  45. dfm_python/trainer/__init__.py +17 -86
  46. dfm_python/trainer/ddfm.py +489 -11
  47. dfm_python/trainer/dfm.py +10 -9
  48. dfm_python/trainer/kdfm.py +138 -0
  49. dfm_python/utils/__init__.py +252 -99
  50. dfm_python/utils/common.py +298 -0
  51. dfm_python/utils/errors.py +73 -0
  52. dfm_python/utils/helper.py +200 -0
  53. dfm_python/utils/metric.py +455 -0
  54. dfm_python/utils/misc.py +269 -0
  55. dfm_python/utils/tensor_utils.py +199 -0
  56. dfm_python/utils/validation.py +121 -0
  57. dfm_python-0.5.54.dist-info/METADATA +380 -0
  58. dfm_python-0.5.54.dist-info/RECORD +66 -0
  59. dfm_python/config/results.py +0 -405
  60. dfm_python/config/schema.py +0 -931
  61. dfm_python/config/utils.py +0 -547
  62. dfm_python/data/__init__.py +0 -30
  63. dfm_python/data/utils.py +0 -334
  64. dfm_python/lightning/__init__.py +0 -53
  65. dfm_python/lightning/ddfm_dm.py +0 -417
  66. dfm_python/lightning/dfm_dm.py +0 -379
  67. dfm_python/lightning/utils.py +0 -516
  68. dfm_python/models/utils.py +0 -470
  69. dfm_python/ssm/em.py +0 -1256
  70. dfm_python/ssm/utils.py +0 -430
  71. dfm_python/trainer/denoising.py +0 -490
  72. dfm_python/utils/data.py +0 -604
  73. dfm_python/utils/diagnostics.py +0 -577
  74. dfm_python/utils/helpers.py +0 -454
  75. dfm_python/utils/statespace.py +0 -1270
  76. dfm_python/utils/time.py +0 -1047
  77. dfm_python-0.5.52.dist-info/METADATA +0 -510
  78. dfm_python-0.5.52.dist-info/RECORD +0 -48
  79. {dfm_python-0.5.52.dist-info → dfm_python-0.5.54.dist-info}/WHEEL +0 -0
  80. {dfm_python-0.5.52.dist-info → dfm_python-0.5.54.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/sample_data.csv')
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
@@ -40,7 +40,7 @@ Example (Standard Lightning Pattern):
40
40
  >>> model.load_config('config/default.yaml')
41
41
  >>>
42
42
  >>> # Step 4: Create trainer and fit (standard Lightning pattern)
43
- >>> trainer = DFMTrainer(max_epochs=100)
43
+ >>> trainer = DFMTrainer(max_epochs=100) # DEFAULT_MAX_EPOCHS
44
44
  >>> trainer.fit(model, dm)
45
45
  >>>
46
46
  >>> # Step 5: Predict
@@ -55,17 +55,17 @@ Example (Standard Lightning Pattern):
55
55
  >>> ddfm_model = DDFM(encoder_layers=[64, 32], num_factors=2)
56
56
  >>> ddfm_model.load_config('config/default.yaml')
57
57
  >>>
58
- >>> trainer_ddfm = DDFMTrainer(max_epochs=100)
58
+ >>> trainer_ddfm = DDFMTrainer(max_epochs=100) # DEFAULT_MAX_EPOCHS
59
59
  >>> trainer_ddfm.fit(ddfm_model, dm_ddfm)
60
60
  >>> Xf, Zf = ddfm_model.predict(horizon=6)
61
61
 
62
- Note: DFMConfig and SeriesConfig are internal implementation details.
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.5"
68
+ __version__ = "0.5.54"
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, SeriesConfig, config sources
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, HydraSource,
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 (for backward compatibility, but not recommended)
92
- from .config import DFMConfig, SeriesConfig # Internal use only
91
+ # Internal imports (internal use only)
92
+ from .config import DFMConfig
93
93
 
94
94
  # Results
95
- from .config.results import DFMResult, DDFMResult, BaseResult
95
+ from .config import DFMResult, DDFMResult, KDFMResult, BaseResult
96
96
 
97
97
  # Utilities (from utils/ subpackage)
98
- from .utils.diagnostics import diagnose_series, print_series_diagnosis
99
- from .utils.time import calculate_rmse
98
+ from .utils.metric import calculate_rmse
100
99
 
101
- # PyTorch Lightning modules (mandatory dependency)
100
+ # DataModule classes (includes both Lightning and custom implementations)
102
101
  # Users can import these directly from dfm_python
103
- from .lightning import (
102
+ from .datamodule import (
104
103
  DFMDataModule,
105
104
  DDFMDataModule, # DDFM-specific DataModule
106
- DFMDataset, # Dataset class (usually not needed directly)
105
+ KDFMDataModule, # KDFM-specific DataModule
107
106
  DDFMDataset, # DDFM Dataset class (usually not needed directly)
108
- KalmanFilter, # Module class
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', 'HydraSource',
126
+ 'ConfigSource', 'YamlSource',
128
127
  'make_config_source',
129
128
  # Low-level API (functional interface - advanced usage)
130
- 'BaseResult', 'DFMResult', 'DDFMResult', 'calculate_rmse', 'diagnose_series', 'print_series_diagnosis',
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
- 'DFMDataset', # Dataset class (usually not needed directly)
146
+ 'KDFMDataModule', # KDFM-specific DataModule
143
147
  'DDFMDataset', # DDFM Dataset class (usually not needed directly)
144
- 'KalmanFilter', # Module class
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
 
@@ -1,60 +1,176 @@
1
1
  """Configuration subpackage for DFM.
2
2
 
3
3
  This subpackage provides:
4
- - Schema (DFMConfig, SeriesConfig) in schema.py
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, SeriesConfig,
10
- DEFAULT_BLOCK_NAME,
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 .results import BaseResult, DFMResult, DDFMResult, FitParams
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
- from .utils import (
23
- FREQUENCY_HIERARCHY,
24
- PERIODS_PER_YEAR,
25
- get_periods_per_year,
26
- get_annual_factor,
27
- compute_idio_lengths,
28
- get_tent_weights,
29
- generate_tent_weights,
30
- generate_R_mat,
31
- get_agg_structure,
32
- group_by_freq,
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
- # Schema
37
- 'BaseModelConfig', 'DFMConfig', 'DDFMConfig', 'SeriesConfig',
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
- # Results
42
- 'BaseResult', 'DFMResult', 'DDFMResult',
111
+ # Model-specific results (from results.py)
112
+ 'DFMResult', 'DDFMResult', 'KDFMResult',
43
113
  # Utilities
44
- 'validate_frequency', 'validate_transformation',
114
+ 'validate_frequency',
45
115
  # IO
46
116
  'ConfigSource', 'YamlSource', 'DictSource',
47
- 'HydraSource', 'MergedConfigSource', 'make_config_source',
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