Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

DA 3.3, 3.4: Time series: trends and seasons

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

55 minutes

Why is Saturday always busy?

A made-up cafe writes down its sales every day for two weeks. Saturday is always the best day. Sunday is always the worst. And each week is a bit better than the last.

Three different patterns are mixed in one list of numbers. Time series analysis pulls them apart.

Key words

WordWhat it meansExample
SQLA language for asking questions of data held in tables.select avg(sales) from daily
Time seriesNumbers recorded in time order, such as sales each day.Daily cafe sales
TrendThe long-term direction: up, down or flat.Each week is better
SeasonalityA pattern that repeats at a fixed length of time.A busy Saturday every week
NoiseSmall random ups and downs with no pattern.A sunny hour brings extra customers
StationaryA series whose average and spread stay about the same over time.Sales that go up and down around 50
Moving averageThe average of the last few values, moved along one step at a time.The last 7 days
Window functionAn SQL tool that looks at nearby rows while it keeps every row.avg over the last 7 rows
LagThe value from an earlier row.Sales on the same day last week

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can tell trend, seasonality and noise apart. I can say what stationary means. I can use a moving average and a lag in SQL to see daily and weekly patterns.

Starter: what you already know

Have a go

Starter: what you already know

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Three parts of a time series

Every time series can be split into parts.

  1. Trend. Where the numbers are heading over weeks or months.
  2. Seasonality. A pattern that repeats on a fixed cycle. A week is a cycle. So is a year.
  3. Noise. What is left over: small random changes.

Worked example: the cafe, part by part

PartWhat we seeHow to find it
TrendWeek 2 is 8 higher than week 1 on every dayCompare with the same day last week
SeasonalityFriday and Saturday are high, Sunday is lowAverage each weekday
NoiseSmall changes no pattern explainsSmooth with a moving average

The cells students fill in are left blank in this preview.

Try it: a seven day moving average

Code: duckdb
select day, weekday, sales,
       round(avg(sales) over (order by day rows between 6 preceding and current row), 1) as week_average
from daily
order by day;

-- From day 7, the average covers a whole week. The weekday pattern cancels out and the trend shows.

Try it: the weekly pattern

Code: duckdb
select weekday, avg(sales) as average_sales
from daily
group by weekday
order by min(day);

-- Which weekdays are above 60? That is the seasonality.

Stationary series

A series is stationary when its average and its spread stay about the same over time. Sales that bounce around 50 for ever are stationary.

The cafe is not stationary, because its average climbs. Many methods work best on a stationary series. One fix is to look at the change from the same day last week. If the trend is steady, those changes are all about the same.

The lesson goes on

5 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.