## I(0) and I(1) Factors

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

### I(0) and I(1) Factors

Dear all,

Here's a subroutine I wrote to recursively (!j) estimate Stock and Watson (2002) I(0) factors and Bai (2004) I(1) factors based on a Gauss code by Igor Masten where N>T.

Note: X is our matrix of time series created from g1_standstat1 - a group of standardized stationary variables.

Code: Select all

`'Stock and Watson Code:%start = "1980q1"   %endminusone = "2002q4-1"   !noiterations = 20for !j = 1 to !noiterationssmpl {%start} {%endminusone}+!jstom(g1_standstat1,X)scalar T = @rows(X)scalar N = @columns(X)'maximum number of factors:scalar k_max = 4'Because N>T:  sym XX = X*@transpose(X)           scalar il = T-1-k_max+1           scalar iu = T-1'Note: VA corresponds to VEctors, VE corresponds to VAluesmatrix va = @eigenvectors(XX) vector ve = @eigenvalues(XX)for !i = il to iuvector factor!i = ((T-1)^(0.5))*@columnextract(va,!i)matrix(T,k_max) factorscolplace(factors,factor!i,!i-il+1)nextmatrix loadings = @transpose(factors)*X/(T-1)'For a normalization, multiply/divide by the element [kmax,1] of the loadings matrix.scalar norm = loadings(k_max,1)factors = factors*normloadings = loadings/norm'Re-extract normalized factors from this matrix as series for future subroutines, in reverse order: 'i.e. the factor associated with the largest eigenvalue is F_1, second is F_2,...,F_k_maxmtos(factors,factorgroup1)for !i = 1 to k_maxscalar temp = k_max-!i+1rename ser0{temp} f_1_!i_!jnextdelete tempdelete factorgroup1`

This is our subroutine for estimating I(1) factors, where g1_standnonstat1 is our group of nonstationary standardized variables and the subscript denotes the I(1) property.

Code: Select all

`'Bai (2004) Codestom(g1_standnonstat1,i_X) scalar i_T = @rows(i_X)scalar i_N = @columns(i_X)scalar i_k_max = 4  sym i_XX = i_X*@transpose(i_X)'Note the difference with the stationary routine here:           scalar i_il = T-k_max+1           scalar i_iu = Tmatrix i_va = @eigenvectors(i_XX) vector i_ve = @eigenvalues(i_XX)for !i = i_il to i_iu'Again, note the difference with the stationary routine here:vector i_factor!i = (T)*@columnextract(i_va,!i)matrix(i_T,i_k_max) i_factorscolplace(i_factors,i_factor!i,!i-i_il+1)nextmatrix i_loadings = @transpose(i_factors)*X/(i_T^2)'normalizescalar i_norm = i_loadings(i_k_max,1)i_factors = i_factors*i_normi_loadings = i_loadings/i_norm'Re-extract normalized factors from this matrix, in reverse order: i.e. the factor associated with the largest eigenvalue is if1, second is i_F_2,...,i_F_km_maxmtos(i_factors,i_factorsgroup1)for !i = 1 to i_k_maxscalar temp = i_k_max-!i+1rename ser0{temp} if1_!i_!jnextdelete tempdelete i_factorsgroup1`

Best wishes
Last edited by CharlieEVIEWS on Sun Feb 22, 2015 8:01 am, edited 1 time in total.

CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

### Re: I(0) and I(1) Factors

Where T>N:

Code: Select all

`%start = "1960q1"   %endminusone = "2002q4-1"   !noiterations = 20   for !j = 1 to !noiterationssmpl {%start}+1 {%endminusone}+!j'Stock and Watson (2002) - I(0) factors where T>Nstom(g1_standstat1,X)scalar T = @rows(X)scalar N = @columns(X)'maximum number of factors:scalar k_max = 4  sym XX = @transpose(X)*X           scalar il = N-k_max+1           scalar iu = Nmatrix va = @eigenvectors(XX) vector ve = @eigenvalues(XX)for !i = il to iuvector loadings!i = (N)^(0.5)*@rowextract(@transpose(va),!i)matrix(k_max,N) loadingsrowplace(loadings,loadings!i,!i-il+1)nextmatrix factors = ((X*@transpose(loadings))/N)'For a normalization, multiply/divide by the element [kmax,1] of the loadings matrix.scalar norm = loadings(k_max,1)factors = factors*normloadings = loadings/norm'Re-extract normalized factors from this matrix as series for future subroutines, in reverse order: 'i.e. the factor associated with the largest eigenvalue is F_1, second is F_2,...,F_km_maxmtos(factors,factorgroup1)for !i = 1 to k_maxscalar temp = k_max-!i+1rename ser0{temp} f_1_!i_!jnextdelete tempdelete factorgroup1for !i = il to iudelete loadings!inext`

And again, for the case where we want to extract I(1) factors:

Code: Select all

