Try It: Exponential Smoothing Explorer
Try It: Exponential Smoothing Explorer
animation
analytics-ml
data-quality
Try Change Smoothing factor (alpha) from 0.3 to 0.7.
Observe Compare Smoothed with Raw signal and watch the time constant at alpha 0.7.
Explain Explain why alpha 0.7 passes more noise through than alpha 0.3.
Technical boundaries. The tool uses a generated 40-sample noisy signal with a step change from 22.0 to 25.0. Smoothing starts from the first raw sample.
Try It: Exponential Smoothing Explorer
{
// Generate a noisy signal with a step change
const n = 40;
const raw = Array.from({length: n}, (_, i) => {
const base = i < 20 ? 22.0 : 25.0;
const noise = (Math.sin(i * 3.7) * 0.8 + Math.cos(i * 7.1) * 0.5);
return base + noise;
});
// Apply exponential smoothing
const smoothed = [];
let s = raw[0];
for (let i = 0; i < n; i++) {
s = expAlpha * raw[i] + (1 - expAlpha) * s;
smoothed.push(s);
}
const timeConstant = (-1 / Math.log(1 - expAlpha)).toFixed(1);
const width = 580;
const height = 220;
const margin = {top: 25, right: 15, bottom: 30, left: 45};
const plotW = width - margin.left - margin.right;
const plotH = height - margin.top - margin.bottom;
const yMin = Math.min(...raw, ...smoothed) - 0.5;
const yMax = Math.max(...raw, ...smoothed) + 0.5;
const xScale = (i) => margin.left + (i / (n - 1)) * plotW;
const yScale = (v) => margin.top + plotH - ((v - yMin) / (yMax - yMin)) * plotH;
const rawPath = raw.map((v, i) => `${i === 0 ? "M" : "L"}${xScale(i).toFixed(1)},${yScale(v).toFixed(1)}`).join(" ");
const smoothPath = smoothed.map((v, i) => `${i === 0 ? "M" : "L"}${xScale(i).toFixed(1)},${yScale(v).toFixed(1)}`).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="14" text-anchor="middle" font-size="12" font-weight="bold" fill="var(--bs-body-color)">Exponential Smoothing: alpha = ${expAlpha.toFixed(2)} (time constant = ${timeConstant} samples)</text>
<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="10" 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("")}
<text x="${margin.left + plotW/2}" y="${height - 3}" text-anchor="middle" font-size="10" fill="var(--bs-body-color)">Sample</text>
<path d="${rawPath}" fill="none" stroke="#7F8C8D" stroke-width="1" stroke-dasharray="3,2"/>
<path d="${smoothPath}" fill="none" stroke="#16A085" stroke-width="2"/>
<rect x="${margin.left + plotW - 140}" y="${margin.top + 5}" width="135" height="35" fill="var(--bs-body-bg, white)" stroke="#dee2e6" rx="3"/>
<line x1="${margin.left + plotW - 133}" y1="${margin.top + 17}" x2="${margin.left + plotW - 115}" y2="${margin.top + 17}" stroke="#7F8C8D" stroke-width="1" stroke-dasharray="3,2"/>
<text x="${margin.left + plotW - 110}" y="${margin.top + 21}" font-size="10" fill="var(--bs-body-color)">Raw signal</text>
<line x1="${margin.left + plotW - 133}" y1="${margin.top + 31}" x2="${margin.left + plotW - 115}" y2="${margin.top + 31}" stroke="#16A085" stroke-width="2"/>
<text x="${margin.left + plotW - 110}" y="${margin.top + 35}" font-size="10" fill="var(--bs-body-color)">Smoothed</text>
</svg>
<p style="margin-top: 0.5rem; margin-bottom:0; font-size: 0.9em;"><strong>Interpretation:</strong> ${expAlpha < 0.15
? "Very heavy smoothing - slow to respond to real changes but excellent noise rejection."
: expAlpha < 0.4
? "Balanced smoothing - good noise reduction while tracking gradual trends."
: expAlpha < 0.7
? "Light smoothing - responsive to changes but passes more noise through."
: "Minimal smoothing - nearly passes raw signal through. Consider a lower alpha."}</p>
</div>`;
}