• No results found

# i n s t a l l and l o a d r e q u i r e d p a c k a g e s i p a k <− function ( pkg ) {

new . pkg <− pkg [ ! ( pkg %i n% i n s t a l l e d . packages ( ) [ , ” Package ” ] ) ] i f ( length (new . pkg ) )

i n s t a l l . packages (new . pkg , dependencies = TRUE) sapply ( pkg , require , character . o n l y = TRUE)

}

packages <− c ( ” P e r f o r m a n c e A n a l y t i c s ” , ” r u g a r c h ” , ”FinTS” , ” tsDyn ” , ” zoo ” , ” u r c a ” , ” v a r s ” , ” x t s ” , ” n o r t e s t ” , ” s t a r g a z e r ” , ”MSBVAR” , ” vcd ” )

i p a k ( packages )

# Read d a t a

PCE . data = read . csv ( ”˜/ m a s t e r /Consumption . c s v ” ) IP . data = read . csv ( ”˜/ m a s t e r /IP . c s v ” )

M2. data = read . csv ( ”˜/ m a s t e r /M2 2 . c s v ” )

YIELD S . data = read . csv ( ”˜/ m a s t e r /YIELD S . c s v ” ) s p 5 0 0 . data = read . csv ( ”˜/ m a s t e r / s p 5 0 0 . c s v ” )

UEMP. data = read . csv ( ”˜/ m a s t e r /Unemployment Rate . c s v ” )

# C r e a t e v e c t o r s f o r v a r i a b l e s SP500 = s p 5 0 0 . data [ , 7 ]

SP500 = rev ( SP500 )

PCE = PCE . data [ , 2 ] IP = IP . data [ , 2 ] M2 = M2. data [ , 2 ]

SPREAD = YIELD S . data [ , 4 ] # d i v by 100 f o r % UEMP = UEMP. data [ , 2 ] # d i v by 100 f o r %

raw . data = cbind ( IP , M2, PCE, SP500 ,SPREAD,UEMP)

raw . data = t s ( raw . data , s t a r t=c ( 1 9 6 4 , 1 ) , frequency =12) # p l o t raw d a t a

par ( mfrow=c ( 3 , 1 ) )

f o r ( j i n 1 : ncol ( raw . data ) ) {

plot ( raw . data [ , j ] , main=colnames ( raw . data ) [ j ] , y l a b=NULL, t y p e=” l ” )

grid ( l t y =3) }

# c h a n g e SPREAD and UEMP t o % SPREAD=SPREAD/100

UEMP= UEMP/100

# make raw d a t a s t a t i o n a r y nobs=nrow(UEMP. data )

Detrended . PCE = PCE [ 2 : nobs ] /PCE [ 1 : ( nobs −1)] −1 Detrended . IP = IP [ 2 : nobs ] /IP [ 1 : ( nobs −1)] −1 Detrended .M2 = M2 [ 2 : nobs ] /M2 [ 1 : ( nobs −1)] −1

Detrended . SP500 = SP500 [ 2 : nobs ] /SP500 [ 1 : ( nobs −1)] −1 Detrended .SPREAD = d i f f (SPREAD)

Detrended .UEMP = d i f f (UEMP) #out−o f −s a m p l e d a t a m a t r i x form

sample . matrix = t s ( ( cbind ( Detrended . IP , Detrended . M2, Detrended . PCE, Detrended . SP500 , Detrended .SPREAD, Detrended .UEMP) ) , s t a r t=

c ( 1 9 6 4 , 1 ) , frequency =12)

#p l o t t s t r a n s f o r m e d s t a t i o n a r y d a t a r e t u r n s par ( mfrow=c ( 3 , 1 ) )

f o r ( j i n 1 : ncol ( sample . matrix ) ) {

plot ( sample . matrix [ , j ] , main=colnames ( sample . matrix ) [ j ] , y l a b= NULL)

grid ( l t y =3) }

# C r e a t e v e c t o r s f o r r e a l i z e d s t a n d a r d d e v i a t i o n

Sd .PCE = abs ( Detrended . PCE−mean( Detrended .PCE) ) Sd . IP = abs ( Detrended . IP−mean( Detrended . IP ) ) Sd .M2 = abs ( Detrended . M2−mean( Detrended .M2) )

Sd . SP500 = abs ( Detrended . SP500−mean( Detrended . SP500 ) ) Sd .SPREAD = abs ( Detrended .SPREAD−mean( Detrended .SPREAD) ) Sd .UEMP = abs ( Detrended .UEMP−mean( Detrended .UEMP) )

