
    M/Phl                         d Z ddlZddlmZmZ ddlmZ g dZ	 d Z	d Z
d Zd	 Zd
 Zd!dZd Zd Zd Zd ZeedZdefdZd ZdefdZd Zd Zd"dZd#dZd"dZdedfdZeZd Zd ZedfdZ edfd Z!dS )$uW  Sandwich covariance estimators


Created on Sun Nov 27 14:10:57 2011

Author: Josef Perktold
Author: Skipper Seabold for HCxxx in linear_model.RegressionResults
License: BSD-3

Notes
-----

for calculating it, we have two versions

version 1: use pinv
pinv(x) scale pinv(x)   used currently in linear_model, with scale is
1d (or diagonal matrix)
(x'x)^(-1) x' scale x (x'x)^(-1),  scale in general is (nobs, nobs) so
pretty large general formulas for scale in cluster case are in [4],
which can be found (as of 2017-05-20) at
http://www.tandfonline.com/doi/abs/10.1198/jbes.2010.07136
This paper also has the second version.

version 2:
(x'x)^(-1) S (x'x)^(-1)    with S = x' scale x,    S is (kvar,kvars),
(x'x)^(-1) is available as normalized_covparams.



S = sum (x*u) dot (x*u)' = sum x*u*u'*x'  where sum here can aggregate
over observations or groups. u is regression residual.

x is (nobs, k_var)
u is (nobs, 1)
x*u is (nobs, k_var)


For cluster robust standard errors, we first sum (x*w) over other groups
(including time) and then take the dot product (sum of outer products)

S = sum_g(x*u)' dot sum_g(x*u)
For HAC by clusters, we first sum over groups for each time period, and then
use HAC on the group sums of (x*w).
If we have several groups, we have to sum first over all relevant groups, and
then take the outer product sum. This can be done by summing using indicator
functions or matrices or with explicit loops. Alternatively we calculate
separate covariance matrices for each group, sum them and subtract the
duplicate counted intersection.

Not checked in details yet: degrees of freedom or small sample correction
factors, see (two) references (?)


This is the general case for MLE and GMM also

in MLE     hessian H, outerproduct of jacobian S,   cov_hjjh = HJJH,
which reduces to the above in the linear case, but can be used
generally, e.g. in discrete, and is misnomed in GenericLikelihoodModel

in GMM it's similar but I would have to look up the details, (it comes
out in sandwich form by default, it's in the sandbox), standard Newey
West or similar are on the covariance matrix of the moment conditions

quasi-MLE: MLE with mis-specified model where parameter estimates are
fine (consistent ?) but cov_params needs to be adjusted similar or
same as in sandwiches. (I did not go through any details yet.)

TODO
----
* small sample correction factors, Done for cluster, not yet for HAC
* automatic lag-length selection for Newey-West HAC,
  -> added: nlag = floor[4(T/100)^(2/9)]  Reference: xtscc paper, Newey-West
     note this will not be optimal in the panel context, see Peterson
* HAC should maybe return the chosen nlags
* get consistent notation, varies by paper, S, scale, sigma?
* replace diag(hat_matrix) calculations in cov_hc2, cov_hc3


References
----------
[1] John C. Driscoll and Aart C. Kraay, “Consistent Covariance Matrix Estimation
with Spatially Dependent Panel Data,” Review of Economics and Statistics 80,
no. 4 (1998): 549-560.

[2] Daniel Hoechle, "Robust Standard Errors for Panel Regressions with
Cross-Sectional Dependence", The Stata Journal

[3] Mitchell A. Petersen, “Estimating Standard Errors in Finance Panel Data
Sets: Comparing Approaches,” Review of Financial Studies 22, no. 1
(January 1, 2009): 435 -480.

[4] A. Colin Cameron, Jonah B. Gelbach, and Douglas L. Miller, “Robust Inference
With Multiway Clustering,” Journal of Business and Economic Statistics 29
(April 2011): 238-249.


not used yet:
A.C. Cameron, J.B. Gelbach, and D.L. Miller, “Bootstrap-based improvements
for inference with clustered errors,” The Review of Economics and
Statistics 90, no. 3 (2008): 414–427.

    N)combine_indices
group_sums)se_cov)cov_clustercov_cluster_2groupscov_haccov_nw_panelcov_white_simplecov_hc0cov_hc1cov_hc2cov_hc3r   weights_bartlettweights_uniformc                 |    t          j        | j        j        |dddf         | j        j        j        z            }|S )zt
    sandwich with pinv(x) * diag(scale) * pinv(x).T

    where pinv(x) = (X'X)^(-1) X
    and scale is (nobs,)
    N)npdotmodel
