Escolar Documentos
Profissional Documentos
Cultura Documentos
Pedro Campelo
Novembro de 2018
1 Revisão Econometria
2 Previsão
3 Série de Tempo
4 Machine Learning
1 Revisão Econometria
2 Previsão
3 Série de Tempo
4 Machine Learning
peso = β0 + β1 altura + i , i = i, · · · , n
Intuitivamente,
peso = β0 + β1 altura + ei , i = i, · · · , n
OLS,
Hipóteses:
1) Linearidade: A relação entre a variável dependente e os regressores
é linear.
n
X
ê = (yi − ŷi ) = y − X 0 β
i=1
n
X
SQR(β) = (yi − xi0 β)2 = (y − X 0 β)0 (y − X 0 β)
i=1
min (y − X 0 β)0 (y − X 0 β)
min (yy 0 − 2y 0 X β + β 0 X 0 X β)
CPO
−2y 0 X + 2X 0 X β̂
β̂ = (X 0 X )−1 X 0 y
import numpy a s np
import p a t s y a s ps
from p a nd a s import S e r i e s
from p a nd a s import r e a d c s v
import s t a t s m o d e l s . a p i a s sm
from s k l e a r n . m e t r i c s import r 2 s c o r e
model = sm . OLS( y , X)
m o d e l f i t = model2 . f i t ( )
p r i n t ( m o d e l f i t . summary ( ) )
c o e f=m o d e l f i t 2 . params
R2=m o d e l f i t 2 . r s q u a r e d
1 Revisão Econometria
2 Previsão
3 Série de Tempo
4 Machine Learning
y t r e i n o , X t r e i n o = p s . d m a t r i c e s ( ' y ˜ x1+x2+x3+x4+
x5+x6+x7 ' , d a t a=d a d o s t r e i n o ,
r e t u r n t y p e= ' d a t a f r a m e ' )
y t e s t e , X t e s t e = p s . d m a t r i c e s ( ' y ˜ x1+x2+x3+x4+
x5+x6+x7 ' , d a t a=d a d o s t e s t e ,
r e t u r n t y p e= ' d a t a f r a m e ' )
import s t a t s m o d e l s . a p i a s sm
from s k l e a r n . m e t r i c s import r 2 s c o r e
model = sm . OLS( y t r e i n o , X t r e i n o )
m o d e l f i t = model . f i t ( )
p r i n t ( m o d e l f i t . summary ( ) )
c o e f=m o d e l f i t . params
R2=m o d e l f i t . r s q u a r e d
# previsao
y predictions = model fit . predict ( X teste )
#C a l c u l a n d o o e r r o
SQR = m e a n s q u a r e d e r r o r ( y t e s t e , y p r e d i c t i o n s )
r e s i d u o = np . s q r t (SQR)
p r i n t ( ' T e s t MSE, R e s i d u a l : %.3 f ' % SQR , r e s i d u o )
#p r e c i s a o do modelo ( R2 )
accuracy = r2 score ( y teste , y p r e d i c t i o n s )
R 2 t e s t = sm . OLS( y t e s t e , X t e s t e ) . f i t ( ) . r s q u a r e d
p r i n t ( ' a c c u r a c y , R 2 t e s t : %.3 f ' % a c c u r a c y ,
R2 test )
import m a t p l o t l i b . p y p l o t a s p l t
from m a t p l o t l i b import p y p l o t
plt . figure ()
p y p l o t . p l o t ( y t r e i n o , l a b e l= ' T r e i n o ' )
p y p l o t . p l o t ( y t e s t e , c o l o r= ' b l a c k ' , l a b e l= ' T e s t e ' )
p y p l o t . p l o t ( y p r e d i c t i o n s , c o l o r= ' r e d ' ,
l a b e l= ' P r e v i s a o ' )
p l t . l e g e n d ( l o c= ' b e s t ' )
plt . ylabel ( 'y ')
p l t . x t i c k s ( r o t a t i o n =30)
plt . t i t l e ( ' Previsao ' )
p y p l o t . show ( )
import numpy a s np
from s k l e a r n . l i n e a r m o d e l import L i n e a r R e g r e s s i o n
c o e f r e g=np . t r a n s p o s e ( r e g . c o e f )
R2=r e g . s c o r e ( X t r e i n o , y t r e i n o )
print ( reg . score ( X treino , y t r e i n o ) , reg . coef )
p r e d i c t i o n s = reg . p r e d i c t ( X teste )
y p r e d i c t i o n s=pd . DataFrame ( p r e d i c t i o n s , i n d e x=t e s t e )
1 Revisão Econometria
2 Previsão
3 Série de Tempo
4 Machine Learning
yt = Φ1 yt−1 + βxt + e
Autocorrelação:
ρtj = Corr y + t, yt − j
Estacionaridade:
E (yt ) = µ < ∞, Var (yt ) = σ < ∞, Corr (yt , yt+k ) = γk
Dickey-Fuller Test
Primeira diferença - I (1) (∆y = yt − yt−1 ), segunda diferença - I (2) ,
etc.
AR (p):
yt = Φ0 + Φ1 yt−1 + t
MA (q):
yt = γ + et + θ1 et−1
ARIMA (p,i,q):
from p a nd a s import S e r i e s
from m a t p l o t l i b import p y p l o t
from p a nd a s . t o o l s . p l o t t i n g import a u t o c o r r e l a t i o n
from s t a t s m o d e l s . g r a p h i c s . t s a p l o t s import p l o t a c f
#A u t o c o r r e l a t i o n p l o t
autocorrelation plot ( 'y ')
p l t . show ( )
#Lag p l o t
p l o t a c f ( ' y ' , l a g s =50)
p y p l o t . show ( )
def a d f t e s t ( y ) :
p r i n t ( ' R e s u l t s o f Augmented DF t e s t : ' )
d f t e s t = a d f u l l e r ( y , a u t o l a g= ' AIC ' )
d f o u t p u t = pd . S e r i e s ( d f t e s t [ 0 : 4 ] ,
i n d e x =[ ' t e s t s t a t i s t i c ' , ' p−v a l u e ' ,
'# o f l a g s ' , '# o f o b s e r v a t i o n s ' ] )
f o r key , v a l u e i n d f t e s t [ 4 ] . i t e m s ( ) :
d f o u t p u t [ ' C r i t i c a l V a l u e ( { } ) ' . format ( k e y )]= v a l u e
print ( dfoutput )
data = y t r e i n o . i l o c [ : , 0 ] . v a l u e s
data2 = y t e s t e . i l o c [ : , 0 ] . v a l u e s
print ( a d f t e s t ( data ) , a d f t e s t ( data2 ) , a d f t e s t ( y ))
#N r e j e i t a a h i p . n u l a a 10% −> Nao E s t a c i o n a r i a
Pedro Campelo Introdução aos Modelos de Previsão Série de Tempo 31 / 48
Séries de Tempo
from s t a t s m o d e l s . t s a . a r m o d e l import AR
from s t a t s m o d e l s . t s a . a r i m a m o d e l import ARIMA
#model = AR( y t r e i n o )
model = ARIMA( y t r e i n o , o r d e r = ( 4 , 0 , 2 ) )
m o d e l f i t = model . f i t ( )
p r i n t ( ' Lag : %s ' % m o d e l f i t . k a r )
p r i n t ( ' C o e f f i c i e n t s : %s ' % m o d e l f i t . params )
# make p r e d i c t i o n s
y p r e d i c t i o n s = m o d e l f i t . p r e d i c t ( s t a r t=l e n ( y t r e i n o ) ,
SQR = m e a n s q u a r e d e r r o r ( y t e s t e , y p r e d i c t i o n s )
r e s i d=np . s q r t (EQM)
p r i n t ( ' T e s t MSE : %.3 f ' % SQR , r e s i d )
1 Revisão Econometria
2 Previsão
3 Série de Tempo
4 Machine Learning
Adalasso
R2 = m o d e l f i t . s c o r e ( X t r e i n o , y t r e i n o )
c o e f=model . c o e f
p r i n t ( R2 , c o e f )
r s d=np . s q r t ( m e a n s q u a r e d e r r o r ( y t e s t e , y p r e d i c t i o n s ) )
Pedro Campelo Introdução aos Modelos de Previsão Machine Learning 37 / 48
Ridge Regression
" #
β̂ = argmin kY − X βk22 + λkβk22
β̂
c o e f=np . t r a n s p o s e ( m o d e l f i t . c o e f )
R2 = m o d e l f i t . s c o r e ( X t r e i n o , y t r e i n o )
p r i n t ( c o e f , R2 )
SQR = m e a n s q u a r e d e r r o r ( y t e s t e , y p r e d i c t i o n s )
r e s i d = np . s q r t (SQR ) )
accuracy = r2 score ( y teste , y p r e d i c t i o n s )
p r i n t ( ' T e s t MSE, A c c u r a c y : %.3 f ' % r e s i d , a c c u r a c y )
Pedro Campelo Introdução aos Modelos de Previsão Machine Learning 39 / 48
Elastic Net
" #
β̂ = argmin kY − X βk22 + λ1 kβk1 + λ2 kβk22
β̂
model = E l a s t i c N e t ( ) .
m o d e l f i t=model . f i t ( X t r e i n o , y t r e i n o )
c o e f=m o d e l f i t . c o e f
R2 = m o d e l f i t . s c o r e ( X t r e i n o , y t r e i n o )
p r i n t ( c o e f , R2 )
SQR = m e a n s q u a r e d e r r o r ( y t e s t e , y p r e d i c t i o n s )
r e s i d = np . s q r t (SQR ) )
accuracy = r2 score ( y teste , y p r e d i c t i o n s )
p r i n t ( ' T e s t MSE, A c c u r a c y : %.3 f ' % r e s i d , a c c u r a c y )
Pedro Campelo Introdução aos Modelos de Previsão Machine Learning 41 / 48
Lars
model = l i n e a r m o d e l . L a r s ( n n o n z e r o c o e f s =100)
m o d e l f i t=model . f i t ( X t r e i n o , y t r e i n o )
c o e f=np . t r a n s p o s e ( m o d e l f i t . c o e f )
R2 = m o d e l f i t . s c o r e ( X t r e i n o , y t r e i n o )
p r i n t ( c o e f , R2 )
SQR = m e a n s q u a r e d e r r o r ( y t e s t e , y p r e d i c t i o n s )
r e s i d = np . s q r t (SQR ) )
accuracy = r2 score ( y teste , y p r e d i c t i o n s )
p r i n t ( ' T e s t MSE, A c c u r a c y : %.3 f ' % r e s i d , a c c u r a c y )
Pedro Campelo Introdução aos Modelos de Previsão Machine Learning 43 / 48
Random Forest
c o e f=model . f e a t u r e i m p o r t a n c e s
R2 = model . s c o r e ( X t r e i n o , y t r e i n o )
p r i n t ( R2 , c o e f )
SQR = m e a n s q u a r e d e r r o r ( y t e s t e , y p r e d i c t i o n s )
r e s i d = np . s q r t (SQR ) )
accuracy = r2 score ( y teste , y p r e d i c t i o n s )
p r i n t ( ' T e s t MSE, A c c u r a c y : %.3 f ' % r e s i d , a c c u r a c y )
Pedro Campelo Introdução aos Modelos de Previsão Machine Learning 45 / 48
Problemas
Horizonte de Previsão
Nowcasting
def t i m e S e r i e s P r e p a r a t i o n ( df , v a r D i c t , constantName ,
dropNaN , f o r e c a s t H o r i z o n ) :
v a r L i s t =[]
f o r var in df . columns :
i f ( ( v a r i n v a r D i c t ) and ( v a r != ' y ' ) ) :
print ( var )
v a r L i s t . append ( v a r )
f o r var in df . columns :
i f var in varDict :
print ( var )
maxLag=v a r D i c t [ v a r ]+1
f o r l a g i n range ( 1 , maxLag ) :
print ( lag )
t i m e S e r i e s [ v a r+ ' l a g ' + s t r ( l a g ) ] =
timeSeries [ var ] . s h i f t
( l a g+f o r e c a s t H o r i z o n −1)
i f ( dropNaN ) :
t i m e S e r i e s . d r o p n a ( i n p l a c e=True )
return t i m e S e r i e s