Interpolation vs Forward Fill
Interpolation vs Forward Fill
animation
analytics-ml
data-quality
Try Change Signal trend (slope) from 0.3 to 1.0.
Observe Compare Mean Absolute Error for Forward-fill and Linear interp at slope 1.0.
Explain Explain why changing the trend slope from 0.3 affects the error of forward-fill and linear interpolation.
Technical boundaries. The signal uses 24 samples with a trend and sine. Linear interpolation requires left and right anchors; error is calculated over the gap.
NoteInterpolation vs Forward Fill
{
const n = 24;
const base = Array.from({length: n}, (_, i) => 20 + lin_trend * i + 1.5 * Math.sin(i * 0.5));
const gapEnd = Math.min(lin_gapStart + lin_gapLen, n);
const withGap = base.map((v, i) => (i >= lin_gapStart && i < gapEnd) ? null : v);
// Forward fill
let lastV = null;
const ffilled = withGap.map(v => {
if (v !== null) { lastV = v; return v; }
return lastV;
});
// Linear interpolation
const linterp = withGap.slice();
// Find left and right anchors
let leftIdx = lin_gapStart - 1;
let rightIdx = gapEnd < n ? gapEnd : null;
if (leftIdx >= 0 && rightIdx !== null) {
const leftVal = base[leftIdx];
const rightVal = base[rightIdx];
for (let i = lin_gapStart; i < gapEnd; i++) {
const t = (i - leftIdx) / (rightIdx - leftIdx);
linterp[i] = leftVal + t * (rightVal - leftVal);
}
}
const width = 600;
const height = 260;
const margin = {top: 30, right: 20, bottom: 35, left: 50};
const plotW = width - margin.left - margin.right;
const plotH = height - margin.top - margin.bottom;
const allVals = base;
const yMin = Math.min(...allVals) - 1;
const yMax = Math.max(...allVals) + 1;
const xS = (i) => margin.left + (i / (n - 1)) * plotW;
const yS = (v) => margin.top + plotH - ((v - yMin) / (yMax - yMin)) * plotH;
const truePath = base.map((v, i) => `${i === 0 ? "M" : "L"}${xS(i).toFixed(1)},${yS(v).toFixed(1)}`).join(" ");
// Compute error
let ffillErr = 0, linterpErr = 0, count = 0;
for (let i = lin_gapStart; i < gapEnd && i < n; i++) {
if (ffilled[i] !== null) ffillErr += Math.abs(ffilled[i] - base[i]);
if (linterp[i] !== null) linterpErr += Math.abs(linterp[i] - base[i]);
count++;
}
const ffillMAE = count > 0 ? (ffillErr / count).toFixed(3) : "N/A";
const linterpMAE = count > 0 ? (linterpErr / count).toFixed(3) : "N/A";
const gapShade = `<rect x="${xS(lin_gapStart)}" y="${margin.top}" width="${xS(Math.min(gapEnd - 1, n - 1)) - xS(lin_gapStart)}" height="${plotH}" fill="#9B59B6" opacity="0.07" rx="3"/>`;
// Forward-fill dots
const ffDots = [];
for (let i = lin_gapStart; i < gapEnd && i < n; i++) {
if (ffilled[i] !== null) ffDots.push(`<circle cx="${xS(i).toFixed(1)}" cy="${yS(ffilled[i]).toFixed(1)}" r="4.5" fill="#E67E22" opacity="0.85"/>`);
}
// Interp dots
const liDots = [];
for (let i = lin_gapStart; i < gapEnd && i < n; i++) {
if (linterp[i] !== null) liDots.push(`<circle cx="${xS(i).toFixed(1)}" cy="${yS(linterp[i]).toFixed(1)}" r="4.5" fill="#3498DB" opacity="0.85"/>`);
}
// Orig dots (non-gap)
const origDots = withGap.map((v, i) => v !== null ? `<circle cx="${xS(i).toFixed(1)}" cy="${yS(v).toFixed(1)}" r="3" fill="#2C3E50"/>` : "").join("");
return html`<div style="background: var(--bs-light, #f8f9fa); padding: 1rem; border-radius: 8px; color: var(--bs-body-color);">
<svg viewBox="0 0 ${width} ${height}" style="width:100%; max-width:${width}px; font-family: Arial, sans-serif;">
<text x="${width / 2}" y="16" text-anchor="middle" font-size="12" font-weight="bold" fill="var(--bs-body-color)">Linear Interpolation vs Forward Fill (trend=${lin_trend >= 0 ? "+" : ""}${lin_trend.toFixed(2)}/sample)</text>
${gapShade}
<line x1="${margin.left}" y1="${margin.top}" x2="${margin.left}" y2="${margin.top + plotH}" stroke="var(--bs-body-color)" stroke-width="1"/>
<line x1="${margin.left}" y1="${margin.top + plotH}" x2="${margin.left + plotW}" y2="${margin.top + plotH}" stroke="var(--bs-body-color)" stroke-width="1"/>
${[yMin, (yMin + yMax) / 2, yMax].map(v => `<text x="${margin.left - 5}" y="${yS(v) + 4}" text-anchor="end" font-size="9" fill="var(--bs-body-color)">${v.toFixed(1)}</text><line x1="${margin.left}" y1="${yS(v)}" x2="${margin.left + plotW}" y2="${yS(v)}" stroke="#dee2e6" stroke-width="0.5"/>`).join("")}
<path d="${truePath}" fill="none" stroke="#7F8C8D" stroke-width="1" stroke-dasharray="4,3"/>
${origDots}
${ffDots.join("")}
${liDots.join("")}
<text x="${margin.left + plotW / 2}" y="${height - 5}" text-anchor="middle" font-size="10" fill="var(--bs-body-color)">Sample Index</text>
<rect x="${margin.left + plotW - 170}" y="${margin.top + 5}" width="165" height="50" fill="var(--bs-body-bg, white)" stroke="#dee2e6" rx="3"/>
<circle cx="${margin.left + plotW - 157}" cy="${margin.top + 17}" r="3" fill="#2C3E50"/>
<text x="${margin.left + plotW - 150}" y="${margin.top + 21}" font-size="9" fill="var(--bs-body-color)">Original / True signal</text>
<circle cx="${margin.left + plotW - 157}" cy="${margin.top + 31}" r="4.5" fill="#E67E22" opacity="0.85"/>
<text x="${margin.left + plotW - 150}" y="${margin.top + 35}" font-size="9" fill="var(--bs-body-color)">Forward-fill</text>
<circle cx="${margin.left + plotW - 157}" cy="${margin.top + 45}" r="4.5" fill="#3498DB" opacity="0.85"/>
<text x="${margin.left + plotW - 150}" y="${margin.top + 49}" font-size="9" fill="var(--bs-body-color)">Linear interpolation</text>
</svg>
<p style="margin: 0.5rem 0 0 0; font-size: 0.9em;">
<strong>Mean Absolute Error:</strong>
Forward-fill = <span style="color:#E67E22; font-weight:bold;">${ffillMAE}</span> |
Linear interp = <span style="color:#3498DB; font-weight:bold;">${linterpMAE}</span>
${parseFloat(linterpMAE) < parseFloat(ffillMAE) ? " -- Linear interpolation wins here, as expected for trending data." : parseFloat(linterpMAE) > parseFloat(ffillMAE) ? " -- Forward-fill is closer for this configuration." : " -- Both methods perform equally."}
Try increasing the trend slope to see when interpolation outperforms forward-fill.
</p>
</div>`;
}