-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquant_math.py
More file actions
835 lines (663 loc) · 27.7 KB
/
quant_math.py
File metadata and controls
835 lines (663 loc) · 27.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
"""
===============================================================================
ADVANCED QUANTITATIVE MATHEMATICS MODULE
===============================================================================
Implementaciones de matemática cuantitativa avanzada:
1. Fractional Differentiation (López de Prado)
- Transforma series no-estacionarias en estacionarias
- PRESERVA memoria (a diferencia de diff(1) que la destruye)
2. Gaussian HMM (implementación propia sin hmmlearn)
- Baum-Welch algorithm (EM) para estimar parámetros
- Viterbi algorithm para secuencia de estados óptima
- 3 regímenes: Bull, Bear, Sideways
3. Wavelet Multi-Resolution Analysis
- Descomposición tiempo-frecuencia
- Extracción de tendencia eliminando ruido
4. Ornstein-Uhlenbeck Process
- Detección de mean-reversion speed (θ)
- Si θ alto → mean-reverting → usar señales contrarian
- Si θ bajo → trending → usar momentum
5. Kelly Criterion (óptimo y fraccional)
- Position sizing matemáticamente óptimo
- Ajuste por incertidumbre (half-Kelly, quarter-Kelly)
6. Hierarchical Risk Parity (HRP)
- Portfolio optimization que no requiere inversión de matriz de covarianza
- Robust a estimation error (vs Markowitz)
7. Bayesian Online Changepoint Detection (BOCD)
- Detecta cambios de régimen en tiempo real
- No requiere ventana fija (a diferencia de rolling stats)
===============================================================================
"""
import numpy as np
import pandas as pd
from scipy import stats as sp_stats
from scipy.cluster.hierarchy import linkage, leaves_list
from scipy.spatial.distance import squareform
import pywt
from arch import arch_model
import warnings
warnings.filterwarnings('ignore')
# =============================================================================
# 1. FRACTIONAL DIFFERENTIATION (Marcos López de Prado)
# =============================================================================
# "Advances in Financial Machine Learning" - Chapter 5
# La diferenciación fraccionaria permite hacer una serie estacionaria
# sin destruir toda la memoria de largo plazo.
# d=0: serie original (no estacionaria)
# d=1: diff completo (estacionaria pero sin memoria)
# d=0.3-0.5: sweet spot (estacionaria + memoria)
def get_weights_ffd(d, threshold=1e-5):
"""
Compute weights for Fixed-Width Window Fractional Differentiation.
w_k = -w_{k-1} * (d - k + 1) / k
Los pesos decaen como una serie de potencias, preservando
contribución de observaciones pasadas.
"""
w = [1.]
k = 1
while True:
w_ = -w[-1] * (d - k + 1) / k
if abs(w_) < threshold:
break
w.append(w_)
k += 1
return np.array(w[::-1]).reshape(-1, 1)
def frac_diff_ffd(series, d, threshold=1e-5):
"""
Apply Fractionally Differentiated Features (FFD).
Y_t = Σ_{k=0}^{K} w_k * X_{t-k}
donde w_k son los pesos de la diferenciación fraccionaria de orden d.
Args:
series: pd.Series de precios
d: orden de diferenciación (0 < d < 1)
threshold: peso mínimo para truncar
Returns:
pd.Series fraccionalmente diferenciada
"""
w = get_weights_ffd(d, threshold)
width = len(w) - 1
if width >= len(series):
return pd.Series(np.nan, index=series.index)
result = pd.Series(index=series.index, dtype=float)
vals = series.values
for i in range(width, len(vals)):
window = vals[i - width:i + 1].reshape(-1, 1)
result.iloc[i] = np.dot(w.T, window)[0, 0]
return result
def find_min_ffd(series, max_d=1.0, step=0.05, p_threshold=0.05):
"""
Find minimum d that makes the series stationary (ADF test).
This is the optimal d: maximum memory preservation with stationarity.
"""
from statsmodels.tsa.stattools import adfuller
for d in np.arange(0, max_d + step, step):
ffd = frac_diff_ffd(series, d)
ffd_clean = ffd.dropna()
if len(ffd_clean) < 50:
continue
try:
adf_stat, p_value, _, _, _, _ = adfuller(ffd_clean, maxlag=1)
if p_value < p_threshold:
return d, p_value, adf_stat
except:
continue
return max_d, 1.0, 0
# =============================================================================
# 2. GAUSSIAN HMM (Hidden Markov Model)
# =============================================================================
# Implementación manual usando Expectation-Maximization (Baum-Welch)
# 3 estados: Bull (high return, low vol), Bear (neg return, high vol), Sideways
class GaussianHMM:
"""
Hidden Markov Model con emisiones Gaussianas.
Modelo generativo:
z_t ~ Categorical(A[z_{t-1}, :]) # Transición de estado
x_t ~ N(μ_{z_t}, σ²_{z_t}) # Emisión observada
Parámetros:
π: distribución inicial de estados
A: matriz de transición (K x K)
μ: medias de emisión (K,)
σ: desviaciones estándar de emisión (K,)
Estimación via EM (Baum-Welch):
E-step: Forward-Backward algorithm
M-step: Re-estimación de parámetros
"""
def __init__(self, n_states=3, n_iter=50, tol=1e-4):
self.n_states = n_states
self.n_iter = n_iter
self.tol = tol
self.pi = None # Initial state distribution
self.A = None # Transition matrix
self.means = None # Emission means
self.stds = None # Emission stds
def _init_params(self, X):
"""Initialize parameters using k-means-like heuristic."""
K = self.n_states
n = len(X)
# Sort data and split into K quantiles for initial means/stds
sorted_x = np.sort(X)
chunk = n // K
self.means = np.array([sorted_x[i * chunk:(i + 1) * chunk].mean() for i in range(K)])
self.stds = np.array([max(sorted_x[i * chunk:(i + 1) * chunk].std(), 1e-6) for i in range(K)])
# Uniform initial and slightly sticky transition
self.pi = np.ones(K) / K
self.A = np.full((K, K), 0.1 / (K - 1))
np.fill_diagonal(self.A, 0.9) # States are sticky
self.A /= self.A.sum(axis=1, keepdims=True)
def _emission_prob(self, X):
"""
P(x_t | z_t = k) = N(x_t; μ_k, σ_k²)
Returns: (T, K) matrix of emission probabilities
"""
T = len(X)
K = self.n_states
B = np.zeros((T, K))
for k in range(K):
B[:, k] = sp_stats.norm.pdf(X, self.means[k], self.stds[k])
# Floor to avoid underflow
B = np.maximum(B, 1e-300)
return B
def _forward(self, B):
"""
Forward algorithm (α-pass).
α_t(k) = P(x_1, ..., x_t, z_t = k)
Uses log-scaling for numerical stability.
Returns: alpha (T, K), scale_factors (T,)
"""
T, K = B.shape
alpha = np.zeros((T, K))
scale = np.zeros(T)
alpha[0] = self.pi * B[0]
scale[0] = alpha[0].sum()
alpha[0] /= max(scale[0], 1e-300)
for t in range(1, T):
alpha[t] = (alpha[t - 1] @ self.A) * B[t]
scale[t] = alpha[t].sum()
alpha[t] /= max(scale[t], 1e-300)
return alpha, scale
def _backward(self, B, scale):
"""
Backward algorithm (β-pass).
β_t(k) = P(x_{t+1}, ..., x_T | z_t = k)
"""
T, K = B.shape
beta = np.zeros((T, K))
beta[-1] = 1.0
for t in range(T - 2, -1, -1):
beta[t] = (self.A @ (B[t + 1] * beta[t + 1]))
beta[t] /= max(scale[t + 1], 1e-300)
return beta
def fit(self, X):
"""
Baum-Welch (EM) algorithm.
E-step: Compute γ_t(k) = P(z_t=k | X) and ξ_t(i,j) = P(z_t=i, z_{t+1}=j | X)
M-step: Update π, A, μ, σ using sufficient statistics
"""
X = np.asarray(X, dtype=float)
self._init_params(X)
T = len(X)
K = self.n_states
prev_ll = -np.inf
for iteration in range(self.n_iter):
B = self._emission_prob(X)
# E-step
alpha, scale = self._forward(B)
beta = self._backward(B, scale)
# γ_t(k) = P(z_t = k | X_1:T)
gamma = alpha * beta
gamma /= gamma.sum(axis=1, keepdims=True).clip(1e-300)
# ξ_t(i,j) = P(z_t=i, z_{t+1}=j | X_1:T)
xi = np.zeros((T - 1, K, K))
for t in range(T - 1):
numerator = np.outer(alpha[t], B[t + 1] * beta[t + 1]) * self.A
denom = numerator.sum()
xi[t] = numerator / max(denom, 1e-300)
# M-step
self.pi = gamma[0] / gamma[0].sum()
for i in range(K):
for j in range(K):
self.A[i, j] = xi[:, i, j].sum() / max(gamma[:-1, i].sum(), 1e-300)
self.A /= self.A.sum(axis=1, keepdims=True).clip(1e-300)
for k in range(K):
wk = gamma[:, k]
wk_sum = wk.sum()
if wk_sum > 1e-10:
self.means[k] = (wk * X).sum() / wk_sum
self.stds[k] = max(np.sqrt((wk * (X - self.means[k]) ** 2).sum() / wk_sum), 1e-6)
# Log-likelihood check
ll = np.sum(np.log(np.maximum(scale, 1e-300)))
if abs(ll - prev_ll) < self.tol:
break
prev_ll = ll
# Sort states by mean (state 0 = bear, state K-1 = bull)
order = np.argsort(self.means)
self.means = self.means[order]
self.stds = self.stds[order]
self.A = self.A[order][:, order]
self.pi = self.pi[order]
return self
def predict_proba(self, X):
"""Return posterior state probabilities γ_t(k)."""
X = np.asarray(X, dtype=float)
B = self._emission_prob(X)
alpha, scale = self._forward(B)
beta = self._backward(B, scale)
gamma = alpha * beta
gamma /= gamma.sum(axis=1, keepdims=True).clip(1e-300)
return gamma
def predict(self, X):
"""Return most likely state sequence (Viterbi-like via argmax gamma)."""
gamma = self.predict_proba(X)
return np.argmax(gamma, axis=1)
# =============================================================================
# 3. WAVELET MULTI-RESOLUTION ANALYSIS
# =============================================================================
# Descompone la señal de precio en componentes de diferentes frecuencias.
# Reconstruye solo las componentes de baja frecuencia = tendencia suavizada.
def wavelet_trend(prices, wavelet='db4', level=4, keep_levels=None):
"""
Wavelet decomposition para extraer tendencia.
La Discrete Wavelet Transform descompone:
X = A_L + D_L + D_{L-1} + ... + D_1
donde:
A_L = approximation (tendencia de largo plazo)
D_j = detail en nivel j (ruido de alta frecuencia)
Para extraer tendencia: reconstruir solo A_L (y opcionalmente D_L)
Args:
prices: serie de precios
wavelet: familia de wavelet (db4 = Daubechies-4)
level: nivel de descomposición
keep_levels: cuántos niveles de detail mantener (0=solo approx)
"""
if keep_levels is None:
keep_levels = 1 # Keep approximation + 1 detail level
values = prices.values.copy()
n = len(values)
# Pad to power of 2 if needed
pad_len = 0
if n < 2 ** level:
level = max(1, int(np.log2(n)) - 1)
# Decompose
coeffs = pywt.wavedec(values, wavelet, level=level)
# Zero out high-frequency detail coefficients
for i in range(keep_levels + 1, len(coeffs)):
coeffs[i] = np.zeros_like(coeffs[i])
# Reconstruct
reconstructed = pywt.waverec(coeffs, wavelet)[:n]
return pd.Series(reconstructed, index=prices.index)
def wavelet_momentum(prices, wavelet='db4', level=4):
"""
Compute momentum from wavelet-denoised prices.
Much smoother than raw momentum — fewer false signals.
"""
trend = wavelet_trend(prices, wavelet, level, keep_levels=1)
# Momentum = rate of change of wavelet trend
mom = np.log(trend / trend.shift(1))
return mom, trend
# =============================================================================
# 4. ORNSTEIN-UHLENBECK PROCESS
# =============================================================================
# dX_t = θ(μ - X_t)dt + σ dW_t
#
# θ = speed of mean reversion
# μ = long-term mean
# σ = volatility
#
# Si θ alto → serie es mean-reverting → usar señales contrarian
# Si θ ≈ 0 → serie es random walk → momentum puede funcionar
# Half-life = ln(2) / θ
def estimate_ou_params(series, dt=1.0):
"""
Estimate Ornstein-Uhlenbeck parameters via OLS regression.
Discretizado: X_{t+1} - X_t = θ(μ - X_t)Δt + σ√Δt ε_t
Regresión: ΔX_t = a + b*X_t + ε_t
Entonces: θ = -b/Δt, μ = -a/b, σ = std(ε) / √Δt
"""
x = series.dropna().values
if len(x) < 10:
return {'theta': 0, 'mu': 0, 'sigma': 0, 'half_life': np.inf}
dx = np.diff(x)
x_lag = x[:-1]
# OLS: dx = a + b * x_lag
X = np.column_stack([np.ones(len(x_lag)), x_lag])
try:
beta = np.linalg.lstsq(X, dx, rcond=None)[0]
except:
return {'theta': 0, 'mu': 0, 'sigma': 0, 'half_life': np.inf}
a, b = beta
if b >= 0: # No mean reversion
return {'theta': 0, 'mu': x.mean(), 'sigma': dx.std(), 'half_life': np.inf}
theta = -b / dt
mu = -a / b
residuals = dx - (a + b * x_lag)
sigma = residuals.std() / np.sqrt(dt)
half_life = np.log(2) / theta if theta > 0 else np.inf
return {
'theta': theta,
'mu': mu,
'sigma': sigma,
'half_life': half_life
}
def ou_regime_signal(prices, window=60):
"""
Rolling OU estimation to determine regime:
- half_life < 10 days → mean-reverting → contrarian signal
- half_life > 30 days → trending → momentum signal
- between → mixed regime
"""
log_prices = np.log(prices)
n = len(prices)
regime = pd.Series(0.5, index=prices.index) # 0=mean-rev, 1=trending
half_lives = pd.Series(np.nan, index=prices.index)
for i in range(window, n):
segment = log_prices.iloc[i - window:i]
params = estimate_ou_params(segment)
hl = params['half_life']
half_lives.iloc[i] = hl
if hl < 10:
regime.iloc[i] = 0.0 # Mean-reverting
elif hl > 30:
regime.iloc[i] = 1.0 # Trending
else:
regime.iloc[i] = (hl - 10) / 20 # Linear interpolation
return regime, half_lives
# =============================================================================
# 5. KELLY CRITERION
# =============================================================================
# f* = (p * b - q) / b = p - q/b
# donde: p = prob of win, q = 1-p, b = win/loss ratio
#
# Para retornos continuos:
# f* = μ / σ²
#
# En la práctica se usa fractional Kelly (half-Kelly o quarter-Kelly)
# para ser más conservador.
def kelly_fraction(returns, window=60, fraction=0.25):
"""
Rolling Kelly fraction for position sizing.
Full Kelly: f* = μ / σ²
Fractional Kelly: f = fraction * f*
Quarter-Kelly es estándar en hedge funds porque:
1. Reduce drawdown ~75% vs full Kelly
2. Aún captura ~50% del crecimiento óptimo
3. Es robust a estimation error en μ y σ
"""
mu = returns.rolling(window).mean() * 252 # Annualized
var = returns.rolling(window).var() * 252 # Annualized
kelly_full = mu / var.clip(lower=1e-6)
# Fractional Kelly with caps
kelly_frac = (kelly_full * fraction).clip(-2, 2)
return kelly_frac
def kelly_from_win_rate(win_rate, avg_win, avg_loss, fraction=0.25):
"""
Discrete Kelly: f* = (p*b - q) / b
Calibrado con datos reales del CSV:
p = 0.4323, avg_win = 28.73, avg_loss = 14.80
b = 28.73 / 14.80 = 1.94
f* = (0.4323 * 1.94 - 0.5677) / 1.94 = 0.1399
Half-Kelly = 0.07, Quarter-Kelly = 0.035
"""
p = win_rate
q = 1 - p
b = abs(avg_win / avg_loss)
kelly_full = (p * b - q) / b
return kelly_full * fraction
# =============================================================================
# 6. HIERARCHICAL RISK PARITY (HRP)
# =============================================================================
# López de Prado (2016): "Building Diversified Portfolios that Outperform
# Out-of-Sample"
#
# HRP no requiere inversión de la matriz de covarianza (a diferencia de
# Markowitz). Es más robust a estimation error.
#
# Pasos:
# 1. Tree clustering: organiza activos por correlación
# 2. Quasi-diagonalization: reordena la matriz de covarianza
# 3. Recursive bisection: asigna pesos top-down
def hrp_weights(returns_df, method='single'):
"""
Hierarchical Risk Parity portfolio weights.
1. Compute correlation and distance matrices
2. Hierarchical clustering
3. Quasi-diagonalization
4. Recursive bisection for weight allocation
Returns: dict of asset -> weight
"""
cov = returns_df.cov()
corr = returns_df.corr()
# Distance matrix: d(i,j) = sqrt(0.5 * (1 - ρ(i,j)))
dist = np.sqrt(0.5 * (1 - corr))
dist_condensed = squareform(dist.values, checks=False)
# Hierarchical clustering
link = linkage(dist_condensed, method=method)
sort_ix = leaves_list(link)
sorted_assets = [returns_df.columns[i] for i in sort_ix]
# Recursive bisection
def _get_cluster_var(cov_matrix, assets):
"""Inverse-variance of a cluster."""
cov_slice = cov_matrix.loc[assets, assets]
ivp = 1.0 / np.diag(cov_slice.values)
ivp /= ivp.sum()
return np.dot(ivp, np.dot(cov_slice.values, ivp))
def _recursive_bisection(cov_matrix, sorted_assets):
"""Top-down allocation via recursive bisection."""
w = pd.Series(1.0, index=sorted_assets)
clusters = [sorted_assets]
while len(clusters) > 0:
new_clusters = []
for cluster in clusters:
if len(cluster) <= 1:
continue
mid = len(cluster) // 2
left = cluster[:mid]
right = cluster[mid:]
var_left = _get_cluster_var(cov_matrix, left)
var_right = _get_cluster_var(cov_matrix, right)
alpha = 1 - var_left / (var_left + var_right)
w[left] *= alpha
w[right] *= (1 - alpha)
new_clusters.extend([left, right])
clusters = [c for c in new_clusters if len(c) > 1]
return w
weights = _recursive_bisection(cov, sorted_assets)
weights /= weights.sum()
return weights.to_dict()
# =============================================================================
# 7. BAYESIAN ONLINE CHANGEPOINT DETECTION (BOCD)
# =============================================================================
# Adams & MacKay (2007): "Bayesian Online Changepoint Detection"
#
# Detecta cambios de régimen en tiempo real sin ventana fija.
# Mantiene una distribución sobre "run lengths" r_t (tiempo desde
# el último changepoint).
#
# P(r_t | x_1:t) ∝ P(x_t | r_t, x_...) * P(r_t | r_{t-1}) * P(r_{t-1} | x_1:{t-1})
class BayesianChangepoint:
"""
Simplified BOCD with Normal-Inverse-Gamma prior.
Prior: μ ~ N(μ_0, σ²/κ_0), σ² ~ InvGamma(α_0, β_0)
Posterior predictive: Student-t distribution
Hazard function: H(r) = 1/λ (constant hazard = geometric prior on run length)
"""
def __init__(self, hazard_lambda=200, mu_0=0, kappa_0=1, alpha_0=1, beta_0=1):
self.hazard = 1.0 / hazard_lambda
self.mu_0 = mu_0
self.kappa_0 = kappa_0
self.alpha_0 = alpha_0
self.beta_0 = beta_0
def _student_t_pdf(self, x, mu, var, nu):
"""Student-t predictive density."""
return sp_stats.t.pdf(x, df=nu, loc=mu, scale=np.sqrt(var))
def detect(self, data):
"""
Run BOCD on data series.
Returns:
changepoint_prob: P(changepoint at t) for each t
run_lengths: most likely run length at each t
"""
T = len(data)
X = np.asarray(data, dtype=float)
# Run length probabilities: R[t, r] = P(r_t = r | x_1:t)
max_rl = min(T, 300) # Cap run length for memory
R = np.zeros((T + 1, max_rl + 1))
R[0, 0] = 1.0
# Sufficient statistics for each run length
mu_params = np.full(max_rl + 1, self.mu_0)
kappa_params = np.full(max_rl + 1, self.kappa_0)
alpha_params = np.full(max_rl + 1, self.alpha_0)
beta_params = np.full(max_rl + 1, self.beta_0)
changepoint_prob = np.zeros(T)
most_likely_rl = np.zeros(T)
for t in range(T):
x = X[t]
# Predictive probabilities for each run length
pred_mu = mu_params[:max_rl + 1]
pred_var = beta_params[:max_rl + 1] * (kappa_params[:max_rl + 1] + 1) / \
(alpha_params[:max_rl + 1] * kappa_params[:max_rl + 1])
pred_nu = 2 * alpha_params[:max_rl + 1]
pred_prob = np.zeros(max_rl + 1)
for r in range(min(t + 1, max_rl + 1)):
if pred_nu[r] > 0 and pred_var[r] > 0:
pred_prob[r] = self._student_t_pdf(x, pred_mu[r], pred_var[r], pred_nu[r])
# Growth probabilities
R[t + 1, 1:min(t + 2, max_rl + 1)] = R[t, :min(t + 1, max_rl)] * \
pred_prob[:min(t + 1, max_rl)] * (1 - self.hazard)
# Changepoint probability
R[t + 1, 0] = (R[t, :min(t + 1, max_rl + 1)] * pred_prob[:min(t + 1, max_rl + 1)] * self.hazard).sum()
# Normalize
evidence = R[t + 1, :].sum()
if evidence > 0:
R[t + 1, :] /= evidence
changepoint_prob[t] = R[t + 1, 0]
most_likely_rl[t] = np.argmax(R[t + 1, :])
# Update sufficient statistics
new_mu = np.copy(mu_params)
new_kappa = np.copy(kappa_params)
new_alpha = np.copy(alpha_params)
new_beta = np.copy(beta_params)
for r in range(min(t + 1, max_rl), 0, -1):
new_kappa[r] = kappa_params[r - 1] + 1
new_mu[r] = (kappa_params[r - 1] * mu_params[r - 1] + x) / new_kappa[r]
new_alpha[r] = alpha_params[r - 1] + 0.5
new_beta[r] = beta_params[r - 1] + \
kappa_params[r - 1] * (x - mu_params[r - 1]) ** 2 / (2 * new_kappa[r])
# Reset r=0 to prior
new_mu[0] = self.mu_0
new_kappa[0] = self.kappa_0
new_alpha[0] = self.alpha_0
new_beta[0] = self.beta_0
mu_params = new_mu
kappa_params = new_kappa
alpha_params = new_alpha
beta_params = new_beta
return pd.Series(changepoint_prob, index=data.index if hasattr(data, 'index') else range(T)), \
pd.Series(most_likely_rl, index=data.index if hasattr(data, 'index') else range(T))
# =============================================================================
# 8. GARCH-IN-MEAN with EGARCH
# =============================================================================
def fit_garch(returns, p=1, q=1, dist='skewt'):
"""
Fit GARCH(p,q) with skewed Student-t distribution.
Returns conditional volatility series.
"""
try:
model = arch_model(returns * 100, vol='GARCH', p=p, q=q, dist=dist)
res = model.fit(disp='off')
cond_vol = res.conditional_volatility / 100
return cond_vol, res.params
except:
# Fallback to normal dist
try:
model = arch_model(returns * 100, vol='GARCH', p=p, q=q, dist='normal')
res = model.fit(disp='off')
cond_vol = res.conditional_volatility / 100
return cond_vol, res.params
except:
return returns.rolling(20).std(), {}
# =============================================================================
# UTILITY: Combine all advanced signals
# =============================================================================
def generate_advanced_signals(prices, returns, asset_name='BTC', csv_params=None):
"""
Generate trading signals using all advanced mathematical methods.
Returns a dict of named signals and a blended composite signal.
"""
n = len(prices)
result = {}
# 1. Fractional Differentiation
d_opt, _, _ = find_min_ffd(np.log(prices), max_d=0.6, step=0.1)
ffd_series = frac_diff_ffd(np.log(prices), max(d_opt, 0.3))
ffd_z = (ffd_series - ffd_series.rolling(40).mean()) / (ffd_series.rolling(40).std() + 1e-10)
result['ffd_signal'] = ffd_z.clip(-2, 2)
result['ffd_d'] = d_opt
# 2. HMM Regime
hmm = GaussianHMM(n_states=3, n_iter=30)
ret_clean = returns.dropna().values
if len(ret_clean) > 100:
hmm.fit(ret_clean[-min(500, len(ret_clean)):])
proba = hmm.predict_proba(returns.fillna(0).values)
# State 0=bear, 1=sideways, 2=bull
bull_prob = pd.Series(proba[:, -1], index=prices.index)
bear_prob = pd.Series(proba[:, 0], index=prices.index)
hmm_signal = (bull_prob - bear_prob) # -1 to +1
result['hmm_signal'] = hmm_signal
result['hmm_regime'] = pd.Series(np.argmax(proba, axis=1), index=prices.index)
else:
result['hmm_signal'] = pd.Series(0, index=prices.index)
result['hmm_regime'] = pd.Series(1, index=prices.index)
# 3. Wavelet momentum
wv_mom, wv_trend = wavelet_momentum(prices)
wv_mom_z = (wv_mom - wv_mom.rolling(40).mean()) / (wv_mom.rolling(40).std() + 1e-10)
result['wavelet_signal'] = wv_mom_z.clip(-2, 2)
result['wavelet_trend'] = wv_trend
# 4. OU regime
ou_regime, ou_hl = ou_regime_signal(prices, window=60)
result['ou_regime'] = ou_regime # 0=mean-rev, 1=trending
result['ou_half_life'] = ou_hl
# 5. BOCD changepoint
bocd = BayesianChangepoint(hazard_lambda=100)
cp_prob, _ = bocd.detect(returns.fillna(0).values)
result['changepoint_prob'] = cp_prob
# 6. Kelly fraction
kelly = kelly_fraction(returns, window=60, fraction=0.25)
result['kelly_fraction'] = kelly
# 7. GARCH conditional volatility
cond_vol, garch_params = fit_garch(returns.dropna())
if len(cond_vol) == len(returns.dropna()):
result['garch_vol'] = pd.Series(cond_vol.values, index=returns.dropna().index).reindex(prices.index)
else:
result['garch_vol'] = returns.rolling(20).std()
return result
if __name__ == '__main__':
# Quick test
np.random.seed(42)
n = 500
# Simulate: trending + mean-reverting regimes
prices_sim = pd.Series(np.cumsum(np.random.randn(n) * 0.02) + 100,
index=pd.date_range('2020-01-01', periods=n))
returns_sim = np.log(prices_sim / prices_sim.shift(1))
print("Testing Fractional Differentiation...")
d, p, adf = find_min_ffd(np.log(prices_sim))
print(f" Optimal d = {d:.2f}, ADF p-value = {p:.4f}")
print("\nTesting HMM...")
hmm = GaussianHMM(n_states=3, n_iter=20)
hmm.fit(returns_sim.dropna().values)
states = hmm.predict(returns_sim.dropna().values)
print(f" States distribution: {np.bincount(states)}")
print(f" Means: {hmm.means}")
print("\nTesting Wavelet...")
wm, wt = wavelet_momentum(prices_sim)
print(f" Wavelet trend range: [{wt.min():.2f}, {wt.max():.2f}]")
print("\nTesting OU Process...")
params = estimate_ou_params(np.log(prices_sim))
print(f" theta={params['theta']:.4f}, half_life={params['half_life']:.1f} days")
print("\nTesting Kelly Criterion...")
kf = kelly_from_win_rate(0.4323, 28.73, -14.80, fraction=0.25)
print(f" Quarter-Kelly from CSV params: {kf:.4f} ({kf*100:.2f}% of capital)")
print("\nTesting BOCD...")
bocd = BayesianChangepoint(hazard_lambda=100)
cp, rl = bocd.detect(returns_sim.dropna())
print(f" Max changepoint prob: {cp.max():.4f}")
print(f" Changepoints detected (>0.3): {(cp > 0.3).sum()}")
print("\nAll tests passed!")