
    M/Ph                     
   d Z ddlZddlmZ ddlmZ ddlmZ ddlZ	ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ 	 	 ddZddZd Z G d d          Z G d de          Z G d de          ZddZ G d de          ZdS )zkInfluence and Outlier Measures

Created on Sun Jan 29 11:16:09 2012

Author: Josef Perktold
License: BSD-3
    N)Appender)lzip)defaultdict)_plot_influence_doc)OLS)multipletests)cache_readonly)maybe_unwrap_resultsbonf皙?Fc                    ddl m} |t          | j        j        dd          }t          | dd          }|+t          |           }t          d|j        j        z             |            j	        }	|rSt          j        |	                                          ddd         }
|	|
         }	|t          j        |          |
         }| j        dz
  }|j                            t          j        |	          |          d	z  }t#          |||
          }t          j        |	||d         f         }||dddf         |k     }||         }nt'          d          }|2ddlm}  ||dd|dz   gt          j        |          |                   S |S )aU  
    Outlier Tests for RegressionResults instances.

    Parameters
    ----------
    model_results : RegressionResults
        Linear model results
    method : str
        - `bonferroni` : one-step correction
        - `sidak` : one-step correction
        - `holm-sidak` :
        - `holm` :
        - `simes-hochberg` :
        - `hommel` :
        - `fdr_bh` : Benjamini/Hochberg
        - `fdr_by` : Benjamini/Yekutieli
        See `statsmodels.stats.multitest.multipletests` for details.
    alpha : float
        familywise error rate
    labels : None or array_like
        If `labels` is not None, then it will be used as index to the
        returned pandas DataFrame. See also Returns below
    order : bool
        Whether or not to order the results by the absolute value of the
        studentized residuals. If labels are provided they will also be sorted.
    cutoff : None or float in [0, 1]
        If cutoff is not None, then the return only includes observations with
        multiple testing corrected p-values strictly below the cutoff. The
        returned array or dataframe can be empty if there are no outlier
        candidates at the specified cutoff.

    Returns
    -------
    table : ndarray or DataFrame
        Returns either an ndarray or a DataFrame if labels is not None.
        Will attempt to get labels from model_results if available. The
        columns are the Studentized residuals, the unadjusted p-value,
        and the corrected p-value according to method.

    Notes
    -----
    The unadjusted p-value is stats.t.sf(abs(resid), df) where
    df = df_resid - 1.
    r   statsN
row_labelsget_influencez=model_results object %s does not have a get_influence method.      )alphamethod	DataFramestudent_residunadj_pz(p)columnsindex)scipyr   getattrmodeldatar
   AttributeError	__class____name__resid_studentized_externalnpabsargsortasarraydf_residtsfr   c_slicepandasr   )model_resultsr   r   labelsordercutoffr   inflresultsresididxdfr   adj_pr!   maskr   s                    d/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/outliers_influence.pyoutlier_testr<      s   \ ~,1<FF=/488D|&}55 ')0):)CD E E 	E DFF-E -fUmm##%%ddd+c
Z'',F		!	#Bgjj++a/G'v>>>E5q)*DAAArE{V#DzT{{$$$$$$y"19fun!M!z&11$79 9 9 	9 K       c                    |dz   }| j         j        j        d         }t          j        | j                  }|t          j        |dz                                            z  }t          j        ||          ddddf         }t          j	        | j         j        |f          }|t          j        |dz                      d                    z  }| j         j
        | j         j
        dz                                  z  }t          ||                                          }t          j        |dz
  |j        d         |          }	|                    |	          S )ar  Ramsey's RESET specification test for linear models

    This is a general specification test, for additional non-linear effects
    in a model.

    Parameters
    ----------
    degree : int
        Maximum power to include in the RESET test.  Powers 0 and 1 are
        excluded, so that degree tests powers 2, ..., degree of the fitted
        values.

    Notes
    -----
    The test fits an auxiliary OLS regression where the design matrix, exog,
    is augmented by powers 2 to degree of the fitted values. Then it performs
    an F-test whether these additional terms are significant.

    If the p-value of the f-test is below a threshold, e.g. 0.1, then this
    indicates that there might be additional non-linear effects in the model
    and that the linear model is mis-specified.

    References
    ----------
    https://en.wikipedia.org/wiki/Ramsey_RESET_test
    r   r   Nr   )r    exogshaper&   r)   fittedvaluessqrtmeanvandercolumn_stackendogr   fiteyef_test)
resdegreer2   k_varsnorm_valuesy_fitted_vanderrA   rH   res_auxr_matrixs
             r;   reset_ramseyrS   l   s$   6 QJEY^!!$F*S-..K)9(?(?(A(A B BBKiU33AAAssF;O?CINO<==DBGTQY$$Q''(((DIOsy!399;;;E%""$$Gvfqj$*Q-88H >>(###r=   c                    | j         d         }t          j        |           } | dd|f         }t          j        |          |k    }| dd|f         }t	          ||                                          j        }dd|z
  z  }|S )a  
    Variance inflation factor, VIF, for one exogenous variable

    The variance inflation factor is a measure for the increase of the
    variance of the parameter estimates if an additional variable, given by
    exog_idx is added to the linear regression. It is a measure for
    multicollinearity of the design matrix, exog.

    One recommendation is that if VIF is greater than 5, then the explanatory
    variable given by exog_idx is highly collinear with the other explanatory
    variables, and the parameter estimates will have large standard errors
    because of this.

    Parameters
    ----------
    exog : {ndarray, DataFrame}
        design matrix with all explanatory variables, as for example used in
        regression
    exog_idx : int
        index of the exogenous variable in the columns of exog

    Returns
    -------
    float
        variance inflation factor

    Notes
    -----
    This function does not save the auxiliary regression.

    See Also
    --------
    xxx : class for regression diagnostics  TODO: does not exist yet

    References
    ----------
    https://en.wikipedia.org/wiki/Variance_inflation_factor
    r   N      ?)rB   r&   r)   aranger   rI   rsquared)rA   exog_idxrN   x_ir:   x_notir_squared_ivifs           r;   variance_inflation_factorr]      s    N Z]F:dD
qqq({
C9V(D!!!T']Fc6""&&((1K
[ 
!CJr=   c                   n    e Zd ZdZ e ej        di ddi          	 	 dd	            Zdd
Z	 	 ddZ	dS )_BaseInfluenceMixinz=common methods between OLSInfluence and MLE/GLMInfluence
    extra_params_doc Nr   cooks0         ?c                     |t          | d          od| j        v }ddlm} | j         || j        | f||||||d|}	n6t          j        d            || j        | f||||| j        | j	        ||d|}	|	S )N_cacheres_looor   )_influence_plot)externalr   	criterionsize