sd . matrix = t s ( ( cbind ( Sd . IP , Sd . PCE, Sd .SPREAD, Sd .UEMP, Sd . M2, Sd . SP500 ) ) , s t a r t=c ( 1 9 6 4 , 1 ) , frequency =12)

sd1 . matrix = sd . matrix [ 3 7 0 : 6 0 9 , ]

#p l o t t s t r a n s f o r m e d s t a t i o n a r y s t a n d a r d d e v i a t i o n par ( mfrow=c ( 3 , 1 ) )

f o r ( j i n 1 : ncol ( sd . matrix ) ) {

plot ( sd . matrix [ , j ] , main=colnames ( sd . matrix ) [ j ] , y l a b=NULL) grid ( l t y =3) } # T e s t f o r s t a t i o n a r i t y i n r e t u r n s # Adf t e s t f o r s t a t i o n a r i t y # S e b a s t i a n F o s s a t i # 1/2013

ur . t e s t = function ( yt , t r e n d , method , p e n a l t y=”MAIC” , kmax=NULL, kmin=NULL) {

# I mp lem en ts u n i t r o o t t e s t s d i s c u s s e d i n Ng , S . # and P . Perron ( 2 0 0 1 ) : ” Lag L e n g t h S e l e c t i o n and # t h e C o n s t r u c t i o n o f U n i t Root T e s t s w i t h Good

# S i z e and Power ” , Econometrica , 6 9 , 6 , 1519 −1554. # # I n p u t s : # y t −> u n i v a r i a t e t i m e s e r i e s # t r e n d −> t h e o r d e r o f t h e p o l y n o m i a l t r e n d # a ) t r e n d=c f o r c o n s t a n t # b ) t r e n d=c t f o r c o n s t a n t and l i n e a r t r e n d # method −> u n i t r o o t t e s t t o b e c o n s t r u c t e d # a ) method=a d f . o l s f o r augmented Dickey−F u l l e r t e s t # b ) method=a d f . g l s f o r Dickey− F u l l e r GLS t e s t # c ) method=p t f o r f e a s i b l e p o i n t o p t i m a l t e s t # p e n a l t y −> p e n a l t y f o r l a g l e n g t h s e l e c t i o n # a ) p e n a l t y −> MAIC f o r MAIC # b ) p e n a l t y −> AIC f o r AIC # c ) p e n a l t y −> BIC f o r BIC

# kmin −> minimum number o f l a g s # # Output : # s t a t : t h e t e s t s t a t i s t i c # c v : t h e c o r r e s p o n d i n g . 0 1 , . 0 5 , and . 1 0 c r i t i c a l v a l u e s # k r u l e : t h e o r d e r o f t h e a u t o r e g r e s s i o n s e l e c t e d by # t h e d a t a d e p e n d e n t r u l e ( u s e d i n c o n s t r u c t i n g # t h e s p e c t r a l d e n s i t y e s t i m a t o r a t f r e q u e n c y z e r o ) # p h i : e s t i m a t e o f p h i # s e t d a t a a s m a t r i x ! y t = as . matrix ( y t ) nt = nrow( y t ) i f ( i s . n u l l ( kmax ) ) {kmax = f l o o r ( 1 2 ∗ ( nt / 1 0 0 ) ˆ ( . 2 5 ) ) } i f ( i s . n u l l ( kmin ) ) {kmin = 0} # g e t d e t e r m i n i s t i c components i f ( t r e n d==” c ” ) { z = matrix ( 1 , nr=nt ) ; c b a r = −7} i f ( t r e n d==” c t ” ) {

z = cbind ( matrix ( 1 , nr=nt ) , matrix ( seq ( 1 : nt ) ) ) c b a r = −13.5

}

# t r a n s f o r m t h e d a t a tmp = g l s d ( yt , z , c b a r ) yd = tmp$yd