`'Factors extracted as per Bai (2004): with a subscript i_ denoting integrated variables.stom(g1_standnonstat1,i_X) scalar i_T = @rows(i_X)scalar i_N = @columns(i_X)scalar i_k_max = 4sym i_XX = @transpose(i_X)*i_X           scalar i_il = N-i_k_max+1           scalar i_iu = Nmatrix i_va = @eigenvectors(i_XX) vector i_ve = @eigenvalues(i_XX)for !i = i_il to i_iuvector i_loadings!i = (i_N)^(0.5)*@rowextract(@transpose(i_va),!i)matrix(i_k_max,i_N) i_loadingsrowplace(i_loadings,i_loadings!i,!i-il+1)nextmatrix i_factors = ((i_X*@transpose(i_loadings))/N)'Normalizescalar i_norm = i_loadings(i_k_max,1)i_factors = i_factors*i_normi_loadings = i_loadings/i_norm'Re-extract normalized factors from this matrix, in reverse order: i.e. the factor associated with the largest eigenvalue is if1, second is i_F_2,...,i_F_km_maxmtos(i_factors,i_factorsgroup1)for !i = 1 to i_k_maxscalar temp = i_k_max-!i+1rename ser0{temp} if1_!i_!jnextdelete tempdelete i_factorsgroup1for !i = i_il to i_iudelete i_loadings!inext`

Warmest regards,

Charlie

hamidlalkhezri
Posts: 7
Joined: Sat Mar 25, 2017 11:54 pm

### Re: I(0) and I(1) Factors

hi
im working favar model with Eviews and I thank you for your guidance.
I have a few questions
1- what do you mean from I(0),I(1) factors?You're talking about stability?
2-when I run program[ 'Stock and Watson (2002) - I(0) factors where T>N] "for statement unterminated in "for !J=1 to 20"
please guide me

hamidlalkhezri
Posts: 7
Joined: Sat Mar 25, 2017 11:54 pm

### Re: I(0) and I(1) Factors

hi
im working favar model with Eviews and I thank you for your guidance.
I have a few questions
1- what do you mean from I(0),I(1) factors?You're talking about stability?
2-when I run program[ 'Stock and Watson (2002) - I(0) factors where T>N] "for statement unterminated in "for !J=1 to 20"
please guide me

hamidlalkhezri
Posts: 7
Joined: Sat Mar 25, 2017 11:54 pm

### Re: I(0) and I(1) Factors

CharlieEVIEWS wrote:Where T>N:

Code: Select all

`%start = "1960q1"   %endminusone = "2002q4-1"   !noiterations = 20   for !j = 1 to !noiterationssmpl {%start}+1 {%endminusone}+!j'Stock and Watson (2002) - I(0) factors where T>Nstom(g1_standstat1,X)scalar T = @rows(X)scalar N = @columns(X)'maximum number of factors:scalar k_max = 4  sym XX = @transpose(X)*X           scalar il = N-k_max+1           scalar iu = Nmatrix va = @eigenvectors(XX) vector ve = @eigenvalues(XX)for !i = il to iuvector loadings!i = (N)^(0.5)*@rowextract(@transpose(va),!i)matrix(k_max,N) loadingsrowplace(loadings,loadings!i,!i-il+1)nextmatrix factors = ((X*@transpose(loadings))/N)'For a normalization, multiply/divide by the element [kmax,1] of the loadings matrix.scalar norm = loadings(k_max,1)factors = factors*normloadings = loadings/norm'Re-extract normalized factors from this matrix as series for future subroutines, in reverse order: 'i.e. the factor associated with the largest eigenvalue is F_1, second is F_2,...,F_km_maxmtos(factors,factorgroup1)for !i = 1 to k_maxscalar temp = k_max-!i+1rename ser0{temp} f_1_!i_!jnextdelete tempdelete factorgroup1for !i = il to iudelete loadings!inext`

And again, for the case where we want to extract I(1) factors:

Code: Select all

`'Factors extracted as per Bai (2004): with a subscript i_ denoting integrated variables.stom(g1_standnonstat1,i_X) scalar i_T = @rows(i_X)scalar i_N = @columns(i_X)scalar i_k_max = 4sym i_XX = @transpose(i_X)*i_X           scalar i_il = N-i_k_max+1           scalar i_iu = Nmatrix i_va = @eigenvectors(i_XX) vector i_ve = @eigenvalues(i_XX)for !i = i_il to i_iuvector i_loadings!i = (i_N)^(0.5)*@rowextract(@transpose(i_va),!i)matrix(i_k_max,i_N) i_loadingsrowplace(i_loadings,i_loadings!i,!i-il+1)nextmatrix i_factors = ((i_X*@transpose(i_loadings))/N)'Normalizescalar i_norm = i_loadings(i_k_max,1)i_factors = i_factors*i_normi_loadings = i_loadings/i_norm'Re-extract normalized factors from this matrix, in reverse order: i.e. the factor associated with the largest eigenvalue is if1, second is i_F_2,...,i_F_km_maxmtos(i_factors,i_factorsgroup1)for !i = 1 to i_k_maxscalar temp = i_k_max-!i+1rename ser0{temp} if1_!i_!jnextdelete tempdelete i_factorsgroup1for !i = i_il to i_iudelete i_loadings!inext`

Warmest regards,

Charlie

hi
im working favar model with Eviews and I thank you for your guidance.
I have a few questions
1- what do you mean from I(0),I(1) factors?You're talking about stability?
2-when I run program[ 'Stock and Watson (2002) - I(0) factors where T>N] "for statement unterminated in "for !J=1 to 20"
please guide me

Return to “Program Repository”

### Who is online

Users browsing this forum: No registered users and 1 guest