
    >hCB                        S SK rS SKJr  S SKJs  Jr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJs  Jr  S SKJs  Jr  S SKJr   " S S	5      r " S
 S\5      r " S S\5      r " S S\5      r " S S\R6                  5      r " S S\R:                  5      r " S S\R>                  5      r \RB                  " \ \5        g)    N)defaultdict)families)GLM)links)varfuncs)cache_readonlyc                       \ rS rSrSrS rSrg)QIFCovariance   aY  
A covariance model for quadratic inference function regression.

The mat method returns a basis matrix B such that the inverse
of the working covariance lies in the linear span of the
basis matrices.

Subclasses should set the number of basis matrices `num_terms`,
so that `mat(d, j)` for j=0, ..., num_terms-1 gives the basis
of dimension d.`
c                     [         e)z@
Returns the term'th basis matrix, which is a dim x dim
matrix.
)NotImplementedErrorselfdimterms      iC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/genmod/qif.pymatQIFCovariance.mat   s
    
 "!     N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r
   r
      s    
"r   r
   c                   $    \ rS rSrSrS rS rSrg)QIFIndependence"   a9  
Independent working covariance for QIF regression.  This covariance
model gives identical results to GEE with the independence working
covariance.  When using QIFIndependence as the working covariance,
the QIF value will be zero, and cannot be used for chi^2 testing, or
for model selection using AIC, BIC, etc.
c                     SU l         g )N   	num_termsr   s    r   __init__QIFIndependence.__init__+   	    r   c                 <    US:X  a  [         R                  " U5      $ g )Nr   )npeyer   s      r   r   QIFIndependence.mat.   s    1966#;r   r"   Nr   r   r   r   r   r%   r   r   r   r   r   r   r   "   s    r   r   c                   $    \ rS rSrSrS rS rSrg)QIFExchangeable5   z5
Exchangeable working covariance for QIF regression.
c                     SU l         g )N   r"   r$   s    r   r%   QIFExchangeable.__init__:   r'   r   c                 v    US:X  a  [         R                  " U5      $ US:X  a  [         R                  " X45      $ g )Nr   r!   )r)   r*   onesr   s      r   r   QIFExchangeable.mat=   s2    1966#;QY77C:&&r   r"   Nr,   r   r   r   r.   r.   5   s    r   r.   c                   $    \ rS rSrSrS rS rSrg)QIFAutoregressiveF   z7
Autoregressive working covariance for QIF regression.
c                     SU l         g )N   r"   r$   s    r   r%   QIFAutoregressive.__init__K   r'   r   c                 F   US:  a  Sn[        U5      eUS:X  a  [        R                  " U5      $ US:X  a<  [        R                  " X45      nSUR                  SS US-   2'   XDR
                  -  nU$ US:X  a*  [        R                  " X45      nSUS'   SXAS-
  US-
  4'   U$ g )Nr:   z?Groups must have size at least 3 for autoregressive covariance.r   r!   r1   r   r   )
ValueErrorr)   r*   zerosflatT)r   r   r   msgr   s        r   r   QIFAutoregressive.matN   s    70CS/!1966#;QY((C:&C#$CHHQZQZ 55LCJQY((C:&CCI !CAs1uJr   r"   Nr,   r   r   r   r7   r7   F   s    r   r7   c                   n   ^  \ rS rSrSr  S
U 4S jjrS rS rS r\	SU 4S jj5       r
  SS jrS	rU =r$ )QIFe   a(  
Fit a regression model using quadratic inference functions (QIF).

QIF is an alternative to GEE that can be more efficient, and that
offers different approaches for model selection and inference.

Parameters
----------
endog : array_like
    The dependent variables of the regression.
exog : array_like
    The independent variables of the regression.
groups : array_like
    Labels indicating which group each observation belongs to.
    Observations in different groups should be independent.
family : genmod family
    An instance of a GLM family.
cov_struct : QIFCovariance instance
    An instance of a QIFCovariance.

References
----------
A. Qu, B. Lindsay, B. Li (2000).  Improving Generalized Estimating
Equations using Quadratic Inference Functions, Biometrika 87:4.
www.jstor.org/stable/2673612
c                   > Uc  [         R                  " 5       nO4[        UR                  [         R                  5      (       d  [        S5      eX@l        [        [        5      U l	        Uc  [        5       nO [        U[        5      (       d  [        S5      eXPl        [        R                  " U5      n[         TU ]D  " X4X6S.UD6  [        [%        U5      5      U l        [)        U R*                  5      U l        [        [        5      n[/        U5       H  u  pX   R1                  U	5        M     U R&                   Vs/ s H  oU   PM	     snU l        U R5                  U5        g s  snf )Nz.QIF: `family` must be a genmod family instancez2QIF: `cov_struct` must be a QIFCovariance instance)groupsmissing)r   Gaussian
issubclass	__class__Familyr>   familyr   list_fit_historyr   
isinstancer
   
cov_structr)   asarraysuperr%   setgroup_nameslenendognobs	enumerateappend	groups_ix_check_args)r   rX   exogrH   rN   rR   rI   kwargsr\   ignarL   s               r   r%   QIF.__init__   s6    >&&(Ff..@@  "3 4 4'- (*Jj-88 HJ J$F#	
 &	
;A	
  F,

O	%	f%DAL" &262B2BC2BBB-2BC  Ds   =E%c                     [        U5      [        U R                  5      :w  a  Sn[        U5      e[        U R                  5      U R                  R                  S   :w  a  Sn[        U5      eg )Nz1QIF: groups and endog should have the same lengthr   zGQIF: the length of endog should be equal to the number of rows of exog.)rW   rX   r>   r^   shape)r   rH   rB   s      r   r]   QIF._check_args   sX    v;#djj/)ECS/!tzz?diiooa00-CS/! 1r   c                    U R                   nU R                  n[        R                  " X15      nU R                  R
                  R                  U5      nU R                  R                  U5      nU R                  R
                  R                  U5      nU R                  R
                  R                  U5      nU R                  R                  R                  U5      n	U R                  R                  n
UR                  S   nX-  n[        R                  " U5      n[        R                  " U5      n[        R                  " X45      n[        R                  " X45      nS/U-  n[        R                  " X45      nU R                  R                  [        R                   L n[#        U R                  R
                  [$        R&                  [$        R(                  45      nU R*                   GH  n[        R,                  " UU   5      nUU   UU   -
  nUU-  nUUSS24   UUS4   -  nSn[/        U
5       GH  nU R                  R1                  [3        U5      U5      n[        R                  " UU5      U-  n[        R                  " UR4                  U5      UUUU-   & [        R                  " UU* USS2S4   -  5      USS2S4   -  n[        R                  " UR4                  U5      UUUU-   2SS24'   U(       a  U(       d  [/        U5       H  n[        R                  " UUSS24   R4                  UU   UUU4   -  U-  5      n U(       d  SU	U   -  USS2U4   -  UU   S-  -  n![        R                  " UR4                  U![        R                  " UU5      -  5      n"[        R                  " UR4                  [        R                  " UU!U-  5      U-  5      n#OSu  n"n#UUUU-   2U4==   U U"-   U#-   -  ss'   M     UU-  nGM     [/        U5       H:  n[        R6                  " XSS2U4   5      n$UU==   U$U$R4                  -   -  ss'   M<     X-  nUU-  nU[        R6                  " X5      -  nGM     [3        U R*                  5      n%UU%-  nUU%-  nUU%S-  -  n[        R                  " U[        R8                  R;                  UU5      5      n&[        R                  " U5      n'[/        U5       H  nUU==   [3        U R*                  5      S-  -  ss'   [        R8                  R;                  UUU   5      R4                  n$[        R8                  R;                  UU$5      n$[        R                  " U[        R                  " U$U5      5      U'U'   M     S[        R                  " UR4                  [        R8                  R;                  UU5      5      -  U'-
  n(U&U(UUU4$ )a`  
