×
Daily Seasonality and Anomalies
Daily Seasonality and Anomalies: Weekday Pattern, Robust Z-Score, Flagged Days and Their Chance Count
The tool takes dated daily rows (date, value) and separates them into a slowly moving level, a weekday pattern and what is left over. It then marks the days whose leftover is unusually large, and prints beside that count how many such days noise alone would produce. Flags are days that differ from the fitted pattern; the page does not say why.
The idea: level, weekday, residual
Most daily business series repeat every week: web visits dip on weekends, a shop peaks on Saturday. A day can only be called unusual against what is usual for that weekday at the current level. So the series is split in three parts, and a day is judged by the third.
The value follows the level times the weekday index; a day outside the band is flagged.
The equations
Trend. The level is a centered moving median: for each day, the median of the days around it. The median is used because a few extreme days inside the window do not move it, and because it follows a sudden change of level in one step where a mean would smear it over the whole window. With an even window of m days the page takes m + 1 days and gives the two end days half weight, as the 2 × m moving average of classical decomposition does (NIST e-Handbook, section 6.4.2.2); for the median this is the weighted median with those weights. With a window of 28 days every weekday then carries the same weight. A day gets a trend when its window holds at least four usable days carrying at least a quarter of the window's weight.
Weekday pattern. Each value is divided by its trend (or the trend is subtracted), and the median of these figures is taken weekday by weekday. The seven medians are rescaled so that the indices average 1 (the effects add to 0), which is the adjustment of classical decomposition (Hyndman and Athanasopoulos, section 3.4), with medians in place of means.
Three passes. The median of a window that mixes busy weekdays and quiet weekends is a jumpy level: it sits wherever the border between the two groups happens to fall. So the page fits trend and pattern in turn three times. The first pass is the classical one. In the second and third, the trend is the moving median of the values with the current weekday pattern taken out, and the pattern is then fitted again against that trend. This also keeps the cut-off windows at the two ends of the record from leaning toward whichever weekdays they happen to hold.
Robust scale and flags. The residuals are judged against their own scatter, measured by the median absolute deviation (MAD) times 1.4826, the factor that makes it equal to the standard deviation for normal data. The NIST e-Handbook (section 1.3.5.17) gives the same score as the modified z-score 0.6745 (x − median) ÷ MAD (0.6745 = 1 ÷ 1.4826) and quotes the limit 3.5 from Iglewicz and Hoaglin for labeling potential outliers. A standard deviation would be inflated by the very days one is looking for; the MAD is not.
The chance count. With normal residuals, a share 2 × (1 − Φ(limit)) of the days lies beyond the limit by chance: 0.05 % at 3.5, 0.27 % at 3, 4.6 % at 2. The page prints this count. It also prints a second, larger one: it generates records of pure independent normal noise on the same days (100 for a record of up to 200 days, fewer for longer records, never under 25; seeded, so the same input gives the same figures), puts each through the same fit, and counts the flags. Fitting a level and seven weekday medians to the data pulls the residual scale down a little, so pure noise gives more flags than the normal tail says: at a limit of 3.5, from about twice to five times as many in this page's own example cases. That second figure is the one to compare the count found with.
Special dates. A holiday or a promotion you enter is left out of the trend, the pattern and the scale, and is never flagged. It is reported with its effect: the value against the expected value of that weekday at that level.
Change of level. A moving median steps with a sudden change of level, so the days after a launch are not flagged. The page therefore also compares, at every day, the median of the next h days with the median of the h days before (weekday pattern taken out; h is half the trend window, at least 7), and reports the largest change with its standard error s × √(π ÷ h), the large-sample standard error of a difference of two medians of h normal values. Because it is the largest of many overlapping comparisons, it is printed beside the same statistic of the pure-noise records.
Run rule. The page counts runs of 8 or more (your figure) tested days in a row on the same side of the median residual. Eight in a row on one side of the center line is one of the Western Electric rules listed in the NIST e-Handbook (section 6.3.2). A day whose residual sits exactly on the median (the day that is its weekday's median does) has no side and is passed over. For independent residuals a record of n days holds (n − k + 2) ÷ 2k such runs of length k or more by chance; residuals from a moving trend hold fewer, and the pure-noise records say how many.
Weekday intervals. The calendar weeks are drawn again with replacement 1,000 times, the weekday medians recomputed each time with the trend held fixed, and the interval read off between the matching percentiles (a percentile bootstrap with weeks as blocks). Holding the trend fixed leaves its own uncertainty out, so the intervals are somewhat too narrow.
Month pattern. With two years or more, the level of each day is fitted by least squares as a straight long-run line plus an effect of the calendar month (on the logarithm of the level in the multiplicative model). The month pattern is a description of the level; the expected values already contain it, because the moving level follows the season.
The rows the page reads: one a day; a blank value is a missing day.
What it assumes
One weekday pattern holds for the whole record. A pattern that changed half way (weekend opening hours, a new audience) is averaged over both halves.
The level moves slowly compared with the trend window, apart from sudden steps.
Residuals are roughly normal apart from the unusual days, with the same scatter on every weekday and at every level (in percent for the multiplicative model, in amounts for the additive one).
Days are independent once level and weekday are taken out. A campaign that lifts five days in a row breaks this.
Special dates are the ones you entered. A holiday not entered is treated as an ordinary day.
When it misleads
Small counts. A series of a few events a day is far from normal and far from constant percent scatter; quiet days get large negative z-scores and the chance counts do not apply.
The wrong model. If the weekend dip is a fixed number of visits, the multiplicative model overstates it when the level is high; if it is a fixed percentage, the additive model does. The calendar heat map shows it: residuals of one weekday change sign as the level changes.
The ends of the record. In the first and last half window the level rests on one side only. On a rising series the latest days then sit above a lagging level and can be flagged for that reason alone. The flagged-days table marks them.
Days next to a holiday. The day before or after an entered special date often moves too. The table notes such neighbors; they are flagged as ordinary days unless you enter them.
Many unusual days. The median and the MAD tolerate a minority of unusual days. If a third of a window is a promotion, the level follows the promotion.
Different scatter by weekday. If weekends are noisier than weekdays, weekend days are flagged more often than their share.
The largest change. A steady trend and a season produce a change between neighboring spans as well; the pure-noise reference has neither, so a large figure is not by itself a step.
A low limit. At a limit of 2, one day in twenty is flagged by chance. The count found means little without the chance count beside it.
Common mistakes
Comparing a day with the overall average. A Saturday 30 % under the average is an ordinary Saturday when Saturdays run 30 % low.
Using mean and standard deviation to find outliers: one outage day inflates the standard deviation and hides itself and its neighbors.
Using a moving window that is not a multiple of 7, so that the weekday pattern leaks into the level.
Counting flags without asking how many arise by chance. With a year of days and a limit of 2.5, four or five flags are what noise gives.
Leaving holidays in: they pull the pattern of their weekday and widen the scale, and so hide other days.
Reading a missing row as zero (or a zero as missing). Say which one a day without a row is.
Taking a flagged day as explained. The arithmetic says the day differs from the pattern, not what happened.
Related tools
This page adds dated daily rows, the weekday pattern, special dates and flagged days with their chance count to its neighbors: Forecasting decomposes a regular monthly or quarterly series and projects it ahead; Forecast Value Added measures the error of forecasts already made; Data Summary and Outliers finds outliers in a set of values without dates; CUSUM and EWMA Charts signal small sustained shifts of a measured process against a target; A/B Test compares two groups run side by side, which a before-and-after reading of one daily series cannot replace.
Close