plot_alphaaxz0Plot uses pearson residuals and exog hat matrix.)ri   r   rj   rk   leverager6   rl   rm   )
hasattrrf   $statsmodels.graphics.regressionplotsrh   hat_matrix_diagr5   warningswarnhat_matrix_exog_diagr6   )
selfri   r   rj   rk   rl   rm   kwargsrh   rL   s
             r;   plot_influencez"_BaseInfluenceMixin.plot_influence   s     tX..L:3LHHHHHHH+!/$, Jx(-,5D-7BJ J CIJ JCC
 MLMMM!/$, Jx(-,5D+/+D(,
-7BJ J
 CIJ JC 
r=   c           	         ddl m} |                    |          \  }}|d}t          | j                  }	t          j        |	          }
 |j        |
|fi | |dk    r t          j        |	t
          j	                  }nt          j
        |          |k    }dt          j        |	          z  }| j        j        j        j        }|t          j        |	          }|                    t          j        |          d         |t#          |
|          t#          | |          d|          }ddd	} |j        |fi |  |j        di |  |j        |fi | |S )Nr   )utilsz
Index Plotall   large   black)fontsizecolorObservation)r   )statsmodels.graphicsry   create_mpl_axlenrH   r&   rV   scatteronesbool_r'   r5   r    r!   r   annotate_axeswherer   
set_ylabel
set_xlabel	set_title)ru   yylabel	thresholdtitlerm   kwdsry   fignobsr   large_pointspsizer1   fonts                  r;   _plot_indexz_BaseInfluenceMixin._plot_index   sx   ......%%b))R= E4:	$
5!$$t$$$7422LL6!99y0LBGDMM!#(3>Yt__F  ,!7!7!:F!%eQ!%ufe!4!4g!#% %
 11f%%%%%,,t,,,U##d###
