viewof smoothAlpha = Inputs.range([0.01, 0.99], {
value: 0.3, step: 0.01, label: "Smoothing factor (alpha):"
})
viewof noiseLevel = Inputs.range([0, 5], {
value: 2, step: 0.1, label: "Noise amplitude:"
})
viewof signalType = Inputs.select(
["Steady (25 C)", "Gradual rise (20-30 C)", "Step change at t=25"],
{ value: "Steady (25 C)", label: "Signal pattern:" }
)Exponential Smoothing Explorer
Exponential Smoothing Explorer
animation
analytics-ml
normalization
Try Select Step change at t=25 and change Smoothing factor (alpha): from 0.3 to 0.99.
Observe Compare Smoothed with True and watch MSE (smoothed vs true) and Approx. response time at alpha 0.99.
Explain Explain why alpha 0.99 is more responsive but lets more noise through.
Technical boundaries. The tool uses 50 samples with seeded noise and three signal patterns. Smoothed starts at the first noisy reading; response time is approximate.
NoteTry It: Exponential Smoothing Explorer
Adjust the smoothing factor (alpha) and noise level to see how exponential smoothing filters noisy sensor data. A low alpha trusts the history more (smoother), while a high alpha trusts new readings more (more responsive).
{
const nPts = 50;
const alpha = smoothAlpha;
const noise = noiseLevel;
const sig = signalType;
function trueSignal(i) {
if (sig === "Steady (25 C)") return 25;
if (sig === "Gradual rise (20-30 C)") return 20 + (10 * i / (nPts - 1));
return i < 25 ? 22 : 28;
}
// Seeded pseudo-random for consistent noise per alpha/noise change
function seededRand(seed) {
let x = Math.sin(seed * 127.1 + 311.7) * 43758.5453;
return x - Math.floor(x);
}
const trueVals = [];
const noisyVals = [];
const smoothVals = [];
for (let i = 0; i < nPts; i++) {
const tv = trueSignal(i);
const nv = tv + (seededRand(i) - 0.5) * 2 * noise;
trueVals.push(tv);
noisyVals.push(nv);
if (i === 0) {
smoothVals.push(nv);
} else {
smoothVals.push(alpha * nv + (1 - alpha) * smoothVals[i - 1]);
}
}
const allVals = [...noisyVals, ...smoothVals, ...trueVals];
const yMin = Math.min(...allVals) - 1;
const yMax = Math.max(...allVals) + 1;
const w = 580;
const h = 260;
const pad = { top: 30, right: 20, bottom: 40, left: 50 };
const plotW = w - pad.left - pad.right;
const plotH = h - pad.top - pad.bottom;
function xPos(i) { return pad.left + (i / (nPts - 1)) * plotW; }
function yPos(v) { return pad.top + plotH - ((v - yMin) / (yMax - yMin)) * plotH; }
let noisyPath = `M ${xPos(0)} ${yPos(noisyVals[0])}`;
let smoothPath = `M ${xPos(0)} ${yPos(smoothVals[0])}`;
let truePath = `M ${xPos(0)} ${yPos(trueVals[0])}`;
for (let i = 1; i < nPts; i++) {
noisyPath += ` L ${xPos(i)} ${yPos(noisyVals[i])}`;
smoothPath += ` L ${xPos(i)} ${yPos(smoothVals[i])}`;
truePath += ` L ${xPos(i)} ${yPos(trueVals[i])}`;
}
const mse = noisyVals.reduce((sum, v, i) => sum + (smoothVals[i] - trueVals[i]) ** 2, 0) / nPts;
const lag = sig === "Step change at t=25" ? Math.round(-1 / Math.log(1 - alpha)) : 0;
return html`<div style="background: var(--bs-light, #f8f9fa); padding: 1.2rem; border-radius: 8px; border-left: 4px solid #3498DB; color: var(--bs-body-color);">
<h4 style="margin-top: 0; color: #2C3E50;">Exponential Smoothing: alpha = ${alpha.toFixed(2)}</h4>
<svg width="${w}" height="${h}" style="font-family: Arial, sans-serif; max-width: 100%;" viewBox="0 0 ${w} ${h}">
<!-- Grid lines -->
${[0, 0.25, 0.5, 0.75, 1].map(f => {
const y = pad.top + f * plotH;
const val = (yMax - (yMax - yMin) * f).toFixed(1);
return `<line x1="${pad.left}" y1="${y}" x2="${w - pad.right}" y2="${y}" stroke="#dee2e6" stroke-width="0.5"/>
<text x="${pad.left - 5}" y="${y + 4}" text-anchor="end" font-size="10" fill="#7F8C8D">${val}</text>`;
}).join("")}
<!-- Axes -->
<line x1="${pad.left}" y1="${pad.top}" x2="${pad.left}" y2="${h - pad.bottom}" stroke="#2C3E50" stroke-width="1"/>
<line x1="${pad.left}" y1="${h - pad.bottom}" x2="${w - pad.right}" y2="${h - pad.bottom}" stroke="#2C3E50" stroke-width="1"/>
<text x="${w / 2}" y="${h - 5}" text-anchor="middle" font-size="12" fill="#2C3E50">Sample index</text>
<text x="14" y="${h / 2}" text-anchor="middle" font-size="12" fill="#2C3E50" transform="rotate(-90, 14, ${h / 2})">Value</text>
<!-- Noisy data -->
<path d="${noisyPath}" fill="none" stroke="#E74C3C" stroke-width="1" opacity="0.4"/>
<!-- True signal -->
<path d="${truePath}" fill="none" stroke="#7F8C8D" stroke-width="1.5" stroke-dasharray="6,3"/>
<!-- Smoothed -->
<path d="${smoothPath}" fill="none" stroke="#3498DB" stroke-width="2.5"/>
<!-- Legend -->
<line x1="${pad.left + 10}" y1="${pad.top + 8}" x2="${pad.left + 30}" y2="${pad.top + 8}" stroke="#E74C3C" stroke-width="1.5" opacity="0.5"/>
<text x="${pad.left + 34}" y="${pad.top + 12}" font-size="11" fill="#2C3E50">Noisy</text>
<line x1="${pad.left + 80}" y1="${pad.top + 8}" x2="${pad.left + 100}" y2="${pad.top + 8}" stroke="#7F8C8D" stroke-width="1.5" stroke-dasharray="4,2"/>
<text x="${pad.left + 104}" y="${pad.top + 12}" font-size="11" fill="#2C3E50">True</text>
<line x1="${pad.left + 145}" y1="${pad.top + 8}" x2="${pad.left + 165}" y2="${pad.top + 8}" stroke="#3498DB" stroke-width="2.5"/>
<text x="${pad.left + 169}" y="${pad.top + 12}" font-size="11" fill="#2C3E50">Smoothed</text>
</svg>
<p style="margin-top: 0.6rem; margin-bottom: 0; font-size: 0.9em;">
<strong>MSE (smoothed vs true):</strong> <span style="color: #3498DB; font-family: monospace;">${mse.toFixed(3)}</span>
${sig === "Step change at t=25" ? ` | <strong>Approx. response time:</strong> <span style="color: #E67E22; font-family: monospace;">${lag} samples</span>` : ""}
| <strong>Interpretation:</strong> ${alpha < 0.2 ? "Very smooth output but slow to react to real changes." : alpha < 0.5 ? "Good balance between smoothing and responsiveness." : "Highly responsive but lets more noise through."}
</p>
</div>`;
}