p h i = solve ( t ( yd [ 1 : ( nt −1) ] )%∗%yd [ 1 : ( nt −1) ] )%∗% t ( yd [ 1 : ( nt −1) ] )%∗%yd [ 2 : nt ] s s r a = tmp$ s s r # g e t o p t i m a l number o f l a g s i f ( kmax==kmin ) { k r u l e = kmax} e l s e { k r u l e = s 2 a r ( yd , p e n a l t y , kmax , kmin ) } # a d f −o l s i f ( method==” a d f . o l s ” ) { # c o n s t r u c t a d f −o l s a d f . o l s = a d f . o l s ( yt , z , k r u l e ) # 5% c r i t i c a l v a l u e s i f ( t r e n d==” c ” ) { cv = c ( − 3 . 4 3 , − 2 . 8 6 , − 2 . 5 7 ) } i f ( t r e n d==” c t ” ) { cv = c ( − 3 . 9 6 , − 3 . 4 1 , − 3 . 1 3 ) } l i s t s s = l i s t ( method=method , s t a t=a d f . o l s $ t s t a t , cv=cv , p e n a l t y=p e n a l t y , k=k r u l e , p h i=p h i ) } # a d f −g l s o f ERS ( 1 9 9 6 ) i f ( method==” a d f . g l s ” ) { # c o n s t r u c t a d f −o l s a d f . g l s = a d f k ( yd , k r u l e ) # 5% c r i t i c a l v a l u e s i f ( t r e n d==” c ” ) { cv = c ( − 2 . 5 8 , − 1 . 9 8 , − 1 . 6 2 ) } i f ( t r e n d==” c t ” ) { cv = c ( − 3 . 4 2 , − 2 . 9 1 , − 2 . 6 2 ) } l i s t s s = l i s t ( method=method , s t a t=a d f . g l s $ t s t a t , cv=cv , p e n a l t y=p e n a l t y , k=k r u l e , p h i=p h i ) } # p o i n t o p t i m a l t e s t o f ERS ( 1 9 9 6 )