r=   c                    |}|d}|dk    r,| j         dd|f         }d| j        j        j        |         z   }	n|                    d          r| j        d         }d}	n|                    d          s|                    d	          r
| j        }d
}	n`|                    d          r| j        d         }d}	n;|                    d          r
| j        }d}	nt          | |          }|||         }|}	 | j	        ||	f|||d|}
|
S )a.  index plot for influence attributes

        Parameters
        ----------
        y_var : str
            Name of attribute or shortcut for predefined attributes that will
            be plotted on the y-axis.
        threshold : None or float
            Threshold for adding annotation with observation labels.
            Observations for which the absolute value of the y_var is larger
            than the threshold will be annotated. Set to a negative number to
            label all observations or to a large number to have no annotation.
        title : str
            If provided, the title will replace the default "Index Plot" title.
        ax : matplolib axis instance
            The plot will be added to the `ax` if provided, otherwise a new
            figure is created.
        idx : {None, int}
            Some attributes require an additional index to select the y-var.
            In dfbetas this refers to the column indes.
        kwds : optional keywords
            Keywords will be used in the call to matplotlib scatter function.
        Nrz   dfbetazDFBETA for cookr   zCook's distancehatleverz!Leverage (diagonal of hat matrix)	resid_stuz Internally Studentized Residuals)r   r   rm   )
dfbetasr5   r    
exog_names
startswithcooks_distancerq   resid_studentizedr   r   )ru   y_varr   r   rm   r7   r   rj   r   r   r   s              r;   
plot_indexz_BaseInfluenceMixin.plot_index  sY   2 	I  QQQV$A"T\%7%B3%GGFF!!&)) 	#A&A&FF!!%(( 	I,@,@,I,I 	$A8FF!!&)) 	#A&A&FF!!+.. 	&A7FF e$$AcFFdq& .IU"$. .(,. .
r=    )Nr   rb   rc   rd   N)NNN)rb   NNNN)
r$   
__module____qualname____doc__r   r   formatrw   r   r   r   r=   r;   r_   r_      s          X(!(DD,>+CDDEEAH37   FE*   < HL6 6 6 6 6 6r=   r_   c                      e Zd ZdZ	 	 ddZed             Zed             Zed             Zed             Z	ed             Z
ed	             Zed
             Zd ZddZed             Zed             Zed             Zd ZdS )MLEInfluencea  Global Influence and outlier measures (experimental)

    Parameters
    ----------
    results : instance of results class
        This only works for model and results classes that have the necessary
        helper methods.
    other arguments :
        Those are only available to override default behavior and are used
        instead of the corresponding attribute of the results class.
        By default resid_pearson is used as resid.

    Attributes
    ----------
    hat_matrix_diag (hii) : This is the generalized leverage computed as the
        local derivative of fittedvalues (predicted mean) with respect to the
        observed response for each observation.
        Not available for ZeroInflated models because of nondifferentiability.
    d_params : Change in parameters computed with one Newton step using the
        full Hessian corrected by division by (1 - hii).
        If hat_matrix_diag is not available, then the division by (1 - hii) is
        not included.
    dbetas : change in parameters divided by the standard error of parameters
        from the full model results, ``bse``.
    cooks_distance : quadratic form for change in parameters weighted by
        ``cov_params`` from the full model divided by the number of variables.
        It includes p-values based on the F-distribution which are only
        approximate outside of linear Gaussian models.
    resid_studentized : In the general MLE case resid_studentized are
        computed from the score residuals scaled by hessian factor and
        leverage. This does not use ``cov_params``.
    d_fittedvalues : local change of expected mean given the change in the
        parameters as computed in ``d_params``.
    d_fittedvalues_scaled : same as d_fittedvalues but scaled by the standard
        errors of a predicted mean of the response.
    params_one : is the one step parameter estimate computed as ``params``
        from the full sample minus ``d_params``.

    Notes
    -----
    MLEInfluence uses generic definitions based on maximum likelihood models.

    MLEInfluence produces the same results as GLMInfluence for canonical
    links (verified for GLM Binomial, Poisson and Gaussian). There will be
    some differences for non-canonical links or if a robust cov_type is used.
    For example, the generalized leverage differs from the definition of the
    GLM hat matrix in the case of Probit, which corresponds to family
    Binomial with a non-canonical link.

    The extension to non-standard models, e.g. multi-link model like
    BetaModel and the ZeroInflated models is still experimental and might still
    change.
    Additonally, ZeroInflated and some threshold models have a
    nondifferentiability in the generalized leverage. How this case is treated
    might also change.

    Warning: This does currently not work for constrained or penalized models,
    e.g. models estimated with fit_constrained or fit_regularized.

    This has not yet been tested for correctness when offset or exposure
    are used, although they should be supported by the code.

    status: experimental,
    This class will need changes to support different kinds of models, e.g.
    extra parameters in discrete.NegativeBinomial or two-part models like
    ZeroInflatedPoisson.
    Nc                    t          |          x| _        }|j        j        j        \  | _        | _        t          j        |j	                  | _
        ||n|j        j        | _        ||n|j        j        | _        ||n|j        | _        ||| _        nCt          |dd           | _        | j        &| j        t          j        | j                  z  | _        ||n|                                | _        |j        j        | _        | j        j                            | j        j	                  | _        | j        j                            | j        j	                  | _        |	|| _        d S d S )Nresid_pearson)r
   r5   r    rA   rB   r   rN   r&   rk   paramsk_paramsrH   scaler6   r   rD   
cov_paramsr#   model_classhessian	score_obs_hat_matrix_diag)ru   r5   r6   rH   rA   rq   r   r   s           r;   __init__zMLEInfluence.__init__  sH    "6g!>!>>w!(!3!9	4;//#/UUW]5H
 ,DD'-2D	#/UUW]
DJJ /4@@DJz%!Z"'$**=*==
)3)?:: ' 2 2 4 4 	"=2|)11$,2EFF+55dl6IJJ&$3D!!! '&r=   c                    t          | d          r| j        S 	 | j        j                            | j        j                  }n# t          $ r d}Y nw xY w|t          j        dt                     dS | j        j        
                    | j        j                  }|t          j                            | j         |j                  j        z                      d          }|S )zwDiagonal of the generalized leverage

        This is the analogue of the hat matrix diagonal for general MLE.
        r   Nz0hat matrix is not available, missing derivativesr   )ro   r   r5   r    _deriv_score_obs_dendogr   NotImplementedErrorrr   rs   UserWarning_deriv_mean_dparamsr&   linalgsolver   Tsum)ru   dsdydmu_dphs       r;   rq   zMLEInfluence.hat_matrix_diag  s     4+,, 	)((	<%==#% %DD" 	 	 	DDD	 <ML%' ' '4#778KLL bioot|mTV<<>>CCAFFs   )A AAc                     t          | j        j        dd          }|t          j         |                      }n| j        }|t          j                            |          j        z  	                    d          S )z>Diagonal of the hat_matrix using only exog as in OLS

        
_get_exogsNr   )
r   r5   r    r&   rG   rA   r   pinvr   r   )ru   	get_exogsrA   s      r;   rt   z!MLEInfluence.hat_matrix_exog_diag  sh    
 DL.dCC	 ?99;;//DD9Dry~~d++--221555r=   c                     | j                             d          | j         z
  }t          j                            | j        |j                  j        }| j        |d| j        z
  dddf         z  }|S )zApproximate change in parameter estimates when dropping observation.

        This uses one-step approximation of the parameter change to deleting
        one observation.
        r   Nr   )r   r   r&   r   r   r   r   rq   )ru   so_notibeta_is      r;   d_paramszMLEInfluence.d_params  si     .$$Q''$.8wy99;+q4//D99Fr=   c                 .    | j         | j        j        z  }|S )zScaled change in parameter estimates.

        The one-step change of parameters in d_params is rescaled by dividing
        by the standard error of the parameter estimate given by results.bse.
        )r   r5   bseru   r   s     r;   r   zMLEInfluence.dfbetas  s     !11r=   c                 *    | j         j        | j        z
  S )zParameter estimate based on one-step approximation.

        This the one step parameter estimate computed as
        ``params`` from the full sample minus ``d_params``.
        )r5   r   r   ru   s    r;   
params_onezMLEInfluence.params_one  s     |"T]22r=   c                    | j         t          j                            | j        | j         j                  j        z                      d          }|| j        z  }ddlm	} |j
                            || j        | j        j                  }||fS )a  Cook's distance and p-values.

        Based on one step approximation d_params and on results.cov_params
        Cook's distance divides by the number of explanatory variables.

        p-values are based on the F-distribution which are only approximate
        outside of linear Gaussian models.

        Warning: The definition of p-values might change if we switch to using
        chi-square distribution instead of F-distribution, or if we make it
        dependent on the fit keyword use_t.
        r   r   r   )r   r&   r   r   r   r   r   r   r   r   fr,   r5   r*   )ru   cooks_d2r   pvalss       r;   r   zMLEInfluence.cooks_distance  s     MBIOODO48MO%E %EEFGHKA 	DM!
 

8T]DL4IJJr=   c                 J    | j         t          j        d| j        z
            z  S )a@  studentized default residuals.

        This uses the residual in `resid` attribute, which is by default
        resid_pearson and studentizes is using the generalized leverage.

        self.resid / np.sqrt(1 - self.hat_matrix_diag)

        Studentized residuals are not available if hat_matrix_diag is None.

        r   )r6   r&   rD   rq   r   s    r;   r   zMLEInfluence.resid_studentized  s#     zBGA(<$<====r=   c                    ddl m} | j        j                            | j        j                  }| j        j                            | j        j                  }t          |t                    r|d         }t          |t                    r|d         }t          | j        j        |          s| }|t          j
        |          z  t          j
        d| j        z
            z  S )a  Score residual divided by sqrt of hessian factor.

        experimental, agrees with GLMInfluence for Binomial and Gaussian.
        This corresponds to considering the linear predictors as parameters
        of the model.

        Note: Nhis might have nan values if second derivative, hessian_factor,
        is positive, i.e. loglikelihood is not globally concave w.r.t. linear
        predictor. (This occured in an example for GeneralizedPoisson)
        r   )GLMr   )+statsmodels.genmod.generalized_linear_modelr   r5   r    score_factorr   hessian_factor
isinstancetupler&   rD   rq   )ru   r   r,   hfs       r;   resid_score_factorzMLEInfluence.resid_score_factor  s     	DCCCCC\,,T\-@AA\..t|/BCCb%   	ABb%   	AB$,,c22 	BBGBKK"'!d.B*B"C"CCCr=   TFc                 d   | j         j                            | j         j                  }| j         j                            | j         j                  }| |dd|f         }||dddf         |f         }|rB|j        t          j                            | |j                  z  	                    d          }n*|t          j
        t          j        |                     z  }|rK|r |t          j
        d| j        z
            z  }n)|t          j
        d| j        dddf         z
            z  }|S )a  Score observations scaled by inverse hessian.

        Score residual in resid_score are defined in analogy to a score test
        statistic for each observation.

        Parameters
        ----------
        joint : bool
            If joint is true, then a quadratic form similar to score_test is
            returned for each observation.
            If joint is false, then standardized score_obs are returned. The
            returned array is two-dimensional
        index : ndarray (optional)
            Optional index to select a subset of score_obs columns.
            By default, all columns of score_obs will be used.
        studentize : bool
            If studentize is true, the the scaled residuals are also
            studentized using the generalized leverage.

        Returns
        -------
        array :  1-D or 2-D residuals

        Notes
        -----
        Status: experimental

        Because of the one srep approacimation of d_params, score residuals
        are identical to cooks_distance, except for

        - cooks_distance is normalized by the number of parameters
        - cooks_distance uses cov_params, resid_score is based on Hessian.
          This will make them differ in the case of robust cov_params.

        Nr   r   )r5   r    r   r   r   r   r&   r   r   r   rD   diagrq   )ru   jointr   
studentizer   hessr6   s          r;   resid_scorezMLEInfluence.resid_score#  s(   J L&001DEE	|!))$,*=>>!!!!U(+Iaaag-.D 	8[29??D5)+#F#FFKKANNEE 7 77E 	D DT%9!9::: T%9!!!T'%B!BCCCr=   c                     t          j                    5  d}t          j        d|t                     | j                                        }d d d            n# 1 swxY w Y   |S )Nz0linear keyword is deprecated, use which="linear"ignore)messagecategory)rr   catch_warningsfilterwarningsFutureWarningr5   get_prediction)ru   msgpreds      r;   _get_predictionzMLEInfluence._get_prediction\  s     $&& 	1 	1DC#Hc-:< < < <<..00D		1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1
 s   8AAAc                     t          j        | j        j                  }| j        j                            |          }|| j        z                      d          S )a2  Change in expected response, fittedvalues.

        Local change of expected mean given the change in the parameters as
        computed in d_params.

        Notes
        -----
        This uses the one-step approximation of the parameter change to
        deleting one observation ``d_params``.
        r   )r&   r)   r5   r   r    r   r   r   )ru   r   derivs      r;   d_fittedvalueszMLEInfluence.d_fittedvaluesg  sK     DL/00"66v>>%**1---r=   c                 *    | j         | j        j        z  S )a  
        Change in fittedvalues scaled by standard errors.

        This uses one-step approximation of the parameter change to deleting
        one observation ``d_params``, and divides by the standard errors
        for the predicted mean provided by results.get_prediction.
        )r   r   ser   s    r;   d_fittedvalues_scaledz"MLEInfluence.d_fittedvalues_scaledx  s     "T%9%<<<r=   c                    ddl m} | j        j        j        }|j        }d |j        D             }| j        9 |t          | j	        d         | j
        | j        | j                  |          }n, |t          | j	        d         | j                  |          } || j        ||          }|                    |          S )	a.  
        Creates a DataFrame with influence results.

        Returns
        -------
        frame : pandas DataFrame
            A DataFrame with selected results for each observation.
            The index will be the same as provided to the model.

        Notes
        -----
        The resultant DataFrame contains six variables in addition to the
        ``dfbetas``. These are:

        * cooks_d : Cook's Distance defined in ``cooks_distance``
        * standard_resid : Standardized residuals defined in
          `resid_studentizedl`
        * hat_diag : The diagonal of the projection, or hat, matrix defined in
          `hat_matrix_diag`. Not included if None.
        * dffits_internal : DFFITS statistics using internally Studentized
          residuals defined in `d_fittedvalues_scaled`
        r   r   c                     g | ]}d |z   S dfb_r   .0is     r;   
<listcomp>z.MLEInfluence.summary_frame.<locals>.<listcomp>      777avz777r=   N)cooks_dstandard_residhat_diagdffits_internalr   )r   r  r   )r/   r   r5   r    r!   r   xnamesrq   dictr   r   r   r   joinru   r   r!   r   beta_labelssummary_datar   s          r;   summary_framezMLEInfluence.summary_frame  s   . 	%$$$$$ |!&_
774;777 +$9T+A.#5- $ :	&< &< &<
 !" " "LL %9T+A. !% :	&< &< &<
 !" " "L 4<!+- - - {{<(((r=   )NNNNNN)TNF)r$   r   r   r   r   r	   rq   rt   r   r   r   r   r   r   r   r   r   propertyr   r
  r   r=   r;   r   r   :  s       B BH >B>B4 4 4 46   ^4 	6 	6 ^	6   ^   ^ 3 3 ^3   ^2 > > ^>D D D07 7 7 7r   ^ . . ^.  = = X=2) 2) 2) 2) 2)r=   r   c                      e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zd dZed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd!dZd Zed             Zd Zd"dZdS )#OLSInfluencea[  class to calculate outlier and influence measures for OLS result

    Parameters
    ----------
    results : RegressionResults
        currently assumes the results are from an OLS regression

    Notes
    -----
    One part of the results can be calculated without any auxiliary regression
    (some of which have the `_internal` postfix in the name. Other statistics
    require leave-one-observation-out (LOOO) auxiliary regression, and will be
    slower (mainly results with `_external` postfix in the name).
    The auxiliary LOOO regression only the required results are stored.

    Using the LOO measures is currently only recommended if the data set
    is not too large. One possible approach for LOOO measures would be to
    identify possible problem observations with the _internal measures, and
    then run the leave-one-observation-out only with observations that are
    possible outliers. (However, this is not yet available in an automated way.)

    This should be extended to general least squares.

    The leave-one-variable-out (LOVO) auxiliary regression are currently not
    used.
    c                    t          |          | _        |j        j        j        \  | _        | _        |j        j        | _        |j        j        | _        |j        | _        |j        j	        | _
        |j        | _        i | _        i | _        d S N)r
   r5   r    rA   rB   r   rN   rH   r6   r#   r   	mse_residr   aux_regression_exogaux_regression_endog)ru   r5   s     r;   r   zOLSInfluence.__init__  sw    +G44!(!3!9	4;](
M&	]
"=2 &
#% $&!!!r=   c                 d    | j         | j        j        j        j        z                      d          S )zDiagonal of the hat_matrix for OLS

        Notes
        -----
        temporarily calculated here, this should go to model class
        r   )rA   r5   r    
pinv_wexogr   r   r   s    r;   rq   zOLSInfluence.hat_matrix_diag  s*     	DL.9;;@@CCCr=   c                 *    | j         }| j        d|z
  z  S )zPRESS residuals
        r   rq   r6   ru   hiis     r;   resid_presszOLSInfluence.resid_press  s     "zQW%%r=   c                 0    | j         }| j        |z  d|z
  z  S )zInfluence measure

        matches the influence measure that gretl reports
        u * h / (1 - h)
        where u are the residuals and h is the diagonal of the hat_matrix
        r   r  r  s     r;   	influencezOLSInfluence.influence  s"     "zC1s7++r=   c                      | j         }|d|z
  z  S )zFactor of diagonal of hat_matrix used in influence

        this might be useful for internal reuse
        h / (1 - h)
        r   )rq   r  s     r;   hat_diag_factorzOLSInfluence.hat_diag_factor  s     "a#gr=   c                 @    t          j        | j        | j                  S )z0Error sum of squares of PRESS residuals
        )r&   dotr  r   s    r;   	ess_presszOLSInfluence.ess_press  s     vd&(8999r=   c                     | j         S )zStudentized residuals using variance from OLS

        alias for resid_studentized_internal for compatibility with
        MLEInfluence this uses sigma from original estimate and does
        not require leave one out loop
        )resid_studentized_internalr   s    r;   r   zOLSInfluence.resid_studentized  s     ..r=   c                 .    |                      d          S )zStudentized residuals using variance from OLS

        this uses sigma from original estimate
        does not require leave one out loop
        Nsigma)get_resid_studentized_externalr   s    r;   r"  z'OLSInfluence.resid_studentized_internal  s     222>>>r=   c                 `    t          j        | j                  }|                     |          S )zStudentized residuals using LOOO variance

        this uses sigma from leave-one-out estimates

        requires leave one out loop for observations
        r$  )r&   rD   sigma2_not_obsir&  )ru   
