|
| 1 | +import jax |
| 2 | +import jax.numpy as np |
| 3 | + |
| 4 | +def jacobi(A, b, w=2.0/3.0, tol=1e-9, max_iters=100): |
| 5 | + """ |
| 6 | + Jacobi iterative linear solver for Ax = b. |
| 7 | +
|
| 8 | + Parameters |
| 9 | + ---------- |
| 10 | + A : array |
| 11 | + Matrix A. |
| 12 | + b : array |
| 13 | + Vector b. |
| 14 | + w : float |
| 15 | + Relaxation parameter. |
| 16 | + tol : float |
| 17 | + Tolerance. |
| 18 | + max_iters : int |
| 19 | + Maximum number of iterations. |
| 20 | +
|
| 21 | + Returns |
| 22 | + ------- |
| 23 | + x : array |
| 24 | + Solution vector. |
| 25 | + n_iter : int |
| 26 | + Number of iterations performed. |
| 27 | + """ |
| 28 | + |
| 29 | + # Precompute diagonal inverse and LU |
| 30 | + diag_A = np.diag(A) |
| 31 | + inv_diag_A = 1.0 / diag_A |
| 32 | + w_m_invD = w * np.diag(inv_diag_A) |
| 33 | + LU = A - np.diag(diag_A) |
| 34 | + |
| 35 | + x0 = b |
| 36 | + |
| 37 | + def cond(state): |
| 38 | + x, dx, n_iter = state |
| 39 | + return (n_iter < max_iters) & (np.linalg.norm(dx) > tol) |
| 40 | + |
| 41 | + def body(state): |
| 42 | + x, dx, n_iter = state |
| 43 | + xn = w_m_invD @ (b - LU @ x) + (1.0 - w) * x |
| 44 | + dx = x - xn |
| 45 | + return xn, dx, n_iter + 1 |
| 46 | + |
| 47 | + dx = np.ones_like(x0) |
| 48 | + # Using jax.lax.while_loop for the iterative solver |
| 49 | + x, dx, n_iter = jax.lax.while_loop(cond, body, (x0, dx, 0)) |
| 50 | + |
| 51 | + return x, n_iter |
| 52 | + |
| 53 | + |
| 54 | +def _theta_dy(f, j, h, th, y0, y1, f_y0, pars, tol=1e-4): |
| 55 | + J = np.eye(y1.size) - h * th * j(y1, pars) |
| 56 | + F = y0 + h * (th * f(y1, pars) + f_y0) - y1 |
| 57 | + |
| 58 | + dy, _ = jacobi(J, F, w=2.0/3.0, tol=tol, max_iters=10) |
| 59 | + return dy |
| 60 | + |
| 61 | +def _compute_noise(gfun, x, p, sqrt_dt, z_t): |
| 62 | + g = gfun(x, p) |
| 63 | + return g * sqrt_dt * z_t |
| 64 | + |
| 65 | + |
| 66 | +def make_implicit_sde(dt, dfun, jfun, gfun, th=0.5, tol=1e-4, max_iters=10): |
| 67 | + """ |
| 68 | + Construct an implicit SDE integrator using the Theta method. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + dt : float |
| 73 | + Time step. |
| 74 | + dfun : callable |
| 75 | + Drift function dfun(x, p). |
| 76 | + jfun : callable |
| 77 | + Jacobian of drift function jfun(x, p). |
| 78 | + gfun : callable or float |
| 79 | + Diffusion function gfun(x, p) or constant sigma. |
| 80 | + th : float |
| 81 | + Theta parameter (0.5 for Crank-Nicolson, 1.0 for backward Euler). |
| 82 | + tol : float |
| 83 | + Tolerance for the Newton-Jacobi iteration. |
| 84 | + max_iters : int |
| 85 | + Maximum iterations for the Newton loop. |
| 86 | +
|
| 87 | + Returns |
| 88 | + ------- |
| 89 | + step : callable |
| 90 | + Step function step(x, z_t, p). |
| 91 | + loop : callable |
| 92 | + Loop function loop(x0, zs, p). |
| 93 | + """ |
| 94 | + |
| 95 | + if not hasattr(gfun, '__call__'): |
| 96 | + sig = gfun |
| 97 | + gfun = lambda *_: sig |
| 98 | + |
| 99 | + sqrt_dt = np.sqrt(dt) |
| 100 | + |
| 101 | + def step(x, z_t, p): |
| 102 | + # Euler guess |
| 103 | + f_val = dfun(x, p) |
| 104 | + f_y0 = (1 - th) * f_val |
| 105 | + y1_euler = x + dt * f_val # Initial guess using explicit Euler |
| 106 | + |
| 107 | + # Refine if implicit |
| 108 | + def refine(y1): |
| 109 | + # First Newton step |
| 110 | + # We use y1 (Euler guess) to compute Jacobian and Residual |
| 111 | + dy = _theta_dy(dfun, jfun, dt, th, x, y1, f_y0, p, tol=tol) |
| 112 | + y1 = y1 + dy |
| 113 | + |
| 114 | + def refinement_cond(state): |
| 115 | + y1, dy, n_iter = state |
| 116 | + return (n_iter < max_iters) & (np.linalg.norm(dy) > tol) |
| 117 | + |
| 118 | + def refinement_body(state): |
| 119 | + y1, _, n_iter = state |
| 120 | + dy = _theta_dy(dfun, jfun, dt, th, x, y1, f_y0, p, tol=tol) |
| 121 | + y1 = y1 + dy |
| 122 | + return y1, dy, n_iter + 1 |
| 123 | + |
| 124 | + init_state = (y1, dy, 1) |
| 125 | + final_state = jax.lax.while_loop(refinement_cond, refinement_body, init_state) |
| 126 | + return final_state[0] |
| 127 | + |
| 128 | + y1 = jax.lax.cond(th > 0.0, refine, lambda x: x, y1_euler) |
| 129 | + |
| 130 | + noise = _compute_noise(gfun, x, p, sqrt_dt, z_t) |
| 131 | + y1 = y1 + noise |
| 132 | + |
| 133 | + return y1 |
| 134 | + |
| 135 | + @jax.jit |
| 136 | + def loop(x0, zs, p): |
| 137 | + def op(x, z): |
| 138 | + x = step(x, z, p) |
| 139 | + return x, x |
| 140 | + _, xs = jax.lax.scan(op, x0, zs) |
| 141 | + return xs |
| 142 | + |
| 143 | + return step, loop |
0 commit comments