export function nnInterp()

in src/utils/stats.js [36:51]


export function nnInterp(x, y, xOut) {
  // returns yOut by mapping x -> xOut, finding the index, then applying to y
  // constant (nearest-neighbors) interpolation with f=1 (always right-sided)
  // find ind in x where xOut values should below, then look at y[ind]
  // assume x & y are pre-sorted and match.
  const xInds = xOut.map((xi) => x.findIndex((xx) => xx > xi));
  const low = Math.min(...x);
  const high = Math.max(...x);
  return xInds.map((i) => {
    // left & right censor extreme values.
    if (i === -1 && xOut[i] <= low) return y[0];
    if (i === -1 && xOut[i] >= high) return y[y.length - 1];
    // otherwise, return the corresponding y.
    return y[i];
  });
}