sigma_looos     r;   r%   z'OLSInfluence.resid_studentized_external&  s-     WT122
222DDDr=   Nc                     | j         }|| j        }t          j        |          }| j        |z  t          j        d|z
            z  S )a  calculate studentized residuals

        Parameters
        ----------
        sigma : None or float
            estimate of the standard deviation of the residuals. If None, then
            the estimate from the regression results is used.

        Returns
        -------
        stzd_resid : ndarray
            studentized residuals

        Notes
        -----
        studentized residuals are defined as ::

           resid / sigma / np.sqrt(1 - hii)

        where resid are the residuals from the regression, sigma is an
        estimate of the standard deviation of the residuals, and hii is the
        diagonal of the hat_matrix.
        Nr   )rq   r   r&   rD   r6   )ru   r%  r  
sigma2_ests       r;   r&  z+OLSInfluence.get_resid_studentized_external1  sG    0 "=JGJ''EzE!BGAG$4$444r=   c                     | j         }| j        dz  | j        z  }||d|z
  z  z  }ddlm} |j                            || j        | j        j                  }||fS )aa  
        Cooks distance

        Uses original results, no nobs loop

        References
        ----------
        .. [*] Eubank, R. L. (1999). Nonparametric regression and spline
            smoothing. CRC press.
        .. [*] Cook's distance. (n.d.). In Wikipedia. July 2019, from
            https://en.wikipedia.org/wiki/Cook%27s_distance
        r   r   r   r   	rq   r   rN   r   r   r   r,   r5   r*   ru   r  r   r   r   s        r;   r   zOLSInfluence.cooks_distanceR  sn     ")Q.<C1s7O# 

