Files
optimiz-rs/examples/notebooks/10_bsde.ipynb
T
ThotDjehuty d8682f61e5 release(v2.0.0-alpha.2): PyO3 bindings + executed companion notebooks + Sphinx RST with inline plots
PyO3 abi3 bindings for the 13 v2.0.0 functions across 8 module groups:
  bsde, pde, stochastic_control, optimal_control::quadratic_impact_control,
  mean_field::mckean_vlasov, agent_based, inference, optimization.

8 executed companion notebooks under examples/notebooks/10_bsde.ipynb …
17_generative_calibration.ipynb (cell outputs and matplotlib figures
preserved as proof-of-work; verified against analytic ground truths).

8 Sphinx RST pages under docs/source/algorithms/{bsde,pde,stochastic_control,
quadratic_impact_control,mckean_vlasov,agent_based,robust_drift,
generative_calibration_hooks}.rst with .. math:: derivations and inline
.. image:: directives placed immediately after each .. code-block:: python
so each plot appears directly under the code that produced it.

18 PNG plot assets under docs/source/_static/v2/<group>/.

index.rst extended with a new 'v2.0 Generic Stochastic Control & PDE'
toctree caption.

Forbidden-vocabulary audit on new src/, docs/source/algorithms/ and
binding files: zero matches.

All previously stable APIs untouched; v2.0.0 is additive at the binding
level — no v1.x function signature was changed.
2026-05-12 12:18:14 +02:00

99 KiB
Raw Blame History

10 — BSDE θ-scheme

Generic CPU-only CrankNicolson scheme for linear backward stochastic differential equations. Reference doc page: bsde.rst.

In [1]:
import numpy as np
import matplotlib.pyplot as plt
from optimizr import _core as opt
plt.rcParams['figure.figsize'] = (7, 4)
plt.rcParams['figure.dpi'] = 110

Exponential ground-truth check

With a(t) \equiv -\rho, b = c = 0 and Y_T = 1 the analytic deterministic solution is Y_t = e^{-\rho (T-t)}.

In [2]:
rho = 0.3
T   = 1.0
res = opt.linear_bsde_constant_coeffs(
    a_const=-rho, b_const=0.0, c_const=0.0,
    terminal=1.0, n_steps=200, t_horizon=T, theta=0.5,
)
tg = np.array(res['time_grid'])
yg = np.array(res['y'])
analytic = np.exp(-rho * (T - tg))
print('Y0 =', yg[0], '   exp(-rho T) =', analytic[0])
print('max abs error =', float(np.max(np.abs(yg - analytic))))
Y0 = 0.740818179010676    exp(-rho T) = 0.7408182206817179
max abs error = 4.167104183938619e-08
In [3]:
fig, ax = plt.subplots()
ax.plot(tg, yg, label='θ-scheme', lw=2)
ax.plot(tg, analytic, '--', label='analytic exp(-ρ(T-t))')
ax.set_xlabel('t'); ax.set_ylabel('Y_t')
ax.set_title('Linear BSDE — CrankNicolson vs analytic')
ax.legend(); ax.grid(alpha=0.3)
fig.tight_layout(); plt.show()

Convergence rate study

CrankNicolson is second-order in Δt.

In [4]:
errs = []
ns = [25, 50, 100, 200, 400, 800]
for n in ns:
    r = opt.linear_bsde_constant_coeffs(-rho, 0.0, 0.0, 1.0, n, T, 0.5)
    errs.append(abs(r['y'][0] - np.exp(-rho * T)))
print(list(zip(ns, errs)))
[(25, np.float64(2.666998401679166e-06)), (50, np.float64(6.667396952320104e-07)), (100, np.float64(1.6668430979915883e-07)), (200, np.float64(4.167104183938619e-08)), (400, np.float64(1.0417760876180182e-08)), (800, np.float64(2.6044438827810268e-09))]
In [5]:
fig, ax = plt.subplots()
ax.loglog(ns, errs, 'o-')
ax.loglog(ns, [errs[0] * (ns[0] / n) ** 2 for n in ns],
          ':', label='O(Δt²) reference')
ax.set_xlabel('n_steps'); ax.set_ylabel('|Y0  analytic|')
ax.set_title('CrankNicolson convergence'); ax.grid(which='both', alpha=0.3); ax.legend()
fig.tight_layout(); plt.show()

Verified against analytic ground truth: Y_t = exp(-ρ (T - t)) — relative error at t = 0 below 1e-3 for n_steps = 200.