i f ( method==” pt ” ) { # c o n s t r u c t a d f −g l s f o r s a r a d f . g l s = a d f k ( yd , k r u l e ) s a r = a d f . g l s $ s 2 v e c # c o n s t r u c t p t tmp = g l s d ( yt , z , 0 ) s s r 1 = tmp$ s s r pt = ( s s r a − (1+ c b a r / nt ) ∗ s s r 1 ) / s a r # 5% c r i t i c a l v a l u e s i f ( t r e n d==” c ” ) { cv = c ( 1 . 7 8 , 3 . 1 7 , 4 . 4 5 ) } i f ( t r e n d==” c t ” ) { cv = c ( 4 . 0 3 , 5 . 4 8 , 6 . 6 7 ) } l i s t s s = l i s t ( method=method , s t a t=pt , cv=cv , p e n a l t y=p e n a l t y , k=k r u l e , p h i=p h i ) } return ( l i s t s s ) } print . ur . t e s t = function ( t e s t ) { method = t e s t $method # p r i n t r e s u l t s

cat ( s p r i n t f ( ” \n Test f o r Unit Root : ” ) )

i f ( method==” a d f . o l s ” ) { cat ( s p r i n t f ( ”ADF Test ” ) ) }

i f ( method==” a d f . g l s ” ) { cat ( s p r i n t f ( ”ADF Test w i t h GLS D e t r e n d i n g ” ) ) }

i f ( method==” pt ” ) { cat ( s p r i n t f ( ”ERS Test ” ) ) }

cat ( s p r i n t f ( ” \n Test S t a t i s t i c : %.3 f ” , t e s t $ s t a t ) )

cat ( s p r i n t f ( ” \n C r i t i c a l V a l u e s ( . 0 1 , . 0 5 , . 1 0 ) : %.2 f %.2 f %.2 f ” ,

t e s t $cv [ 1 ] , t e s t $cv [ 2 ] , t e s t $cv [ 3 ] ) )

cat ( s p r i n t f ( ” \n\n Lag Order S e l e c t i o n Rule : %s ” , t e s t $ p e n a l t y ) )

cat ( s p r i n t f ( ” \n S e l e c t e d Lag Order : %1.0 f ” , t e s t $k ) )

cat ( s p r i n t f ( ” \n E s t i m a t e d C o e f f i c i e n t : %.4 f \n\n” , t e s t $ p h i ) ) } a d f k = function ( yt , k s t a r ) { r e g = l a g n ( yt , 1 ) dyt = d i f f k ( yt , 1 ) i f ( k s t a r >0){ f o r ( i i n 1 : k s t a r ) { r e g = cbind ( r e g , l a g n ( dyt , i ) ) }} dyt = t r i m r ( dyt , k s t a r +1 ,0) r e g = t r i m r ( r e g , k s t a r +1 ,0) b = solve ( t ( r e g )%∗%r e g )%∗%t ( r e g )%∗%dyt e e = dyt − r e g%∗%b n e f = nrow( dyt )−k s t a r −1 s 2 e = t ( e e )%∗%e e / n e f xx = solve ( t ( r e g )%∗%r e g ) s r e = xx [ 1 , 1 ] ∗ s 2 e sumb = 0 i f ( k s t a r >0){sumb = sum( b [ 2 : ( k s t a r +1) ] ) }

return ( l i s t ( t s t a t=b [ 1 ] / sqrt ( s r e ) , rho=b [ 1 ] + 1 , s 2 v e c=s 2 e /(1−sumb ) ˆ 2 ) )

a d f . o l s = function ( yt , z , k s t a r ) { r e g = cbind ( l a g n ( yt , 1 ) , z ) dyt = d i f f k ( yt , 1 ) i f ( k s t a r >0){ f o r ( i i n 1 : k s t a r ) { r e g = cbind ( r e g , l a g n ( dyt , i ) ) }} dyt = t r i m r ( dyt , k s t a r +1 ,0) r e g = t r i m r ( r e g , k s t a r +1 ,0) b = solve ( t ( r e g )%∗%r e g )%∗%t ( r e g )%∗%dyt e e = dyt − r e g%∗%b

n e f = nrow( dyt )−k s t a r −1−ncol ( z ) s 2 e = t ( e e )%∗%e e / n e f

xx = solve ( t ( r e g )%∗%r e g ) s r e = xx [ 1 , 1 ] ∗ s 2 e

return ( l i s t ( t s t a t=b [ 1 ] / sqrt ( s r e ) ) ) }

s 2 a r = function ( yt , p e n a l t y , kmax , kmin ) { tau = rep ( 0 , kmax+1)

s 2 e = 999999 ∗rep ( 1 , kmax+1) dyt = d i f f k ( yt , 1 )

r e g = l a g n ( yt , 1 )

i f ( kmax>0){ f o r ( i i n 1 : kmax ) { r e g = cbind ( r e g , l a g n ( dyt , i ) ) }} dyt = t r i m r ( dyt , kmax+1 ,0)

r e g = t r i m r ( r e g , kmax+1 ,0) sumy = t ( r e g [ , 1 ] )%∗%r e g [ , 1 ] n e f = nrow( dyt ) f o r ( k i n kmin : kmax ) { b = solve ( t ( r e g [ , 1 : ( k+1) ] )%∗%r e g [ , 1 : ( k+1) ] )%∗% t ( r e g [ , 1 : ( k+1) ] )%∗%dyt

e = dyt − r e g [ , 1 : ( k+1) ]%∗%b s 2 e [ k +1] = t ( e )%∗%e/ n e f tau [ k +1] = b [ 1 ] ∗b [ 1 ] ∗sumy/ s 2 e [ k +1] } kk = seq ( 0 , kmax ) i f ( p e n a l t y==”AIC” ) { i c = log ( s 2 e )+2∗kk/ n e f }

e l s e i f ( p e n a l t y==”BIC” ) { i c = log ( s 2 e )+log ( n e f ) ∗kk/ n e f } e l s e { i c = log ( s 2 e )+2∗ ( kk+tau ) / n e f } tmp = which . min( i c ) return ( tmp−1) } g l s d = function ( yt , z , c b a r ) { nt = nrow( y t ) a b a r = 1 + c b a r / nt ya = matrix ( 0 , nt , 1 ) za = matrix ( 0 , nt , ncol ( z ) ) ya [ 1 , 1 ] = y t [ 1 , 1 ] za [ 1 , ] = z [ 1 , ] ya [ 2 : nt , 1 ] = y t [ 2 : nt , 1 ] − a b a r ∗ y t [ 1 : ( nt −1) , 1 ] za [ 2 : nt , ] = z [ 2 : nt ,] − a b a r ∗z [ 1 : ( nt −1) , ] # c o n s t r u c t g l s d e t r e n d e d s e r i e s

bhat = solve ( t ( za )%∗%za )%∗%t ( za )%∗%ya yd = y t − z%∗%bhat

s s r = t ( ya−za%∗%bhat )%∗%( ya−za%∗%bhat ) return ( l i s t ( yd=yd , s s r=s s r ) )

o l s d = function ( yt , z ) {

# c o n s t r u c t o l s d e t r e n d e d s e r i e s bhat = solve ( t ( z )%∗%z )%∗%t ( z )%∗%y t yd = y t − z%∗%bhat

return ( yd=yd ) }

d i f f k = function ( x , k ) { i f ( k==0){ return ( x ) }

e l s e { return ( rbind ( matrix ( 0 , k , ncol ( x ) ) , d i f f ( x , l a g=k ) ) ) } }

l a g n = function ( x , n ) {

i f ( n<0){ return ( rbind ( t r i m r ( x , abs ( n ) , 0 ) ,

matrix ( 0 , abs ( n ) , ncol ( x ) ) ) ) }

e l s e { return ( rbind ( matrix ( 0 , n , ncol ( x ) ) , t r i m r ( x , 0 , n ) ) ) } }

t r i m r = function ( x , n1 , n2 ) {

return ( as . matrix ( x [ ( n1+1) : ( nrow( x )−n2 ) , ] , nc=ncol ( x ) ) ) }

# a d f t e s t raw d a t a

raw . PCEtest = ur . t e s t (PCE, t r e n d=” c t ” , method=” a d f . o l s ” , kmax=NULL )

raw . print . ur . t e s t ( raw . PCEtest ) # s t a t i o n a r y 1%

raw . I P t e s t = ur . t e s t ( IP , t r e n d=” c t ” , method=” a d f . o l s ” , kmax=NULL) print . ur . t e s t ( raw . I P t e s t ) # s t a t i o n a r y 1%

=NULL)

print . ur . t e s t ( raw . S P 5 0 0 t e s t ) # S t a t i o n a r y 1%

raw . SPREADtest = ur . t e s t (SPREAD, t r e n d=” c t ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( raw . SPREADtest ) # s t a t i o n a r y 1%

raw . M2test = ur . t e s t (M2, t r e n d=” c t ” , method=” a d f . o l s ” , kmax=NULL) print . ur . t e s t ( raw . M2test ) # s t a t i o n a r y 5%

raw . UEMPtest = ur . t e s t (UEMP, t r e n d=” c t ” , method=” a d f . o l s ” , kmax= NULL)

print . ur . t e s t ( raw . UEMPtest )# s t a t i o n a r y 1%

#a d f t e s t r e t u r n s

PCEtest = ur . t e s t ( Detrended . PCE, t r e n d=” c t ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( PCEtest ) # s t a t i o n a r y 1%

I P t e s t = ur . t e s t ( Detrended . IP , t r e n d=” c ” , method=” a d f . o l s ” , kmax= NULL)

print . ur . t e s t ( I P t e s t ) # s t a t i o n a r y 1%

S P 5 0 0 t e s t = ur . t e s t ( Detrended . SP500 , t r e n d=” c ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( S P 5 0 0 t e s t ) # S t a t i o n a r y 1%

SPREADtest = ur . t e s t ( Detrended . SPREAD, t r e n d=” c ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( SPREADtest ) # s t a t i o n a r y 1%

M2test = ur . t e s t ( Detrended . M2, t r e n d=” c t ” , method=” a d f . o l s ” , kmax= NULL)

print . ur . t e s t ( M2test ) # s t a t i o n a r y 5%

UEMPtest = ur . t e s t ( Detrended .UEMP, t r e n d=” c ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( UEMPtest )# s t a t i o n a r y 1%

#a d f t e s t s d

PCE1test = ur . t e s t ( sd . matrix [ , 1 ] , t r e n d=” c ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( PCE1test ) # s t a t i o n a r y 1%

I P 1 t e s t = ur . t e s t ( sd . matrix [ , 2 ] , t r e n d=” c ” , method=” a d f . o l s ” , kmax =NULL)

print . ur . t e s t ( I P 1 t e s t ) # s t a t i o n a r y 1%

S P 5 0 0 1 t e s t = ur . t e s t ( sd . matrix [ , 3 ] , t r e n d=” c ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( S P 5 0 0 1 t e s t ) # S t a t i o n a r y 1%

SPREAD1test = ur . t e s t ( sd . matrix [ , 4 ] , t r e n d=” c ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( SPREAD1test ) # s t a t i o n a r y 1%

M21test = ur . t e s t ( sd . matrix [ , 5 ] , t r e n d=” c ” , method=” a d f . o l s ” , kmax =NULL)

print . ur . t e s t ( M21test ) # s t a t i o n a r y 5%

UEMP1test = ur . t e s t ( sd . matrix [ , 6 ] , t r e n d=” c ” , method=” a d f . o l s ” , kmax=NULL)

print . ur . t e s t ( UEMP1test )# s t a t i o n a r y 1%

# compute e x c e s s k u r t o s i s , s k e w n e s s and p l o t d e n s i t y o f s t a t i o n a r y d a t a

Ex . Kurt = c ( rep ( 0 , 6 ) ) Skew = c ( rep ( 0 , 6 ) ) Mean = c ( rep ( 0 , 6 ) ) Sd = c ( rep ( 0 , 6 ) )

f o r ( j i n 1 : ncol ( sample . matrix ) ) { Mean [ j ] = mean( sample . matrix [ , j ] ) Sd [ j ] = sd ( sample . matrix [ , j ] )

Skew [ j ] = s k e w n e s s ( sample . matrix [ , j ] ) Ex . Kurt [ j ] = k u r t o s i s ( sample . matrix [ , j ] )

Moments = cbind ( Mean , Sd , Skew , Ex . Kurt , rownames=colnames ( raw . data

Related documents