8T[$,2GHHr=   c                     | j         }| j        t          j        |d|z
  z            z  }dt          j        | j        dz  | j        z            z  }||fS )zdffits measure for influence of an observation

        based on resid_studentized_internal
        uses original results, no nobs loop
        r   r   rU   )rq   r"  r&   rD   rN   r   ru   r  dffits_dffits_thresholds       r;   r  zOLSInfluence.dffits_internalm  sZ     "1BGC1s7O4L4LLrwt{R'7$)'CDDD(((r=   c                     | j         }| j        t          j        |d|z
  z            z  }dt          j        | j        dz  | j        z            z  }||fS )a  
        dffits measure for influence of an observation

        based on resid_studentized_external,
        uses results from leave-one-observation-out loop

        It is recommended that observations with dffits large than a
        threshold of 2 sqrt{k / n} where k is the number of parameters, should
        be investigated.

        Returns
        -------
        dffits : float
        dffits_threshold : float

        References
        ----------
        `Wikipedia <https://en.wikipedia.org/wiki/DFFITS>`_
        r   r   rU   )rq   r%   r&   rD   rN   r   r0  s       r;   dffitszOLSInfluence.dffits|  sZ    0 "1BGC1s7O4L4LLrwt{R'7$)'CDDD(((r=   c                     | j         j        | j        z
  }|t          j        | j        dddf                   z  }|t          j        t          j        | j         j                            z  }|S )Jdfbetas

        uses results from leave-one-observation-out loop
        N)r5   r   params_not_obsir&   rD   r(  r   normalized_cov_params)ru   r   s     r;   r   zOLSInfluence.dfbetas  sb     ,%(<<274/4899927274<#EFFGGGr=   c                 .    | j         j        | j        z
  }|S )r6  )r5   r   r7  )ru   r   s     r;   r   zOLSInfluence.dfbeta  s     $t';;r=   c                 @    t          j        | j        d                   S )zerror variance for all LOOO regressions

        This is 'mse_resid' from each auxiliary regression.

        uses results from leave-one-observation-out loop
        r  r&   r)   	_res_looor   s    r;   r(  zOLSInfluence.sigma2_not_obsi  s     z$.5666r=   c                 @    t          j        | j        d                   S )zoparameter estimates for all LOOO regressions

        uses results from leave-one-observation-out loop
        r   r;  r   s    r;   r7  zOLSInfluence.params_not_obsi  s     z$.2333r=   c                 @    t          j        | j        d                   S )ztdeterminant of cov_params of all LOOO regressions

        uses results from leave-one-observation-out loop
        det_cov_paramsr;  r   s    r;   det_cov_params_not_obsiz$OLSInfluence.det_cov_params_not_obsi  s     z$.)9:;;;r=   c                     | j         t          j                            | j                                                  z  }|S )zcovariance ratio between LOOO and original

        This uses determinant of the estimate of the parameter covariance
        from leave-one-out estimates.
        requires leave one out loop for observations
        )r@  r&   r   detr5   r   )ru   	cov_ratios     r;   rC  zOLSInfluence.cov_ratio  s7     1y}}T\%<%<%>%>??@	r=   c                 &    | j         d| j        z
  z  S )zestimate of variance of the residuals

        ::

           sigma2 = sigma2_OLS * (1 - hii)

        where hii is the diagonal of the hat matrix
        r   )r   rq   r   s    r;   	resid_varzOLSInfluence.resid_var  s     zQ!5566r=   c                 4    t          j        | j                  S )zmestimate of standard deviation of the residuals

        See Also
        --------
        resid_var
        )r&   rD   rE  r   s    r;   	resid_stdzOLSInfluence.resid_std  s     wt~&&&r=   rH   Tc                    |dk    r1| j         }t          ||          r||         S | j        j        j        }nE	 | j        |         |          n# t          $ r Y nw xY w| j        |         }i }| j        dd|f         }| j        j        d         }t          j
        |          |k    }| j        dd|f         }t          ||                                          }	|r|	||<   |	S )a  regression results from LOVO auxiliary regression with cache


        The result instances are stored, which could use a large amount of
        memory if the datasets are large. There are too many combinations to
        store them all, except for small problems.

        Parameters
        ----------
        drop_idx : int
            index of exog that is dropped from the regression
        endog_idx : 'endog' or int
            If 'endog', then the endogenous variable of the result instance
            is regressed on the exogenous variables, excluding the one at
            drop_idx. If endog_idx is an integer, then the exog with that
            index is regressed with OLS on all other exogenous variables.
            (The latter is the auxiliary regression for the variance inflation
            factor.)

        this needs more thought, memory versus speed
        not yet used in any other parts, not sufficiently tested
        rH   Nr   )r  ro   r5   r    rH   r  KeyErrorrA   rB   r&   rV   r   rI   )