pinv_wexogTresultsscaleHs      e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/sandwich_covariance.py_HCCMr      s=     	w}'aaafgm.00	2 	2AH    c                 :    | j         dz  }t          | |          }|S +
    See statsmodels.RegressionResults
       )residr   )r   	het_scaler   s      r   r   r      s$    
 q IGY''GNr   c                 Z    | j         | j        z  | j        dz  z  }t          | |          }|S r   )nobsdf_residr"   r   )r   r#   r   s      r   r   r      s3    
 g./1ABIGY''GNr   c           	          t          j        t          j        | j        j        t          j        | j        | j        j        j                                      }| j        dz  d|z
  z  }t          | |          }|S )r    r!      	r   diagr   r   exognormalized_cov_paramsr   r"   r   )r   hr#   cov_hc2_s       r   r   r      st     	w})&!>!-,.0 01 1 	2 	2A q !A#&IWi((HOr   c           	          t          j        t          j        | j        j        t          j        | j        | j        j        j                                      }| j        d|z
  z  dz  }t          | |          }|S )r    r(   r!   r)   )r   r-   r#   cov_hc3_s       r   r   r      st     	w})&!>!-,.0 01 1 	2 	2A }ac"Q&IWi((HOr    c                    t          | t                    r1| \  }}t          j        |          x}}t          j        |          }nt	          | d          rt	          | d          r| j        } t	          | j        d          r\| j                            | j                  }t          j	        
                    | j                            | j                            }nt	          | j        d          r\| j                            | j                  }t          j	        
                    | j                            | j                            }n7| j        j        | j        dddf         z  }t          j        | j                  }t	          | j        d          rC|dk    s=|t          j        t          j        | j        j                  dddf                   z  }nt%          d          ||fS )	z?Helper function to get scores from results

    Parameters
    r   _resultsjac	score_obsNfreq_weightscluz:need either tuple of (jac, hessian_inv) or resultsinstance)
