release(v2.0.0): finalize PyPI metadata, README rewrite, v2 benchmark + McKean-Vlasov animation
- Restore correct PyPI distribution name 'optimiz-rs' (continuity with v1.0.x).
Rust crate stays 'optimiz-rs'; Python module is 'optimizr'.
- python/optimizr/__init__.py:
* Bump __version__ from stale '0.2.0' to '2.0.0'.
* Eagerly bind every v2 primitive from _core (so dir(optimizr), IDE
auto-complete and 'from optimizr import X' all work without relying on
the lazy __getattr__ fallback).
* Extend __all__ with 38 new v2 entries.
- README.md: full v2 features section grouped by domain (rough volatility,
BSDE/PDE, stochastic control, mean-field, topology/graphs/signatures,
risk/robust inference, point processes, Kalman). Embedded
examples/mckean_vlasov.gif at the top. Added v2 benchmark table.
- examples/benchmark_v2.py: honest benchmark vs pure-Python/NumPy
references on intrinsically loopy workloads. Best-of-3, single-thread,
Apple M2: HMM 67.7x, DE 13.9x, signatures 11.2x, Hawkes 3.3x, MCMC 1.7x.
- examples/animate_mckean_vlasov.py + examples/mckean_vlasov.gif (5MB):
cinematic 800-particle mean-reverting McKean-Vlasov flow animation
using optimizr.mean_reverting_mckean_vlasov.
- tests/test_v2_api.py already in place: 20/20 pass.
This commit is contained in:
+3
-3
@@ -9,9 +9,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
### Added — public release of the v2 API
|
||||
|
||||
- Promoted `2.0.0-alpha.1` to the stable `2.0.0` release.
|
||||
- **PyPI distribution name re-aligned to `optimizr`** (continuity with the
|
||||
v1.4.x line). The Rust crate stays `optimiz-rs`; both expose the same
|
||||
Python module name `optimizr`.
|
||||
- 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`,
|
||||
|
||||
@@ -11,28 +11,91 @@
|
||||
[](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
|
||||
|
||||
This release adds **Python bindings for the v1.1 generic numerical primitives** and ships **eight brand-new CPU-only modules** covering rough volatility, mean-field control, BSDEs and robust inference. All v1.x APIs remain available — `import optimizr as opt` is unchanged.
|
||||
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`.
|
||||
|
||||
New Python primitives (all importable directly from `optimizr`):
|
||||
### 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.
|
||||
- **`linear_bsde_constant_coeffs(a, b, c, terminal, n_steps, t_horizon, theta=0.5)`** — backward SDE θ-scheme.
|
||||
- **`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`).
|
||||
- **`historical_var_py(losses, alpha)`** — empirical Value-at-Risk estimator.
|
||||
- Plus: `path_signature`, `random_signature`, `signature_kernel`, `persistent_homology`, `bottleneck_distance`, `spectral_cluster_py`, `mmd_gaussian`, `quadratic_impact_control_py`, and more.
|
||||
- **`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.
|
||||
|
||||
A full non-regression suite for the v2 public API lives in `tests/test_v2_api.py` (analytic ground-truth checks for every advertised primitive).
|
||||
### 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).
|
||||
|
||||
### 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 optimizr
|
||||
python -c "import optimizr; print(optimizr.solve_fractional_ode(1.0, 0.5, 1.0, 100, lambda t,h: 0.0)['h'][-1])"
|
||||
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
|
||||
|
||||
This release adds a broad collection of **CPU-only generic numerical primitives**, all purely additive:
|
||||
@@ -53,7 +116,7 @@ All new modules are exposed via the **Rust API only** in this release; Python bi
|
||||
🎉 **Production Ready** - First stable release with comprehensive documentation
|
||||
📚 **ReadTheDocs** - Full documentation at https://optimiz-r.readthedocs.io
|
||||
🏗️ **Published to crates.io** - Install with `cargo add optimiz-rs`
|
||||
🐍 **Published to PyPI** - Install with `pip install optimizr`
|
||||
🐍 **Published to PyPI** - Install with `pip install optimiz-rs`
|
||||
🔒 **Stable API** - Semantic versioning from v1.0.0 forward
|
||||
|
||||
## Features
|
||||
@@ -87,10 +150,10 @@ All new modules are exposed via the **Rust API only** in this release; Python bi
|
||||
### From PyPI (Python)
|
||||
|
||||
```bash
|
||||
pip install optimizr
|
||||
pip install optimiz-rs
|
||||
```
|
||||
|
||||
> **Note**: the historical PyPI distribution name is `optimizr` (no dash). The Python import name is also `optimizr`: `import optimizr as opt`.
|
||||
> 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)
|
||||
|
||||
@@ -98,7 +161,7 @@ pip install optimizr
|
||||
cargo add optimiz-rs
|
||||
```
|
||||
|
||||
> The Rust crate is `optimiz-rs` (with dash); its library name is `optimizr` (no dash) — matching the Python module.
|
||||
> The Rust crate is also `optimiz-rs`; its library name is `optimizr` (no dash) — matching the Python module.
|
||||
|
||||
### From Source
|
||||
|
||||
|
||||
@@ -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,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 |
+1
-1
@@ -3,7 +3,7 @@ requires = ["maturin>=1.0,<2.0"]
|
||||
build-backend = "maturin"
|
||||
|
||||
[project]
|
||||
name = "optimizr"
|
||||
name = "optimiz-rs"
|
||||
version = "2.0.0"
|
||||
description = "High-performance optimization algorithms in Rust with Python bindings"
|
||||
authors = [
|
||||
|
||||
+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",
|
||||
]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user