rwls 0.0.1__tar.gz

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.
rwls-0.0.1/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ build/
5
+ dist/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ htmlcov/
12
+ .idea/
13
+ .vscode/
rwls-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Francisco Ibanez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
rwls-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,96 @@
1
+ Metadata-Version: 2.5
2
+ Name: rwls
3
+ Version: 0.0.1
4
+ Summary: Regime-weighted least squares: regime-dependent factor models estimated with hidden Markov posterior probabilities.
5
+ Author: Francisco Ibanez
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Keywords: factor models,financial econometrics,hidden Markov model,regime switching,weighted least squares
9
+ Classifier: Development Status :: 1 - Planning
10
+ Classifier: Intended Audience :: Financial and Insurance Industry
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Topic :: Office/Business :: Financial
14
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
15
+ Requires-Python: >=3.10
16
+ Description-Content-Type: text/markdown
17
+
18
+ # rwls
19
+
20
+ Regime-weighted least squares (RWLS) for estimating regime-dependent factor models.
21
+
22
+ ## The estimator
23
+
24
+ Factor models usually assume that an asset's exposures to common risk factors are
25
+ constant. In practice, betas, factor premia and idiosyncratic risk shift with
26
+ market conditions. RWLS estimates a separate factor model for every asset in every
27
+ latent market regime, without fitting a regime-switching model asset by asset.
28
+
29
+ Estimation proceeds in two stages:
30
+
31
+ 1. **Regime identification.** A multivariate Gaussian hidden Markov model is fitted
32
+ to a small set of common factor returns `f_t`. Its posterior probabilities
33
+ `γ_t(s) = P(S_t = s | data)` measure how strongly each period belongs to each
34
+ regime `s`.
35
+
36
+ 2. **Regime-weighted regressions.** For each asset `i` and regime `s`, the
37
+ loadings solve a weighted least-squares problem in which each observation is
38
+ weighted by its posterior regime probability:
39
+
40
+ ```
41
+ (α_is, β_is) = argmin Σ_t γ_t(s) · (r_it − α − β′ f_t)²
42
+ ```
43
+
44
+ The regime-specific residual variance is measured on the regime's effective
45
+ sample, the posterior mass `Σ_t γ_t(s)`:
46
+
47
+ ```
48
+ σ²_is = Σ_t γ_t(s) · e_it² / (Σ_t γ_t(s) − k)
49
+ ```
50
+
51
+ where `k` is the number of regression coefficients.
52
+
53
+ Every observation contributes to every regime in proportion to the evidence that
54
+ it belongs there. Assets are not hard-classified, and no observation is discarded.
55
+ Because all assets share the same factor history and weights, the regressions
56
+ for a whole cross-section are solved jointly. The cost of adding an asset is one
57
+ weighted regression per regime, which makes the approach practical for universes
58
+ of thousands of securities.
59
+
60
+ ## From loadings to regime-conditional moments
61
+
62
+ Combined with the regime-conditional factor means `λ_s` and covariances `Ω_s`
63
+ from the hidden Markov model, the loadings imply asset-level moments in each
64
+ regime:
65
+
66
+ ```
67
+ μ_s = α_s + B_s λ_s
68
+ Σ_s = B_s Ω_s B_s′ + diag(σ²_s)
69
+ ```
70
+
71
+ Weighting these by forecast regime probabilities gives the asset return
72
+ distribution as a Gaussian mixture. Its mean and covariance are available in
73
+ closed form.
74
+
75
+ ## Planned scope
76
+
77
+ - Hidden Markov regime identification on factor returns, with state alignment
78
+ across refits and one-step regime forecasts.
79
+ - Vectorized RWLS estimation for large cross-sections, with effective sample
80
+ sizes and sampling covariances of the regime-specific loadings.
81
+ - Regime-conditional and mixture moments of asset returns.
82
+ - Simulation tools for regime-switching factor models, for Monte Carlo
83
+ evaluation of the estimators.
84
+
85
+ Applications include conditional beta estimation, regime-dependent risk models
86
+ and portfolio construction. Portfolio optimization is one use of the estimated
87
+ moments, not the purpose of the package.
88
+
89
+ ## Status
90
+
91
+ Under development. This release reserves the package name; the implementation
92
+ will follow in a later version.
93
+
94
+ ## License
95
+
96
+ MIT
rwls-0.0.1/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # rwls
2
+
3
+ Regime-weighted least squares (RWLS) for estimating regime-dependent factor models.
4
+
5
+ ## The estimator
6
+
7
+ Factor models usually assume that an asset's exposures to common risk factors are
8
+ constant. In practice, betas, factor premia and idiosyncratic risk shift with
9
+ market conditions. RWLS estimates a separate factor model for every asset in every
10
+ latent market regime, without fitting a regime-switching model asset by asset.
11
+
12
+ Estimation proceeds in two stages:
13
+
14
+ 1. **Regime identification.** A multivariate Gaussian hidden Markov model is fitted
15
+ to a small set of common factor returns `f_t`. Its posterior probabilities
16
+ `γ_t(s) = P(S_t = s | data)` measure how strongly each period belongs to each
17
+ regime `s`.
18
+
19
+ 2. **Regime-weighted regressions.** For each asset `i` and regime `s`, the
20
+ loadings solve a weighted least-squares problem in which each observation is
21
+ weighted by its posterior regime probability:
22
+
23
+ ```
24
+ (α_is, β_is) = argmin Σ_t γ_t(s) · (r_it − α − β′ f_t)²
25
+ ```
26
+
27
+ The regime-specific residual variance is measured on the regime's effective
28
+ sample, the posterior mass `Σ_t γ_t(s)`:
29
+
30
+ ```
31
+ σ²_is = Σ_t γ_t(s) · e_it² / (Σ_t γ_t(s) − k)
32
+ ```
33
+
34
+ where `k` is the number of regression coefficients.
35
+
36
+ Every observation contributes to every regime in proportion to the evidence that
37
+ it belongs there. Assets are not hard-classified, and no observation is discarded.
38
+ Because all assets share the same factor history and weights, the regressions
39
+ for a whole cross-section are solved jointly. The cost of adding an asset is one
40
+ weighted regression per regime, which makes the approach practical for universes
41
+ of thousands of securities.
42
+
43
+ ## From loadings to regime-conditional moments
44
+
45
+ Combined with the regime-conditional factor means `λ_s` and covariances `Ω_s`
46
+ from the hidden Markov model, the loadings imply asset-level moments in each
47
+ regime:
48
+
49
+ ```
50
+ μ_s = α_s + B_s λ_s
51
+ Σ_s = B_s Ω_s B_s′ + diag(σ²_s)
52
+ ```
53
+
54
+ Weighting these by forecast regime probabilities gives the asset return
55
+ distribution as a Gaussian mixture. Its mean and covariance are available in
56
+ closed form.
57
+
58
+ ## Planned scope
59
+
60
+ - Hidden Markov regime identification on factor returns, with state alignment
61
+ across refits and one-step regime forecasts.
62
+ - Vectorized RWLS estimation for large cross-sections, with effective sample
63
+ sizes and sampling covariances of the regime-specific loadings.
64
+ - Regime-conditional and mixture moments of asset returns.
65
+ - Simulation tools for regime-switching factor models, for Monte Carlo
66
+ evaluation of the estimators.
67
+
68
+ Applications include conditional beta estimation, regime-dependent risk models
69
+ and portfolio construction. Portfolio optimization is one use of the estimated
70
+ moments, not the purpose of the package.
71
+
72
+ ## Status
73
+
74
+ Under development. This release reserves the package name; the implementation
75
+ will follow in a later version.
76
+
77
+ ## License
78
+
79
+ MIT
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.24"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "rwls"
7
+ version = "0.0.1"
8
+ description = "Regime-weighted least squares: regime-dependent factor models estimated with hidden Markov posterior probabilities."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [{ name = "Francisco Ibanez" }]
14
+ keywords = ["financial econometrics", "factor models", "regime switching", "hidden Markov model", "weighted least squares"]
15
+ classifiers = [
16
+ "Development Status :: 1 - Planning",
17
+ "Intended Audience :: Science/Research",
18
+ "Intended Audience :: Financial and Insurance Industry",
19
+ "Programming Language :: Python :: 3",
20
+ "Topic :: Scientific/Engineering :: Mathematics",
21
+ "Topic :: Office/Business :: Financial",
22
+ ]
23
+ dependencies = []
24
+
25
+ [tool.hatch.build.targets.wheel]
26
+ packages = ["src/rwls"]
@@ -0,0 +1,11 @@
1
+ """Regime-weighted least squares (RWLS) for regime-dependent factor models.
2
+
3
+ Latent market regimes are identified with a multivariate Gaussian hidden Markov
4
+ model fitted to common factor returns. Each asset's factor loadings and residual
5
+ variance are then estimated separately for every regime by weighted least squares,
6
+ with each observation weighted by its posterior regime probability.
7
+
8
+ This release reserves the package name. The implementation is under development.
9
+ """
10
+
11
+ __version__ = "0.0.1"