Risk Models & Solve Helpers¶
Supporting data structures used internally by the engine and exposed for advanced usage.
FactorModel¶
basanos.math.FactorModel
dataclass
¶
Frozen dataclass for a factor risk model decomposition (Section 4.1).
Encapsulates the three components of the factor model
where
- \(\mathbf{B} \in \mathbb{R}^{n \times k}\) is the factor loading matrix: column \(j\) gives the sensitivity of each asset to factor \(j\).
- \(\mathbf{F} \in \mathbb{R}^{k \times k}\) is the factor covariance matrix (positive definite), capturing how the \(k\) factors co-vary.
- \(\mathbf{D} = \operatorname{diag}(d_1, \dots, d_n)\) with \(d_i > 0\) is the idiosyncratic variance diagonal, capturing the asset-specific variance unexplained by the common factors.
The central assumption is \(k \ll n\): the dominant systematic sources of risk are captured by a handful of factors while the idiosyncratic component is, by construction, uncorrelated across assets.
Attributes:
| Name | Type | Description |
|---|---|---|
factor_loadings |
ndarray
|
Factor loading matrix \(\mathbf{B}\),
shape |
factor_covariance |
ndarray
|
Factor covariance matrix \(\mathbf{F}\),
shape |
idiosyncratic_var |
ndarray
|
Idiosyncratic variance vector
\((d_1, \dots, d_n)\), shape |
Examples:
>>> import numpy as np
>>> loadings = np.eye(3, 2)
>>> cov = np.eye(2) * 0.5
>>> idio = np.array([0.5, 0.5, 1.0])
>>> fm = FactorModel(factor_loadings=loadings, factor_covariance=cov, idiosyncratic_var=idio)
>>> fm.n_assets
3
>>> fm.n_factors
2
>>> fm.covariance.shape
(3, 3)
Source code in src/basanos/math/_factor_model.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
covariance
property
¶
Reconstruct the full \(n \times n\) covariance matrix.
Computes \(\bm{\Sigma} = \mathbf{B}\mathbf{F}\mathbf{B}^\top + \mathbf{D}\) by combining the low-rank systematic component with the diagonal idiosyncratic component.
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Shape |
Examples:
n_assets
property
¶
Number of assets n (rows of factor_loadings).
n_factors
property
¶
Number of factors k (columns of factor_loadings).
woodbury_condition_number
property
¶
Condition number of the inner \(k \times k\) Woodbury matrix.
Returns the condition number of the matrix
which is the matrix actually inverted during solve. A large
value (above _DEFAULT_COND_THRESHOLD ≈ 1e12) indicates that the
Woodbury solve is numerically unreliable.
This property gives callers a way to inspect the numerical health of the model without performing a full solve. Unlike the condition number of the full \(n \times n\) covariance matrix, this measure is specific to the \(k \times k\) inner system solved inside the Woodbury identity.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Condition number \(\kappa(\mathbf{M})\). Returns |
float
|
|
|
float
|
singular or indefinite), as the Cholesky decomposition used to |
|
float
|
form \(\mathbf{F}^{-1}\) fails in that case. |
Examples:
__post_init__()
¶
Validate shape consistency and strict positivity after initialization.
Raises:
| Type | Description |
|---|---|
FactorModelError
|
If |
FactorModelError
|
If |
FactorModelError
|
If |
FactorModelError
|
If any element of
|
Source code in src/basanos/math/_factor_model.py
from_returns(returns, k)
classmethod
¶
Fit a rank-k factor model from a return matrix via truncated SVD.
Extracts latent factors from the return matrix \(\mathbf{R} \in \mathbb{R}^{T \times n}\) using the Singular Value Decomposition (SVD). The top-k singular triplets define the factor model components:
where \(\mathbf{V}_k\) and \(\bm{\Sigma}_k\) are the top-k
right singular vectors and singular values of \(\mathbf{R}\)
respectively. When returns contains unit-variance columns (as
produced by vol_adj), the sample
covariance has unit diagonal; the idiosyncratic term
\(\hat{d}_i = 1 - (\mathbf{B}\mathbf{F}\mathbf{B}^\top)_{ii}\)
absorbs the residual so the full covariance \(\hat{\mathbf{C}}^{(k)}\)
also has unit diagonal. Each \(\hat{d}_i\) is clamped from below
at machine epsilon to guarantee strict positivity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
ndarray
|
Return matrix of shape |
required |
k
|
int
|
Number of factors to retain. Must satisfy
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
FactorModel |
FactorModel
|
Fitted factor model with |
Raises:
| Type | Description |
|---|---|
FactorModelError
|
If returns is not 2-D. |
FactorModelError
|
If k is outside the range |
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> ret = rng.standard_normal((50, 5))
>>> fm = FactorModel.from_returns(ret, k=2)
>>> fm.n_factors
2
>>> fm.n_assets
5
>>> fm.covariance.shape
(5, 5)
Source code in src/basanos/math/_factor_model.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
solve(rhs, cond_threshold=_DEFAULT_COND_THRESHOLD)
¶
Solve \(\bm{\Sigma}\,\mathbf{x} = \mathbf{b}\) via the Woodbury identity.
Applies the Sherman--Morrison--Woodbury formula (Section 4.3 of basanos.pdf) to avoid forming or factorising the full \(n \times n\) covariance matrix:
Because \(\mathbf{D}\) is diagonal, \(\mathbf{D}^{-1}\) is free. The inner matrix is \(k \times k\) with cost \(O(k^3)\), and the surrounding multiplications cost \(O(kn)\). Total cost is \(O(k^3 + kn)\) rather than \(O(n^3)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
ndarray
|
Right-hand side vector \(\mathbf{b}\), shape |
required |
cond_threshold
|
float
|
Condition-number threshold above which an
|
_DEFAULT_COND_THRESHOLD
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Solution vector \(\mathbf{x}\), shape |
Raises:
| Type | Description |
|---|---|
DimensionMismatchError
|
If |
SingularMatrixError
|
If the inner \(k \times k\) matrix is singular. |
Examples:
>>> import numpy as np
>>> loadings = np.eye(3, 1)
>>> cov = np.eye(1)
>>> idio = np.ones(3)
>>> fm = FactorModel(factor_loadings=loadings, factor_covariance=cov, idiosyncratic_var=idio)
>>> rhs = np.array([1.0, 2.0, 3.0])
>>> x = fm.solve(rhs)
>>> np.allclose(fm.covariance @ x, rhs)
True
Source code in src/basanos/math/_factor_model.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
MatrixBundle¶
basanos.math.MatrixBundle
dataclass
¶
Container for the covariance matrix and any mode-specific auxiliary state.
Wrapping the covariance matrix in a dataclass decouples
_compute_position from the raw array so that future
covariance modes (e.g. DCC-GARCH, RMT-cleaned) can carry additional fields
through the same interface without changing the method signature.
Attributes:
| Name | Type | Description |
|---|---|---|
matrix |
ndarray
|
The |
Source code in src/basanos/math/_engine_solve.py
WarmupState¶
basanos.math.WarmupState
dataclass
¶
Final state produced by a full batch solve; consumed by from_warmup.
Returned by warmup_state and used by
from_warmup to initialise the streaming state without
coupling to the private _iter_solve generator.
Attributes:
| Name | Type | Description |
|---|---|---|
prev_cash_pos |
ndarray
|
Cash positions at the last warmup row, shape
|
corr_iir_state |
_EwmCorrState | None
|
Final IIR filter memory from the EWM correlation pass,
or |
Source code in src/basanos/math/_engine_solve.py
SolveStatus¶
basanos.math.SolveStatus
¶
Bases: StrEnum
Solver outcome labels for each timestamp.
Since SolveStatus inherits from str via StrEnum,
values compare equal to their string equivalents (e.g.
SolveStatus.VALID == "valid"), preserving backward compatibility
with code that matches on string literals.
Attributes:
| Name | Type | Description |
|---|---|---|
WARMUP |
Insufficient history for the sliding-window covariance mode. |
|
ZERO_SIGNAL |
The expected-return vector was all-zero; positions zeroed. |
|
DEGENERATE |
Normalisation denominator was non-finite, solve failed, or no asset had a finite price; positions zeroed for safety. |
|
VALID |
Linear system solved successfully; positions are non-trivially non-zero. |