Professional Documents
Culture Documents
Sondre Hølleland
1/26/23
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 1
Outline
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 2
Calender adjustments
$75k
$70k
Total production
$65k
$60k
2015 Jan 2016 Jan 2017 Jan 2018 Jan 2019 Jan 2020 Jan
Figure 1: production YearMonth
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 3
Calender adjustments
$3.18k
$3.16k
Mean production
$3.14k
$3.12k
$3.1k
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 4
Calender Adjustments
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 5
Population adjustment
Let Yt denote a quantity of interested that is somehow related to population size Popt .
Population adjusted quantity: Yt /Popt .
Examples:
▶ GDP per capita
▶ Car accidents per driver / driven 1000km
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 6
Population adjustment
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 7
Population adjustment
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 8
Population adjustment
scandinaviaUSA %>%
autoplot(GDP)+
labs(title= "GDP", y = "$US")
GDP
2.0e+13
1.5e+13
Country
Denmark
$US
1.0e+13 Norway
Sweden
United States
5.0e+12
0.0e+00
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 9
Population adjustment
scandinaviaUSA %>%
autoplot(Population)+
labs(title= "Population", y = "Number of people")
Population
3e+08
Number of people
Country
2e+08
Denmark
Norway
Sweden
United States
1e+08
0e+00
1960 1980 2000
Year [1Y]
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 10
Population adjustment
scandinaviaUSA %>%
autoplot(Population)+
scale_y_log10()+
labs(title= "Population (log-scale)",y = "Number of people")
Population (log−scale)
3e+08
1e+08
Number of people
Country
Denmark
Norway
3e+07
Sweden
United States
1e+07
3e+06
1960 1980 2000
Year [1Y]
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 11
Population adjustment
scandinaviaUSA %>%
autoplot(GDP/Population) +
labs(title= "GDP per capita = GDP / Population", y = "$US")
100000
75000
Country
Denmark
$US
Norway
50000
Sweden
United States
25000
0
1960 1980 2000
Year [1Y]
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 12
Inflation adjustment
scandinaviaUSA %>%
autoplot(GDP/Population / CPI*100) +
labs(title= "GDP per capita (inflation adjusted)", y = "$US (2010)")
75000 Country
$US (2010)
Denmark
Norway
Sweden
50000
United States
25000
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 13
Consumer price index
CPI %>%
autoplot(CPI, color = "blue", lwd = 1.2) +
labs(title= "Consumer Price Index", y = "NOK",
subtitle = "Data source: Statistics Norway")+
geom_hline(yintercept = 100, lty = 2) + geom_vline(xintercept = 2015, lty = 2)+
scale_x_continuous(breaks = seq(1925,2025,10))+
scale_y_continuous(breaks = seq(0,120,10))
60
50
40
30
20
10
0
1925 1935 1945 1955 1965 1975 1985 1995 2005 2015 2025
Year [1Y]
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 14
Inflation adjusted big mac prices in Norway
norBigMac %>%
autoplot(local_price) +
labs(title= "Big Mac price in Norway", y = "NOK",
subtitle = "Data source: The Economist") +
geom_smooth(method = "lm", se=FALSE)
55
50
NOK
45
40
35
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 15
Inflation adjusted big mac prices in Norway
norBigMac %>%
mutate(cpiAdjusted =local_price / CPI * 100) %>%
as_tsibble(index = date) %>%
autoplot(cpiAdjusted)+
labs(title= "Inflation adjusted Big Mac price in Norway", y = "NOK (2015)",
subtitle = "Data sources: The Economist (big mac index), Statistics Norway (CPI)") +
geom_smooth(method = "lm", se=FALSE)
48
NOK (2015)
44
40
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 16
Mathematical transformations
Goal: transform the data to something that is more linear and/or stationary.
Logarithm: wt = log yt
Difference: wt = yt − yt−1
Power transformation: wt = ytp for p ∈ {1/2, 1/3, . . .}.
Box-Cox transformation: (
log yt if λ = 0,
wt = λ
(yt − 1)/λ if λ ̸= 0.
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 17
Mathematical transformations: Differencing for a trend
Say
70
yt = µ + α t + ϵt
y
60
Then
50
wt = yt − yt−1
= µ + α t + ϵt − µ − α (t − 1) − ϵt−1 4
= α + ϵt − ϵt−1 0
w
−4
−8
0 25 50 75 100
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 18
Mathematical transformations: Stock prices
Apple Closing Price transformations
5
Log : wt = log yt 150
4
logclose
close
100
Diff : wt = yt − yt−1 50
3
2
√ 1/2 0 1
Squareroot : wt = yt = yt
5
√
squareroot
10
1/3
thirdroot
4
Thirdroot : wt = 3 yt = yt 3
5
yt − yt−1 2
Returns: wt = .
yt−1 6 10
diffclose
boxcox
Log-returns: wt = log yt − log yt−1 4
0
−5
yt 2
= log . −10
logreturn
yλ − 1 0.0
return
0.0
λ −0.2
2010 2015 2020 2010 2015 2020
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 19
Time series components: Additive
Yt = Tt + St + Rt
Observations: Yt
Trend-Cycle: Tt
Season: St
Remainder / Residual: Rt
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 20
Time series components: Additive
300
200
Yt
100
Yt = Tt + St + Rt
0
Observations: Yt 200
Trend-Cycle: Tt
Tt
100
Season: St
Remainder / Residual: Rt 20
10
St
0
−20
each component.
30
0
Rt
−30
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 21
Time series components: Multiplicative
300
Yt = Tt · St · Rt
log Yt
200
100
or
0
log Yt = log Tt + log St + log Rt
200
log Tt
Observations: log Yt 100
Trend-Cycle: log Tt 20
Season: log St 10
log St
0
Remainder / Residual: −10
log Rt −20
30
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 22
Seasonal adjustment
log Yt − log St .
Seasonal variation not of
primary interest 200
Yt − St
Ex: State of economy
(positive/negative cycle) vs
monthly variations 100
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 23
Decomposition methods
Classical decomposition
X11
Seats
STL
But first, some moving averages. . .
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 24
Moving averages
where k = 0, 1, 2, . . ..
The window width is then m = 2k + 1.
m-MA
Ex: 3-MA means k = 1.
If the order m is even, the window become non-symmetric.
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 25
Moving average
set.seed(123)
MAex <- tibble(y = rnorm(10))
MAex
# A tibble: 10 x 1
y
<dbl>
1 -0.560
2 -0.230
3 1.56
4 0.0705
5 0.129
6 1.72
7 0.461
8 -1.27
9 -0.687
10 -0.446
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 26
Moving average
# A tibble: 10 x 2
y `3-MA`
<dbl> <dbl>
1 -0.560 NA
2 -0.230 0.256
3 1.56 0.466
4 0.0705 0.586
5 0.129 0.638
6 1.72 0.768
7 0.461 0.304
8 -1.27 -0.497
9 -0.687 -0.799
10 -0.446 NA
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 27
Moving average
# A tibble: 10 x 3
y `3-MA` `4-MA`
<dbl> <dbl> <dbl>
1 -0.560 NA NA
2 -0.230 0.256 0.210
3 1.56 0.466 0.382
4 0.0705 0.586 0.868
5 0.129 0.638 0.594
6 1.72 0.768 0.260
7 0.461 0.304 0.0560
8 -1.27 -0.497 -0.484
9 -0.687 -0.799 NA
10 -0.446 NA NA
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 28
Moving average
# A tibble: 10 x 4
y `3-MA` `4-MA` `5-MA`
<dbl> <dbl> <dbl> <dbl>
1 -0.560 NA NA NA
2 -0.230 0.256 0.210 NA
3 1.56 0.466 0.382 0.194
4 0.0705 0.586 0.868 0.649
5 0.129 0.638 0.594 0.787
6 1.72 0.768 0.260 0.222
7 0.461 0.304 0.0560 0.0707
8 -1.27 -0.497 -0.484 -0.0443
9 -0.687 -0.799 NA NA
10 -0.446 NA NA NA
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 29
Moving average
# A tibble: 10 x 5
y `3-MA` `4-MA` `5-MA` `6-MA`
<dbl> <dbl> <dbl> <dbl> <dbl>
1 -0.560 NA NA NA NA
2 -0.230 0.256 0.210 NA NA
3 1.56 0.466 0.382 0.194 0.447
4 0.0705 0.586 0.868 0.649 0.617
5 0.129 0.638 0.594 0.787 0.445
6 1.72 0.768 0.260 0.222 0.0706
7 0.461 0.304 0.0560 0.0707 -0.0154
8 -1.27 -0.497 -0.484 -0.0443 NA
9 -0.687 -0.799 NA NA NA
10 -0.446 NA NA NA NA
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 30
Moving average
# A tibble: 10 x 6
y `3-MA` `4-MA` `5-MA` `6-MA` `7-MA`
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -0.560 NA NA NA NA NA
2 -0.230 0.256 0.210 NA NA NA
3 1.56 0.466 0.382 0.194 0.447 NA
4 0.0705 0.586 0.868 0.649 0.617 0.449
5 0.129 0.638 0.594 0.787 0.445 0.348
6 1.72 0.768 0.260 0.222 0.0706 0.283
7 0.461 0.304 0.0560 0.0707 -0.0154 -0.00311
8 -1.27 -0.497 -0.484 -0.0443 NA NA
9 -0.687 -0.799 NA NA NA NA
10 -0.446 NA NA NA NA NA
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 31
Moving average
MAex %>% mutate(
`3-MA` = slider::slide_dbl(y, mean, .before = 1, .after = 1, .complete = TRUE),
`4-MA` = slider::slide_dbl(y, mean, .before = 1, .after = 2, .complete = TRUE),
`5-MA` = slider::slide_dbl(y, mean, .before = 2, .after = 2, .complete = TRUE),
`6-MA` = slider::slide_dbl(y, mean, .before = 2, .after = 3, .complete = TRUE),
`7-MA` = slider::slide_dbl(y, mean, .before = 3, .after = 3, .complete = TRUE),
`8-MA` = slider::slide_dbl(y, mean, .before = 3, .after = 4, .complete = TRUE),
`9-MA` = slider::slide_dbl(y, mean, .before = 4, .after = 4, .complete = TRUE),
`10-MA` = slider::slide_dbl(y,mean, .before = 4, .after = 5, .complete = TRUE),
`11-MA` = slider::slide_dbl(y,mean, .before = 5, .after = 5, .complete = TRUE)
)
# A tibble: 10 x 10
y `3-MA` `4-MA` `5-MA` `6-MA` `7-MA` `8-MA` `9-MA` `10-MA` `11-MA`
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -0.560 NA NA NA NA NA NA NA NA NA
2 -0.230 0.256 0.210 NA NA NA NA NA NA NA
3 1.56 0.466 0.382 0.194 0.447 NA NA NA NA NA
4 0.0705 0.586 0.868 0.649 0.617 0.449 0.235 NA NA NA
5 0.129 0.638 0.594 0.787 0.445 0.348 0.219 0.132 0.0746 NA
6 1.72 0.768 0.260 0.222 0.0706 0.283 0.192 0.145 NA NA
7 0.461 0.304 0.0560 0.0707 -0.0154 -0.00311 NA NA NA NA
8 -1.27 -0.497 -0.484 -0.0443 NA NA NA NA NA NA
9 -0.687 -0.799 NA NA NA NA NA NA NA NA
10 -0.446 NA NA NA NA NA NA NA NA NA
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 32
Moving averages of moving averages
When? - m is even
Why? - To get symmetric weighting
MAex %>%
mutate(`4-MA` = slider::slide_dbl(y , mean, .before = 1, .after = 2, .complete = TRUE),
`2x4-MA` = slider::slide_dbl(`4-MA`, mean, .before = 1, .after = 0, .complete = TRUE),
`5-MA` = slider::slide_dbl(y , mean, .before = 2, .after = 2, .complete = TRUE)
)
# A tibble: 10 x 4
y `4-MA` `2x4-MA` `5-MA`
<dbl> <dbl> <dbl> <dbl>
1 -0.560 NA NA NA
2 -0.230 0.210 NA NA
3 1.56 0.382 0.296 0.194
4 0.0705 0.868 0.625 0.649
5 0.129 0.594 0.731 0.787
6 1.72 0.260 0.427 0.222
7 0.461 0.0560 0.158 0.0707
8 -1.27 -0.484 -0.214 -0.0443
9 -0.687 NA NA NA
10 -0.446 NA NA NA
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 33
Moving averages of moving averages
(4) 1
4-MA : T
b
t = (yt−1 + yt + yt+1 + yt+2 )
4
(2×4) 1 b (4) b (4)
2 × 4-MA : T
b
t = Tt−1 + T t
2
1 1 1
= (yt−2 + yt−1 + yt + yt+1 ) + (yt−1 + yt + yt+1 + yt+2 )
2 4 4
1 1 1 1 1
= yt−2 + yt−1 + yt + yt+1 + yt+2
8 4 4 4 8
(5) 1
5-MA Tt = (yt−2 + yt−1 + yt + yt+1 + yt+2 )
5
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 34
Moving averages of moving averages
2 × 4-MA : b (2×4) = 1 y
T
1 1 1 1
Q3,Y Q1,Y + yQ2,Y + yQ3,Y + yQ4,Y + yQ1,Y+1
8 4 4 4 8
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 35
Moving averages of moving averages
To estimate a trend-cycle using MA, m is set to the seasonal period of the data. If m is even,
use 2 × m-MA, else m-MA.
Quarterly observations –> m = 4 –> use 2 × 4-MA.
Monthly observations –> m = 12 –> use 2 × 12-MA.
Daily observations (with weekly season) –> m = 7 –> use 7-MA.
Can also use moving average of moving averages when m is odd (to get a weighted average),
but then both averages should be odd (e.g. 3 × m, 5 × m, etc.). Similarly, even m should be
followed by even order (e.g. 2 × m, 4 × m, etc.).
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 36
Classical decomposition (additive)
5 b t = yt − T
Calculate remainder: R bt − S
bt .
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 37
Classical decomposition
Norwegian employment
Source: Statistics Norway
2750
Employed
2500
2250
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 39
Classical decomposition
Classical decomposition
Employed = trend + seasonal + random
Employed
2750
2500
2250
2700
trend
2500
2300
10
seasonal
5
0
−5
−10
−15
20
10
random
0
−10
−20
2000 Q1 2010 Q1 2020 Q1
Quarter
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 40
Problems
Missing values
Over-smooth rapid changes
Not flexible seasonal component
Not robust against outliers
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 41
X11
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 42
SEATS
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 43
X11 and SEATS vs classical
Classical & X−13ARIMA−SEATS using X−11 adjustment & X−13ARIMA−SEATS decompositi
Employed = trend * seasonal * random
Employed
2750
2500
2250
2700
trend
2500 .model
2300
classical
2100
seats
seasonal
1.004
1.000 x11
0.996
0.992
1.005
random
1.000
0.995
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 45
STL: Parameters
Trend window
Season window
dat %>%
model(STL=STL(Employed ~ trend(window = 7)+ season(window = 11), robust = TRUE) ) %>%
components() %>%
autoplot()
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 46
STL decomposition
STL decomposition
Employed = trend + season_year + remainder
Employed
2750
2500
2250
2900
2700
trend
2500
2300
2100
season_year
10
0
−10
20
remainder
0
−20
−40
2000 Q1 2010 Q1 2020 Q1
Quarter
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 47
Your turn!
Wholesale and retail sales index for Norway
Source: Statistics Norway
150
Wholesale and retail sales index
125
100
75
50
2000 Jan 2005 Jan 2010 Jan 2015 Jan 2020 Jan
yearmonth [1M]
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 48
Exercise
Sondre Hølleland BAN430: Adjustments, Transformations and Time Series Decomposition 1/26/23 49