<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from pandas . compat import range <EOL> from pandas import compat <EOL> import warnings <EOL> import numpy as np <EOL> from pandas . core . panel import Panel <EOL> from pandas . core . frame import DataFrame <EOL> from pandas . core . reshape import get_dummies <EOL> from pandas . core . series import Series <EOL> from pandas . core . sparse import SparsePanel <EOL> from pandas . stats . ols import OLS , MovingOLS <EOL> import pandas . stats . common as com <EOL> import pandas . stats . math as math <EOL> from pandas . util . decorators import cache_readonly <EOL> class PanelOLS ( OLS ) : <EOL> """<STR_LIT>""" <EOL> _panel_model = True <EOL> def __init__ ( self , y , x , weights = None , intercept = True , nw_lags = None , <EOL> entity_effects = False , time_effects = False , x_effects = None , <EOL> cluster = None , dropped_dummies = None , verbose = False , <EOL> nw_overlap = False ) : <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> FutureWarning , stacklevel = <NUM_LIT:4> ) <EOL> self . _x_orig = x <EOL> self . _y_orig = y <EOL> self . _weights = weights <EOL> self . _intercept = intercept <EOL> self . _nw_lags = nw_lags <EOL> self . _nw_overlap = nw_overlap <EOL> self . _entity_effects = entity_effects <EOL> self . _time_effects = time_effects <EOL> self . _x_effects = x_effects <EOL> self . _dropped_dummies = dropped_dummies or { } <EOL> self . _cluster = com . _get_cluster_type ( cluster ) <EOL> self . _verbose = verbose <EOL> ( self . _x , self . _x_trans , <EOL> self . _x_filtered , self . _y , <EOL> self . _y_trans ) = self . _prepare_data ( ) <EOL> self . _index = self . _x . index . levels [ <NUM_LIT:0> ] <EOL> self . _T = len ( self . _index ) <EOL> def log ( self , msg ) : <EOL> if self . _verbose : <EOL> print ( msg ) <EOL> def _prepare_data ( self ) : <EOL> """<STR_LIT>""" <EOL> ( x , x_filtered , y , weights , cat_mapping ) = self . _filter_data ( ) <EOL> self . log ( '<STR_LIT>' ) <EOL> x = self . _add_dummies ( x , cat_mapping ) <EOL> self . log ( '<STR_LIT>' ) <EOL> x_filtered = self . _add_dummies ( x_filtered , cat_mapping ) <EOL> if self . _x_effects : <EOL> x = x . drop ( self . _x_effects , axis = <NUM_LIT:1> ) <EOL> x_filtered = x_filtered . drop ( self . _x_effects , axis = <NUM_LIT:1> ) <EOL> if self . _time_effects : <EOL> x_regressor = x . sub ( x . mean ( level = <NUM_LIT:0> ) , level = <NUM_LIT:0> ) <EOL> unstacked_y = y . unstack ( ) <EOL> y_regressor = unstacked_y . sub ( unstacked_y . mean ( <NUM_LIT:1> ) , axis = <NUM_LIT:0> ) . stack ( ) <EOL> y_regressor . index = y . index <EOL> elif self . _intercept : <EOL> self . log ( '<STR_LIT>' ) <EOL> x = x_regressor = add_intercept ( x ) <EOL> x_filtered = add_intercept ( x_filtered ) <EOL> y_regressor = y <EOL> else : <EOL> self . log ( '<STR_LIT>' ) <EOL> x_regressor = x <EOL> y_regressor = y <EOL> if weights is not None : <EOL> if not y_regressor . index . equals ( weights . index ) : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not x_regressor . index . equals ( weights . index ) : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> rt_weights = np . sqrt ( weights ) <EOL> y_regressor = y_regressor * rt_weights <EOL> x_regressor = x_regressor . mul ( rt_weights , axis = <NUM_LIT:0> ) <EOL> return x , x_regressor , x_filtered , y , y_regressor <EOL> def _filter_data ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> data = self . _x_orig <EOL> cat_mapping = { } <EOL> if isinstance ( data , DataFrame ) : <EOL> data = data . to_panel ( ) <EOL> else : <EOL> if isinstance ( data , Panel ) : <EOL> data = data . copy ( ) <EOL> if not isinstance ( data , SparsePanel ) : <EOL> data , cat_mapping = self . _convert_x ( data ) <EOL> if not isinstance ( data , Panel ) : <EOL> data = Panel . from_dict ( data , intersect = True ) <EOL> x_names = data . items <EOL> if self . _weights is not None : <EOL> data [ '<STR_LIT>' ] = self . _weights <EOL> filtered = data . to_frame ( ) <EOL> y = self . _y_orig <EOL> if isinstance ( y , Series ) : <EOL> y = y . unstack ( ) <EOL> data [ '<STR_LIT>' ] = y <EOL> data_long = data . to_frame ( ) <EOL> x_filt = filtered . filter ( x_names ) <EOL> x = data_long . filter ( x_names ) <EOL> y = data_long [ '<STR_LIT>' ] <EOL> if self . _weights is not None and not self . _weights . empty : <EOL> weights = data_long [ '<STR_LIT>' ] <EOL> else : <EOL> weights = None <EOL> return x , x_filt , y , weights , cat_mapping <EOL> def _convert_x ( self , x ) : <EOL> x_converted = { } <EOL> cat_mapping = { } <EOL> iteritems = getattr ( x , '<STR_LIT>' , x . items ) <EOL> for key , df in iteritems ( ) : <EOL> if not isinstance ( df , DataFrame ) : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( type ( df ) ) ) <EOL> if _is_numeric ( df ) : <EOL> x_converted [ key ] = df <EOL> else : <EOL> try : <EOL> df = df . astype ( float ) <EOL> except ( TypeError , ValueError ) : <EOL> values = df . values <EOL> distinct_values = sorted ( set ( values . flat ) ) <EOL> cat_mapping [ key ] = dict ( enumerate ( distinct_values ) ) <EOL> new_values = np . searchsorted ( distinct_values , values ) <EOL> x_converted [ key ] = DataFrame ( new_values , index = df . index , <EOL> columns = df . columns ) <EOL> if len ( cat_mapping ) == <NUM_LIT:0> : <EOL> x_converted = x <EOL> return x_converted , cat_mapping <EOL> def _add_dummies ( self , panel , mapping ) : <EOL> """<STR_LIT>""" <EOL> panel = self . _add_entity_effects ( panel ) <EOL> panel = self . _add_categorical_dummies ( panel , mapping ) <EOL> return panel <EOL> def _add_entity_effects ( self , panel ) : <EOL> """<STR_LIT>""" <EOL> from pandas . core . reshape import make_axis_dummies <EOL> if not self . _entity_effects : <EOL> return panel <EOL> self . log ( '<STR_LIT>' ) <EOL> dummies = make_axis_dummies ( panel , '<STR_LIT>' ) <EOL> if not self . _use_all_dummies : <EOL> if '<STR_LIT>' in self . _dropped_dummies : <EOL> to_exclude = str ( self . _dropped_dummies . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> to_exclude = dummies . columns [ <NUM_LIT:0> ] <EOL> if to_exclude not in dummies . columns : <EOL> raise Exception ( '<STR_LIT>' % ( to_exclude , <EOL> dummies . columns ) ) <EOL> self . log ( '<STR_LIT>' % to_exclude ) <EOL> dummies = dummies . filter ( dummies . columns . difference ( [ to_exclude ] ) ) <EOL> dummies = dummies . add_prefix ( '<STR_LIT>' ) <EOL> panel = panel . join ( dummies ) <EOL> return panel <EOL> def _add_categorical_dummies ( self , panel , cat_mappings ) : <EOL> """<STR_LIT>""" <EOL> if not self . _x_effects : <EOL> return panel <EOL> dropped_dummy = ( self . _entity_effects and not self . _use_all_dummies ) <EOL> for effect in self . _x_effects : <EOL> self . log ( '<STR_LIT>' % effect ) <EOL> dummies = get_dummies ( panel [ effect ] ) <EOL> val_map = cat_mappings . get ( effect ) <EOL> if val_map : <EOL> val_map = dict ( ( v , k ) for k , v in compat . iteritems ( val_map ) ) <EOL> if dropped_dummy or not self . _use_all_dummies : <EOL> if effect in self . _dropped_dummies : <EOL> to_exclude = mapped_name = self . _dropped_dummies . get ( <EOL> effect ) <EOL> if val_map : <EOL> mapped_name = val_map [ to_exclude ] <EOL> else : <EOL> to_exclude = mapped_name = dummies . columns [ <NUM_LIT:0> ] <EOL> if mapped_name not in dummies . columns : <EOL> raise Exception ( '<STR_LIT>' % ( to_exclude , <EOL> dummies . columns ) ) <EOL> self . log ( <EOL> '<STR_LIT>' % ( effect , to_exclude ) ) <EOL> dummies = dummies . filter ( <EOL> dummies . columns . difference ( [ mapped_name ] ) ) <EOL> dropped_dummy = True <EOL> dummies = _convertDummies ( dummies , cat_mappings . get ( effect ) ) <EOL> dummies = dummies . add_prefix ( '<STR_LIT>' % effect ) <EOL> panel = panel . join ( dummies ) <EOL> return panel <EOL> @ property <EOL> def _use_all_dummies ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( not self . _intercept and not self . _time_effects ) <EOL> @ cache_readonly <EOL> def _beta_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> X = self . _x_trans . values <EOL> Y = self . _y_trans . values . squeeze ( ) <EOL> beta , _ , _ , _ = np . linalg . lstsq ( X , Y ) <EOL> return beta <EOL> @ cache_readonly <EOL> def beta ( self ) : <EOL> return Series ( self . _beta_raw , index = self . _x . columns ) <EOL> @ cache_readonly <EOL> def _df_model_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _df_raw - <NUM_LIT:1> <EOL> @ cache_readonly <EOL> def _df_resid_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _nobs - self . _df_raw <EOL> @ cache_readonly <EOL> def _df_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> df = math . rank ( self . _x_trans . values ) <EOL> if self . _time_effects : <EOL> df += self . _total_times <EOL> return df <EOL> @ cache_readonly <EOL> def _r2_raw ( self ) : <EOL> Y = self . _y_trans . values . squeeze ( ) <EOL> X = self . _x_trans . values <EOL> resid = Y - np . dot ( X , self . _beta_raw ) <EOL> SSE = ( resid ** <NUM_LIT:2> ) . sum ( ) <EOL> if self . _use_centered_tss : <EOL> SST = ( ( Y - np . mean ( Y ) ) ** <NUM_LIT:2> ) . sum ( ) <EOL> else : <EOL> SST = ( Y ** <NUM_LIT:2> ) . sum ( ) <EOL> return <NUM_LIT:1> - SSE / SST <EOL> @ property <EOL> def _use_centered_tss ( self ) : <EOL> return self . _intercept or self . _entity_effects or self . _time_effects <EOL> @ cache_readonly <EOL> def _r2_adj_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> nobs = self . _nobs <EOL> factors = ( nobs - <NUM_LIT:1> ) / ( nobs - self . _df_raw ) <EOL> return <NUM_LIT:1> - ( <NUM_LIT:1> - self . _r2_raw ) * factors <EOL> @ cache_readonly <EOL> def _resid_raw ( self ) : <EOL> Y = self . _y . values . squeeze ( ) <EOL> X = self . _x . values <EOL> return Y - np . dot ( X , self . _beta_raw ) <EOL> @ cache_readonly <EOL> def resid ( self ) : <EOL> return self . _unstack_vector ( self . _resid_raw ) <EOL> @ cache_readonly <EOL> def _rmse_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> X = self . _x_trans . values <EOL> Y = self . _y_trans . values . squeeze ( ) <EOL> resid = Y - np . dot ( X , self . _beta_raw ) <EOL> ss = ( resid ** <NUM_LIT:2> ) . sum ( ) <EOL> return np . sqrt ( ss / ( self . _nobs - self . _df_raw ) ) <EOL> @ cache_readonly <EOL> def _var_beta_raw ( self ) : <EOL> cluster_axis = None <EOL> if self . _cluster == '<STR_LIT:time>' : <EOL> cluster_axis = <NUM_LIT:0> <EOL> elif self . _cluster == '<STR_LIT>' : <EOL> cluster_axis = <NUM_LIT:1> <EOL> x = self . _x <EOL> y = self . _y <EOL> if self . _time_effects : <EOL> xx = _xx_time_effects ( x , y ) <EOL> else : <EOL> xx = np . dot ( x . values . T , x . values ) <EOL> return _var_beta_panel ( y , x , self . _beta_raw , xx , <EOL> self . _rmse_raw , cluster_axis , self . _nw_lags , <EOL> self . _nobs , self . _df_raw , self . _nw_overlap ) <EOL> @ cache_readonly <EOL> def _y_fitted_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( self . _x . values , self . _beta_raw ) <EOL> @ cache_readonly <EOL> def y_fitted ( self ) : <EOL> return self . _unstack_vector ( self . _y_fitted_raw , index = self . _x . index ) <EOL> def _unstack_vector ( self , vec , index = None ) : <EOL> if index is None : <EOL> index = self . _y_trans . index <EOL> panel = DataFrame ( vec , index = index , columns = [ '<STR_LIT>' ] ) <EOL> return panel . to_panel ( ) [ '<STR_LIT>' ] <EOL> def _unstack_y ( self , vec ) : <EOL> unstacked = self . _unstack_vector ( vec ) <EOL> return unstacked . reindex ( self . beta . index ) <EOL> @ cache_readonly <EOL> def _time_obs_count ( self ) : <EOL> return self . _y_trans . count ( level = <NUM_LIT:0> ) . values <EOL> @ cache_readonly <EOL> def _time_has_obs ( self ) : <EOL> return self . _time_obs_count > <NUM_LIT:0> <EOL> @ property <EOL> def _nobs ( self ) : <EOL> return len ( self . _y ) <EOL> def _convertDummies ( dummies , mapping ) : <EOL> new_items = [ ] <EOL> for item in dummies . columns : <EOL> if not mapping : <EOL> var = str ( item ) <EOL> if isinstance ( item , float ) : <EOL> var = '<STR_LIT>' % item <EOL> new_items . append ( var ) <EOL> else : <EOL> new_items . append ( mapping [ int ( item ) ] ) <EOL> dummies = DataFrame ( dummies . values , index = dummies . index , <EOL> columns = new_items ) <EOL> return dummies <EOL> def _is_numeric ( df ) : <EOL> for col in df : <EOL> if df [ col ] . dtype . name == '<STR_LIT:object>' : <EOL> return False <EOL> return True <EOL> def add_intercept ( panel , name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> panel = panel . copy ( ) <EOL> panel [ name ] = <NUM_LIT:1.> <EOL> return panel . consolidate ( ) <EOL> class MovingPanelOLS ( MovingOLS , PanelOLS ) : <EOL> """<STR_LIT>""" <EOL> _panel_model = True <EOL> def __init__ ( self , y , x , weights = None , <EOL> window_type = '<STR_LIT>' , window = None , <EOL> min_periods = None , <EOL> min_obs = None , <EOL> intercept = True , <EOL> nw_lags = None , nw_overlap = False , <EOL> entity_effects = False , <EOL> time_effects = False , <EOL> x_effects = None , <EOL> cluster = None , <EOL> dropped_dummies = None , <EOL> verbose = False ) : <EOL> self . _args = dict ( intercept = intercept , <EOL> nw_lags = nw_lags , <EOL> nw_overlap = nw_overlap , <EOL> entity_effects = entity_effects , <EOL> time_effects = time_effects , <EOL> x_effects = x_effects , <EOL> cluster = cluster , <EOL> dropped_dummies = dropped_dummies , <EOL> verbose = verbose ) <EOL> PanelOLS . __init__ ( self , y = y , x = x , weights = weights , <EOL> ** self . _args ) <EOL> self . _set_window ( window_type , window , min_periods ) <EOL> if min_obs is None : <EOL> min_obs = len ( self . _x . columns ) + <NUM_LIT:1> <EOL> self . _min_obs = min_obs <EOL> @ cache_readonly <EOL> def resid ( self ) : <EOL> return self . _unstack_y ( self . _resid_raw ) <EOL> @ cache_readonly <EOL> def y_fitted ( self ) : <EOL> return self . _unstack_y ( self . _y_fitted_raw ) <EOL> @ cache_readonly <EOL> def y_predict ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _unstack_y ( self . _y_predict_raw ) <EOL> def lagged_y_predict ( self , lag = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> x = self . _x . values <EOL> betas = self . _beta_matrix ( lag = lag ) <EOL> return self . _unstack_y ( ( betas * x ) . sum ( <NUM_LIT:1> ) ) <EOL> @ cache_readonly <EOL> def _rolling_ols_call ( self ) : <EOL> return self . _calc_betas ( self . _x_trans , self . _y_trans ) <EOL> @ cache_readonly <EOL> def _df_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> df = self . _rolling_rank ( ) <EOL> if self . _time_effects : <EOL> df += self . _window_time_obs <EOL> return df [ self . _valid_indices ] <EOL> @ cache_readonly <EOL> def _var_beta_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> x = self . _x <EOL> y = self . _y <EOL> dates = x . index . levels [ <NUM_LIT:0> ] <EOL> cluster_axis = None <EOL> if self . _cluster == '<STR_LIT:time>' : <EOL> cluster_axis = <NUM_LIT:0> <EOL> elif self . _cluster == '<STR_LIT>' : <EOL> cluster_axis = <NUM_LIT:1> <EOL> nobs = self . _nobs <EOL> rmse = self . _rmse_raw <EOL> beta = self . _beta_raw <EOL> df = self . _df_raw <EOL> window = self . _window <EOL> if not self . _time_effects : <EOL> cum_xx = self . _cum_xx ( x ) <EOL> results = [ ] <EOL> for n , i in enumerate ( self . _valid_indices ) : <EOL> if self . _is_rolling and i >= window : <EOL> prior_date = dates [ i - window + <NUM_LIT:1> ] <EOL> else : <EOL> prior_date = dates [ <NUM_LIT:0> ] <EOL> date = dates [ i ] <EOL> x_slice = x . truncate ( prior_date , date ) <EOL> y_slice = y . truncate ( prior_date , date ) <EOL> if self . _time_effects : <EOL> xx = _xx_time_effects ( x_slice , y_slice ) <EOL> else : <EOL> xx = cum_xx [ i ] <EOL> if self . _is_rolling and i >= window : <EOL> xx = xx - cum_xx [ i - window ] <EOL> result = _var_beta_panel ( y_slice , x_slice , beta [ n ] , xx , rmse [ n ] , <EOL> cluster_axis , self . _nw_lags , <EOL> nobs [ n ] , df [ n ] , self . _nw_overlap ) <EOL> results . append ( result ) <EOL> return np . array ( results ) <EOL> @ cache_readonly <EOL> def _resid_raw ( self ) : <EOL> beta_matrix = self . _beta_matrix ( lag = <NUM_LIT:0> ) <EOL> Y = self . _y . values . squeeze ( ) <EOL> X = self . _x . values <EOL> resid = Y - ( X * beta_matrix ) . sum ( <NUM_LIT:1> ) <EOL> return resid <EOL> @ cache_readonly <EOL> def _y_fitted_raw ( self ) : <EOL> x = self . _x . values <EOL> betas = self . _beta_matrix ( lag = <NUM_LIT:0> ) <EOL> return ( betas * x ) . sum ( <NUM_LIT:1> ) <EOL> @ cache_readonly <EOL> def _y_predict_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> x = self . _x . values <EOL> betas = self . _beta_matrix ( lag = <NUM_LIT:1> ) <EOL> return ( betas * x ) . sum ( <NUM_LIT:1> ) <EOL> def _beta_matrix ( self , lag = <NUM_LIT:0> ) : <EOL> if lag < <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( lag ) ) <EOL> index = self . _y_trans . index <EOL> major_labels = index . labels [ <NUM_LIT:0> ] <EOL> labels = major_labels - lag <EOL> indexer = self . _valid_indices . searchsorted ( labels , side = '<STR_LIT:left>' ) <EOL> beta_matrix = self . _beta_raw [ indexer ] <EOL> beta_matrix [ labels < self . _valid_indices [ <NUM_LIT:0> ] ] = np . NaN <EOL> return beta_matrix <EOL> @ cache_readonly <EOL> def _enough_obs ( self ) : <EOL> rank_threshold = len ( self . _x . columns ) + <NUM_LIT:1> <EOL> if self . _min_obs < rank_threshold : <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> enough_observations = self . _nobs_raw >= self . _min_obs <EOL> enough_time_periods = self . _window_time_obs >= self . _min_periods <EOL> return enough_time_periods & enough_observations <EOL> def create_ols_dict ( attr ) : <EOL> def attr_getter ( self ) : <EOL> d = { } <EOL> for k , v in compat . iteritems ( self . results ) : <EOL> result = getattr ( v , attr ) <EOL> d [ k ] = result <EOL> return d <EOL> return attr_getter <EOL> def create_ols_attr ( attr ) : <EOL> return property ( create_ols_dict ( attr ) ) <EOL> class NonPooledPanelOLS ( object ) : <EOL> """<STR_LIT>""" <EOL> ATTRIBUTES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:x>' , <EOL> '<STR_LIT:y>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def __init__ ( self , y , x , window_type = '<STR_LIT>' , window = None , <EOL> min_periods = None , intercept = True , nw_lags = None , <EOL> nw_overlap = False ) : <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> FutureWarning , stacklevel = <NUM_LIT:4> ) <EOL> for attr in self . ATTRIBUTES : <EOL> setattr ( self . __class__ , attr , create_ols_attr ( attr ) ) <EOL> results = { } <EOL> for entity in y : <EOL> entity_y = y [ entity ] <EOL> entity_x = { } <EOL> for x_var in x : <EOL> entity_x [ x_var ] = x [ x_var ] [ entity ] <EOL> from pandas . stats . interface import ols <EOL> results [ entity ] = ols ( y = entity_y , <EOL> x = entity_x , <EOL> window_type = window_type , <EOL> window = window , <EOL> min_periods = min_periods , <EOL> intercept = intercept , <EOL> nw_lags = nw_lags , <EOL> nw_overlap = nw_overlap ) <EOL> self . results = results <EOL> def _var_beta_panel ( y , x , beta , xx , rmse , cluster_axis , <EOL> nw_lags , nobs , df , nw_overlap ) : <EOL> xx_inv = math . inv ( xx ) <EOL> yv = y . values <EOL> if cluster_axis is None : <EOL> if nw_lags is None : <EOL> return xx_inv * ( rmse ** <NUM_LIT:2> ) <EOL> else : <EOL> resid = yv - np . dot ( x . values , beta ) <EOL> m = ( x . values . T * resid ) . T <EOL> xeps = math . newey_west ( m , nw_lags , nobs , df , nw_overlap ) <EOL> return np . dot ( xx_inv , np . dot ( xeps , xx_inv ) ) <EOL> else : <EOL> Xb = np . dot ( x . values , beta ) . reshape ( ( len ( x . values ) , <NUM_LIT:1> ) ) <EOL> resid = DataFrame ( yv [ : , None ] - Xb , index = y . index , columns = [ '<STR_LIT>' ] ) <EOL> if cluster_axis == <NUM_LIT:1> : <EOL> x = x . swaplevel ( <NUM_LIT:0> , <NUM_LIT:1> ) . sortlevel ( <NUM_LIT:0> ) <EOL> resid = resid . swaplevel ( <NUM_LIT:0> , <NUM_LIT:1> ) . sortlevel ( <NUM_LIT:0> ) <EOL> m = _group_agg ( x . values * resid . values , x . index . _bounds , <EOL> lambda x : np . sum ( x , axis = <NUM_LIT:0> ) ) <EOL> if nw_lags is None : <EOL> nw_lags = <NUM_LIT:0> <EOL> xox = <NUM_LIT:0> <EOL> for i in range ( len ( x . index . levels [ <NUM_LIT:0> ] ) ) : <EOL> xox += math . newey_west ( m [ i : i + <NUM_LIT:1> ] , nw_lags , <EOL> nobs , df , nw_overlap ) <EOL> return np . dot ( xx_inv , np . dot ( xox , xx_inv ) ) <EOL> def _group_agg ( values , bounds , f ) : <EOL> """<STR_LIT>""" <EOL> if values . ndim == <NUM_LIT:1> : <EOL> N = len ( values ) <EOL> result = np . empty ( len ( bounds ) , dtype = float ) <EOL> elif values . ndim == <NUM_LIT:2> : <EOL> N , K = values . shape <EOL> result = np . empty ( ( len ( bounds ) , K ) , dtype = float ) <EOL> testagg = f ( values [ : min ( <NUM_LIT:1> , len ( values ) ) ] ) <EOL> if isinstance ( testagg , np . ndarray ) and testagg . ndim == <NUM_LIT:2> : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> for i , left_bound in enumerate ( bounds ) : <EOL> if i == len ( bounds ) - <NUM_LIT:1> : <EOL> right_bound = N <EOL> else : <EOL> right_bound = bounds [ i + <NUM_LIT:1> ] <EOL> result [ i ] = f ( values [ left_bound : right_bound ] ) <EOL> return result <EOL> def _xx_time_effects ( x , y ) : <EOL> """<STR_LIT>""" <EOL> xx = np . dot ( x . values . T , x . values ) <EOL> xt = x . sum ( level = <NUM_LIT:0> ) . values <EOL> count = y . unstack ( ) . count ( <NUM_LIT:1> ) . values <EOL> selector = count > <NUM_LIT:0> <EOL> xt = xt [ selector ] <EOL> count = count [ selector ] <EOL> return xx - np . dot ( xt . T / count , xt ) </s>
