Try It: Forward Fill Simulator
Try It: Forward Fill Simulator
animation
analytics-ml
data-quality
Try Change Gap length (samples) from 4 to 10 while keeping Max allowed gap at 6.
Observe Watch Forward-filled, Gap too large (rejected), and Result with a 10-sample gap.
Explain Explain why a 10-sample gap with Max allowed gap at 6 leaves some values rejected.
Technical boundaries. The tool creates one missing data gap in a generated 24-sample signal. Forward fill repeats the last valid reading until the maximum allowed gap is exceeded.
NoteTry It: Forward Fill Simulator
{
const n = 24;
const baseSignal = Array.from({length: n}, (_, i) => 20 + 2 * Math.sin(i * 0.3) + (Math.sin(i * 1.7) * 0.3));
// Create missing data gap
const gapEnd = Math.min(fwd_gapStart + fwd_gapLen, n);
const withGap = baseSignal.map((v, i) => (i >= fwd_gapStart && i < gapEnd) ? null : v);
// Apply forward fill
let lastValid = null;
let gapCount = 0;
const filled = withGap.map((v, i) => {
if (v !== null) {
lastValid = v;
gapCount = 0;
return {value: v, status: "original"};
}
if (lastValid === null) return {value: null, status: "no_history"};
gapCount++;
if (gapCount > fwd_maxGap) return {value: null, status: "gap_too_large"};
return {value: lastValid, status: "imputed"};
});
const width = 600;
const height = 250;
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 = baseSignal;
const yMin = Math.min(...allVals) - 1;
const yMax = Math.max(...allVals) + 1;
const xScale = (i) => margin.left + (i / (n - 1)) * plotW;
const yScale = (v) => margin.top + plotH - ((v - yMin) / (yMax - yMin)) * plotH;
// Original signal path
const origPath = baseSignal.map((v, i) => `${i === 0 ? "M" : "L"}${xScale(i).toFixed(1)},${yScale(v).toFixed(1)}`).join(" ");
// Imputed segments
const imputedDots = filled.map((f, i) => {
if (f.status === "imputed" && f.value !== null) {
return `<circle cx="${xScale(i).toFixed(1)}" cy="${yScale(f.value).toFixed(1)}" r="5" fill="#16A085" stroke="white" stroke-width="1.5"/>`;
}
if (f.status === "gap_too_large") {
return `<line x1="${xScale(i) - 4}" y1="${yScale(yMin + (yMax - yMin) / 2) - 4}" x2="${xScale(i) + 4}" y2="${yScale(yMin + (yMax - yMin) / 2) + 4}" stroke="#E74C3C" stroke-width="2.5"/>
<line x1="${xScale(i) - 4}" y1="${yScale(yMin + (yMax - yMin) / 2) + 4}" x2="${xScale(i) + 4}" y2="${yScale(yMin + (yMax - yMin) / 2) - 4}" stroke="#E74C3C" stroke-width="2.5"/>`;
}
return "";
}).join("");
// Gap shading
const gapShade = (fwd_gapStart < n) ? `<rect x="${xScale(fwd_gapStart)}" y="${margin.top}" width="${xScale(Math.min(gapEnd, n - 1)) - xScale(fwd_gapStart)}" height="${plotH}" fill="#E74C3C" opacity="0.08" rx="3"/>` : "";
// Original data dots
const origDots = withGap.map((v, i) => {
if (v !== null) {
return `<circle cx="${xScale(i).toFixed(1)}" cy="${yScale(v).toFixed(1)}" r="3.5" fill="#2C3E50"/>`;
}
return "";
}).join("");
const imputedCount = filled.filter(f => f.status === "imputed").length;
const rejectedCount = filled.filter(f => f.status === "gap_too_large").length;
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)">Forward Fill: gap=${fwd_gapLen} samples, max_gap=${fwd_maxGap}</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="${yScale(v) + 4}" text-anchor="end" font-size="9" fill="var(--bs-body-color)">${v.toFixed(1)}</text><line x1="${margin.left}" y1="${yScale(v)}" x2="${margin.left + plotW}" y2="${yScale(v)}" stroke="#dee2e6" stroke-width="0.5"/>`).join("")}
<path d="${origPath}" fill="none" stroke="#7F8C8D" stroke-width="1" stroke-dasharray="4,3"/>
${origDots}
${imputedDots}
<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 - 175}" y="${margin.top + 5}" width="170" height="50" fill="var(--bs-body-bg, white)" stroke="#dee2e6" rx="3"/>
<circle cx="${margin.left + plotW - 162}" cy="${margin.top + 18}" r="3.5" fill="#2C3E50"/>
<text x="${margin.left + plotW - 155}" y="${margin.top + 22}" font-size="9" fill="var(--bs-body-color)">Original data</text>
<circle cx="${margin.left + plotW - 162}" cy="${margin.top + 33}" r="5" fill="#16A085" stroke="white" stroke-width="1.5"/>
<text x="${margin.left + plotW - 155}" y="${margin.top + 37}" font-size="9" fill="var(--bs-body-color)">Forward-filled</text>
<line x1="${margin.left + plotW - 166}" y1="${margin.top + 44}" x2="${margin.left + plotW - 158}" y2="${margin.top + 52}" stroke="#E74C3C" stroke-width="2"/>
<text x="${margin.left + plotW - 155}" y="${margin.top + 51}" font-size="9" fill="var(--bs-body-color)">Gap too large (rejected)</text>
</svg>
<p style="margin: 0.5rem 0 0 0; font-size: 0.9em;">
<strong>Result:</strong> ${imputedCount} sample${imputedCount !== 1 ? "s" : ""} imputed${rejectedCount > 0 ? `, <span style="color:#E74C3C; font-weight:bold;">${rejectedCount} rejected</span> (exceeded max_gap=${fwd_maxGap})` : ""}.
${fwd_gapLen > fwd_maxGap ? "The gap exceeds the maximum allowed length -- some values cannot be filled. Increase max_gap or accept missing data." : "All gap values were successfully forward-filled from the last valid reading."}
</p>
</div>`;
}