Files
ML-Porosity/ml_porosity/registration/tie_likelihood.py
lamlvh cee2ff0859 P2 diagnosis on dev, P3 dev experiments, lock protocol_p3
- P2: SMC tie posterior (numpy + torch), RML/soft-DTW losses, bank training,
  D2 synthetic generator, scripts 09-13, protocol_p2 (locked) and its outcome
  (G-B failed; H1 not rejected, H3 rejected, H4 not rejected).
- Diagnosis on dev realizations 900/901 only (scripts 14-17, run_p3_dev.sh):
  posterior screen, SMC stability (P = 1024, K = 1), sigma_a misspecification
  stress and training, H3 with the SMC posterior, F3 placement on sigma_post.
- protocol_p3: H2 split into H2a (posterior-mean labels vs point ties) and
  H2b (RML vs posterior mean as a function of sigma_post); H3 descriptive only;
  sigma_a arm; new D2 config d2_synth_p3.yaml; main grid on realizations 8-15.
  Locked by configs/protocol_p3.sha256 and tag protocol-p3.
- runlog: per-run logs and runtime index (logs/index.csv).
- Docs: plan §12 items 10-11, theory, technical overview, report; wording
  aligned with Gunning & Glinsky (2006): the tie posterior is not claimed as new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 06:03:33 +00:00

67 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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))