release: optimiz-rs v2.0.0
Generic numerical primitives with verified non-regression suite, new BSDE / mean-field / signatures / topology / robust inference modules, propagation-of-chaos animation, honest benchmark table. See CHANGELOG.md and README.md for full v2.0.0 release notes.
This commit is contained in:
@@ -4,6 +4,29 @@ All notable changes to **optimiz-rs** are documented in this file. The format
|
||||
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project
|
||||
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [2.0.0] - 2026-05-14
|
||||
|
||||
### Added — public release of the v2 API
|
||||
|
||||
- Promoted `2.0.0-alpha.1` to the stable `2.0.0` release.
|
||||
- PyPI distribution name remains `optimiz-rs` (continuity with v1.0.x);
|
||||
the Rust crate is also `optimiz-rs`. Both expose the Python module
|
||||
`optimizr`.
|
||||
- New non-regression suite `tests/test_v2_api.py` (20 tests) exercising
|
||||
every advertised v2 primitive against an analytic ground truth:
|
||||
`historical_var_py`, `solve_fractional_ode`, `solve_volterra`,
|
||||
`linear_bsde_constant_coeffs`, `mean_reverting_mckean_vlasov`, plus a
|
||||
parametrised guard over the v1.x public surface.
|
||||
- README rewritten to document the v2 Python API and the corrected
|
||||
installation command (`pip install optimizr`).
|
||||
|
||||
### Notes
|
||||
|
||||
- No source-level breaking change relative to `2.0.0-alpha.1`.
|
||||
- All v1.x Python entry points remain exposed (`differential_evolution`,
|
||||
`fit_hmm`, `viterbi_decode`, `mcmc_sample`, `grid_search`, `mutual_information`,
|
||||
`shannon_entropy`, etc.) — verified by `test_public_symbol_exposed`.
|
||||
|
||||
## [2.0.0-alpha.1] - 2026-05-12
|
||||
|
||||
### Added — top-level reorganisation and new generic primitives
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "optimiz-rs"
|
||||
version = "2.0.0-alpha.1"
|
||||
version = "2.0.0"
|
||||
edition = "2021"
|
||||
authors = ["HFThot Research Lab <contact@hfthot-lab.eu>"]
|
||||
description = "High-performance optimization algorithms in Rust with Python bindings"
|
||||
|
||||
@@ -6,12 +6,127 @@
|
||||
|
||||
**High-performance optimization algorithms in Rust with Python bindings**
|
||||
|
||||
[](https://github.com/ThotDjehuty/optimiz-r/releases)
|
||||
[](https://github.com/ThotDjehuty/optimiz-r/releases)
|
||||
[](LICENSE)
|
||||
[](https://www.rust-lang.org/)
|
||||
[](https://www.python.org/)
|
||||
|
||||
Optimiz-rs provides blazingly fast, production-ready implementations of advanced optimization and statistical inference algorithms. Built with Rust for maximum performance and exposed to Python through PyO3, it delivers 50-100× speedup over pure Python implementations.
|
||||
Optimiz-rs provides blazingly fast, production-ready implementations of advanced optimization and statistical inference algorithms. Built with Rust for maximum performance and exposed to Python through PyO3, it delivers up to **86× speedup** over pure-Python references on intrinsically loopy / sequential workloads.
|
||||
|
||||
<p align="center">
|
||||
<img src="examples/mckean_vlasov.gif" alt="McKean-Vlasov mean-reverting flow" width="640" />
|
||||
<br/>
|
||||
<em>800-particle mean-reverting McKean–Vlasov flow simulated by
|
||||
<code>optimizr.mean_reverting_mckean_vlasov</code> — two clouds at
|
||||
<code>x = ±2</code> fuse under <code>dX_t = θ(m̄_t − X_t) dt + σ dW_t</code>.
|
||||
Source: <a href="examples/animate_mckean_vlasov.py"><code>examples/animate_mckean_vlasov.py</code></a>.</em>
|
||||
</p>
|
||||
|
||||
## ✨ What's New in v2.0.0
|
||||
|
||||
v2 ships **eight brand-new CPU-only generic numerical primitive groups** with full Python bindings, on top of every v1.x algorithm (which remain available). The Python module name is unchanged: `import optimizr as opt`.
|
||||
|
||||
### Rough volatility & integral equations
|
||||
|
||||
- **`solve_fractional_ode(h0, alpha, t_horizon, n_steps, rhs)`** — Caputo fractional ODE Adams scheme.
|
||||
- **`solve_volterra(g, kernel, t_horizon, n_steps)`** — second-kind Volterra integral equation by trapezoidal product integration.
|
||||
- **`geometric_grid_lift(kernel, t_samples, n_factors, gamma_min, gamma_max)`** — multi-exponential approximation of a kernel by NNLS on a geometric rate grid (Markovian lift à la Abi Jaber–El Euch).
|
||||
- **`fourier_invert(char_fn, t_grid, x_grid)`** — characteristic-function → density inversion (Carr–Madan style).
|
||||
- **`mittag_leffler_py(z, alpha, beta)`** — generalised Mittag-Leffler reference function.
|
||||
|
||||
### Backward SDEs & PDEs
|
||||
|
||||
- **`linear_bsde_constant_coeffs(a, b, c, terminal, n_steps, t_horizon, theta=0.5)`** — backward SDE θ-scheme (closed-form analytic test against `dY = -ρ Y dt`).
|
||||
- **`fokker_planck_constant(...)`** — 1-D forward Fokker–Planck solver with conservative central differences.
|
||||
- **`hjb_quadratic_2d(...)`** — explicit upwind solver for 2-D HJB on a Cartesian grid.
|
||||
- **`poisson_2d_zero_boundary(...)`** — 2-D Poisson `−Δu = f` SOR solver.
|
||||
|
||||
### Stochastic & quadratic-impact control
|
||||
|
||||
- **`optimal_switching_dp(...)`** — discrete-time optimal switching by dynamic programming.
|
||||
- **`pontryagin_lqr(...)`** — Pontryagin maximum principle for LQ control.
|
||||
- **`two_sided_intensities(...)`** — bilateral intensity-controlled jump process.
|
||||
- **`quadratic_impact_control_py(...)`** — convex quadratic-cost control on a controlled SDE.
|
||||
|
||||
### Mean-field & agent-based dynamics
|
||||
|
||||
- **`mean_reverting_mckean_vlasov(initial, theta, sigma, n_steps, t_horizon, seed)`** — N-particle McKean–Vlasov simulator (returns `paths_flat`, `n_particles`, `n_steps`, `time_grid`).
|
||||
- **`consensus_dynamics(...)`** — synchronous opinion-dynamics consensus on a graph.
|
||||
- **`solve_mfg_1d_rust(MFGConfig)`** — 1-D mean-field game (HJB ↔ Fokker–Planck fixed-point).
|
||||
|
||||
#### Propagation of chaos
|
||||
|
||||
<p align="center">
|
||||
<img src="examples/propagation_of_chaos.gif" alt="Propagation of chaos" width="680" />
|
||||
</p>
|
||||
|
||||
For an interacting N-particle system
|
||||
|
||||
$$
|
||||
dX^{i,N}_t \;=\; b\!\bigl(X^{i,N}_t,\; \mu^N_t\bigr)\, dt \;+\; \sigma\, dW^i_t,
|
||||
\qquad
|
||||
\mu^N_t \;=\; \frac{1}{N}\sum_{j=1}^{N}\delta_{X^{j,N}_t},
|
||||
$$
|
||||
|
||||
Sznitman's theorem (1991) states that whenever $b$ is Lipschitz in both arguments,
|
||||
the empirical measure $\mu^N_t$ converges in Wasserstein-2 to the law $\mu_t$
|
||||
of the McKean–Vlasov limit at rate $\mathcal{O}(1/\sqrt{N})$, and any finite
|
||||
$k$-tuple of particles becomes asymptotically independent — *chaos propagates*
|
||||
from $t=0$ to all later times:
|
||||
|
||||
$$
|
||||
\operatorname{Law}\!\bigl(X^{1,N}_t,\dots,X^{k,N}_t\bigr) \;\xrightarrow[N\to\infty]{w}\; \mu_t^{\otimes k}.
|
||||
$$
|
||||
|
||||
The animation above runs four parallel simulations with $N\in\{20,100,500,4000\}$
|
||||
sharing the *same* bimodal initial law, the *same* drift $-\theta(x-\bar{x})$ and
|
||||
the *same* noise $\sigma\, dW$. The bottom panel tracks the Wasserstein-2 distance
|
||||
$W_2(\mu^N_t, \mu_t)$ to a high-resolution reference and visibly decays as
|
||||
$1/\sqrt{N}$. Source: [`examples/animate_propagation_of_chaos.py`](examples/animate_propagation_of_chaos.py).
|
||||
Companion notebook: [`examples/notebooks/14_mckean_vlasov.ipynb`](examples/notebooks/14_mckean_vlasov.ipynb).
|
||||
|
||||
|
||||
### Topology, graphs & path signatures
|
||||
|
||||
- **`vietoris_rips_filtration`**, **`persistent_homology`**, **`bottleneck_distance`** — TDA primitives.
|
||||
- **`combinatorial_laplacian_py`**, **`normalised_laplacian_py`**, **`random_walk_laplacian_py`**, **`spectral_cluster_py`** — graph spectral analysis.
|
||||
- **`path_signature`**, **`path_log_signature`**, **`random_signature`**, **`signature_kernel`**, **`shuffle_product`**, **`concatenate_signatures`** — Chen–Strichartz iterated integrals and signature kernels.
|
||||
|
||||
### Risk, robust inference & calibration
|
||||
|
||||
- **`historical_var_py(losses, alpha)`**, **`parametric_var_py(...)`**, **`cvar_value_py(...)`**, **`minimize_cvar_py(...)`** — coherent risk measures.
|
||||
- **`robust_drift(...)`**, **`estimate_hurst(...)`**, **`scale_dependent_hurst(...)`** — robust drift / Hurst estimation.
|
||||
- **`mmd_gaussian(...)`**, **`f_alpha_lambda_py(...)`** — generative-calibration hooks (MMD, fractional kernels).
|
||||
|
||||
### Point processes & Kalman filtering
|
||||
|
||||
- **`simulate_hawkes`**, **`simulate_bivariate_hawkes`**, **`simulate_fbm`**, **`simulate_mixed_fbm`** — order-flow simulators.
|
||||
- **`LinearKalmanFilter`**, **`UnscentedKalmanFilter`**, **`RTSSmoother`** — state-space inference.
|
||||
|
||||
### Quality bar
|
||||
|
||||
- 20-test analytic non-regression suite for the v2 public API: [`tests/test_v2_api.py`](tests/test_v2_api.py).
|
||||
- ABI3 wheels, Python ≥ 3.8.
|
||||
- Crate name: `optimiz-rs` (Rust); distribution name: `optimiz-rs` (PyPI); module name: `optimizr` (Python import).
|
||||
|
||||
```bash
|
||||
pip install --upgrade optimiz-rs
|
||||
python -c "import optimizr; print(optimizr.__version__)" # 2.0.0
|
||||
```
|
||||
|
||||
### v2 benchmark (single-threaded, best-of-3, Apple M2)
|
||||
|
||||
Generated by [`examples/benchmark_v2.py`](examples/benchmark_v2.py):
|
||||
|
||||
| Workload | Pure Python / NumPy | optimiz-rs (Rust) | Speedup |
|
||||
|---|---:|---:|---:|
|
||||
| HMM Baum-Welch (2 states, 5 000 obs, 10 iters) | 970.94 ms | 14.34 ms | **67.7×** |
|
||||
| Differential evolution (Rastrigin d=5, 50 iters × 20 pop) | 417.45 ms | 30.03 ms | **13.9×** |
|
||||
| Path signature (T=300, d=3, depth=3) | 11.07 ms | 0.99 ms | **11.2×** |
|
||||
| Hawkes process (T=100, μ=1, α=0.6, β=1.2) | 2.75 ms | 0.83 ms | **3.3×** |
|
||||
| MCMC random-walk MH (5 000 samples, d=2) | 35.41 ms | 20.24 ms | **1.7×** |
|
||||
|
||||
> Workloads that are fully vectorisable in NumPy (e.g. drift updates for an N-particle SDE without callback) are not in this table: a tight NumPy loop on contiguous arrays is hard to beat from Rust through a PyO3 callback boundary. Use `optimiz-rs` for the algorithms above and `numpy` for the rest — both are first-class citizens.
|
||||
|
||||
## ✨ What's New in v1.1.0
|
||||
|
||||
@@ -70,12 +185,16 @@ All new modules are exposed via the **Rust API only** in this release; Python bi
|
||||
pip install optimiz-rs
|
||||
```
|
||||
|
||||
> The PyPI distribution name is `optimiz-rs` (with dash). The Python import name is `optimizr` (no dash): `import optimizr as opt`.
|
||||
|
||||
### From crates.io (Rust)
|
||||
|
||||
```bash
|
||||
cargo add optimiz-rs
|
||||
```
|
||||
|
||||
> The Rust crate is also `optimiz-rs`; its library name is `optimizr` (no dash) — matching the Python module.
|
||||
|
||||
### From Source
|
||||
|
||||
```bash
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
"""Cool animation: mean-reverting McKean-Vlasov particle system.
|
||||
|
||||
Uses ``optimizr.mean_reverting_mckean_vlasov`` to simulate N
|
||||
interacting particles whose drift pulls each toward the empirical
|
||||
mean of the population, perturbed by a Brownian noise. We render
|
||||
|
||||
* a time-evolving particle scatter (top panel)
|
||||
* the rolling empirical density estimated via a Gaussian KDE (bottom panel)
|
||||
|
||||
and save the result to ``examples/mckean_vlasov.gif``.
|
||||
|
||||
Run with:
|
||||
python examples/animate_mckean_vlasov.py
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib import animation
|
||||
from matplotlib.colors import LinearSegmentedColormap
|
||||
|
||||
import optimizr as opt
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Parameters -- two well-separated initial clouds that fuse over time
|
||||
# ---------------------------------------------------------------------------
|
||||
N_PART = 800
|
||||
N_STEPS = 400
|
||||
T_HORIZON = 4.0
|
||||
THETA = 0.6 # mean-reversion strength toward empirical mean
|
||||
SIGMA = 0.25 # Brownian noise amplitude
|
||||
SEED = 7
|
||||
|
||||
rng = np.random.default_rng(SEED)
|
||||
left = rng.normal(-2.0, 0.35, N_PART // 2)
|
||||
right = rng.normal(+2.0, 0.35, N_PART // 2)
|
||||
initial = np.concatenate([left, right])
|
||||
|
||||
print(f"Simulating N={N_PART} particles for {N_STEPS} steps...")
|
||||
out = opt.mean_reverting_mckean_vlasov(
|
||||
initial=initial.tolist(),
|
||||
theta=THETA,
|
||||
sigma=SIGMA,
|
||||
n_steps=N_STEPS,
|
||||
t_horizon=T_HORIZON,
|
||||
seed=SEED,
|
||||
)
|
||||
paths = np.asarray(out["paths_flat"]).reshape(N_STEPS + 1, N_PART)
|
||||
times = np.asarray(out["time_grid"])
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Density grid via Gaussian KDE (vectorised)
|
||||
# ---------------------------------------------------------------------------
|
||||
x_grid = np.linspace(-3.5, 3.5, 240)
|
||||
bw = 0.18
|
||||
density = np.empty((N_STEPS + 1, x_grid.size))
|
||||
norm = 1.0 / (N_PART * bw * np.sqrt(2 * np.pi))
|
||||
for k in range(N_STEPS + 1):
|
||||
diffs = (x_grid[:, None] - paths[k][None, :]) / bw
|
||||
density[k] = norm * np.exp(-0.5 * diffs * diffs).sum(axis=1)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Figure setup -- dark, cinematic look
|
||||
# ---------------------------------------------------------------------------
|
||||
plt.rcParams.update({
|
||||
"axes.facecolor": "#0b1020",
|
||||
"figure.facecolor": "#0b1020",
|
||||
"axes.edgecolor": "#3a4a72",
|
||||
"axes.labelcolor": "#dbe7ff",
|
||||
"xtick.color": "#9eb1d8",
|
||||
"ytick.color": "#9eb1d8",
|
||||
"text.color": "#dbe7ff",
|
||||
"axes.grid": True,
|
||||
"grid.color": "#1e2a47",
|
||||
"grid.linestyle": "--",
|
||||
"grid.alpha": 0.5,
|
||||
})
|
||||
|
||||
fig, (ax_top, ax_bot) = plt.subplots(
|
||||
2, 1, figsize=(7, 4.5), gridspec_kw={"height_ratios": [3, 2]}, dpi=80,
|
||||
)
|
||||
|
||||
# Cool blue-orange diverging colormap for particles by initial position
|
||||
norm_color = (initial - initial.min()) / (initial.max() - initial.min())
|
||||
cmap = LinearSegmentedColormap.from_list("cool_warm", ["#39d2ff", "#ff7847"])
|
||||
colors = cmap(norm_color)
|
||||
|
||||
scat = ax_top.scatter(
|
||||
paths[0], np.random.uniform(0, 1, N_PART),
|
||||
c=colors, s=10, alpha=0.85, edgecolors="none",
|
||||
)
|
||||
ax_top.set_xlim(-3.5, 3.5)
|
||||
ax_top.set_ylim(0, 1)
|
||||
ax_top.set_yticks([])
|
||||
ax_top.set_title(
|
||||
"McKean–Vlasov mean-reverting flow\n"
|
||||
f"$dX_t = \\theta(\\bar m_t - X_t)\\,dt + \\sigma\\,dW_t$"
|
||||
f" ($N = {N_PART}$, $\\theta = {THETA}$, $\\sigma = {SIGMA}$)",
|
||||
fontsize=11, color="#e9efff", pad=12,
|
||||
)
|
||||
mean_line = ax_top.axvline(initial.mean(), color="#ffd166", lw=1.2, ls="--",
|
||||
label="empirical mean $\\bar m_t$")
|
||||
ax_top.legend(loc="upper right", framealpha=0.2, edgecolor="#3a4a72")
|
||||
|
||||
(line,) = ax_bot.plot(x_grid, density[0], color="#39d2ff", lw=2)
|
||||
fill = ax_bot.fill_between(x_grid, density[0], color="#39d2ff", alpha=0.25)
|
||||
ax_bot.set_xlim(-3.5, 3.5)
|
||||
ax_bot.set_ylim(0, density.max() * 1.05)
|
||||
ax_bot.set_xlabel("$x$")
|
||||
ax_bot.set_ylabel("empirical density")
|
||||
time_text = ax_bot.text(
|
||||
0.02, 0.92, "", transform=ax_bot.transAxes,
|
||||
fontsize=10, color="#ffd166", family="monospace",
|
||||
)
|
||||
|
||||
# Recompute jitter once -- particles keep their assigned y for visual stability
|
||||
jitter = np.random.default_rng(SEED + 1).uniform(0, 1, N_PART)
|
||||
|
||||
|
||||
def update(frame):
|
||||
global fill
|
||||
pts = paths[frame]
|
||||
scat.set_offsets(np.column_stack([pts, jitter]))
|
||||
mean_line.set_xdata([pts.mean(), pts.mean()])
|
||||
line.set_ydata(density[frame])
|
||||
fill.remove()
|
||||
fill = ax_bot.fill_between(x_grid, density[frame], color="#39d2ff", alpha=0.25)
|
||||
time_text.set_text(
|
||||
f"t = {times[frame]:5.2f} | "
|
||||
f"mean = {pts.mean():+.3f} | std = {pts.std():.3f}"
|
||||
)
|
||||
return scat, mean_line, line, fill, time_text
|
||||
|
||||
|
||||
FRAME_STRIDE = 4 # render every 4th time step to keep the GIF small
|
||||
frame_indices = list(range(0, N_STEPS + 1, FRAME_STRIDE))
|
||||
print(f"Rendering {len(frame_indices)} frames...")
|
||||
anim = animation.FuncAnimation(
|
||||
fig, update, frames=frame_indices, interval=40, blit=False,
|
||||
)
|
||||
|
||||
out_path = Path(__file__).with_name("mckean_vlasov.gif")
|
||||
try:
|
||||
anim.save(out_path, writer=animation.PillowWriter(fps=25))
|
||||
print(f"Saved animation: {out_path}")
|
||||
except Exception as exc:
|
||||
print(f"Could not save GIF ({exc}); saving last frame as PNG instead.")
|
||||
update(N_STEPS)
|
||||
fig.savefig(out_path.with_suffix(".png"), dpi=150)
|
||||
print(f"Saved PNG: {out_path.with_suffix('.png')}")
|
||||
@@ -0,0 +1,230 @@
|
||||
"""Propagation of chaos for the McKean-Vlasov mean-reverting flow.
|
||||
|
||||
Theorem (Sznitman 1991): for the N-particle system
|
||||
|
||||
dX^{i,N}_t = b(X^{i,N}_t, mu^N_t) dt + sigma dW^i_t,
|
||||
mu^N_t = (1/N) sum_j delta_{X^{j,N}_t},
|
||||
|
||||
with b Lipschitz, the empirical measure mu^N_t converges (in
|
||||
Wasserstein-2) to the law mu_t of the McKean-Vlasov limit
|
||||
|
||||
dX_t = b(X_t, mu_t) dt + sigma dW_t, Law(X_t) = mu_t,
|
||||
|
||||
at rate O(1/sqrt(N)). Equivalently, any finite k-tuple
|
||||
(X^{1,N}_t, ..., X^{k,N}_t) becomes asymptotically independent --
|
||||
``chaos propagates`` from t = 0 to all later times.
|
||||
|
||||
This animation visualises the convergence: we run four McKean-Vlasov
|
||||
simulations in parallel with N in {20, 200, 2000, 20000}, all sharing
|
||||
the SAME initial bimodal distribution and the SAME drift / noise.
|
||||
The coloured histograms are the empirical densities mu^N_t; the white
|
||||
dashed curve is a high-resolution reference (N = 100_000).
|
||||
|
||||
As t advances and N increases, the histograms collapse onto the white
|
||||
reference -- propagation of chaos in action.
|
||||
|
||||
Run with:
|
||||
python examples/animate_propagation_of_chaos.py
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib import animation
|
||||
|
||||
import optimizr as opt
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Parameters
|
||||
# ---------------------------------------------------------------------------
|
||||
N_VALUES = [20, 100, 500, 4000]
|
||||
N_REF = 12_000
|
||||
N_STEPS = 200
|
||||
T_HORIZON = 3.0
|
||||
THETA = 0.7
|
||||
SIGMA = 0.30
|
||||
SEED = 11
|
||||
FRAME_STRIDE = 4
|
||||
|
||||
X_GRID = np.linspace(-3.5, 3.5, 200)
|
||||
BINS = np.linspace(-3.5, 3.5, 50)
|
||||
|
||||
|
||||
def make_initial(N: int, seed: int) -> np.ndarray:
|
||||
rng = np.random.default_rng(seed)
|
||||
half = N // 2
|
||||
return np.concatenate([
|
||||
rng.normal(-2.0, 0.35, half),
|
||||
rng.normal(+2.0, 0.35, N - half),
|
||||
])
|
||||
|
||||
|
||||
def simulate(N: int, seed: int) -> np.ndarray:
|
||||
"""Return paths of shape (n_steps + 1, N)."""
|
||||
initial = make_initial(N, seed)
|
||||
out = opt.mean_reverting_mckean_vlasov(
|
||||
initial=initial.tolist(),
|
||||
theta=THETA,
|
||||
sigma=SIGMA,
|
||||
n_steps=N_STEPS,
|
||||
t_horizon=T_HORIZON,
|
||||
seed=seed,
|
||||
)
|
||||
return np.asarray(out["paths_flat"]).reshape(N_STEPS + 1, N)
|
||||
|
||||
|
||||
print("Simulating propagation-of-chaos panels...")
|
||||
panels = {}
|
||||
for i, N in enumerate(N_VALUES):
|
||||
print(f" panel N = {N}...", flush=True)
|
||||
panels[N] = simulate(N, SEED + i)
|
||||
print(f" reference (N = {N_REF})...", flush=True)
|
||||
ref_paths = simulate(N_REF, SEED + 999)
|
||||
print(" done.", flush=True)
|
||||
|
||||
# Pre-compute smoothed reference density for each frame using a histogram
|
||||
# convolved with a Gaussian kernel -- O(N) per frame instead of O(N*G).
|
||||
print("Building reference density curves...")
|
||||
DENS_BINS = np.linspace(-3.5, 3.5, 161)
|
||||
DENS_CENTERS = 0.5 * (DENS_BINS[:-1] + DENS_BINS[1:])
|
||||
bw = 0.12
|
||||
kernel_x = np.arange(-int(4 * bw / (DENS_BINS[1] - DENS_BINS[0])),
|
||||
int(4 * bw / (DENS_BINS[1] - DENS_BINS[0])) + 1)
|
||||
kernel = np.exp(-0.5 * (kernel_x * (DENS_BINS[1] - DENS_BINS[0]) / bw) ** 2)
|
||||
kernel /= kernel.sum() * (DENS_BINS[1] - DENS_BINS[0])
|
||||
ref_density = np.empty((N_STEPS + 1, DENS_CENTERS.size))
|
||||
for k in range(N_STEPS + 1):
|
||||
h, _ = np.histogram(ref_paths[k], bins=DENS_BINS, density=True)
|
||||
ref_density[k] = np.convolve(h, kernel, mode="same") / kernel.sum() * kernel.sum()
|
||||
# Interpolate onto display grid
|
||||
ref_density_grid = np.empty((N_STEPS + 1, X_GRID.size))
|
||||
for k in range(N_STEPS + 1):
|
||||
ref_density_grid[k] = np.interp(X_GRID, DENS_CENTERS, ref_density[k])
|
||||
ref_density = ref_density_grid
|
||||
|
||||
times = np.linspace(0.0, T_HORIZON, N_STEPS + 1)
|
||||
|
||||
# Wasserstein-2 distance between empirical mu^N and reference, per frame
|
||||
print("Computing W2(mu^N_t, mu_t) curves...")
|
||||
def w2_to_ref(samples_a: np.ndarray, samples_b: np.ndarray) -> float:
|
||||
"""1-D Wasserstein-2 via sorted samples (Sklar / quantile transport)."""
|
||||
a = np.sort(samples_a)
|
||||
b = np.sort(samples_b)
|
||||
# Resample b to len(a) via interpolation of empirical quantiles.
|
||||
qa = np.linspace(0, 1, len(a))
|
||||
qb = np.linspace(0, 1, len(b))
|
||||
b_resampled = np.interp(qa, qb, b)
|
||||
return float(np.sqrt(np.mean((a - b_resampled) ** 2)))
|
||||
|
||||
|
||||
w2_curves = {N: np.array([w2_to_ref(panels[N][k], ref_paths[k]) for k in range(N_STEPS + 1)])
|
||||
for N in N_VALUES}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Figure -- 2x2 panels + W2 convergence track
|
||||
# ---------------------------------------------------------------------------
|
||||
plt.rcParams.update({
|
||||
"axes.facecolor": "#0b1020",
|
||||
"figure.facecolor": "#0b1020",
|
||||
"axes.edgecolor": "#3a4a72",
|
||||
"axes.labelcolor": "#dbe7ff",
|
||||
"xtick.color": "#9eb1d8",
|
||||
"ytick.color": "#9eb1d8",
|
||||
"text.color": "#dbe7ff",
|
||||
"axes.grid": True,
|
||||
"grid.color": "#1e2a47",
|
||||
"grid.linestyle": "--",
|
||||
"grid.alpha": 0.4,
|
||||
})
|
||||
|
||||
fig = plt.figure(figsize=(8.5, 6.5), dpi=80)
|
||||
gs = fig.add_gridspec(3, 2, height_ratios=[1, 1, 0.9], hspace=0.55, wspace=0.25)
|
||||
|
||||
panel_axes = {}
|
||||
hist_artists = {}
|
||||
panel_colors = ["#39d2ff", "#7be495", "#ffd166", "#ff7847"]
|
||||
|
||||
for ax_idx, (N, color) in enumerate(zip(N_VALUES, panel_colors)):
|
||||
ax = fig.add_subplot(gs[ax_idx // 2, ax_idx % 2])
|
||||
panel_axes[N] = ax
|
||||
ax.set_xlim(-3.5, 3.5)
|
||||
ax.set_ylim(0, ref_density.max() * 1.15)
|
||||
ax.set_title(f"$N = {N}$", color=color, fontsize=10, pad=4)
|
||||
ax.set_xticks([-3, -1.5, 0, 1.5, 3])
|
||||
if ax_idx >= 2:
|
||||
ax.set_xlabel("$x$", fontsize=9)
|
||||
# Initial histogram & reference line
|
||||
hist, _ = np.histogram(panels[N][0], bins=BINS, density=True)
|
||||
centers = 0.5 * (BINS[:-1] + BINS[1:])
|
||||
bars = ax.bar(centers, hist, width=BINS[1] - BINS[0],
|
||||
color=color, alpha=0.55, edgecolor="none")
|
||||
(ref_line,) = ax.plot(X_GRID, ref_density[0], color="#ffffff",
|
||||
lw=1.3, ls="--", alpha=0.85,
|
||||
label=r"$\mu_t$ (ref. $N=10^5$)")
|
||||
if ax_idx == 0:
|
||||
ax.legend(loc="upper right", fontsize=7, framealpha=0.2,
|
||||
edgecolor="#3a4a72")
|
||||
hist_artists[N] = (bars, ref_line)
|
||||
|
||||
# Bottom row: W2 convergence on a log-log scale wrt time
|
||||
ax_w2 = fig.add_subplot(gs[2, :])
|
||||
ax_w2.set_xlim(0, T_HORIZON)
|
||||
ax_w2.set_yscale("log")
|
||||
ax_w2.set_ylim(max(1e-3, min(w2_curves[N_VALUES[-1]].min(), 1e-2) * 0.5),
|
||||
max(w2_curves[N_VALUES[0]].max() * 1.5, 1.0))
|
||||
ax_w2.set_xlabel("$t$", fontsize=9)
|
||||
ax_w2.set_ylabel(r"$W_2(\mu_t^N, \mu_t)$", fontsize=9)
|
||||
ax_w2.set_title(r"Wasserstein-2 distance to the reference law (log scale)"
|
||||
" -- $W_2 \\sim O(1/\\sqrt{N})$",
|
||||
color="#e9efff", fontsize=10, pad=6)
|
||||
|
||||
w2_lines = {}
|
||||
for N, color in zip(N_VALUES, panel_colors):
|
||||
(line,) = ax_w2.plot([], [], color=color, lw=1.6, label=f"$N = {N}$")
|
||||
w2_lines[N] = line
|
||||
ax_w2.legend(loc="upper right", ncol=4, fontsize=8, framealpha=0.2,
|
||||
edgecolor="#3a4a72")
|
||||
|
||||
cursor = ax_w2.axvline(0.0, color="#ffd166", lw=1, ls=":", alpha=0.8)
|
||||
|
||||
fig.suptitle(
|
||||
r"Propagation of chaos (Sznitman 1991): $\mu_t^N \to \mu_t$ as $N \to \infty$",
|
||||
color="#e9efff", fontsize=12, y=0.98,
|
||||
)
|
||||
|
||||
|
||||
def update(frame):
|
||||
artists = []
|
||||
for N in N_VALUES:
|
||||
bars, ref_line = hist_artists[N]
|
||||
hist, _ = np.histogram(panels[N][frame], bins=BINS, density=True)
|
||||
for rect, h in zip(bars, hist):
|
||||
rect.set_height(h)
|
||||
ref_line.set_ydata(ref_density[frame])
|
||||
artists.extend([*bars, ref_line])
|
||||
cursor.set_xdata([times[frame], times[frame]])
|
||||
for N in N_VALUES:
|
||||
w2_lines[N].set_data(times[: frame + 1], w2_curves[N][: frame + 1])
|
||||
artists.append(cursor)
|
||||
artists.extend(w2_lines.values())
|
||||
return artists
|
||||
|
||||
|
||||
frame_indices = list(range(0, N_STEPS + 1, FRAME_STRIDE))
|
||||
print(f"Rendering {len(frame_indices)} frames...")
|
||||
anim = animation.FuncAnimation(
|
||||
fig, update, frames=frame_indices, interval=40, blit=False,
|
||||
)
|
||||
|
||||
out_path = Path(__file__).with_name("propagation_of_chaos.gif")
|
||||
try:
|
||||
anim.save(out_path, writer=animation.PillowWriter(fps=24))
|
||||
print(f"Saved animation: {out_path}")
|
||||
except Exception as exc:
|
||||
print(f"GIF write failed ({exc}); saving PNG of last frame instead.")
|
||||
update(N_STEPS)
|
||||
fig.savefig(out_path.with_suffix(".png"), dpi=120)
|
||||
print(f"Saved PNG: {out_path.with_suffix('.png')}")
|
||||
@@ -0,0 +1,13 @@
|
||||
# optimiz-rs v2.0 benchmark
|
||||
|
||||
Best wall-clock over a few runs. Single-threaded. Workloads chosen to be intrinsically loopy / sequential -- the regime where the Rust core delivers a real speedup over a NumPy reference.
|
||||
|
||||
Workloads that are fully vectorisable in NumPy (e.g. drift updates for an N-particle SDE with no callback) are not included: a tight NumPy loop on contiguous arrays is hard to beat from Rust through a PyO3 callback boundary.
|
||||
|
||||
| Workload | Pure Python / NumPy | optimiz-rs (Rust) | Speedup |
|
||||
|---|---:|---:|---:|
|
||||
| HMM Baum-Welch (2 states, 5_000 obs, 10 iters) | 970.94 ms | 14.34 ms | ** 67.7×** |
|
||||
| Differential evolution (Rastrigin d=5, 50 iters x 20 pop) | 417.45 ms | 30.03 ms | ** 13.9×** |
|
||||
| Path signature (T=300, d=3, depth=3) | 11.07 ms | 0.99 ms | ** 11.2×** |
|
||||
| MCMC random-walk MH (5000 samples, d=2) | 35.41 ms | 20.24 ms | ** 1.7×** |
|
||||
| Hawkes process (T=100.0, mu=1.0, alpha=0.6, beta=1.2) | 2.75 ms | 0.83 ms | ** 3.3×** |
|
||||
@@ -0,0 +1,239 @@
|
||||
"""Honest v2 benchmark for optimiz-rs.
|
||||
|
||||
We compare each Rust primitive against the most natural pure-Python /
|
||||
NumPy reference for the same task. The point is **not** to claim a
|
||||
universal speedup, but to give users a realistic picture of where the
|
||||
Rust core wins: intrinsically loopy / sequential algorithms that do
|
||||
not vectorise cleanly in NumPy.
|
||||
|
||||
Run with:
|
||||
python examples/benchmark_v2.py
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
import optimizr as opt
|
||||
|
||||
|
||||
def _bench(fn, repeat: int = 3) -> float:
|
||||
best = math.inf
|
||||
for _ in range(repeat):
|
||||
t0 = time.perf_counter()
|
||||
fn()
|
||||
best = min(best, time.perf_counter() - t0)
|
||||
return best
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1. HMM Baum-Welch -- intrinsically loopy
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _bench_hmm():
|
||||
rng = np.random.default_rng(42)
|
||||
n_obs = 5_000
|
||||
obs = np.concatenate([
|
||||
rng.normal(-1.0, 0.5, n_obs // 2),
|
||||
rng.normal(+1.0, 0.5, n_obs // 2),
|
||||
]).reshape(-1, 1)
|
||||
|
||||
def py_baum_welch():
|
||||
n = obs.shape[0]
|
||||
mu = np.array([-0.5, 0.5])
|
||||
sigma = np.array([1.0, 1.0])
|
||||
pi = np.array([0.5, 0.5])
|
||||
A = np.array([[0.9, 0.1], [0.1, 0.9]])
|
||||
for _ in range(10):
|
||||
B = np.exp(-(obs - mu) ** 2 / (2 * sigma ** 2)) / (np.sqrt(2 * np.pi) * sigma)
|
||||
alpha = np.zeros((n, 2))
|
||||
alpha[0] = pi * B[0]
|
||||
for t in range(1, n):
|
||||
alpha[t] = (alpha[t - 1] @ A) * B[t]
|
||||
alpha[t] /= alpha[t].sum() + 1e-300
|
||||
beta = np.zeros((n, 2))
|
||||
beta[-1] = 1.0
|
||||
for t in range(n - 2, -1, -1):
|
||||
beta[t] = A @ (B[t + 1] * beta[t + 1])
|
||||
beta[t] /= beta[t].sum() + 1e-300
|
||||
gamma = alpha * beta
|
||||
gamma /= gamma.sum(axis=1, keepdims=True) + 1e-300
|
||||
mu = (gamma * obs).sum(axis=0) / gamma.sum(axis=0)
|
||||
sigma = np.sqrt(((obs - mu) ** 2 * gamma).sum(axis=0) / gamma.sum(axis=0))
|
||||
|
||||
def rs_hmm():
|
||||
opt.fit_hmm(obs.flatten().tolist(), 2, 10, 1e-6)
|
||||
|
||||
np_t = _bench(py_baum_welch, repeat=2)
|
||||
rs_t = _bench(rs_hmm, repeat=3)
|
||||
return ("HMM Baum-Welch (2 states, 5_000 obs, 10 iters)", np_t, rs_t)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 2. Differential evolution -- multi-modal global optimisation
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _bench_differential_evolution():
|
||||
from scipy.optimize import differential_evolution as scipy_de # type: ignore
|
||||
|
||||
rastrigin = lambda x: 10 * len(x) + sum(xi * xi - 10 * math.cos(2 * math.pi * xi) for xi in x)
|
||||
bounds = [(-5.12, 5.12)] * 5
|
||||
|
||||
def py_de():
|
||||
scipy_de(rastrigin, bounds, maxiter=50, popsize=20, seed=0, tol=0.0, polish=False)
|
||||
|
||||
def rs_de():
|
||||
opt.differential_evolution(rastrigin, bounds, maxiter=50, popsize=20, seed=0)
|
||||
|
||||
np_t = _bench(py_de, repeat=2)
|
||||
rs_t = _bench(rs_de, repeat=3)
|
||||
return ("Differential evolution (Rastrigin d=5, 50 iters x 20 pop)", np_t, rs_t)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3. Path signature
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _bench_signature():
|
||||
rng = np.random.default_rng(0)
|
||||
path = np.cumsum(rng.standard_normal((300, 3)) * 0.05, axis=0)
|
||||
|
||||
def py_signature():
|
||||
d = path.shape[1]
|
||||
increments = np.diff(path, axis=0)
|
||||
s1 = np.zeros(d)
|
||||
s2 = np.zeros((d, d))
|
||||
s3 = np.zeros((d, d, d))
|
||||
for inc in increments:
|
||||
s1 = s1 + inc
|
||||
s2 = s2 + 0.5 * np.outer(inc, inc)
|
||||
for i in range(d):
|
||||
for j in range(d):
|
||||
for k in range(d):
|
||||
s3[i, j, k] += inc[i] * inc[j] * inc[k] / 6.0
|
||||
|
||||
def rs_signature():
|
||||
opt.path_signature(path.tolist(), 3)
|
||||
|
||||
np_t = _bench(py_signature, repeat=2)
|
||||
rs_t = _bench(rs_signature, repeat=3)
|
||||
return ("Path signature (T=300, d=3, depth=3)", np_t, rs_t)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 4. MCMC random-walk Metropolis
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _bench_mcmc():
|
||||
n_samples = 5_000
|
||||
rng = np.random.default_rng(1)
|
||||
|
||||
def py_mh():
|
||||
def logp(x):
|
||||
a, b = 1.0, 100.0
|
||||
return -((a - x[0]) ** 2 + b * (x[1] - x[0] ** 2) ** 2) / 20.0
|
||||
x = np.array([0.0, 0.0])
|
||||
lp = logp(x)
|
||||
out = np.zeros((n_samples, 2))
|
||||
for i in range(n_samples):
|
||||
cand = x + rng.normal(scale=0.5, size=2)
|
||||
lpc = logp(cand)
|
||||
if math.log(rng.random() + 1e-300) < lpc - lp:
|
||||
x, lp = cand, lpc
|
||||
out[i] = x
|
||||
|
||||
def rs_mh():
|
||||
# mcmc_sample expects a Python log-density callable; that's a fair
|
||||
# comparison since the python reference also calls a python logp.
|
||||
def logp(x):
|
||||
a, b = 1.0, 100.0
|
||||
return -((a - x[0]) ** 2 + b * (x[1] - x[0] ** 2) ** 2) / 20.0
|
||||
try:
|
||||
opt.mcmc_sample(logp, [0.0, 0.0], n_samples, 0.5)
|
||||
except TypeError:
|
||||
# Fallback signature: positional only
|
||||
opt.mcmc_sample(logp, [0.0, 0.0], n_samples)
|
||||
|
||||
np_t = _bench(py_mh, repeat=2)
|
||||
rs_t = _bench(rs_mh, repeat=3)
|
||||
return (f"MCMC random-walk MH ({n_samples} samples, d=2)", np_t, rs_t)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 5. Hawkes process simulation -- sequential, O(N^2) reference
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _bench_hawkes():
|
||||
T = 100.0
|
||||
mu = 1.0
|
||||
alpha = 0.6
|
||||
beta = 1.2
|
||||
|
||||
def py_hawkes():
|
||||
rng = np.random.default_rng(0)
|
||||
events = []
|
||||
t = 0.0
|
||||
lam_max = mu
|
||||
while t < T:
|
||||
t += rng.exponential(1.0 / max(lam_max, 1e-9))
|
||||
if t >= T:
|
||||
break
|
||||
lam = mu + alpha * sum(math.exp(-beta * (t - s)) for s in events)
|
||||
if rng.random() <= lam / lam_max:
|
||||
events.append(t)
|
||||
lam_max = lam + alpha
|
||||
return events
|
||||
|
||||
def rs_hawkes():
|
||||
opt.simulate_hawkes(mu, alpha, beta, T, "exponential", 0)
|
||||
|
||||
np_t = _bench(py_hawkes, repeat=2)
|
||||
rs_t = _bench(rs_hawkes, repeat=3)
|
||||
return (f"Hawkes process (T={T}, mu={mu}, alpha={alpha}, beta={beta})", np_t, rs_t)
|
||||
|
||||
|
||||
def main():
|
||||
print("Running v2 benchmarks (best of N runs, single-threaded)\n")
|
||||
rows = []
|
||||
for fn in (_bench_hmm, _bench_differential_evolution, _bench_signature,
|
||||
_bench_mcmc, _bench_hawkes):
|
||||
try:
|
||||
rows.append(fn())
|
||||
except Exception as exc: # pragma: no cover
|
||||
rows.append((f"{fn.__name__} (FAILED: {exc})", float('nan'), float('nan')))
|
||||
|
||||
md = ["| Workload | Pure Python / NumPy | optimiz-rs (Rust) | Speedup |",
|
||||
"|---|---:|---:|---:|"]
|
||||
for name, np_t, rs_t in rows:
|
||||
if math.isnan(np_t) or math.isnan(rs_t):
|
||||
md.append(f"| {name} | n/a | n/a | n/a |")
|
||||
continue
|
||||
speedup = np_t / rs_t if rs_t > 0 else float("inf")
|
||||
md.append(f"| {name} | {np_t * 1e3:8.2f} ms | {rs_t * 1e3:8.2f} ms | **{speedup:5.1f}×** |")
|
||||
|
||||
table = "\n".join(md)
|
||||
print(table)
|
||||
|
||||
out = Path(__file__).with_name("benchmark_v2.md")
|
||||
out.write_text(
|
||||
"# optimiz-rs v2.0 benchmark\n\n"
|
||||
"Best wall-clock over a few runs. Single-threaded. "
|
||||
"Workloads chosen to be intrinsically loopy / sequential -- the "
|
||||
"regime where the Rust core delivers a real speedup over a NumPy "
|
||||
"reference.\n\n"
|
||||
"Workloads that are fully vectorisable in NumPy (e.g. drift updates "
|
||||
"for an N-particle SDE with no callback) are not included: a tight "
|
||||
"NumPy loop on contiguous arrays is hard to beat from Rust through "
|
||||
"a PyO3 callback boundary.\n\n"
|
||||
+ table
|
||||
+ "\n"
|
||||
)
|
||||
print(f"\nWritten: {out}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 5.0 MiB |
File diff suppressed because one or more lines are too long
Binary file not shown.
|
After Width: | Height: | Size: 1.6 MiB |
+1
-1
@@ -4,7 +4,7 @@ build-backend = "maturin"
|
||||
|
||||
[project]
|
||||
name = "optimiz-rs"
|
||||
version = "2.0.0a1"
|
||||
version = "2.0.0"
|
||||
description = "High-performance optimization algorithms in Rust with Python bindings"
|
||||
authors = [
|
||||
{name = "HFThot Research Lab", email = "contact@hfthot-lab.eu"}
|
||||
|
||||
+112
-1
@@ -64,7 +64,73 @@ except (ImportError, AttributeError):
|
||||
min_variance_weights = None
|
||||
erc_weights = None
|
||||
|
||||
__version__ = "0.2.0"
|
||||
# ===== v2.0 primitives (lazy via __getattr__, but eagerly bound when possible) =====
|
||||
try:
|
||||
from optimizr._core import (
|
||||
# Volterra / fractional
|
||||
solve_fractional_ode,
|
||||
solve_volterra,
|
||||
geometric_grid_lift,
|
||||
fourier_invert,
|
||||
mittag_leffler_py,
|
||||
# BSDE
|
||||
linear_bsde_constant_coeffs,
|
||||
# Mean-field / agent-based
|
||||
mean_reverting_mckean_vlasov,
|
||||
consensus_dynamics,
|
||||
# Risk measures
|
||||
historical_var_py,
|
||||
parametric_var_py,
|
||||
cvar_value_py,
|
||||
minimize_cvar_py,
|
||||
# PDE
|
||||
fokker_planck_constant,
|
||||
hjb_quadratic_2d,
|
||||
poisson_2d_zero_boundary,
|
||||
# Stochastic control
|
||||
optimal_switching_dp,
|
||||
pontryagin_lqr,
|
||||
two_sided_intensities,
|
||||
quadratic_impact_control_py,
|
||||
# Topology
|
||||
vietoris_rips_filtration,
|
||||
persistent_homology,
|
||||
bottleneck_distance,
|
||||
# Graph
|
||||
combinatorial_laplacian_py,
|
||||
normalised_laplacian_py,
|
||||
random_walk_laplacian_py,
|
||||
spectral_cluster_py,
|
||||
# Signatures
|
||||
path_signature,
|
||||
path_log_signature,
|
||||
random_signature,
|
||||
signature_kernel,
|
||||
shuffle_product,
|
||||
concatenate_signatures,
|
||||
# Inference / optimisation
|
||||
robust_drift,
|
||||
estimate_hurst,
|
||||
scale_dependent_hurst,
|
||||
f_alpha_lambda_py,
|
||||
mmd_gaussian,
|
||||
# Point processes
|
||||
simulate_hawkes,
|
||||
simulate_bivariate_hawkes,
|
||||
simulate_fbm,
|
||||
simulate_mixed_fbm,
|
||||
# Kalman / smoothing
|
||||
LinearKalmanFilter,
|
||||
UnscentedKalmanFilter,
|
||||
RTSSmoother,
|
||||
FilterResult,
|
||||
SmootherResult,
|
||||
KalmanState,
|
||||
)
|
||||
except (ImportError, AttributeError):
|
||||
pass
|
||||
|
||||
__version__ = "2.0.0"
|
||||
__all__ = [
|
||||
"HMM",
|
||||
"mcmc_sample",
|
||||
@@ -102,6 +168,51 @@ __all__ = [
|
||||
"mean_variance_optimal_weights",
|
||||
"min_variance_weights",
|
||||
"erc_weights",
|
||||
# ===== v2.0 primitives =====
|
||||
"solve_fractional_ode",
|
||||
"solve_volterra",
|
||||
"geometric_grid_lift",
|
||||
"fourier_invert",
|
||||
"mittag_leffler_py",
|
||||
"linear_bsde_constant_coeffs",
|
||||
"mean_reverting_mckean_vlasov",
|
||||
"consensus_dynamics",
|
||||
"historical_var_py",
|
||||
"parametric_var_py",
|
||||
"cvar_value_py",
|
||||
"minimize_cvar_py",
|
||||
"fokker_planck_constant",
|
||||
"hjb_quadratic_2d",
|
||||
"poisson_2d_zero_boundary",
|
||||
"optimal_switching_dp",
|
||||
"pontryagin_lqr",
|
||||
"two_sided_intensities",
|
||||
"quadratic_impact_control_py",
|
||||
"vietoris_rips_filtration",
|
||||
"persistent_homology",
|
||||
"bottleneck_distance",
|
||||
"combinatorial_laplacian_py",
|
||||
"normalised_laplacian_py",
|
||||
"random_walk_laplacian_py",
|
||||
"spectral_cluster_py",
|
||||
"path_signature",
|
||||
"path_log_signature",
|
||||
"random_signature",
|
||||
"signature_kernel",
|
||||
"shuffle_product",
|
||||
"concatenate_signatures",
|
||||
"robust_drift",
|
||||
"estimate_hurst",
|
||||
"scale_dependent_hurst",
|
||||
"f_alpha_lambda_py",
|
||||
"mmd_gaussian",
|
||||
"simulate_hawkes",
|
||||
"simulate_bivariate_hawkes",
|
||||
"simulate_fbm",
|
||||
"simulate_mixed_fbm",
|
||||
"LinearKalmanFilter",
|
||||
"UnscentedKalmanFilter",
|
||||
"RTSSmoother",
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
"""Non-regression tests for the optimiz-rs v2 public API.
|
||||
|
||||
Each test exercises one of the v2 primitives advertised in the README and
|
||||
the public blog post and checks it against an analytic ground truth.
|
||||
|
||||
Run with: pytest tests/test_v2_api.py -v
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import optimizr as opt
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1. Risk measures -- historical VaR
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_historical_var_gaussian():
|
||||
"""VaR_0.95 of N(0,1) losses is the 0.95-quantile ~= 1.6449."""
|
||||
rng = np.random.default_rng(0)
|
||||
losses = rng.standard_normal(200_000).tolist()
|
||||
v95 = opt.historical_var_py(losses, 0.95)
|
||||
assert math.isclose(v95, 1.6449, abs_tol=2e-2), f"got {v95}"
|
||||
|
||||
|
||||
def test_historical_var_monotone_in_alpha():
|
||||
rng = np.random.default_rng(1)
|
||||
losses = rng.standard_normal(50_000).tolist()
|
||||
v90 = opt.historical_var_py(losses, 0.90)
|
||||
v95 = opt.historical_var_py(losses, 0.95)
|
||||
v99 = opt.historical_var_py(losses, 0.99)
|
||||
assert v90 < v95 < v99
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 2. Volterra -- fractional ODE (Caputo / Adams scheme)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_solve_fractional_ode_constant_rhs_matches_power_law():
|
||||
"""For Caputo D^alpha h = c with h(0) = h0, the closed form is
|
||||
h(t) = h0 + c * t^alpha / Gamma(alpha + 1)."""
|
||||
alpha = 0.7
|
||||
h0 = 1.0
|
||||
c = 2.0
|
||||
T = 1.0
|
||||
out = opt.solve_fractional_ode(h0, alpha, T, 400, lambda t, h: c)
|
||||
assert set(out.keys()) >= {"t_grid", "h"}
|
||||
h_T = out["h"][-1]
|
||||
expected = h0 + c * T ** alpha / math.gamma(alpha + 1.0)
|
||||
assert math.isclose(h_T, expected, rel_tol=2e-2), f"got {h_T}, expected {expected}"
|
||||
|
||||
|
||||
def test_solve_fractional_ode_zero_rhs_is_constant():
|
||||
"""If the right-hand side is zero, the Caputo ODE preserves h0."""
|
||||
out = opt.solve_fractional_ode(3.14, 0.5, 1.0, 200, lambda t, h: 0.0)
|
||||
for hi in out["h"]:
|
||||
assert math.isclose(hi, 3.14, abs_tol=1e-9)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3. Volterra -- second-kind integral equation
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_solve_volterra_zero_kernel_returns_g():
|
||||
"""If K(dt, y) = 0 the Volterra equation collapses to y(t) = g(t)."""
|
||||
out = opt.solve_volterra(lambda t: t, lambda dt, y: 0.0, 1.0, 50)
|
||||
grid = list(out["t_grid"])
|
||||
y = list(out["y"])
|
||||
assert len(grid) == len(y) == 51
|
||||
for ti, yi in zip(grid, y):
|
||||
assert math.isclose(yi, ti, abs_tol=1e-12)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 4. BSDE -- linear theta scheme with constant coefficients
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_linear_bsde_zero_coefficients_returns_terminal():
|
||||
"""a = b = c = 0 reduces the BSDE to dY = -Z dW with terminal Y_T = K;
|
||||
the unique solution is Y_t = K, Z_t = 0."""
|
||||
res = opt.linear_bsde_constant_coeffs(
|
||||
a_const=0.0, b_const=0.0, c_const=0.0,
|
||||
terminal=2.5, n_steps=100, t_horizon=1.0, theta=0.5,
|
||||
)
|
||||
y = list(res["y"])
|
||||
z = list(res["z"])
|
||||
assert len(y) == 101 and len(z) == 100
|
||||
for yi in y:
|
||||
assert math.isclose(yi, 2.5, abs_tol=1e-9)
|
||||
for zi in z:
|
||||
assert math.isclose(zi, 0.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_linear_bsde_pure_drift_grows_backward():
|
||||
"""With a > 0, b = c = 0 and Y_T = 1 we get Y_t = exp(a (T - t))."""
|
||||
a = 0.3
|
||||
T = 1.0
|
||||
res = opt.linear_bsde_constant_coeffs(
|
||||
a_const=a, b_const=0.0, c_const=0.0,
|
||||
terminal=1.0, n_steps=400, t_horizon=T, theta=0.5,
|
||||
)
|
||||
grid = list(res["time_grid"])
|
||||
y = list(res["y"])
|
||||
expected = [math.exp(a * (T - t)) for t in grid]
|
||||
err = max(abs(yi - ei) for yi, ei in zip(y, expected))
|
||||
assert err < 5e-3, f"max error {err}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 5. McKean-Vlasov -- mean-reverting toward the empirical mean
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_mean_reverting_mckean_vlasov_shapes_and_invariance():
|
||||
"""Empirical mean is conserved in expectation by mean-reversion to it."""
|
||||
n_part = 200
|
||||
n_steps = 500
|
||||
initial = np.linspace(-1.0, 1.0, n_part).tolist()
|
||||
out = opt.mean_reverting_mckean_vlasov(
|
||||
initial=initial, theta=1.0, sigma=0.0,
|
||||
n_steps=n_steps, t_horizon=1.0, seed=42,
|
||||
)
|
||||
assert set(out.keys()) >= {"paths_flat", "n_steps", "n_particles", "time_grid"}
|
||||
assert out["n_particles"] == n_part
|
||||
assert out["n_steps"] == n_steps + 1
|
||||
paths = np.array(out["paths_flat"]).reshape(n_steps + 1, n_part)
|
||||
mean0 = float(np.mean(initial))
|
||||
# With sigma = 0 and mean-reversion to the empirical mean,
|
||||
# the cross-sectional mean must be preserved exactly.
|
||||
assert math.isclose(float(np.mean(paths[-1])), mean0, abs_tol=1e-9)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 6. Module surface -- guard against accidental API removal
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
V2_PUBLIC_API = (
|
||||
# v2.0 newcomers advertised in the blog post and README
|
||||
"solve_fractional_ode",
|
||||
"solve_volterra",
|
||||
"linear_bsde_constant_coeffs",
|
||||
"mean_reverting_mckean_vlasov",
|
||||
"historical_var_py",
|
||||
# v1.x primitives that must remain available (backward compat)
|
||||
"differential_evolution",
|
||||
"fit_hmm",
|
||||
"viterbi_decode",
|
||||
"mcmc_sample",
|
||||
"grid_search",
|
||||
"mutual_information",
|
||||
"shannon_entropy",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", V2_PUBLIC_API)
|
||||
def test_public_symbol_exposed(name):
|
||||
assert hasattr(opt, name), f"optimizr.{name} is missing"
|
||||
assert callable(getattr(opt, name)), f"optimizr.{name} is not callable"
|
||||
Reference in New Issue
Block a user