ru   drop_idx	endog_idxstorestoredrY   rN   r:   rZ   rL   s
             r;   
_ols_xnotizOLSInfluence._ols_xnoti  s   2 .Fvx(( (h'',$*CC(3H===    -i8FF)AAAyL)C#y  H,111d7##v""$$ 	#"F8
s   A 
AAc                 Z   ddl m} | j        j        j        }| j        } || j                  }t          t                    }|D ]e\  }}|D ]]}	| 	                    ||dd|f                   
                                }
||	                             t          |
|	                     ^f|S )az  
        regress endog on exog without one of the variables

        This uses a k_vars loop, only attributes of the OLS instance are
        stored.

        Parameters
        ----------
        attributes : list[str]
           These are the names of the attributes of the auxiliary OLS results
           instance that are stored and returned.

        not yet used
        r   LeaveOneOutN)#statsmodels.sandbox.tools.cross_valrQ  r5   r    rH   rA   rN   r   listr   rI   appendr   )ru   
attributesrQ  rH   rA   cv_iterres_looinidxoutidxattres_is              r;   _get_drop_varizOLSInfluence._get_drop_vari  s     	DCCCCC"(y+dk**d##$ 	9 	9ME6! 9 9((QQQX??CCEE##GE3$7$788889 r=   c                    ddl m} d }| j        j        j        }| j        j        j        }t          j        |j        t                    }t          j        |j        t                    }t          j        |j        t                    } || j
                  }|D ][\  }	}
|                     ||	         ||	                                                   }|j        ||
<   |j        ||
<    ||          ||
<   \t          |||          S )a9  collect required results from the LOOO loop

        all results will be attached.
        currently only 'params', 'mse_resid', 'det_cov_params' are stored

        regresses endog on exog dropping one observation at a time

        this uses a nobs loop, only attributes of the OLS instance are stored.
        r   rP  c                 d    t           j                            |                                           S r  r&   r   rB  r   rL   s    r;   get_det_cov_paramsz2OLSInfluence._res_looo.<locals>.get_det_cov_paramsI  s     9==!1!1222r=   dtype)r   r  r?  )rR  rQ  r5   r    rH   rA   r&   zerosrB   floatr   r   rI   r   r  r  )ru   rQ  ra  rH   rA   r   r  r?  rV  rX  rY  r[  s               r;   r<  zOLSInfluence._res_looo<  s    	DCCCCC	3 	3 	3 "(|!&$*E222HU[666	%+U;;;+di(($ 	? 	?ME6$$U5\4;??CCEEE"\F6N %If%7%7%>%>N6""6Y#13 3 3 	3r=   c                 P   ddl m} | j        j        j        }|j        }d |j        D             } |t          | j        d         | j	        | j
        | j        d         | j        | j        d                   |          } || j        ||          }|                    |          S )a  
        Creates a DataFrame with all available influence results.

        Returns
        -------
        frame : DataFrame
            A DataFrame with all results.

        Notes
        -----
        The resultant DataFrame contains six variables in addition to the
        DFBETAS. These are:

        * cooks_d : Cook's Distance defined in `Influence.cooks_distance`
        * standard_resid : Standardized residuals defined in
          `Influence.resid_studentized_internal`
        * hat_diag : The diagonal of the projection, or hat, matrix defined in
          `Influence.hat_matrix_diag`
        * dffits_internal : DFFITS statistics using internally Studentized
          residuals defined in `Influence.dffits_internal`
        * dffits : DFFITS statistics using externally Studentized residuals
          defined in `Influence.dffits`
        * student_resid : Externally Studentized residuals defined in
          `Influence.resid_studentized_external`
        r   r   c                     g | ]}d |z   S r   r   r   s     r;   r   z.OLSInfluence.summary_frame.<locals>.<listcomp>|  r   r=   )r   r   r  r  r   r4  r  r   )r/   r   r5   r    r!   r   r  r  r   r"  rq   r  r%   r4  r   r  r  s          r;   r
  zOLSInfluence.summary_frame]  s    4 	%$$$$$ |!&_
