<s> """<STR_LIT>""" <EOL> from statsmodels . compat . python import range <EOL> import numpy as np <EOL> def kernel ( d1 , d2 , r = None , weights = None ) : <EOL> '''<STR_LIT>''' <EOL> diff = d1 - d2 <EOL> if ( weights is None ) or ( r [ <NUM_LIT:0> ] == <NUM_LIT:0> ) : <EOL> return np . all ( ( r * diff ) == <NUM_LIT:0> ) <EOL> else : <EOL> return weights [ diff ] * np . all ( ( r [ <NUM_LIT:1> : ] * diff [ <NUM_LIT:1> : ] ) == <NUM_LIT:0> ) <EOL> def aggregate_cov ( x , d , r = None , weights = None ) : <EOL> '''<STR_LIT>''' <EOL> nobs = x . shape [ <NUM_LIT:0> ] <EOL> count = <NUM_LIT:0> <EOL> res = <NUM_LIT:0> * np . outer ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:0> ] ) <EOL> for ii in range ( nobs ) : <EOL> for jj in range ( nobs ) : <EOL> w = kernel ( d [ ii ] , d [ jj ] , r = r , weights = weights ) <EOL> if w : <EOL> res += w * np . outer ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:0> ] ) <EOL> count *= <NUM_LIT:1> <EOL> return res , count <EOL> def weights_bartlett ( nlags ) : <EOL> return <NUM_LIT:1> - np . arange ( nlags + <NUM_LIT:1> ) / ( nlags + <NUM_LIT:1.> ) <EOL> def S_all_hac ( x , d , nlags = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> r = np . zeros ( d . shape [ <NUM_LIT:1> ] ) <EOL> r [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> weights = weights_bartlett ( nlags ) <EOL> return aggregate_cov ( x , d , r = r , weights = weights ) <EOL> def S_within_hac ( x , d , nlags = <NUM_LIT:1> , groupidx = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> r = np . zeros ( d . shape [ <NUM_LIT:1> ] ) <EOL> r [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> r [ groupidx ] = <NUM_LIT:1> <EOL> weights = weights_bartlett ( nlags ) <EOL> return aggregate_cov ( x , d , r = r , weights = weights ) <EOL> def S_cluster ( x , d , groupidx = [ <NUM_LIT:1> ] ) : <EOL> r = np . zeros ( d . shape [ <NUM_LIT:1> ] ) <EOL> r [ groupidx ] = <NUM_LIT:1> <EOL> return aggregate_cov ( x , d , r = r , weights = None ) <EOL> def S_white ( x , d ) : <EOL> '''<STR_LIT>''' <EOL> r = np . ones ( d . shape [ <NUM_LIT:1> ] ) <EOL> return aggregate_cov ( x , d , r = r , weights = None ) </s>
