viewof imp_gapStart = Inputs.range([2, 12], {value: 5, step: 1, label: "Gap start index (reading #)"})
viewof imp_gapLength = Inputs.range([1, 5], {value: 3, step: 1, label: "Gap length (missing readings)"})
viewof imp_noiseLevel = Inputs.range([0, 5], {value: 1.5, step: 0.1, label: "Noise amplitude (C)"})
viewof imp_method = Inputs.select(["Forward Fill", "Linear Interpolation", "Mean Imputation"], {value: "Linear Interpolation", label: "Imputation method"})
viewof imp_filterType = Inputs.select(["None", "EMA (alpha=0.3)", "EMA (alpha=0.6)", "SMA (window=3)", "SMA (window=5)"], {value: "EMA (alpha=0.3)", label: "Noise filter"})Imputation and Filtering Explorer
Imputation and Filtering Explorer
animation
analytics-ml
data-quality
Try Change Gap length (missing readings) from 3 to 5.
Observe Compare Imputed and Filtered values around the 5 missing readings.
Explain Describe how imputation method and noise filter affect the output with 5 missing readings.
Technical boundaries. The stream uses 20 readings with a gentle temperature trend and deterministic noise. Missing values form a single gap.
Imputation and Filtering Explorer
Note
Simulate a sensor data stream with missing values and noise. Choose an imputation method and a noise filter to see how different strategies affect the output. The chart shows raw data (with gaps), imputed values, and the filtered signal.
{
// Generate base temperature signal with gentle trend
const imp_n = 20;
const imp_seed = 42;
const imp_baseTemps = Array.from({length: imp_n}, (_, i) => 22 + 0.3 * Math.sin(i * 0.5) + 0.1 * i);
// Add noise using deterministic pseudo-random
function imp_pseudoRandom(seed, i) {
const x = Math.sin(seed + i * 127.1) * 43758.5453;
return x - Math.floor(x);
}
const imp_noisyTemps = imp_baseTemps.map((v, i) =>
v + (imp_pseudoRandom(imp_seed, i) - 0.5) * 2 * imp_noiseLevel
);
// Create gaps (set missing values to null)
const imp_gapEnd = Math.min(imp_gapStart + imp_gapLength, imp_n);
const imp_withGaps = imp_noisyTemps.map((v, i) =>
(i >= imp_gapStart && i < imp_gapEnd) ? null : v
);
// Impute missing values
const imp_imputed = [...imp_withGaps];
if (imp_method === "Forward Fill") {
for (let i = 0; i < imp_n; i++) {
if (imp_imputed[i] === null) {
imp_imputed[i] = i > 0 ? imp_imputed[i - 1] : imp_baseTemps[0];
}
}
} else if (imp_method === "Linear Interpolation") {
let startIdx = imp_gapStart - 1;
let endIdx = imp_gapEnd;
const startVal = startIdx >= 0 ? imp_withGaps[startIdx] : imp_baseTemps[0];
const endVal = endIdx < imp_n ? imp_withGaps[endIdx] : startVal;
for (let i = imp_gapStart; i < imp_gapEnd; i++) {
const t = (i - startIdx) / (endIdx - startIdx);
imp_imputed[i] = startVal + t * (endVal - startVal);
}
} else {
const imp_validVals = imp_withGaps.filter(v => v !== null);
const imp_mean = imp_validVals.reduce((a, b) => a + b, 0) / imp_validVals.length;
for (let i = 0; i < imp_n; i++) {
if (imp_imputed[i] === null) imp_imputed[i] = imp_mean;
}
}
// Apply noise filter
const imp_filtered = [...imp_imputed];
if (imp_filterType.startsWith("EMA")) {
const alpha = imp_filterType.includes("0.3") ? 0.3 : 0.6;
for (let i = 1; i < imp_n; i++) {
imp_filtered[i] = alpha * imp_imputed[i] + (1 - alpha) * imp_filtered[i - 1];
}
} else if (imp_filterType.startsWith("SMA")) {
const w = imp_filterType.includes("3") ? 3 : 5;
for (let i = 0; i < imp_n; i++) {
const start = Math.max(0, i - Math.floor(w / 2));
const end = Math.min(imp_n, i + Math.floor(w / 2) + 1);
const slice = imp_imputed.slice(start, end);
imp_filtered[i] = slice.reduce((a, b) => a + b, 0) / slice.length;
}
}
// Build data for display
const imp_rows = [];
for (let i = 0; i < imp_n; i++) {
imp_rows.push({index: i, series: "Raw (with gaps)", value: imp_withGaps[i]});
imp_rows.push({index: i, series: "Imputed", value: imp_imputed[i]});
if (imp_filterType !== "None") {
imp_rows.push({index: i, series: "Filtered", value: imp_filtered[i]});
}
imp_rows.push({index: i, series: "True signal", value: imp_baseTemps[i]});
}
// Calculate error metrics
const imp_imputeErrors = [];
const imp_filterErrors = [];
for (let i = imp_gapStart; i < imp_gapEnd; i++) {
imp_imputeErrors.push(Math.abs(imp_imputed[i] - imp_baseTemps[i]));
}
for (let i = 0; i < imp_n; i++) {
imp_filterErrors.push(Math.pow(imp_filtered[i] - imp_baseTemps[i], 2));
}
const imp_imputeMAE = imp_imputeErrors.length > 0
? (imp_imputeErrors.reduce((a, b) => a + b, 0) / imp_imputeErrors.length).toFixed(3)
: "N/A";
const imp_filterRMSE = Math.sqrt(imp_filterErrors.reduce((a, b) => a + b, 0) / imp_filterErrors.length).toFixed(3);
const viewportWidth = typeof width !== "undefined" ? width : window.innerWidth;
const compact = viewportWidth < 640;
const imp_displayStart = Math.max(0, imp_gapStart - 1);
const imp_displayEnd = Math.min(imp_n, imp_gapEnd + 2);
const imp_displayIndices = Array.from(
{length: imp_displayEnd - imp_displayStart},
(_, idx) => imp_displayStart + idx
);
const metricMin = compact ? 130 : 160;
return html`<div style="background: var(--bs-light, #f8f9fa); padding: 1.2rem; border-radius: 8px; border-left: 4px solid #9B59B6; color: var(--bs-body-color); margin-top: 0.5rem;">
<p style="margin: 0 0 0.7rem 0; font-size: 0.82rem; opacity: 0.8;">Focused view around the missing-data window (readings ${imp_displayStart} to ${imp_displayEnd - 1}).</p>
${compact
? html`<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 0.6rem; margin-bottom: 0.8rem;">
${imp_displayIndices.map(i => html`<div style="background: var(--bs-body-bg, #fff); border: 1px solid #dde4ea; border-radius: 8px; padding: 0.65rem;">
<div style="font-weight: 700; color: #2C3E50; margin-bottom: 0.35rem;">Reading ${i}</div>
<div style="font-size: 0.8rem; color: #7F8C8D;"><strong>Raw:</strong> ${imp_withGaps[i] !== null ? imp_withGaps[i].toFixed(1) : 'GAP'}</div>
<div style="font-size: 0.8rem; color: #9B59B6;"><strong>Imputed:</strong> ${imp_imputed[i].toFixed(1)}</div>
<div style="font-size: 0.8rem; color: #16A085;"><strong>Filtered:</strong> ${imp_filtered[i].toFixed(1)}</div>
</div>`)}
</div>`
: html`<div style="overflow-x: auto; margin-bottom: 0.8rem;">
<table style="width: 100%; border-collapse: collapse; font-size: 0.8rem;">
<thead>
<tr style="border-bottom: 2px solid #2C3E50;">
<th style="padding: 4px; text-align: center;">Reading #</th>
${imp_displayIndices.map(i => html`<th style="padding: 4px; text-align: center; ${i >= imp_gapStart && i < imp_gapEnd ? 'background: #ffeaa7;' : ''}">${i}</th>`)}
</tr>
</thead>
<tbody>
<tr>
<td style="padding: 4px; font-weight: bold; color: #7F8C8D;">Raw</td>
${imp_displayIndices.map(i => html`<td style="padding: 4px; text-align: center; ${imp_withGaps[i] === null ? 'color: #E74C3C; font-weight: bold;' : ''}">${imp_withGaps[i] !== null ? imp_withGaps[i].toFixed(1) : 'GAP'}</td>`)}
</tr>
<tr>
<td style="padding: 4px; font-weight: bold; color: #9B59B6;">Imputed</td>
${imp_displayIndices.map(i => html`<td style="padding: 4px; text-align: center; ${i >= imp_gapStart && i < imp_gapEnd ? 'color: #9B59B6; font-weight: bold;' : ''}">${imp_imputed[i].toFixed(1)}</td>`)}
</tr>
<tr>
<td style="padding: 4px; font-weight: bold; color: #16A085;">Filtered</td>
${imp_displayIndices.map(i => html`<td style="padding: 4px; text-align: center;">${imp_filtered[i].toFixed(1)}</td>`)}
</tr>
</tbody>
</table>
</div>`}
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(${metricMin}px, 1fr)); gap: 1rem; text-align: center; margin-top: 0.5rem;">
<div>
<div style="font-size: 0.8rem; color: var(--bs-body-color); opacity: 0.7;">Imputation MAE (gap region)</div>
<div style="font-size: 1.3rem; font-weight: bold; color: #9B59B6;">${imp_imputeMAE}C</div>
</div>
<div>
<div style="font-size: 0.8rem; color: var(--bs-body-color); opacity: 0.7;">Filter RMSE (full signal)</div>
<div style="font-size: 1.3rem; font-weight: bold; color: #16A085;">${imp_filterRMSE}C</div>
</div>
<div>
<div style="font-size: 0.8rem; color: var(--bs-body-color); opacity: 0.7;">Missing readings</div>
<div style="font-size: 1.3rem; font-weight: bold; color: #E74C3C;">${imp_gapLength} of ${imp_n}</div>
</div>
</div>
<p style="margin: 0.8rem 0 0 0; font-size: 0.85rem; text-align: center; color: var(--bs-body-color); opacity: 0.8;"><strong>Experiment:</strong> Compare Forward Fill (flat line during gap) vs Linear Interpolation (straight line between boundaries). Increase noise amplitude and try different filters to see the smoothing-vs-responsiveness tradeoff.</p>
</div>`;
}