Calculate the gradient of the QIF objective function.

Parameters
----------
params : array_like
    The model parameters at which the gradient is evaluated.

Returns
-------
grad : array_like
    The gradient vector of the QIF objective function.
gn_deriv : array_like
    The gradients of each estimating equation with
    respect to the parameter.
r!   r   Ng      g      ?r=   r1   )rX   r^   r)   dotrN   linkinversevarianceinverse_derivinverse_deriv2derivrR   r#   re   r?   r   constantrQ   r   Identityidentityr\   sqrtranger   rW   rA   outerlinalgsolve))r   paramsrX   r^   lprmeanvaidlidl2vdmpdgngigi_derivgn_derivcn_derivcmatfastvarfastlinkixsdresidsresidrn   jjjccrs1crs2km1vxm2m3ungrpqifgcggrads)                                            r   	objectiveQIF.objective   s   $ 

yyffT"{{'',[[!!$' kk,,S1{{..s3[[!!''-OO%%JJqMEXXa[XXa[88QF#88QF#37xx++&&(*;*;;KK^^U^^,
 ..BBB"IR(ERZFQK#b$h-/EB1X
 OO''B3vva(2- ffUWWd32bdvva%"QW+!56AtGD')vveggt'<BqD!$ W"1XVVDQKMM$(HtBE{$:T$AC&!%2q!t!<r"vs{!JB!#bffQ6G1G!HB!#2:1F1K!LB%)FB BqD!,R"<, & a+ . 1XHHR!Q$0q133w&  HB HBHHR$$DK !N 4>>"
d
DaffRr23hhqkqAQK3t~~.11K		hqk244A		a(AVVBq".CF	  266(**biioodB&?@@3FD$H,,r   c                    [        U R                  [        R                  [        R                  45      (       a  g[        U S5      (       a  U R                  nOU R                  S   n[        R                  " U R                  U5      nU R                  R                  R                  U5      nU R                  U-
  n[        R                  " US-  5      U R                  U-
  -  nU$ )z
Estimate the dispersion/scale.

The scale parameter for binomial and Poisson families is
fixed at 1, otherwise it is estimated from the data.
g      ?
ddof_scaler!   r1   )rQ   rN   r   BinomialPoissonhasattrr   r^   r)   rh   ri   rj   rX   sumrY   )r   rw   r   rx   ry   r   scales          r   estimate_scaleQIF.estimate_scale  s     dkkH$5$5x7G7G#HII4&&J1JffTYY'{{'',

T!uax DII
$:;r   c                 h   > [        U[        5      (       a  X2   n[        TU ]  " U/UQ7X4US.UD6nU$ )a4  
Create a QIF model instance from a formula and dataframe.

Parameters
----------
formula : str or generic Formula object
    The formula specifying the model
groups : array_like or string
    Array of grouping labels.  If a string, this is the name
    of a variable in `data` that contains the grouping labels.
data : array_like
    The data for the model.
subset : array_like
    An array_like object of booleans, integers, or index
    values that indicate the subset of the data to used when
    fitting the model.

Returns
-------
model : QIF model instance
)datasubsetrH   )rQ   strrT   from_formula)	clsformularH   r   r   argsr_   modelrL   s	           r   r   QIF.from_formula2  sM    2 fc""\F$3#'3!% 3+13 r   c           	         Uc  U R                   R                  S   U l        OXPl        UcF  [        U R                  U R                   U R
                  S9nUR                  5       nUR                  nOUn[        U5       GH@  n	U R                  U5      u  ppn[        R                  " [        R                  " X-  5      5      nU R                  S   R                  U
5        U R                  S   R                  U5        X:  a    OS[        R                  " UR                   [        R"                  R%                  X5      5      -  n[        R"                  R%                  X5      n[        R                  " [        R                  " UU-  5      5      nU R                  S   R                  U5        UU:  a    O
UU-  nGMC     [        R                  " WR                   [        R"                  R%                  WU5      5      n[        R"                  R'                  U5      nU R)                  U5      n[+        XUU-  U5      nU R                  Ul        [/        [0        5      U l        [3        U5      $ )a  
Fit a GLM to correlated data using QIF.

Parameters
----------
maxiter : int
    Maximum number of iterations.
start_params : array_like, optional
    Starting values
tol : float
    Convergence threshold for difference of successive
    estimates.
gtol : float
    Convergence threshold for gradient.
ddof_scale : int, optional
    Degrees of freedom for the scale parameter

Returns
-------
QIFResults object
r!   )rN   r   gradnormr1   stepnorm)r^   re   r   r   rX   rN   fitrw   rs   r   r)   rr   r   rP   r[   rh   rA   ru   rv   invr   
QIFResultsfit_historyr   rO   QIFResultsWrapper)r   maxiterstart_paramstolgtolr   r   resultrw   _r   r   r   r   gnormcjacstepsnormvcovr   rslts                        r   r   QIF.fitT  s   0 "iiooa0DO(O

DIIdkkBEYY[F]]F!FwA+/>>&+A(CtGGBFF4;/0Ee$++C0j)007|rvvhjj"))//$*IJJD99??4.DGGBFF4$;/0Ej)007s{dNF%  ( vvhjj"))//$"AByy}}T"##F+$ue<,,'- &&r   )rP   rR   r   rN   rV   r\   rY   )NNnone)N)d   Ngư>g-C6?N)r   r   r   r   r   r%   r]   r   r   classmethodr   r   r   __classcell__rL   s   @r   rE   rE   e   sR    6 48*0%!N	"f-P.  B BF@' @'r   rE   c                   l   ^  \ rS rSrSr S	U 4S jjr\S 5       r\S 5       r\S 5       r	S
S jr
SrU =r$ )r   i  z Results class for QIF Regressionc                 p   > [         TU ]  XUUS9  U R                  R                  U5      u  U l              ng )N)normalized_cov_paramsr   )rT   r%   r   r   r   )	r   r   rw   
cov_paramsr   use_tkwdsr   rL   s	           r   r%   QIFResults.__init__  sB     	 	 	  $zz33F;!Q1r   c                     [        U R                  R                  [        5      (       a  Sn[	        U5      eU R                  R
                  R                  S   nU R                  SU-  -   $ )z1
An AIC-like statistic for models fit using QIF.
z1AIC not available with QIFIndependence covariancer!   r1   )rQ   r   rR   r   r>   r^   re   r   r   rB   dfs      r   aicQIFResults.aic  sT    
 djj++_==ECS/!ZZ__""1%xx!B$r   c                 (   [        U R                  R                  [        5      (       a  Sn[	        U5      eU R                  R
                  R                  S   nU R                  [        R                  " U R                  R                  5      U-  -   $ )z0
A BIC-like statistic for models fit using QIF.
z1BIC not available with QIFIndependence covariancer!   )rQ   r   rR   r   r>   r^   re   r   r)   logrY   r   s      r   bicQIFResults.bic  sg    
 djj++_==ECS/!ZZ__""1%xx"&&1"444r   c                     U R                   R                  R                  R                  [        R
                  " U R                   R                  U R                  5      5      $ )z+
Returns the fitted values from the model.
)r   rN   ri   rj   r)   rh   r^   rw   r$   s    r   fittedvaluesQIFResults.fittedvalues  sA    
 zz  %%--tzz46 	6r   c           	         SSS/4SU R                   R                  R                  R                  /4SU R                   R                  R                  R                  /4SS/nU R                   R
                   Vs/ s H  n[        U5      PM     nnS[        U5      /4S	[        U5      /4S
[        U5      /4S[        U5      /4SS[        R                  " U5      -  /4SSU R                  -  /4/nUc&  U R                   R                  R                  S-   S-   nUc  U R                   R                  nUc  U R                   R                  nSSKJn	  U	" 5       n
U
R#                  XUXUS9  U
R%                  XUUSS9  U
$ s  snf )a  
Summarize the QIF regression results

Parameters
----------
yname : str, optional
    Default is `y`
xname : list[str], optional
    Names for the exogenous variables, default is `var_#` for ## in
    the number of regressors. Must match the number of parameters in
    the model
title : str, optional
    Title for the top table. If not None, then this replaces
    the default title
alpha : float
    significance level for the confidence intervals

Returns
-------
smry : Summary instance
    this holds the summary tables and text, which can be
    printed or converted to various output formats.

See Also
--------
statsmodels.iolib.summary.Summary : class to hold summary results
)zDep. Variable:NzMethod:rE   zFamily:zCovariance structure:)zDate:N)zTime:NzNo. Observations:zNo. clusters:zMin. cluster size:zMax. cluster size:zMean cluster size:z%.1fzScale:z%.3f zRegression Resultsr   )Summary)gleftgrightynamexnametitleF)r   r   alphar   )r   rN   rL   r   rR   r\   rW   r   minmaxr)   ry   r   
exog_namesendog_namesstatsmodels.iolib.summaryr   add_table_2colsadd_table_params)r   r   r   r   r   top_leftyNY	top_rightr   smrys              r   summaryQIFResults.summary  s   : -(!2!2!<!<!E!E FG,jj++55>>?A## #jj2232c!f23)CG95%By1*SWI6*SWI6*Vbggbk-A,BC$**!4 56	 =JJ((11C7$%E
 =JJ))E=JJ**E 	6yT)#(#( 	 	* 	du$) 	 	8 ? 4s   :F)r   )F)NNNg?)r   r   r   r   r   r%   r   r   r   r   r   r   r   r   s   @r   r   r     sV    *<   5 5 6 6E Er   r   c                       \ rS rSrSrg)r   i  r   N)r   r   r   r   r   r   r   r   r   r     s    r   r   )"numpyr)   collectionsr   statsmodels.base.modelbaser   statsmodels.genmodr   +statsmodels.genmod.generalized_linear_modelr   statsmodels.genmod.familiesr   r   #statsmodels.regression.linear_model
regressionlinear_modellmstatsmodels.base.wrapperwrapperwrapstatsmodels.tools.decoratorsr   r
   r   r.   r7   ModelrE   LikelihoodModelResultsr   RegressionResultsWrapperr   populate_wrapperr   r   r   <module>r	     s     # % % ' ; - 0 0 0 ' ' 7" "*m &m " >o'$** o'd	n,, nb	33 	   ' 4r   