Trends and seasons
Last updated
January's takings were 19% below December's. Is the business in trouble? Every January is below December. Monthly data mixes three things: a slow trend, a pattern that repeats every year, and noise. In this lesson you'll compare months with the same month a year earlier, split three years of takings into trend, season and remainder with STL, and allow for the calendar itself: months of different lengths, and days the stores were shut.
By the end of this lesson you can
- Compare a month with the same month a year earlier
- Decompose a series into trend, seasonal and remainder parts with STL
- Allow for calendar effects such as days in the month
Wrong way: the January slump
The owners' January 2024 report opens: "Sales fell 19% from December. We need to act." Run the cells.
December 2023 took ₱334,815 and January 2024 ₱271,043: down 19.0%. True, and meaningless on its own. December is Christmas: bonuses (the 13th-month pay), Noche Buena shopping, family visiting. January is the month after. The same drop happens every year.
The second cell compares each month of 2024 with the same month of 2023, with pct_change(12): 12 rows back. January 2024 was 10.4% above January 2023. Far from a slump, it was one of the best months of the year. Only October 2024 (−3.8%) is below the year before, and 9.1 found why.
The check: for anything with a yearly pattern (sales, rainfall, school enrolment, tourism), compare a month with the same month a year earlier, not the month before. The year-over-year change takes the season out.
Trend, season and noise
A monthly series can be thought of as three parts added together:
is the value in month ; is the trend, the slow, long-run movement; is the seasonal part, a pattern that repeats every 12 months; and is the remainder, what's left. Step through a simple way to find them:
Splitting a series like this is called decomposition. Each part answers a different question: is the business growing (trend)? When should we stock up (season)? Did something unusual happen (remainder)?
If the seasonal swings grow as the series grows (December 20% above, whatever the level), the parts multiply instead: . Taking logs turns that into the additive form.
STL in statsmodels
The moving average in the animation loses six months at each end, and one odd month pulls the seasonal pattern around. STL (Seasonal-Trend decomposition using Loess, Cleveland and colleagues, 1990) does better: it fits the trend and season by repeated local regression, keeps every month, and with robust=True stops odd months from distorting the parts. Run the cells.
STL(total["pesos"], period=12, robust=True).fit():period=12says the pattern repeats every 12 rows.res.plot()draws the data and the three parts.- The trend rose from about ₱236,625 a month to ₱293,020 over the three years.
- The season, in 2024: December is ₱64,900 above the trend; February ₱30,900 below; June ₱12,300 above (school opening), and April ₱18,100 below (Holy Week). That's a stock plan.
- The check cell shows trend + seasonal + remainder add back to the data exactly.
Look at October in the seasonal part: ₱21,100 below the trend, and the biggest remainder is October 2023, above it. The typhoon closures in October 2024 pulled STL's idea of a "normal October" down. With three years, one odd month moves the season; that's a reason to adjust for closures first (step 4), and to treat a pattern from only a few years as a rough guide.
Calendar effects
Some of the "season" is just the calendar. February has 28 or 29 days; March has 31. A store closed four days takes less. Divide by the days open (9.1) before comparing. Run the cell.
| total | per day open | |
|---|---|---|
| Feb 2024 | −9.2% | −3.0% |
| Mar 2024 | +13.9% | +6.6% |
| Oct 2024 | −2.2% | +3.9% |
| Nov 2024 | +8.8% | +2.4% |
Two-thirds of February's fall and half of March's rise were just the number of days. October went from a fall to a rise. Other calendar effects worth knowing in the Philippines:
- Paydays on the 15th and 30th: in daily data, the days after them are busier.
- Holidays that move: Holy Week falls in March or April, so comparing April with April can mean comparing a holiday month with an ordinary one.
- The number of each weekday: a month with five Saturdays sells more.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- statsmodels: statsmodels.tsa.seasonal.STL: STL, after Cleveland, Cleveland, McRae and Terpenning (Journal of Official Statistics, 1990)
- statsmodels: Seasonal-Trend decomposition using LOESS (STL): STL, period and robust fitting, with examples
- Hyndman and Athanasopoulos, Forecasting: Principles and Practice, 3rd ed., chapter 3: time series components, moving averages and STL, free online
- pandas: Series.pct_change: periods=12 for the change from a year earlier