isinstancetupler   asarrayhasattrr3   r   r4   paramslinalginvhessianr5   wexogwresidr,   sqrtr6   
ValueError)r   cov_typer4   hessian_invxus        r   _get_sandwich_arraysrG      s    '5!! %"[:c??"Sj--	'	"	" %7J'' 	'&G7=%(( 		D""7>22B)--(=(=gn(M(MNNKKW]K00 	D((88B)--(=(=gn(M(MNNKK$w~aaag'>>B*W%BCCK 7=.11 	K(e:K:K
 "'"*W]%?@@DIJJJB  $ % % 	% {?r   c                    |j         dk    r<t          j        | j        j        |dddf         | j        j        j        z            }nAt          j        | j        j        t          j        || j        j        j                            }|S )a  
    sandwich with pinv(x) * scale * pinv(x).T

    where pinv(x) = (X'X)^(-1) X
    and scale is (nobs, nobs), or (nobs,) with diagonal matrix diag(scale)

    Parameters
    ----------
    results : result instance
       need to contain regression results, uses results.model.pinv_wexog
    scale : ndarray (nobs,) or (nobs, nobs)
       scale matrix, treated as diagonal matrix if scale is one-dimensional

    Returns
    -------
    H : ndarray (k_vars, k_vars)
        robust covariance matrix for the parameter estimates

    r(   N)ndimr   r   r   r   r   r   s      r   _HCCM1rJ     s{    ( zQF7=+4=!9!;;= = F7=+6%!9!;<<> >Hr   c                     |j         dk    r|dddf         }| }t          j        t          j        ||          |j                  }|S )a  
    sandwich with (X'X)^(-1) * scale * (X'X)^(-1)

    scale is (kvars, kvars)
    this uses results.normalized_cov_params for (X'X)^(-1)

    Parameters
    ----------
    results : result instance
       need to contain regression results, uses results.normalized_cov_params
    scale : ndarray (k_vars, k_vars)
       scale matrix

    Returns
    -------
    H : ndarray (k_vars, k_vars)
        robust covariance matrix for the parameter estimates

    r(   NrI   r   r   r   )rE   r   xxir   s       r   _HCCM2rN      sI    ( zQaaaf
C
rvc5!!35))AHr   c                 B    dt          j        | dz             | dz   z  z
  S )a!  Bartlett weights for HAC

    this will be moved to another module

    Parameters
    ----------
    nlags : int
       highest lag in the kernel window, this does not include the zero lag

    Returns
    -------
    kernel : ndarray, (nlags+1,)
        weights for Bartlett kernel

    r(         ?)r   arangenlagss    r   r   r   <  s&    $ ryq!!58,,,r   c                 0    t          j        | dz             S )a  uniform weights for HAC

    this will be moved to another module

    Parameters
    ----------
    nlags : int
       highest lag in the kernel window, this does not include the zero lag

    Returns
    -------
    kernel : ndarray, (nlags+1,)
        weights for uniform kernel

    r(   )r   onesrR   s    r   r   r   P  s    $ 757r   )bartlettuniformc                    | j         dk    r| dddf         } | j        d         }|*t          t          j        d|dz  dz  z                      } ||          }|d         t          j        | j        |           z  }t          d|dz             D ]C}t          j        | |d         j        | d|                    }|||         ||j        z   z  z  }D|S )a  inner covariance matrix for HAC (Newey, West) sandwich

    assumes we have a single time series with zero axis consecutive, equal
    spaced time periods


    Parameters
    ----------
    x : ndarray (nobs,) or (nobs, k_var)
        data, for HAC this is array of x_i * u_i
    nlags : int or None
        highest lag to include in kernel window. If None, then
        nlags = floor(4(T/100)^(2/9)) is used.
    weights_func : callable
        weights_func is called with nlags as argument to get the kernel
        weights. default are Bartlett weights

    Returns
    -------
    S : ndarray, (k_vars, k_vars)
        inner covariance matrix for sandwich

    Notes
    -----
    used by cov_hac_simple

    options might change when other kernels besides Bartlett are available.

    r(   Nr      g      Y@gqq?)rI   shapeintr   floorr   r   range)xrS   weights_func	n_periodsweightsSlagss           r   S_hac_simplere   i  s    > 	v{{aaafI
I}BHQ)d"2e!<<==>>l5!!G
RVAC^^#AQa   & &F1STT79a#h''	WS\QW%%Hr   c                 d    | j         dk    r| dddf         } t          j        | j        |           S )aX  inner covariance matrix for White heteroscedastistity sandwich


    Parameters
    ----------
    x : ndarray (nobs,) or (nobs, k_var)
        data, for HAC this is array of x_i * u_i

    Returns
    -------
    S : ndarray, (k_vars, k_vars)
        inner covariance matrix for sandwich

    Notes
    -----
    this is just dot(X.T, X)

    r(   NrL   )r^   s    r   S_white_simplerg     s2    & 	v{{aaafI6!#q>>r   c                 P    t          | |          j        }t          |||          S )a\  inner covariance matrix for HAC over group sums sandwich

    This assumes we have complete equal spaced time periods.
    The number of time periods per group need not be the same, but we need
    at least one observation for each time period

    For a single categorical group only, or a everything else but time
    dimension. This first aggregates x over groups for each time period, then
    applies HAC on the sum per period.

    Parameters
    ----------
    x : ndarray (nobs,) or (nobs, k_var)
        data, for HAC this is array of x_i * u_i
    time : ndarray, (nobs,)
        timeindes, assumed to be integers range(n_periods)
    nlags : int or None
        highest lag to include in kernel window. If None, then
        nlags = floor[4(T/100)^(2/9)] is used.
    weights_func : callable
        weights_func is called with nlags as argument to get the kernel
        weights. default are Bartlett weights

    Returns
    -------
    S : ndarray, (k_vars, k_vars)
        inner covariance matrix for sandwich

    References
    ----------
    Daniel Hoechle, xtscc paper
    Driscoll and Kraay

    rS   r_   )r   r   re   )r^   timerS   r_   x_group_sumss        r   S_hac_groupsumrl     s,    J a&&(LEMMMMr   c                 J    t          | |          j        }t          |          S )zinner covariance matrix for White on group sums sandwich

    I guess for a single categorical group only,
    categorical group, can also be the product/intersection of groups

    This is used by cov_cluster and indirectly verified

    )r   r   rg   )r^   grouprk   s      r   S_crosssectionro     s$     a'')L,'''r   c                     t          | j        dddf         |          }t          j        |          }t	          | |          }|S )z0this one is still wrong, use cov_cluster insteadN)ro   r"   r   squeezerJ   )r   rn   r   covs       r   cov_crosssection_0rs     sE     7=40%88EJuE
%
 
 CJr   Tc                    t          | d          \  }}t          |d          r|j        t          j        d          k    rt          j        |d          \  }}nt          j        |          }t          ||          }|j        \  }}t          |          }	t          ||          }
|r$|
|	|	dz
  z  |dz
  t          ||z
            z  z  z  }
|
S )aj  cluster robust covariance matrix

    Calculates sandwich covariance matrix for a single cluster, i.e. grouped
    variables.

    Parameters
    ----------
    results : result instance
       result of a regression, uses results.model.exog and results.resid
       TODO: this should use wexog instead
    use_correction : bool
       If true (default), then the small sample correction factor is used.

    Returns
    -------
    cov : ndarray, (k_vars, k_vars)
        cluster robust covariance matrix for parameter estimates

    Notes
    -----
    same result as Stata in UCLA example and same as Peterson

    r7   )rD   dtyper[   T)return_inverserP   )
rG   r;   ru   r   uniquero   rZ   lenrN   float)r   rn   use_correctionrF   rE   clustersr   r%   k_paramsn_groupscov_cs              r   r   r     s    2 +7UCCCOB5'"" $ekRXe__&D&D)E$???%%9U##2u%%EXND(8}}H;&&E 8(hm,GuTH_5557 	8 Lr   c                 J   |D|j         dk    s|j        d         dk    rt          d          |dddf         }|dddf         }n|}|}||f}t          | ||          }t          | ||          }t          | t	          |          d         |          }||z   |z
  }	|	||fS )aM  cluster robust covariance matrix for two groups/clusters

    Parameters
    ----------
    results : result instance
       result of a regression, uses results.model.exog and results.resid
       TODO: this should use wexog instead
    use_correction : bool
       If true (default), then the small sample correction factor is used.

    Returns
    -------
    cov_both : ndarray, (k_vars, k_vars)
        cluster robust covariance matrix for parameter estimates, for both
        clusters
    cov_0 : ndarray, (k_vars, k_vars)
        cluster robust covariance matrix for parameter estimates for first
        cluster
    cov_1 : ndarray, (k_vars, k_vars)
        cluster robust covariance matrix for parameter estimates for second
        cluster

    Notes
    -----

    verified against Peterson's table, (4 decimal print precision)
    Nr!   r(   zIif group2 is not given, then groups needs to be an array with two columnsr   )rz   )rI   rZ   rC   r   r   )
r   rn   group2rz   group0group1cov0cov1cov01cov_boths
             r   r   r     s    : ~:>>U[^q00 9 : : :qqq!tqqq!t  w~FFFDw~FFFD '..q1'57 7 7E
 d{U"H T4r   c                     t          |           \  }}t          |          }t          ||          }|r"|j        \  }}||t	          ||z
            z  z  }|S )a  
    heteroscedasticity robust covariance matrix (White)

    Parameters
    ----------
    results : result instance
       result of a regression, uses results.model.exog and results.resid
       TODO: this should use wexog instead

    Returns
    -------
    cov : ndarray, (k_vars, k_vars)
        heteroscedasticity robust covariance matrix for parameter estimates

    Notes
    -----
    This produces the same result as cov_hc0, and does not include any small
    sample correction.

    verified (against LinearRegressionResults and Peterson)

    See Also
    --------
    cov_hc1, cov_hc2, cov_hc3 : heteroscedasticity robust covariance matrices
        with small sample corrections

    )rG   rg   rN   rZ   ry   )r   rz   rF   rE   sigmacov_wr%   r|   s           r   r
   r
   X  sd    8 +733OB2E;&&E /hdXo....Lr   c                     t          |           \  }}t          |||          }t          ||          }|r"|j        \  }}	||t	          ||	z
            z  z  }|S )a  
    heteroscedasticity and autocorrelation robust covariance matrix (Newey-West)

    Assumes we have a single time series with zero axis consecutive, equal
    spaced time periods


    Parameters
    ----------
    results : result instance
       result of a regression, uses results.model.exog and results.resid
       TODO: this should use wexog instead
    nlags : int or None
        highest lag to include in kernel window. If None, then
        nlags = floor[4(T/100)^(2/9)] is used.
    weights_func : callable
        weights_func is called with nlags as argument to get the kernel
        weights. default are Bartlett weights

    Returns
    -------
    cov : ndarray, (k_vars, k_vars)
        HAC robust covariance matrix for parameter estimates

    Notes
    -----
    verified only for nlags=0, which is just White
    just guessing on correction factor, need reference

    options might change when other kernels besides Bartlett are available.

    ri   )rG   re   rN   rZ   ry   )
r   rS   r_   rz   rF   rE   r   r   r%   r|   s
             r   cov_hac_simpler     sl    D +733OB5|DDDE[%((G 1h4%x0000Nr   c                 &   g }g }|D ]N\  }}||z   |k     r@|                     | ||z   |                    |                     | |||z
                      O|g k    rt          d          t          j        |          t          j        |          fS )z
    assumes sorted by time, groupidx is tuple of start and end values
    not optimized, just to get a working version, loop over groups
    z all groups are empty taking lags)appendrC   r   vstack)r^   rc   groupidxout0
out_laggedlus          r   lagged_groupsr     s    
 DJ * *!S5199KK!C%'
###a!C%j)))rzz;<<<9T??BIj1111r   c                 $   t          |          dz
  }|d         t          j        | j        |           z  }t	          d|dz             D ]F}t          | ||          \  }}t          j        |j        |          }|||         ||j        z   z  z  }G|S )zinner covariance matrix for HAC for panel data

    no denominator nobs used

    no reference for this, just accounting for time indices
    r(   r   )rx   r   r   r   r]   r   )	xwra   r   rS   rb   rc   xw0xwlagrd   s	            r   
S_nw_panelr     s     LLNE
RVBD"%%%AQa   & &"2sH55
UF35%  	WS\QW%%Hr   hacc                 `   |dk    rddg}n ||          }t          |           \  }}t          |||          }t          ||          }	|rb|j        \  }
}|dk    r|	|
t	          |
|z
            z  z  }	n9|dv r5t          |          }|	||dz
  z  z  }	|	|
dz
  t	          |
|z
            z  z  }	|	S )a  Panel HAC robust covariance matrix

    Assumes we have a panel of time series with consecutive, equal spaced time
    periods. Data is assumed to be in long format with time series of each
    individual stacked into one array. Panel can be unbalanced.

    Parameters
    ----------
    results : result instance
       result of a regression, uses results.model.exog and results.resid
       TODO: this should use wexog instead
    nlags : int or None
        Highest lag to include in kernel window. Currently, no default
        because the optimal length will depend on the number of observations
        per cross-sectional unit.
    groupidx : list of tuple
        each tuple should contain the start and end index for an individual.
        (groupidx might change in future).
    weights_func : callable
        weights_func is called with nlags as argument to get the kernel
        weights. default are Bartlett weights
    use_correction : 'cluster' or 'hac' or False
        If False, then no small sample correction is used.
        If 'cluster' (default), then the same correction as in cov_cluster is
        used.
        If 'hac', then the same correction as in single time series, cov_hac
        is used.


    Returns
    -------
    cov : ndarray, (k_vars, k_vars)
        HAC robust covariance matrix for parameter estimates

    Notes
    -----
    For nlags=0, this is just White covariance, cov_white.
    If kernel is uniform, `weights_uniform`, with nlags equal to the number
    of observations per unit in a balance panel, then cov_cluster and
    cov_hac_panel are identical.

    Tested against STATA `newey` command with same defaults.

    Options might change when other kernels besides Bartlett and uniform are
    available.

    r   r(   r   )cr7   clusterrP   )rG   r   rN   rZ   ry   rx   )r   rS   r   r_   rz   ra   rF   rE   S_hacr   r%   r|   r}   s                r   r	   r	     s    b zza&,u%%*733OBr7H--E[%((G <hU""teD8O4444GG6668}}Hx8b=11GbE$/$:$::;GNr   c                 \   t          |           \  }}t          ||||          }t          ||          }|rt|j        \  }	}
|dk    r||	t	          |	|
z
            z  z  }nK|dv rGt          t          j        |                    }|||dz
  z  z  }||	dz
  t	          |	|
z
            z  z  }|S )a-  Driscoll and Kraay Panel robust covariance matrix

    Robust covariance matrix for panel data of Driscoll and Kraay.

    Assumes we have a panel of time series where the time index is available.
    The time index is assumed to represent equal spaced periods. At least one
    observation per period is required.

    Parameters
    ----------
    results : result instance
       result of a regression, uses results.model.exog and results.resid
       TODO: this should use wexog instead
    nlags : int or None
        Highest lag to include in kernel window. Currently, no default
        because the optimal length will depend on the number of observations
        per cross-sectional unit.
    time : ndarray of int
        this should contain the coding for the time period of each observation.
        time periods should be integers in range(maxT) where maxT is obs of i
    weights_func : callable
        weights_func is called with nlags as argument to get the kernel
        weights. default are Bartlett weights
    use_correction : 'cluster' or 'hac' or False
        If False, then no small sample correction is used.
        If 'hac' (default), then the same correction as in single time series, cov_hac
        is used.
        If 'cluster', then the same correction as in cov_cluster is
        used.

    Returns
    -------
    cov : ndarray, (k_vars, k_vars)
        HAC robust covariance matrix for parameter estimates

    Notes
    -----
    Tested against STATA xtscc package, which uses no small sample correction

    This first averages relevant variables for each time period over all
    individuals/groups, and then applies the same kernel weighted averaging
    over time as in HAC.

    Warning:
    In the example with a short panel (few time periods and many individuals)
    with mainly across individual variation this estimator did not produce
    reasonable results.

    Options might change when other kernels besides Bartlett and uniform are
    available.

    References
    ----------
    Daniel Hoechle, xtscc paper
    Driscoll and Kraay

    ri   r   )r   r   rP   )rG   rl   rN   rZ   ry   rx   r   rw   )r   rS   rj   r_   rz   rF   rE   r   r   r%   r|   r}   s               r   cov_nw_groupsumr      s    x +733OB 2t5|LLLE[%((G <hU""teD8O4444GG///29T??++Hx8b=11GbE$/$:$::;GNr   )r1   )T)NT)"__doc__numpyr   statsmodels.tools.grouputilsr   r    statsmodels.stats.moment_helpersr   __all__r   r   r   r   r   rG   rJ   rN   r   r   kernel_dictre   rg   rl   ro   rs   r   r   r
   r   r   r   r   r	   r    r   r   <module>r      sO  e eL     D D D D D D D D 3 3 3 3 3 3< < <(V	 	 	        ' ' ' 'T  8  8- - -(  * ,)+ + -= - - - -^  2 #'5E 'N 'N 'N 'NT( ( (  + + + +Z6  6  6  6 r% % % %P #'5E"&+ + + +Z 2 2 2&  " 9I %C C C CL 8H !J J J J J Jr   