Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Metodo di Holt-WintersM. Romito
7 dicembre 2016
Smorzamento esponenziale (SE) e smorzamento esponenziale contrend (SET)
auto.data<-read.csv("Scheda11.csv",header=FALSE)auto=ts(auto.data,frequency=12,start=c(1995,1))moto.data<-read.csv("Scheda12.csv",header=FALSE)moto=ts(moto.data,frequency=12,start=c(2000,1))
Smorzamento esponenziale
Proviamo il metodo di smorzamento esponenziale su una serie con forte trend creata artificiosamente.st=ts((1:100)+10*rnorm(100),frequency=10)se=HoltWinters(st,beta=F,gamma=F)se
## Holt-Winters exponential smoothing without trend and without seasonal component.#### Call:## HoltWinters(x = st, beta = F, gamma = F)#### Smoothing parameters:## alpha: 0.3088877## beta : FALSE## gamma: FALSE#### Coefficients:## [,1]## a 105.0639
plot(se)
1
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2 4 6 8 10
−20
020
4060
8010
0
predict(se,1)
## Time Series:## Start = c(11, 1)## End = c(11, 1)## Frequency = 10## fit## [1,] 105.0639
Passiamo a esaminare la serie esempio (esportazione accessori auto), che contiene dati con trend dominante.auto.se=HoltWinters(auto,beta=F,gamma=F)auto.se
## Holt-Winters exponential smoothing without trend and without seasonal component.#### Call:## HoltWinters(x = auto, beta = F, gamma = F)#### Smoothing parameters:## alpha: 0.1610812## beta : FALSE## gamma: FALSE#### Coefficients:## [,1]## a 45292.56
2
plot(auto.se)
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
Proviamo a variare il valore del parametro α.plot(auto.se);points(HoltWinters(auto,alpha=0.2,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
3
plot(auto.se);points(HoltWinters(auto,alpha=0.4,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
plot(auto.se);points(HoltWinters(auto,alpha=0.6,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
4
plot(auto.se);points(HoltWinters(auto,alpha=0.8,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
plot(auto.se);points(HoltWinters(auto,alpha=1,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
5
plot(auto.se);points(HoltWinters(auto,alpha=0.01,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
plot(auto.se);points(HoltWinters(auto,alpha=0.05,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
6
plot(auto.se);points(HoltWinters(auto,alpha=0.10,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
plot(auto.se);points(HoltWinters(auto,alpha=0.15,beta=F,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
Predizione con smorzamento esponenziale.
7
plot(auto.se,predict(auto.se,12))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1995 2000 2005 2010
020
000
4000
060
000
predict(auto.se,1)
## Jan## 2009 45292.56
Smorzamento esponenziale con trend
auto.set=HoltWinters(auto,gamma=F)auto.set
## Holt-Winters exponential smoothing with trend and without seasonal component.#### Call:## HoltWinters(x = auto, gamma = F)#### Smoothing parameters:## alpha: 0.3078436## beta : 0.2922033## gamma: FALSE#### Coefficients:## [,1]## a 41215.437## b -2734.826
plot(auto.set)
8
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
Proviamo a variare direttamente i valori dei parametri. Primaalpha,plot(auto.set);points(HoltWinters(auto,alpha=0.1,beta=0.3,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
9
plot(auto.set);points(HoltWinters(auto,alpha=0.2,beta=0.3,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
plot(auto.set);points(HoltWinters(auto,alpha=0.3,beta=0.3,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
10
plot(auto.set);points(HoltWinters(auto,alpha=0.4,beta=0.3,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
plot(auto.set);points(HoltWinters(auto,alpha=0.5,beta=0.3,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
poi β,
11
plot(auto.set);points(HoltWinters(auto,alpha=0.3,beta=0.1,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
plot(auto.set);points(HoltWinters(auto,alpha=0.3,beta=0.2,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
12
plot(auto.set);points(HoltWinters(auto,alpha=0.3,beta=0.3,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
plot(auto.set);points(HoltWinters(auto,alpha=0.3,beta=0.4,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
13
plot(auto.set);points(HoltWinters(auto,alpha=0.3,beta=0.5,gamma=F)$fitted,col="blue",type="l")
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
−20
000
020
000
6000
0
Riaggiustiamo la condizione iniziale. Ricordiamo che l.start è la intercetta iniziale, e b.start è la pendenzainiziale.plot(HoltWinters(auto,gamma=F,l.start=auto[1,1]))
14
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008−20
000
020
000
6000
0
plot(HoltWinters(auto,gamma=F,l.start=auto[1,1],b.start=0))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
plot(HoltWinters(auto,gamma=F,l.start=auto[2,1],b.start=100))
15
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
plot(HoltWinters(auto,gamma=F,l.start=6500,b.start=2))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1996 1998 2000 2002 2004 2006 2008
020
000
4000
060
000
Calcoliamo la predizione
16
plot(auto.set,predict(auto.set,12))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
1995 2000 2005 2010
−20
000
020
000
6000
0
predict(auto.set,3)
## Jan Feb Mar## 2009 38480.61 35745.79 33010.96
Previsione con SE, SET
Quale metodo prevede meglio? Confrontiamo le predizioni dei metodi di smorzamento esponenziale senza/contrend.auto.end=window(auto,2007)auto.se.pt=predict(auto.se,6)auto.set.pt=predict(auto.set,6)plot(c(auto.end,auto.se.pt),col="red")points(c(auto.end,auto.set.pt),type="b",col="blue")
17
0 5 10 15 20 25 30
2000
040
000
6000
0
Index
c(au
to.e
nd, a
uto.
se.p
t)
Possiamo applicare i due metodi di smorzamento al trend ottenuto attraverso la decomposizione della serie.auto.d=decompose(auto)auto.se.t=HoltWinters(na.exclude(auto.d$trend),beta=F,gamma=F)auto.set.t=HoltWinters(na.exclude(auto.d$trend),gamma=F)plot(auto.se.t)
Holt−Winters filtering
Time
Obs
erve
d / F
itted
0 50 100 150
1000
030
000
5000
0
18
plot(auto.set.t)
Holt−Winters filtering
Time
Obs
erve
d / F
itted
0 50 100 150
1000
030
000
5000
0
Esercizio: migliorare SET variando α, β a mano
Confrontiamo le previsioni.auto.d.end=na.exclude(auto.d$trend)[140:156]pt.se.t=predict(auto.se.t,6)pt.set.t=predict(auto.set.t,6)plot(c(auto.d.end,pt.se.t),type="b",col="red")points(c(auto.d.end,pt.set.t),type="b",col="blue")points(auto.d.end,type="b")
19
5 10 15 20
4900
051
000
5300
0
Index
c(au
to.d
.end
, pt.s
e.t)
Serie stagionali con SE, SET
Passiamo ad esaminare serie con carattere stagionale. Con SE.st=ts(sin(1:100)+0.2*rnorm(100),frequency=10)se=HoltWinters(st,beta=F,gamma=F)plot(se)
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2 4 6 8 10
−1.
5−
0.5
0.0
0.5
1.0
20
predict(se,1)
## Time Series:## Start = c(11, 1)## End = c(11, 1)## Frequency = 10## fit## [1,] -0.2735261
sin(101)
## [1] 0.4520258
Con SET.set=HoltWinters(st,gamma=F)plot(set)
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2 4 6 8 10
−2
−1
01
2
predict(set,1)
## Time Series:## Start = c(11, 1)## End = c(11, 1)## Frequency = 10## fit## [1,] 0.26984
sin(101)
## [1] 0.4520258
Esercizio: esempio artificioso con trend e stagionalità
21
Esaminiamo la serie delle esportazioni moto (serie con carattere stagionale) Sia SE che SET sono fedeli allaserie senza catturare il trend.moto.se=HoltWinters(moto,beta=F,gamma=F)plot(moto.se)
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2000 2002 2004 2006 2008 2010
5010
015
020
0
moto.set=HoltWinters(moto,gamma=F)plot(moto.set)
22
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2000 2002 2004 2006 2008 2010
5010
015
020
025
0
Per catturare il trend, proviamo ad aggiustare la condizione iniziale.plot(HoltWinters(moto,gamma=F,l.start=moto[1,1]))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2000 2002 2004 2006 2008 2010
050
100
150
200
250
23
plot(HoltWinters(moto,gamma=F,l.start=moto[2,1]))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2000 2002 2004 2006 2008 2010
5010
015
020
025
0
plot(HoltWinters(moto,gamma=F,b.start=-51))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2000 2002 2004 2006 2008 2010
5010
015
020
0
24
plot(HoltWinters(moto,gamma=F,b.start=-4,l.start=120))
Holt−Winters filtering
Time
Obs
erve
d / F
itted
2000 2002 2004 2006 2008 2010
5010
015
020
0
Esercizio: trovare parametri α, β più opportuni per catturare il trend.
25