"""Physical well-tie likelihood (plan §4.2): p(s | g) = N(s; α W r(g), σ_n² I). Forward model shared by the D2 generator and the M2 weights: AI on a fine time axis → reflectivity at the sample midpoints → convolution with the fine wavelet → interpolation to the 4 ms sample times. α and σ_n² are profiled out: log p = −(n/2)·log RSS(α̂). Weights π_s ∝ p_s^γ with γ chosen by sampler.temper (ESS ≥ S/4). Constant wavelet phase: maximum marginal likelihood over the posterior warp samples (fit_phase). """ from __future__ import annotations import numpy as np from scipy import signal from ml_porosity.data.wavelet import rotate_phase def fine_wavelet(w: np.ndarray, factor: int) -> np.ndarray: """Band-limited interpolation of the wavelet (odd length, centred) onto a grid `factor` times finer; keeps the centre.""" wf = signal.resample_poly(w, factor, 1) n = len(w) // 2 return wf[: 2 * n * factor + 1] def forward_fine(ai: np.ndarray, t_fine: np.ndarray, t_out: np.ndarray, w_fine: np.ndarray) -> np.ndarray: """Synthetic seismic at t_out from AI on the uniform fine time grid t_fine. ai (..., nf) → (..., L).""" ai = np.atleast_2d(ai) r = (ai[:, 1:] - ai[:, :-1]) / (ai[:, 1:] + ai[:, :-1]) s = signal.fftconvolve(r, w_fine[None, :], mode="same", axes=1) tm = 0.5 * (t_fine[1:] + t_fine[:-1]) return np.stack([np.interp(t_out, tm, si) for si in s]) def synth_for_warps(tau_fine: np.ndarray, ai_log: np.ndarray, t_of_tau_fine: np.ndarray, t_fine: np.ndarray, t_out: np.ndarray, w_fine: np.ndarray) -> np.ndarray: """(S, L): synthetic for each warp. The AI log (τ axis) is mapped to the t axis by each warp sample.""" t_of_tau_fine = np.atleast_2d(t_of_tau_fine) ai_t = np.stack([np.interp(t_fine, ts, ai_log) for ts in t_of_tau_fine]) return forward_fine(ai_t, t_fine, t_out, w_fine) def profile_loglik(syn: np.ndarray, seis: np.ndarray) -> np.ndarray: """log p_s = −(n/2)·log RSS_s after profiling out α_s ≥ 0 (constrained least squares; synthetic and seismic follow the same polarity convention, so phases φ and φ + 180° no longer give the same likelihood) and σ_n².""" syn = np.atleast_2d(syn) a = np.maximum(syn @ seis / np.maximum(np.sum(syn**2, 1), 1e-30), 0.0) rss = np.sum((seis[None, :] - a[:, None] * syn) ** 2, 1) return -0.5 * len(seis) * np.log(rss) def phase_loglik(syn0: np.ndarray, seis: np.ndarray, phases=np.arange(-180, 180, 15)) -> np.ndarray: """(P, S): log p_s for each constant phase. syn0 (S, L) is the synthetic with the zero-phase wavelet; phase rotation commutes with convolution, so only the synthetic needs to be rotated.""" return np.stack([profile_loglik(rotate_phase(np.atleast_2d(syn0), p), seis) for p in phases]) def fit_phase(ll_list: list[np.ndarray], valid_list: list[np.ndarray], phases=np.arange(-180, 180, 15)) -> int: """Phase maximising the marginal likelihood Σ_w log mean_s p(s_w | g_ws, φ) over the (anchor) posterior warp samples of each training well; ll_list[w] (P, S) from phase_loglik. Returns the phase index in `phases`. Correlation against a single point tie is unusable: a tie error of a few ms already reduces ncc to ~0.1 and the estimated phase is almost random.""" from scipy.special import logsumexp score = sum(logsumexp(ll[:, ok], axis=1) - np.log(ok.sum()) for ll, ok in zip(ll_list, valid_list)) return int(np.argmax(score))