774;777 !y'*:) 039;q>"
 "
 "
    4<!+- - - {{<(((r=   %6.3fc                    dt          j        | j                  fd| j        fd| j        j        fd| j        d         fd| j        fd| j        fd| j	        d         fd	| j
        fd
| j        d         fg	}t          | \  }}t          j        |          }|| _        ddlm} ddlm}m} ddlm}  ||          }	 ||          }
dg|g|j        d         dz
  z  z   |	d<    ||||	|
          S )a  create a summary table with all influence and outlier measures

        This does currently not distinguish between statistics that can be
        calculated from the original regression results and for which a
        leave-one-observation-out loop is needed

        Returns
        -------
        res : SimpleTable
           SimpleTable instance with the results, can be printed

        Notes
        -----
        This also attaches table_data to the instance.
        obsrH   zfitted
valuezCook's
dr   zstudent.
residualzhat diagzdffits 
internalzext.stud.
residualr4  deepcopySimpleTabledefault_html_fmtfmt_base%4dr   	data_fmtsheaderstxt_fmthtml_fmt)r&   rV   r   rH   r5   rC   r   r"  rq   r  r%   r4  r   rG   
table_datacopyrl  statsmodels.iolib.tablern  ro  !statsmodels.iolib.tableformattingrq  rB   )ru   	float_fmt	table_rawcolnamesr!   rl  rn  ro  rq  fmtfmt_htmls              r;   summary_tablezOLSInfluence.summary_table  sK   : RYty112tz*%t|'@A!4#6q#9:*D,KL $"67)4+?+BC+T-LMA/		 y)$t$$!!!!!!IIIIIIII>>>>>>hx  8,--!7i[DJqMA4E%FFK{43$,. . . 	.r=   r  )rH   T)rh  ) r$   r   r   r   r   r	   rq   r  r  r  r   r   r"  r%   r&  r   r  r4  r   r   r(  r  r7  r@  rC  rE  rG  rN  r\  r<  r
  r  r   r=   r;   r  r    s        6' ' ' D D ^D & & ^& , , ^,   ^ : : ^:
 / / ^/ ? ? ^? E E ^E5 5 5 5B   ^4 ) ) ^) ) ) ^)8   ^   ^ 7 7 ^7 4 4 X4 < < X< 
 
 ^
 
7 
7 ^
7 ' ' ^'2 2 2 2h  : 3 3 ^3@/) /) /)b3. 3. 3. 3. 3. 3.r=   r  c                    ddl m} ddlm} t	          |           }t          j        |j        | j        z            }|j	        
                    |dz  | j                  }t          j        | j        ||z  z
  | j        ||z  z   g          } || |          }|\  }	}
}t          j        |
|f          }t          j        | j        d|j        z
  z            }t          j        t          j        | j                  dz   | j        j        | j        ||dddf         |dddf         |dddf         |dddf         | j        ||j        |j        d         g          }|}g d}|}dd	lm} dd
lm}m} ddlm}  ||          } ||          }dgdg|j        d         dz
  z  z   |d<    |||||          }|||fS )a  
    Generate summary table of outlier and influence similar to SAS

    Parameters
    ----------
    alpha : float
       significance level for confidence interval

    Returns
    -------
    st : SimpleTable
       table with results that can be printed
    data : ndarray
       calculated measures and statistics for the table
    ss2 : list[str]
       column_names for table (Note: rows of table are observations)
    r   r   )wls_prediction_stdg       @)r   r   N)ObszDep Var
PopulationzPredicted
ValuezStd Error
Mean PredictzMean ci
95% lowzMean ci
95% uppzPredict ci
95% lowzPredict ci
95% uppResidualzStd Error
ResidualzStudent
ResidualzCook's
Drk  rm  rp  rr  rh  rs  rt  )r   r   &statsmodels.sandbox.regression.predstdr  r  r&   rD   rq   r  r+   isfr*   rG   rC   rV   r   r    rH   r6   r"  r   ry  rl  rz  rn  ro  r{  rq  rB   )rL   r   r   r  r4   predict_mean_setppfpredict_mean_citmp
predict_sepredict_ci_lowpredict_ci_upp
predict_ciresid_setable_smr!   ss2r~  rl  rn  ro  rq  r  r  sts                            r;   r  r    sZ   & IIIIIID gd2S]BCCO7;;urz3<00Do4/114/11'3 4 4O
 
S
.
.
.C14.J..!ABBJ ws}D,@(@ABBH
	#(a	11111a4111a4	'A   H  DD D DC H EEEEEEEE::::::
(8

Cx())Hw'djma.?!@@C	T8S&
( 
( 
(B tS=r=   c                        e Zd ZdZed             Zed             Ze fd            Zed             Ze	d             Z
e	d             Ze	d             Ze	d	             Zed
             Z xZS )GLMInfluencea3  Influence and outlier measures (experimental)

    This uses partly formulas specific to GLM, specifically cooks_distance
    is based on the hessian, i.e. observed or expected information matrix and
    not on cov_params, in contrast to MLEInfluence.
    Standardization for changes in parameters, in fittedvalues and in
    the linear predictor are based on cov_params.

    Parameters
    ----------
    results : instance of results class
        This only works for model and results classes that have the necessary
        helper methods.
    other arguments are only to override default behavior and are used instead
    of the corresponding attribute of the results class.
    By default resid_pearson is used as resid.

    Attributes
    ----------
    dbetas
        change in parameters divided by the standard error of parameters from
        the full model results, ``bse``.
    d_fittedvalues_scaled
        same as d_fittedvalues but scaled by the standard errors of a
        predicted mean of the response.
    d_linpred
        local change in linear prediction.
    d_linpred_scale
        local change in linear prediction scaled by the standard errors for
        the prediction based on cov_params.

    Notes
    -----
    This has not yet been tested for correctness when offset or exposure
    are used, although they should be supported by the code.

    Some GLM specific measures like d_deviance are still missing.

    Computing an explicit leave-one-observation-out (LOOO) loop is included
    but no influence measures are currently computed from it.
    c                 b    t          | d          r| j        S | j                                        S )z
        Diagonal of the hat_matrix for GLM

        Notes
        -----
        This returns the diagonal of the hat matrix that was provided as
        argument to GLMInfluence or computes it using the results method
        `get_hat_matrix`.
        r   )ro   r   r5   get_hat_matrixr   s    r;   rq   zGLMInfluence.hat_matrix_diag@  s4     4+,, 	1((<..000r=   c                     t           j                            | j                  | j        z  }|t          j        d| j        z
            z  }|j        S )zChange in parameter estimates

        Notes
        -----
        This uses one-step approximation of the parameter change to deleting
        one observation.
        r   )r&   r   r   rA   r   rD   rq   r   r   s     r;   r   zGLMInfluence.d_paramsP  sD     	**T-CC"'!d22333xr=   c                 *    t                      j        S )a#  
        Internally studentized pearson residuals

        Notes
        -----
        residuals / sqrt( scale * (1 - hii))

        where residuals are those provided to GLMInfluence which are
        pearson residuals by default, and
        hii is the diagonal of the hat matrix.
        )superr   )ru   r#   s    r;   r   zGLMInfluence.resid_studentized_  s     ww((r=   c                     | j         }| j        dz  | j        z  }||d|z
  z  z  }ddlm} |j                            || j        | j        j                  }||fS )a  Cook's distance

        Notes
        -----
        Based on one step approximation using resid_studentized and
        hat_matrix_diag for the computation.

        Cook's distance divides by the number of explanatory variables.

        Computed using formulas for GLM and does not use results.cov_params.
        It includes p-values based on the F-distribution which are only
        approximate outside of linear Gaussian models.
        r   r   r   r   r-  r.  s        r;   r   zGLMInfluence.cooks_distancep  sn     ")Q.<C1s7O# 

