From cfce00cd6346385f55dd04365089b0842fcd202b Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Wed, 28 Jan 2026 13:11:08 +0100 Subject: [PATCH] Fix seasonality bias: Default to flat weighting if history is sparse (<4 months) --- services/dataProcessor.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/services/dataProcessor.ts b/services/dataProcessor.ts index cb501eb..54eadb5 100644 --- a/services/dataProcessor.ts +++ b/services/dataProcessor.ts @@ -1628,15 +1628,25 @@ export const calculateForecastViewData = ( const getWeights = (records: SalesRecord[]) => { const weights = new Array(12).fill(0); let total = 0; + const seenMonths = new Set(); + records.forEach(r => { const m = r.month.split('-')[0]; const idx = MONTH_ORDER.indexOf(m); if (idx !== -1) { weights[idx] += r.units; total += r.units; + if (r.units > 0) seenMonths.add(m); } }); - if (total === 0) return new Array(12).fill(1 / 12); + + // Safety Fallback: If we have sparse data (e.g., only Jan loaded), + // using it as 100% seasonality skews the forecast entirely to that month. + // We require at least 4 months of history to trust the curve; otherwise, we assume flat seasonality (1/12). + if (total === 0 || seenMonths.size < 4) { + return new Array(12).fill(1 / 12); + } + return weights.map(w => w / total); };