ArXiv β Live Backtest
in Minutes
Regime-switching HMM, Rust Differential Evolution, CARA portfolio optimisation β full reproducible research stack. Jupyter-native. DuckDB-native. Rust-native.
Request Early AccessFrom raw market data to a deployed strategy β one Docker stack
mcmc_sample Rust binding.backtest_with_costs_rust call.Identify Bull / Bear / Sideways regimes in real time with Rust Baum-Welch
The model has hidden states $Q = \{q_1,\ldots,q_K\}$ (market regimes) emitting observable log-returns $O = \{r_1,\ldots,r_T\}$ drawn from regime-conditional Gaussians:
The Viterbi algorithm recovers the most likely hidden path:
Live fit on SPY/QQQ/IWM/EEM/EFA/GLD/TLT/VNQ (Jan 2024 β Mar 2026):
Regime-conditional daily returns (annualised): Bull +29% Sideways +8% Bear -12%
Global optimizer for non-convex utility landscapes β Rust-native DE from Optimiz-R
The CARA (Constant Absolute Risk Aversion) utility yields closed-form weights for a Gaussian regime:
For constrained problems β long-only, leverage cap, or non-Gaussian regimes β
HFThot uses Differential Evolution
from the optimiz-r crate. The DE mutation operator:
# advanced_portfolio_optimization.ipynb β HMM + CARA
import hft_lab_core as hft
# ββ 1. Fit HMM (Rust Baum-Welch, 3 regimes) ββ
hmm_params = hft.fit_hmm(returns.values, n_states=3)
state_seq = hft.viterbi_decode(returns.values, hmm_params)
# ββ 2. Regime-conditional ΞΌ / Ξ£ ββββββββββββββ
regime_portfolios = {}
for k in range(3):
mask = (state_seq == k)
mu_k = returns[mask].mean().values * 252 # annualised
cov_k = returns[mask].cov().values * 252
# ββ CARA closed-form ββββββββββββββββββββββ
gamma = {"Bear": 5.0, "Sideways": 2.0, "Bull": 1.0}[regime_names[k]]
w = hft.cara_optimal_weights_rust(mu_k, cov_k, gamma=gamma)
# ββ DE for constrained optimisation βββββββ
from optimizr import DifferentialEvolution
de = DifferentialEvolution(pop_size=60, F=0.8, CR=0.9, max_iter=200)
w_constrained = de.optimize(
objective=lambda w: -sharpe(mu_k, cov_k, w),
bounds=[(β0.3, 1.0)] * len(mu_k),
constraint=lambda w: np.sum(w) == 1.0
)
regime_portfolios[regime_names[k]] = w_constrained
522-day backtest Β· 8 ETFs Β· 10 bps transaction cost Β· Rust engine
β‘ Rust Performance (vs pure Python)
Every component is open-source or in the HFThot toolkit
docker-compose up β Jupyter + Streamlit + DuckDB + Rust ready- [1] Baum, Petrie, Soules & Weiss (1970) β A maximization technique occurring in the statistical analysis of probabilistic functions of Markov chains. Annals of Mathematical Statistics.
- [2] Rabiner (1989) β A tutorial on hidden Markov models and selected applications in speech recognition. Proc. IEEE 77(2).
- [3] Storn & Price (1997) β Differential Evolution β a simple and efficient heuristic for global optimization over continuous spaces. Journal of Global Optimization.
- [4] Merton (1971) β Optimum consumption and portfolio rules in a continuous-time model. Journal of Economic Theory 3(4).
- [5] Pola (2016) β A Higher Criticism approach to detecting regime changes. Journal of Finance and Data Science.