Chapter 1: 你的生活全是时间序列 / Your Life is Full of Time Series
上周三你放学绕路去买的那杯加了双倍芋圆的奶茶,花了22块,比上周同一家店同一款贵了4块;上周五的数学小测你拿了78分,比上个月的单元测低了9分,你盯着分数条嘀咕“怎么又考砸了”的时候,有没有想过,这些看起来零散的“买了什么、考了多少分”的数据,其实早就排着队,按时间的顺序给你发信号了?这些按时间先后顺序排列、记录着某个事物变化过程的数据集合,就是我们今天要聊的主角——时间序列(Time Series,简称TS),而你已经和它打了十几年交道,只是之前从来没给它起过名字。
Last Wednesday, the bubble tea with extra taro balls you detoured to buy after school cost 22 yuan, 4 yuan more than the same drink at the same shop the week before. Last Friday's math quiz you scored 78 points, 9 points lower than the unit test the month before. While you stared at the score and muttered "why did I mess up again", did you ever think that these seemingly scattered data points about what you bought and what scores you got are actually lining up in chronological order to send you signals? These data sets arranged in chronological order that record the change process of something are the protagonist we are going to talk about today - Time Series (referred to as TS), and you have been dealing with it for more than ten years, you just never gave it a name before.
一、时间序列的三个“身份证”/ Three "ID Cards" of Time Series
不是所有排好序的数据都叫时间序列,它有三个缺一不可的核心要素,就像你的学生证必须有姓名、学号、照片才有效一样:
1. 时间戳(Timestamp):每个数据的“出生证明”
时间序列里的每一个数据点,都必须对应一个明确的时间点,这个时间就是它的“时间戳”。比如你每次打王者荣耀的时候,系统自动记录的“2024年5月20日 19:32:17 击杀数12”,这里的“2024年5月20日 19:32:17”就是时间戳,它不是你随便编的,是系统按真实时间记的。再比如你每次考试的分数,对应的“2024年4月15日 数学期中考试”就是时间戳,没有时间戳的数据,根本不知道自己是什么时候发生的,就没意义了。
Every data point in a time series must correspond to a clear time point, which is its "timestamp". For example, every time you play Honor of Kings, the system automatically records "2024-05-20 19:32:17 Kills 12", where "2024-05-20 19:32:17" is the timestamp, which is not made up by you, but recorded by the system according to the real time. Another example is your test score, the corresponding "2024-04-15 Math Midterm Exam" is the timestamp, data without a timestamp doesn't know when it happened, so it is meaningless.
2. 观测值(Observed Value):每个数据的“真实内容”
时间戳对应着具体的数值,这个数值就是观测值,它是你要记录的那个事物的具体表现。比如刚才的击杀数12,就是你打游戏时的观测值;考试分数78,就是你学习情况的观测值;奶茶价格22块,就是你消费的观测值。观测值可以是数字,也可以是类别,比如你每次记录的“今天喝的是奶茶/果茶/咖啡”,也是观测值,只是不是数字而已。
The timestamp corresponds to a specific value, which is the observed value, it is the specific performance of the thing you want to record. For example, the 12 kills just now is the observed value when you play games; the test score 78 is the observed value of your study situation; the milk tea price of 22 yuan is the observed value of your consumption. Observed values can be numbers, or categories, for example, the "today's drink is milk tea/fruit tea/coffee" you record every time is also an observed value, just not a number.
3. 顺序性(Order):时间序列的“灵魂”
这是时间序列和普通表格数据最大的区别:你不能随便调换数据点的顺序,一旦调换,整个序列就失去了意义。比如你过去7天的步数是[6500, 8200, 4200, 7500, 3000, 9000, 6000],你要是把周日的6000步移到周一,变成[6000, 8200, 4200, 7500, 3000, 9000, 6500],那这个序列就完全错了——你妈问你“周末是不是偷懒没出门”,你就没法用这个数据证明你周日走了6000步了。再比如你每次考试的分数,要是把期中的78分移到期末,你就没法看出你这学期是进步还是退步了。
This is the biggest difference between time series and ordinary table data: you can't change the order of data points at will, once you change it, the whole series loses its meaning. For example, your steps in the past 7 days are [6500, 8200, 4200, 7500, 3000, 9000, 6000], if you move the 6000 steps on Sunday to Monday, it becomes [6000, 8200, 4200, 7500, 3000, 9000, 6500], then this series is completely wrong - when your mom asks you "did you lazy and didn't go out on the weekend", you can't use this data to prove that you walked 6000 steps on Sunday. Another example is your test scores every time, if you move the 78 points in the midterm to the final, you can't see whether you have made progress or regressed this semester.
什么不是时间序列?
为了帮你彻底搞懂,咱们来做个排除题:
- 你班同学的身高,按从高到低排序:不是,因为没有时间顺序,只是按身高排序。
- 你们班上周数学考试分数,按从高到低排序:不是,同样没有时间顺序。
- 你喜欢的5款奶茶的价格,按从便宜到贵排序:不是,没有时间维度。
- 你过去7天每天的奶茶消费金额,按日期排序:是,符合三个要素。
- 你从初一到初三每次期中考试的数学分数,按考试时间排序:是,符合三个要素。
To help you understand thoroughly, let's do an elimination game:
- Your classmates' heights, sorted from high to low: No, because there is no time order, just sorted by height.
- Last week's math test scores of your class, sorted from high to low: No, also no time order.
- The prices of your favorite 5 milk teas, sorted from cheap to expensive: No, no time dimension.
- Your milk tea consumption amount in the past 7 days, sorted by date: Yes, meets the three elements.
- Your math scores in every midterm exam from the first year to the third year of junior high, sorted by exam time: Yes, meets the three elements.
二、时间序列的“四大性格”/ Four "Personalities" of Time Series
每个时间序列都有自己的变化规律,咱们可以把这些规律总结成四个最典型的“性格”,不管是你每天的学习时长,还是奶茶店的销量,都逃不开这四个特点:
1. 趋势(Trend):长期往哪个方向走?
趋势就是时间序列在很长一段时间里的整体变化方向,就像你从初一到初三,身高整体在往上长,这就是“上升趋势”;如果你最近天天熬夜刷番,睡眠时长从8小时降到5小时,这就是“下降趋势”;如果你每天喝的冰美式价格一直是15块,一年都没变,这就是“平稳趋势”。
Trend is the overall change direction of a time series over a long period of time, just like your height is growing overall from the first year to the third year of junior high, this is an "upward trend"; if you stay up late watching anime recently, your sleep duration drops from 8 hours to 5 hours, this is a "downward trend"; if the price of iced Americano you drink every day has always been 15 yuan and has not changed for a year, this is a "stable trend".
2. 季节性(Seasonality):到点就来的“固定节目”
季节性是指时间序列在固定的日历周期里重复出现的规律,它和时间绑定得非常紧,每年、每月、每周的固定时间一定会出现。比如每年6-8月夏天,你喝的冰奶茶消费肯定会比冬天高2倍,这就是“年度季节性”;每周一到周五你都要早起上课,所以闹钟的响起时间就是“周度季节性”;每年开学前一周,你的文具消费肯定会翻5倍,因为要买新本子、新笔,这也是季节性。季节性的核心是“固定周期、规律出现、和日历绑定”。
Seasonality refers to the pattern that repeats in a fixed calendar cycle of a time series, it is very closely bound to time, and it will definitely appear at a fixed time every year, every month, and every week. For example, every June to August in summer, your iced milk tea consumption will definitely be 2 times higher than in winter, this is "annual seasonality"; from Monday to Friday every week you have to get up early for class, so the time the alarm goes off is "weekly seasonality"; one week before the start of school every year, your stationery consumption will definitely increase 5 times, because you need to buy new notebooks and pens, this is also seasonality. The core of seasonality is "fixed cycle, regular occurrence, bound to the calendar".
3. 周期性(Cyclicity):事件驱动的“临时节目”
周期性和季节性很像,也是重复出现的规律,但它不和日历绑定,是由特定事件触发的,周期也不固定。比如你每次月考完都要买一杯奶茶奖励自己,月考是一个月一次,所以你的奶茶消费就有“月度周期性”;再比如你每次参加完篮球赛都要买一瓶运动饮料,篮球赛的时间不固定,有时候一个月一次,有时候两个月一次,所以运动饮料的消费就有“赛事驱动的周期性”。周期性和季节性的最大区别是:季节性是“到点就来,不会缺席”,周期性是“事件触发,可能缺席”——如果这个月你月考没考好,没买奶茶奖励自己,那周期性就断了,但夏天的冰奶茶消费不会断,除非你突然不爱喝了。
Cyclicity is similar to seasonality, it is also a repeating pattern, but it is not bound to the calendar, it is triggered by specific events, and the cycle is not fixed. For example, you buy a cup of milk tea as a reward every time you finish a monthly exam, the monthly exam is once a month, so your milk tea consumption has a "monthly cyclicity"; another example is that you buy a sports drink every time you finish a basketball game, the time of the basketball game is not fixed, sometimes once a month, sometimes once every two months, so the consumption of sports drinks has a "event-driven cyclicity". The biggest difference between cyclicity and seasonality is:
seasonality is "comes on time, never misses class", while cyclicity is "triggered by events, may skip class" - if you didn't buy milk tea as a reward because you messed up the monthly exam this month, the cyclicity is broken, but iced milk tea consumption in summer won't break unless you suddenly stop liking it. The most intuitive example for you gamers: the anime you follow updates 1 episode every Friday, which is weekly seasonality - unless the official stops updating, you can always watch new content on Bilibili every Friday. But if the official releases a special episode without a fixed schedule, sometimes on Wednesday and sometimes on Saturday, this is event-driven cyclicity: the update is the trigger event, the cycle is not fixed, and it may not be updated next week. Anoth