quanttoolbox.mixtures¶
mixtures.gaussian_mixture¶
Python alternatives
Hybrid: sklearn.mixture.GaussianMixture is more numerically robust (covariance regularization, multiple initializations, convergence diagnostics) for the general n-component EM-fitting step (estimate_em_mixture). Keep everything downstream of fitting — VaR/ES, risk contribution, risk budgeting, PDF/skewness under the mixture — since sklearn's GaussianMixture only fits parameters, nothing else.
quanttoolbox.mixtures.gaussian_mixture
¶
Two-component Gaussian mixture models: moments, PDF, simulation, EM estimation, VaR/ES risk measures, and risk budgeting.
Ported from QuantToolBox/mixture/{mixture_moments,mixture_pdf_assets, mixture_pdf_portfolio,mixture_simulate,mixture_skewness, mixture_skewness_portfolio,mixture_univariate_thresholding, mixture_probability_filtering,mixture_compute_var,mixture_compute_es, mixture_compute_rc_var,mixture_compute_rc_es,mixture_compute_rb_var, mixture_compute_rb_es,estimate_em_mixture,logl_em_mixture}.m
Model: a random vector R is pi1-probability drawn from N(mu1, Sigma1) and pi2=1-pi1-probability drawn from N(mu2, Sigma2) -- e.g. a "normal regime" and a "stress regime" for asset returns.
Translation notes:
mixture_compute_rb_var/mixture_compute_rb_esoriginally support two solver algorithms: (1)fminconminimizing the sum of squared deviations between (risk contribution / budget) ratios across assets, and (2) anfminunclog-barrier variant referencing aRB_lagrangianglobal that's never actually set anywhere in the original codebase (a global exists but nothing ever assigns to it before use -- effectively dead/broken code). Only algorithm (1) is ported here, viascipy.optimize.minimize(SLSQP, budget-constrained).- MATLAB's
global MIXTURE_*/RB_*state-passing to nested objective/constraint functions is replaced by closures capturing the mixture parameters directly.
estimate_em_mixture(data, init, estimate_mixing_weights=True, tol=1e-06, max_iters=1000)
¶
Fit a 2-component Gaussian mixture to data via Expectation-Maximization.
estimate_mixing_weights=True (default): re-estimate pi1/pi2 each iteration (standard EM). False: hold pi1/pi2 fixed at their initial values (e.g. when they're set exogenously, as in the jump-diffusion parameterization).
Original: mixture/{estimate_em_mixture,logl_em_mixture}.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
mixture_compute_es(x, params, alpha)
¶
Expected Shortfall of a portfolio x's return under the mixture distribution, at confidence level alpha.
Original: mixture/mixture_compute_es.m
Returns (ES_mixture, VaR_mixture, ES_Gaussian, VaR_Gaussian).
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
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 | |
mixture_compute_rb_es(params, alpha, b=None, x0=None, x_minus=0.0, x_plus=1.0)
¶
ES risk budgeting portfolio under the mixture distribution.
Original: mixture/mixture_compute_rb_es.m (algorithm 1 -- see module docstring for the unported algorithm 2 branch)
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
mixture_compute_rb_var(params, alpha, b=None, x0=None, x_minus=0.0, x_plus=1.0)
¶
VaR risk budgeting portfolio under the mixture distribution: find weights x (in [x_minus, x_plus], summing to 1) whose VaR risk contributions best match the target budgets b.
Original: mixture/mixture_compute_rb_var.m (algorithm 1 -- see module docstring for the unported algorithm 2 branch)
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
mixture_compute_rc_es(x, params, alpha)
¶
ES risk contribution decomposition under the mixture distribution.
Original: mixture/mixture_compute_rc_es.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
mixture_compute_rc_var(x, params, alpha)
¶
VaR risk contribution decomposition under the mixture distribution.
Original: mixture/mixture_compute_rc_var.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
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 | |
mixture_compute_var(x, params, alpha)
¶
Value-at-Risk of a portfolio x's return under the mixture distribution, at confidence level alpha (found via bisection on the mixture CDF).
Original: mixture/mixture_compute_var.m
Returns (VaR_mixture, VaR_Gaussian) -- the latter using only the first (normal-regime) component, for comparison.
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
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 | |
mixture_moments(params)
¶
Mean vector and covariance matrix of the mixture distribution.
Original: mixture/mixture_moments.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
51 52 53 54 55 56 57 58 59 60 | |
mixture_pdf_assets(y, params)
¶
Marginal PDF of each asset under the mixture distribution, evaluated at y (one column per asset, matching mu1/mu2's dimension).
Original: mixture/mixture_pdf_assets.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
mixture_pdf_portfolio(y, x, params)
¶
PDF of a portfolio x's return under the mixture distribution, evaluated at y.
Original: mixture/mixture_pdf_portfolio.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
mixture_probability_filtering(r_t, params)
¶
Posterior regime probabilities given an observation r_t (Bayes' rule applied to the mixture likelihood).
Original: mixture/mixture_probability_filtering.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
mixture_simulate(params, n_samples, rng=None)
¶
Simulate n_samples draws from the mixture distribution.
Original: mixture/mixture_simulate.m
Returns (samples, regime) where regime[i]=1 if sample i was drawn from component 1, else 2.
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
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 | |
mixture_skewness(pi1, mu1, sigma1, pi2, mu2, sigma2)
¶
Mean, standard deviation, and skewness of a scalar 2-component Gaussian mixture.
Original: mixture/mixture_skewness.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
mixture_skewness_portfolio(x, params)
¶
Mean, standard deviation, and skewness of a portfolio x's return under the mixture distribution.
Original: mixture/mixture_skewness_portfolio.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
82 83 84 85 86 87 88 89 90 91 92 93 94 | |
mixture_univariate_thresholding(pi1, mu1, sigma1, pi2, mu2, sigma2, pi2_star)
¶
Find the threshold values [y_minus, y_plus] outside of which the posterior probability of being in regime 2 exceeds pi2_star (a univariate regime-classification boundary).
Original: mixture/mixture_univariate_thresholding.m
Source code in src/quanttoolbox/mixtures/gaussian_mixture.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
mixtures.jump_diffusion¶
Python alternatives
Keep — jump-diffusion-specific risk measures with no general-purpose equivalent.
quanttoolbox.mixtures.jump_diffusion
¶
Jump-diffusion risk measures: thin parameter-transform wrappers around
the Gaussian mixture machinery in mixtures.gaussian_mixture, plus
lognormal moment/skewness formulas.
Ported from QuantToolBox/mixture/{jump_compute_var,jump_compute_es, jump_compute_rc_var,jump_compute_rc_es,jump_compute_rb_var, jump_compute_rb_es,jump_pdf_assets,jump_pdf_portfolio,jump_simulate, jump_skewness,jump_skewness_portfolio,jump_univariate_thresholding, jump_probability_filtering,lognormal_moments,lognormal_skewness, bivariate_lognormal_skewness}.m
Model: over a short time step dt, returns follow a diffusion (mean mu_bar, covariance Sigma_bar) with a Poisson-arrival jump component (intensity lambda, jump mean mu_tilde, jump covariance Sigma_tilde). This is exactly a 2-component Gaussian mixture with
pi1 = 1 - lambda*dt, mu1 = mu_bar*dt, Sigma1 = Sigma_bar*dt
pi2 = lambda*dt, mu2 = mu_bar*dt + mu_tilde, Sigma2 = Sigma_bar*dt + Sigma_tilde
so every jump_*.m function in the original is just this parameter
transform followed by a call into the corresponding mixture_*.m
function -- ported here as jump_to_mixture_params plus one-line
wrappers around gaussian_mixture's functions, rather than
independently re-implementing the same math.
bivariate_lognormal_skewness(mu_x, sigma_x, mu_y, sigma_y, rho)
¶
Skewness of the sum of two correlated lognormal random variables X=exp(N(mu_x,sigma_x^2)), Y=exp(N(mu_y,sigma_y^2)) with correlation rho between the underlying normals.
Original: mixture/bivariate_lognormal_skewness.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
jump_compute_es(x, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, alpha)
¶
Expected Shortfall under the jump-diffusion model. Original: jump_compute_es.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
119 120 121 122 123 124 125 126 127 128 129 130 131 | |
jump_compute_rb_es(mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, alpha, b=None, x0=None, x_minus=0.0, x_plus=1.0)
¶
ES risk budgeting under the jump-diffusion model. Original: jump_compute_rb_es.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
jump_compute_rb_var(mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, alpha, b=None, x0=None, x_minus=0.0, x_plus=1.0)
¶
VaR risk budgeting under the jump-diffusion model. Original: jump_compute_rb_var.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
jump_compute_rc_es(x, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, alpha)
¶
ES risk contribution under the jump-diffusion model. Original: jump_compute_rc_es.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
149 150 151 152 153 154 155 156 157 158 159 160 161 | |
jump_compute_rc_var(x, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, alpha)
¶
VaR risk contribution under the jump-diffusion model. Original: jump_compute_rc_var.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
134 135 136 137 138 139 140 141 142 143 144 145 146 | |
jump_compute_var(x, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, alpha)
¶
Value-at-Risk under the jump-diffusion model. Original: jump_compute_var.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
104 105 106 107 108 109 110 111 112 113 114 115 116 | |
jump_pdf_assets(y, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt)
¶
Marginal asset PDFs under the jump-diffusion model. Original: jump_pdf_assets.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
200 201 202 203 204 205 206 207 208 209 210 211 | |
jump_pdf_portfolio(y, x, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt)
¶
Portfolio return PDF under the jump-diffusion model. Original: jump_pdf_portfolio.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
214 215 216 217 218 219 220 221 222 223 224 225 226 | |
jump_probability_filtering(r_t, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt)
¶
Posterior jump-regime probability given an observation. Original: jump_probability_filtering.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
244 245 246 247 248 249 250 251 252 253 254 255 | |
jump_simulate(mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, n_samples, rng=None)
¶
Simulate returns under the jump-diffusion model. Original: jump_simulate.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
229 230 231 232 233 234 235 236 237 238 239 240 241 | |
jump_skewness(mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt)
¶
Mean/std/skewness of univariate returns under the jump-diffusion model.
Original: jump_skewness.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
258 259 260 261 262 263 264 265 266 267 268 | |
jump_skewness_portfolio(x, mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt)
¶
Mean/std/skewness of a portfolio's return under the jump-diffusion model.
Original: jump_skewness_portfolio.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
jump_to_mixture_params(mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt)
¶
Convert jump-diffusion parameters (diffusion mean/cov, jump intensity/mean/cov, time step) into the equivalent 2-component Gaussian mixture parameterization.
sigma_bar/sigma_tilde may be given as covariance matrices (for the multivariate case) or as scalar standard deviations (for the univariate skewness/thresholding helpers, which take sigma1/sigma2 directly rather than full covariance matrices).
Original: the parameter-transform preamble shared by every jump_*.m function (see module docstring)
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
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 | |
jump_univariate_thresholding(mu_bar, sigma_bar, mu_tilde, sigma_tilde, lambda_, dt, pi2_star)
¶
Univariate jump-regime classification thresholds. Original: jump_univariate_thresholding.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
lognormal_moments(mu, sigma)
¶
Mean, std dev, skewness, and excess kurtosis of exp(N(mu, sigma^2)) (a lognormal random variable), via its raw (uncentered) moments.
Original: mixture/lognormal_moments.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
lognormal_skewness(mu, sigma)
¶
Skewness of a lognormal random variable (closed form, in terms of sigma only).
Original: mixture/lognormal_skewness.m
Source code in src/quanttoolbox/mixtures/jump_diffusion.py
327 328 329 330 331 332 333 334 | |