8T[$,2GHHr=   c                 ^    | j         j        j        }|| j        z                      d          S )z
        Change in linear prediction

        This uses one-step approximation of the parameter change to deleting
        one observation ``d_params``.
        r   )r5   r    rA   r   r   )ru   rA   s     r;   	d_linpredzGLMInfluence.d_linpred  s,     |!&t}$))!,,,r=   c                 4    | j         | j        j        j        z  S )a  
        Change in linpred scaled by standard errors

        This uses one-step approximation of the parameter change to deleting
        one observation ``d_params``, and divides by the standard errors
        for linpred provided by results.get_prediction.
        )r  r   linpredr   r   s    r;   d_linpred_scaledzGLMInfluence.d_linpred_scaled  s     ~ 4 < ???r=   c                      t          j        dt                      j        j        j        t          j         fdt           j	                  D                       }|
                                S )experimental code
        z this ignores offset and exposurec                 b    g | ]+\  }}j         j                            ||                   ,S r   )r5   r    predict)r   r   pirA   ru   s      r;   r   z2GLMInfluence._fittedvalues_one.<locals>.<listcomp>  sL     D D D$q"  <-55b$q'BB D D Dr=   )rr   rs   r   r5   r    rA   r&   array	enumerater   squeeze)ru   fittedrA   s   ` @r;   _fittedvalues_onezGLMInfluence._fittedvalues_one  s     	8+FFF |!& D D D D D(1$/(B(BD D D E E~~r=   c                 D    | j                                         | j        z
  S )r  )r5   r  r  r   s    r;   _diff_fittedvalues_onez#GLMInfluence._diff_fittedvalues_one  s     
 |##%%(>>>r=   c           	      (   ddl m} d }| j        j        j        }| j        j        j        }| j        j                                        }|                    d          }|                    d          }|                    d          x}}	|                    d          x}
}|                    dd	          }t          |d
         d          rd}nd}t          j
        |j        t                    }t          j
        |j        t                    }t          j
        |j        t                    } || j                  }|D ]\  }}|||         }	|
|
|         }|||         |d<    | j        ||         ||         f|	|||         ||         d|}|r|d         |j        _        |                    | j        j        d          }|j                                        ||<   |j        ||<    ||          ||<   t+          ||||          S )a  collect required results from the LOOO loop

        all results will be attached.
        currently only 'params', 'mse_resid', 'det_cov_params' are stored

        Reestimates the model with endog and exog dropping one observation
        at a time

        This uses a nobs loop, only attributes of the results instance are
        stored.

        Warning: This will need refactoring and API changes to be able to
        add options.
        r   rP  c                 d    t           j                            |                                           S r  r_  r`  s    r;   <lambda>z(GLMInfluence._res_looo.<locals>.<lambda>  s    s~~7G7G)H)H r=   freq_weightsvar_weightsoffsetexposuren_trialsNfamily
initializeTFrb  )r  r  r  r  newton)start_paramsr   )r   r   r  r?  )rR  rQ  r5   r    rH   rA   _get_init_kwdspopro   r&   rd  rB   re  r   r   r  nrI   r   ry  r   r  )ru   rQ  ra  rH   rA   	init_kwdsr  r  r  offset_r  	exposure_r  is_binomialr   r   r?  rV  rX  rY  mod_ir[  s                         r;   r<  zGLMInfluence._res_looo  sL     	DCCCCCHH"(|!&L&5577	 }}^44mmM22$==222(}}Z8889==T22 9X&55 	 KKK$*E222E222%+U;;;+di(($ 	? 	?ME6! -#$UO	#(0	*%$D$U5\4; 2,3.72>u2E1<U1C	2 2
 (12 2E  7!*:!6II4<+>%-  / /E"\..00F6N!KE&M%7%7%>%>N6""6%#13 3 3 	3r=   )r$   r   r   r   r	   rq   r   r   r   r  r  r  r  r  r<  __classcell__)r#   s   @r;   r  r    s1       ( (T 1 1 ^1   ^ ) ) ) ) ^)    ^6 
- 
- X
- @ @ X@ 	  	  X	  ? ? X? B3 B3 ^B3 B3 B3 B3 B3r=   r  )r   r   NFN)r>   )r   )r   rr   statsmodels.compat.pandasr   statsmodels.compat.pythonr   collectionsr   numpyr&   )statsmodels.graphics._regressionplots_docr   #statsmodels.regression.linear_modelr   statsmodels.stats.multitestr   statsmodels.tools.decoratorsr	   statsmodels.tools.toolsr
   r<   rS   r]   r_   r   r  r  r  r   r=   r;   <module>r     s     . . . . . . * * * * * * # # # # # #     I I I I I I 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 8 8 8 8 8 8 BF%)M M M Md)$ )$ )$ )$X. . .bn n n n n n n nb~) ~) ~) ~) ~)& ~) ~) ~)BF. F. F. F. F.& F. F. F.RN N N Nbh3 h3 h3 h3 h3< h3 h3 h3 h3 h3r=   