viewof sea_period = Inputs.range([4, 24], {
value: 12, step: 1,
label: "Period (cycle length in samples)"
})
viewof sea_gapStart = Inputs.range([10, 40], {
value: 18, step: 1,
label: "Gap start index"
})
viewof sea_gapLen = Inputs.range([2, 16], {
value: 8, step: 1,
label: "Gap length"
})
viewof sea_amplitude = Inputs.range([1, 8], {
value: 4, step: 0.5,
label: "Seasonal amplitude"
})Try It: Seasonal Decomposition Fill
Try It: Seasonal Decomposition Fill
animation
analytics-ml
data-quality
Try Change Gap length from 8 to 16.
Observe Watch Seasonal-filled values and Mean Absolute Error after pressing Play.
Explain Describe how a gap longer than 1 cycle repeats the seasonal pattern.
Technical boundaries. The signal has 60 samples with a seasonal pattern and slow drift. The algorithm averages valid readings at each phase position to fill gaps.
NoteTry It: Seasonal Decomposition Fill
{
const n = 60;
const period = sea_period;
// Generate signal with clear seasonal pattern + slow drift
const trueSignal = Array.from({length: n}, (_, i) =>
20 + sea_amplitude * Math.sin(2 * Math.PI * i / period) + 0.02 * i + (Math.sin(i * 2.1) * 0.4)
);
// Insert gap
const gapEnd = Math.min(sea_gapStart + sea_gapLen, n);
const withGap = trueSignal.map((v, i) => (i >= sea_gapStart && i < gapEnd) ? null : v);
// Compute seasonal pattern from valid data
const seasonalSum = new Array(period).fill(0);
const seasonalCnt = new Array(period).fill(0);
withGap.forEach((v, i) => {
if (v !== null) {
seasonalSum[i % period] += v;
seasonalCnt[i % period] += 1;
}
});
const seasonalPattern = seasonalSum.map((s, i) => seasonalCnt[i] > 0 ? s / seasonalCnt[i] : null);
// Fill missing using seasonal
const seaFilled = withGap.map((v, i) => v !== null ? v : seasonalPattern[i % period]);
const width = 620;
const height = 280;
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 allV = trueSignal;
const yMin = Math.min(...allV) - 1;
const yMax = Math.max(...allV) + 1;
const xS = (i) => margin.left + (i / (n - 1)) * plotW;
const yS = (v) => margin.top + plotH - ((v - yMin) / (yMax - yMin)) * plotH;
const truePath = trueSignal.map((v, i) => `${i === 0 ? "M" : "L"}${xS(i).toFixed(1)},${yS(v).toFixed(1)}`).join(" ");
const gapShade = `<rect x="${xS(sea_gapStart)}" y="${margin.top}" width="${xS(Math.min(gapEnd - 1, n - 1)) - xS(sea_gapStart)}" height="${plotH}" fill="#9B59B6" opacity="0.08" rx="3"/>`;
const origDots = withGap.map((v, i) => v !== null ? `<circle cx="${xS(i).toFixed(1)}" cy="${yS(v).toFixed(1)}" r="2" fill="#2C3E50"/>` : "").join("");
const filledDots = [];
let seaErr = 0;
let seaCnt = 0;
for (let i = sea_gapStart; i < gapEnd && i < n; i++) {
if (seaFilled[i] !== null) {
filledDots.push(`<circle cx="${xS(i).toFixed(1)}" cy="${yS(seaFilled[i]).toFixed(1)}" r="5" fill="#9B59B6" opacity="0.85"/>`);
seaErr += Math.abs(seaFilled[i] - trueSignal[i]);
seaCnt++;
}
}
const seaMAE = seaCnt > 0 ? (seaErr / seaCnt).toFixed(3) : "N/A";
// Vertical lines showing period boundaries
const periodLines = [];
for (let p = period; p < n; p += period) {
periodLines.push(`<line x1="${xS(p)}" y1="${margin.top}" x2="${xS(p)}" y2="${margin.top + plotH}" stroke="#16A085" stroke-width="0.5" stroke-dasharray="2,4"/>`);
}
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)">Seasonal Fill: period=${period}, amplitude=${sea_amplitude.toFixed(1)}</text>
${gapShade}
${periodLines.join("")}
<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}
${filledDots.join("")}
<text x="${margin.left + plotW / 2}" y="${height - 5}" text-anchor="middle" font-size="10" fill="var(--bs-body-color)">Sample Index (green dashes = period boundaries)</text>
<rect x="${margin.left + plotW - 165}" y="${margin.top + 5}" width="160" height="40" fill="var(--bs-body-bg, white)" stroke="#dee2e6" rx="3"/>
<circle cx="${margin.left + plotW - 152}" cy="${margin.top + 18}" r="2" fill="#2C3E50"/>
<text x="${margin.left + plotW - 145}" y="${margin.top + 22}" font-size="9" fill="var(--bs-body-color)">Original (true signal dashed)</text>
<circle cx="${margin.left + plotW - 152}" cy="${margin.top + 33}" r="5" fill="#9B59B6" opacity="0.85"/>
<text x="${margin.left + plotW - 145}" y="${margin.top + 37}" font-size="9" fill="var(--bs-body-color)">Seasonal-filled values</text>
</svg>
<p style="margin: 0.5rem 0 0 0; font-size: 0.9em;">
<strong>Mean Absolute Error:</strong> <span style="color:#9B59B6; font-weight:bold;">${seaMAE}</span> |
The algorithm averages all valid readings at each phase position (index mod ${period}) and uses that pattern to fill gaps.
${sea_gapLen > period ? "Note: the gap spans more than one full cycle, so the seasonal pattern repeats within the gap." : "The gap is shorter than one cycle -- seasonal fill uses the learned pattern for each phase position."}
</p>
</div>`;
}