"""Surrogate grids shared by 04_signflip.py and 05_sam.py (same code as in 02_search_cost.py). Fact-Checked Science, 2026-11-15. Needs numpy; imports asl_lib.py from this folder.""" import numpy as np import asl_lib as A def grid_fixed_x_matrix_y(x_raw, y_start, Ymat, widths, lags): """R[b, wi, li]: x series fixed (Monthly), y rows in Ymat (B, n_y) from y_start; pairs x_CMA[t], y_CMA[t+L].""" B, ny = Ymat.shape R = np.full((B, len(widths), len(lags)), np.nan) for wi, w in enumerate(widths): xc = A.cma(x_raw, w) YC = A.cma_matrix(Ymat, w) okY = np.isfinite(YC[0]) for li, L in enumerate(lags): ix, iy = [], [] for i, xv in enumerate(xc.v): if not np.isfinite(xv): continue j = xc.start + i + L - y_start if 0 <= j < ny and okY[j]: ix.append(i) iy.append(j) R[:, wi, li] = A.rows_corr(xc.v[np.array(ix)], YC[:, np.array(iy)]) return R def grid_matrix_x_fixed_y(x_start, Xmat, y_raw, widths, lags): """R[b, wi, li]: x rows in Xmat (B, n_x) from x_start, y series fixed (Monthly).""" B, nx = Xmat.shape R = np.full((B, len(widths), len(lags)), np.nan) for wi, w in enumerate(widths): yc = A.cma(y_raw, w) XC = A.cma_matrix(Xmat, w) okX = np.isfinite(XC[0]) for li, L in enumerate(lags): ix, iy = [], [] for i in range(nx): if not okX[i]: continue m = x_start + i + L yv = yc.get(m) if np.isfinite(yv): ix.append(i) iy.append(m - yc.start) R[:, wi, li] = A.cols_corr(XC[:, np.array(ix)], yc.v[np.array(iy)]) return R def paired_matrix_y(x_raw, y_start, Ymat, w, L): """For fixed x and surrogate y rows: return x paired vector (n) and Y paired matrix (B, n), plus t months.""" xc = A.cma(x_raw, w) YC = A.cma_matrix(Ymat, w) okY = np.isfinite(YC[0]) ix, iy, ts = [], [], [] for i, xv in enumerate(xc.v): if not np.isfinite(xv): continue j = xc.start + i + L - y_start if 0 <= j < Ymat.shape[1] and okY[j]: ix.append(i); iy.append(j); ts.append(xc.start + i) return xc.v[np.array(ix)], YC[:, np.array(iy)], np.array(ts) def paired_matrix_x(x_start, Xmat, y_raw, w, L): """For surrogate x rows and fixed y: return X paired matrix (B, n), y paired vector (n), t months.""" yc = A.cma(y_raw, w) XC = A.cma_matrix(Xmat, w) okX = np.isfinite(XC[0]) ix, iy, ts = [], [], [] for i in range(Xmat.shape[1]): if not okX[i]: continue m = x_start + i + L yv = yc.get(m) if np.isfinite(yv): ix.append(i); iy.append(m - yc.start); ts.append(x_start + i) return XC[:, np.array(ix)], yc.v[np.array(iy)], np.array(ts) def rolling_r(X, Y, block): """Rolling Pearson r over consecutive blocks (step 1) for matching (B, n) arrays X and Y. Returns (B, n-block+1).""" X = np.atleast_2d(X).astype(float) Y = np.atleast_2d(Y).astype(float) def cs(a): return np.concatenate([np.zeros((a.shape[0], 1)), np.cumsum(a, axis=1)], axis=1) n = X.shape[1] k = np.arange(0, n - block + 1) Sx, Sy = cs(X), cs(Y) Sxx, Syy, Sxy = cs(X * X), cs(Y * Y), cs(X * Y) sx = Sx[:, k + block] - Sx[:, k] sy = Sy[:, k + block] - Sy[:, k] sxx = Sxx[:, k + block] - Sxx[:, k] syy = Syy[:, k + block] - Syy[:, k] sxy = Sxy[:, k + block] - Sxy[:, k] cov = sxy - sx * sy / block vx = sxx - sx * sx / block vy = syy - sy * sy / block return cov / np.sqrt(vx * vy)