
    M/Ph                        d Z ddlmZ ddlmZ ddlZddlZddlZ	ddl
mZ ddlmZ ddlmZmZmZ ddlmZ  G d	 d
          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d d e          Z dS )!z
Covariance models and estimators for GEE.

Some details for the covariance calculations can be found in the Stata
docs:

http://www.stata.com/manuals13/xtxtgee.pdf
    )AppenderdefaultdictN)linalg)cov_nearest)ConvergenceWarningNotImplementedWarningOutputWarning)	bool_likec                   8    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d	S )	CovStructa  
    Base class for correlation and covariance structures.

    An implementation of this class takes the residuals from a
    regression model that has been fit to grouped data, and uses
    them to estimate the within-group dependence structure of the
    random errors in the model.

    The current state of the covariance structure is represented
    through the value of the `dep_params` attribute.

    The default state of a newly-created instance should always be
    the identity correlation matrix.
    clippedc                 0    d | _         g | _        || _        d S N)
dep_params
cov_adjustcov_nearest_method)selfr   s     ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/genmod/cov_struct.py__init__zCovStruct.__init__+   s$       #5    c                     || _         dS )z
        Called by GEE, used by implementations that need additional
        setup prior to running `fit`.

        Parameters
        ----------
        model : GEE class
            A reference to the parent GEE class instance.
        N)model)r   r   s     r   
initializezCovStruct.initialize8   s     


r   c                     t           )z
        Update the association parameter values based on the current
        regression coefficients.

        Parameters
        ----------
        params : array_like
            Working values for the regression parameters.
        NotImplementedErrorr   paramss     r   updatezCovStruct.updateD   s
     "!r   c                     t           )a  
        Returns the working covariance or correlation matrix for a
        given cluster of data.

        Parameters
        ----------
        endog_expval : array_like
           The expected values of endog for the cluster for which the
           covariance or correlation matrix will be returned
        index : int
           The index of the cluster for which the covariance or
           correlation matrix will be returned

        Returns
        -------
        M : matrix
            The covariance or correlation matrix of endog
        is_cor : bool
            True if M is a correlation matrix, False if M is a
            covariance matrix
        r   )r   endog_expvalindexs      r   covariance_matrixzCovStruct.covariance_matrixP   s
    , "!r   c                 v   |                      ||          \  }}|r|t          j        ||          z  }d}d}d}	t          d          D ]p}
	 t	          j        |          d} nV# t          j        j        $ r? t          || j	        |          }|dz  }|	dz  }	d	}|d
z  }t          j        |           Y mw xY w| j                            |	           |sTt          j        dt                     t          j        t          j        |                    }t	          j        |          fd|D             }|S )a~  
        Solves matrix equations of the form `covmat * soln = rhs` and
        returns the values of `soln`, where `covmat` is the covariance
        matrix represented by this class.

        Parameters
        ----------
        expval : array_like
           The expected value of endog for each observed value in the
           group.
        index : int
           The group index.
        stdev : array_like
            The standard deviation of endog for each observation in
            the group.
        rhs : list/tuple of array_like
            A set of right-hand sides; each defines a matrix equation
            to be solved.

        Returns
        -------
        soln : list/tuple of array_like
            The solutions to the matrix equations.

        Notes
        -----
        Returns None if the solver fails.

        Some dependence structures do not use `expval` and/or `index`
        to determine the correlation matrix.  Some families
        (e.g. binomial) do not use the `stdev` parameter when forming
        the covariance matrix.

        If the covariance matrix is singular or not SPD, it is
        projected to the nearest such matrix.  These projection events
        are recorded in the fit_history attribute of the GEE model.

        Systems of linear equations with the covariance matrix as the
        left hand side (LHS) are solved for different right hand sides
        (RHS); the LHS is only factorized once to save time.

        This is a default implementation, it can be reimplemented in
        subclasses to optimize the linear algebra according to the
        structure of the covariance matrix.
        g{Gz?Fr      T)method	threshold      z+At least one covariance matrix was not PSD zand required projection.zHUnable to condition covariance matrix to an SPD matrix using cov_nearestc                 :    g | ]}t          j        |          S  )spl	cho_solve).0xvcos     r   
<listcomp>z5CovStruct.covariance_matrix_solve.<locals>.<listcomp>   s%    333!c1%%333r   )r$   npouterranger-   
cho_factorr   LinAlgErrorr   r   warningswarnr   appendr   diag)r   expvalr#   stdevrhsvmatis_corr(   successr   itrmsgsolnr1   s                @r   covariance_matrix_solvez!CovStruct.covariance_matrix_solveh   s~   ^ --fe<<f 	+BHUE***D 	
99 	# 	#C#nT**9( # # #"40G-68 8 8Q	a
C11c"""""# 	z***  	'M+,>@ @ @ 7274==))D.&&C3333s333s   A%%AB87B8c                     t           )ze
        Returns a text summary of the current estimate of the
        dependence structure.
        r   r   s    r   summaryzCovStruct.summary   s
    
 "!r   Nr   )
__name__
__module____qualname____doc__r   r   r    r$   rE   rH   r,   r   r   r   r      s         5 5 5 5
 
 

" 
" 
"" " "0Q Q Qf" " " " "r   r   c                       e Zd ZdZ eej        j                  d             Z eej        j                  d             Z eej        j                  d             Zd Z	dS )Independencez7
    An independence working dependence structure.
    c                     d S r   r,   r   s     r   r    zIndependence.update   s	     	r   c                 d    t          |          }t          j        |t          j                  dfS NdtypeT)lenr3   eyefloat64)r   r<   r#   dims       r   r$   zIndependence.covariance_matrix   s)    &kkvc,,,d22r   c                     |dz  }g }|D ]H}|j         dk    r|                    ||z             &|                    ||d d d f         z             I|S )Nr)   r*   )ndimr:   )r   r<   r#   r=   r>   vrsltr0   s           r   rE   z$Independence.covariance_matrix_solve   sm    QJ 	, 	,Av{{AE""""A!!!T'
N++++r   c                     dS )Nz?Observations within a cluster are modeled as being independent.r,   rG   s    r   rH   zIndependence.summary   s    ( (r   N)
rJ   rK   rL   rM   r   r   r    r$   rE   rH   r,   r   r   rO   rO      s          Xi&''  (' Xi)1223 3 323 Xi/788  98) ) ) ) )r   rO   c                        e Zd ZdZd fd	Zd Z eej        j                  d             Z eej	        j                  d             Z	d Z
 xZS )	Unstructureda4  
    An unstructured dependence structure.

    To use the unstructured dependence structure, a `time`
    argument must be provided when creating the GEE.  The
    time argument must be of integer dtype, and indicates
    which position in a complete data vector is occupied
    by each observed value.
    r   c                 J    t                                          |           d S r   )superr   )r   r   	__class__s     r   r   zUnstructured.__init__   s"    +,,,,,r   c                    || _         dd l}t          | j         j        j        j        |j                  sd}t          |          | j         j        d d df                                         dz   }t          j
        |          | _        d S )Nr   z1time must be provided and must have integer dtyper*   )r   numbers
issubclasstimerT   typeIntegral
ValueErrormaxr3   rV   r   )r   r   rd   rC   qs        r   r   zUnstructured.initialize   s|    
$*//4g6FGG 	"ECS//!JOAAAqD!%%''!+&))r   c                     t          | j        d          r@| j        j        }||         d d df         }| j        t	          j        ||                   dfS | j        dfS )Nrf   r   T)hasattrr   time_lir   r3   ix_)r   r"   r#   rn   ixs        r   r$   zUnstructured.covariance_matrix   sa     4:v&& 	8j(G1%B?26"b>>2477$$r   c                 h   | j         j        }| j         j        }| j         j        j        }| j         j        }| j         j        d u}| j         j        }| j         j        }| j         j        	                                dz   }	t          j        |	|	f          }
d}t          j        |	|	f          }d}t          | j         j                  D ]}||         \  }}t          j         ||                    }||         |z
  |z  }||         d d df         }t          j        ||          }t          j        t          j        |                    }|r||         nd}|
t          j        ||          xx         |z  cc<   ||t'          |          z  z  }|t          j        ||          xx         ||z  z  cc<   |||z  z  }| j         j        }||||z
  z  t+          |          z  z  }||
|z
  z  }t          j        t          j        |                    }|t          j        ||          z  }|| _        d S )Nr*           r         ?)r   endog_linobsfamilyvariancecached_meansweightsrn   rf   rj   r3   zerosr5   	num_groupsqrtr4   sumr;   ro   rU   
ddof_scalefloatr   )r   r   endogru   varfuncrx   has_weights
weights_lirn   rk   csumwsumcovscaleir<   _r=   residrp   mssrwddofsds                            r   r    zUnstructured.update  s"    
#z*#,z.j(4Z'
*$JO!!A%xAh1vtz+,, 	 	A %QIFAGGGFOO,,E1X&%/EAAAqD!B&&A&$$C!,4
1"AB   A%   ABKDr21q5(QWEEz$%d33tWRWS\\""rxBr   c                 L    t          d           t          | j                   d S )NzEstimated covariance structure:)printr   rG   s    r   rH   zUnstructured.summary1  s'    /000dor   rI   )rJ   rK   rL   rM   r   r   r   r   r$   r    rH   __classcell__rb   s   @r   r_   r_      s         - - - - - -$ $ $ Xi)122% % 32% Xi&''' ' (''R      r   r_   c                        e Zd ZdZ fdZ eej        j                  d             Z eej        j                  d             Z eej	        j                  d             Z	d Z
 xZS )Exchangeablez7
    An exchangeable working dependence structure.
    c                 V    t                                                       d| _        d S Nrr   )ra   r   r   )r   rb   s    r   r   zExchangeable.__init__;  s&     r   c                    | j         j        }| j         j        }| j         j        j        }| j         j        }| j         j        d u}| j         j        }d\  }}	d\  }
}}t          | j         j                  D ]}||         \  }}t          j
         ||                    }||         |z
  |z  }|r||         nd}t          j        ||z            }|	||z  z  }	|
|t          ||                   z  z  }
|||                                dz  |z
  z  dz  z  }t          |          }d|z  |dz
  z  }|||z  z  }||z  }| j         j        }|	|
||z
  z  t          |          z  z  }	||	z  }||||z
  z  t          |          z  z  | _        d S )Nr   r   rr   rr   rr   rs   r)         ?r*   )r   rt   ru   rv   rw   rx   ry   r5   r{   r3   r|   r}   rU   r~   r   r   )r   r   r   ru   r   rx   r   r   residsq_sumr   fsum1fsum2n_pairsr   r<   r   r=   r   fr   ngrpnprr   s                          r   r    zExchangeable.updateB  s    
#z*#,z.j(4Z'
!U *ugtz+,, 	 	A$QIFAGGGFOO,,E1X&%/E!,4
1"A&''CQWEQU1X&&E1		q 03 67!;;Ku::D*q)CQWEsNGGz$%4$;'%++56u%gn%g68r   c                     t          |          }| j        t          j        ||ft          j                  z  }t          j        |d           |dfS )NrS   r*   T)rU   r   r3   onesrW   fill_diagonal)r   r<   r#   rX   dps        r   r$   zExchangeable.covariance_matrixh  sJ    &kk_rwSzDDDD
Q4xr   c                    t          |          }| j        d| j        z
  z  }|d| j        |dz
  z  z   z  }g }|D ]}|j        dk    r-||z  }	|	d| j        z
  z  }
|
|t          |	          z  z  }
|
|z  }
nF||d d d f         z  }	|	d| j        z
  z  }
|
||	                    d          z  z  }
|
|d d d f         z  }
|                    |
           |S )Nrs   r*   r   )rU   r   rZ   r}   r:   )r   r<   r#   r=   r>   kcr\   r0   x1ys              r   rE   z$Exchangeable.covariance_matrix_solveo  s     KKOrDO34	R$/QU+++ 	 	Av{{Y"t./QR[ U
qqq$w'"t./Q]"U111d7^#KKNNNNr   c                     dd| j         z  z   S )Nz0The correlation between two observations in the zsame cluster is %.3fr   rG   s    r   rH   zExchangeable.summary  s    B&89 	:r   )rJ   rK   rL   rM   r   r   r   r    r$   rE   rH   r   r   s   @r   r   r   6  s              Xi&''#8 #8 ('#8J Xi)122  32 Xi/788  98,: : : : : : :r   r   c                        e Zd ZdZ fdZ eej        j                  d             Z eej        j                  d             Zd Z	 xZ
S )Nesteda*  
    A nested working dependence structure.

    A nested working dependence structure captures unique variance
    associated with each level in a hierarchy of partitions of the
    cases.  For each level of the hierarchy, there is a set of iid
    random effects with mean zero, and with variance that is specific
    to the level.  These variance parameters are estimated from the
    data using the method of moments.

    The top level of the hierarchy is always defined by the required
    `groups` argument to GEE.

    The `dep_data` argument used to create the GEE defines the
    remaining levels of the hierarchy.  it should be either an array,
    or if using the formula interface, a string that contains a
    formula.  If an array, it should contain a `n_obs x k` matrix of
    labels, corresponding to the k levels of partitioning that are
    nested under the top-level `groups` of the GEE instance.  These
    subgroups should be nested from left to right, so that two
    observations with the same label for column j of `dep_data` should
    also have the same label for all columns j' < j (this only applies
    to observations in the same top-level cluster given by the
    `groups` argument to GEE).

    If `dep_data` is a formula, it should usually be of the form `0 +
    a + b + ...`, where `a`, `b`, etc. contain labels defining group
    membership.  The `0 + ` should be included to prevent creation of
    an intercept.  The variable values are interpreted as labels for
    group membership, but the variables should not be explicitly coded
    as categorical, i.e. use `0 + a` not `0 + C(a)`.

    Notes
    -----
    The calculations for the nested structure involve all pairs of
    observations within the top level `group` passed to GEE.  Large
    group sizes will result in slow iterations.
    c                 H   t                                          |           | j        j        t	          j        dt                     t          j        | j        j	                  }|j
        dk    r|dddf         }|| _        | j        j        }g g }}| j        j        d         }t          | j        j                  D ]m}t!          ||                   }| j        j        |         }	| j        j        |	         }
t          j        |d          \  }}| j        |
|         ddf         | j        |
|         ddf         k                        d          }t          j        ||ft          j                  }|dz   |||f<   |dz   |||f<   |                    |           t          j        t!          |          |dz   ft          j                  }d|dddf<   t          j        |          D ]&}t          j        ||k              }d||d|dz   f<   '|                    |           ot          j        |d          | _        || _        t          j                            | j        d          }|d         | _         |d         | _!        |d         j"        | _#        dS )	a  
        Called on the first call to update

        `ilabels` is a list of n_i x n_i matrices containing integer
        labels that correspond to specific correlation parameters.
        Two elements of ilabels[i] with the same label share identical
        variance components.

        `designx` is a matrix, with each row containing dummy
        variables indicating which variance components are associated
        with the corresponding element of QY.
        NzSweights not implemented for nested cov_struct, using unweighted covariance estimater*   rS   r   axisr)   )$ra   r   r   ry   r8   r9   r	   r3   asarraydep_datarZ   	id_matrixrt   shaper5   r{   rU   group_labelsgroup_indicestril_indicesr}   rz   int32r:   rW   uniqueflatnonzeroconcatenatedesignxilabelsr   svd	designx_u	designx_sT	designx_v)r   r   r   r   r   r   n_nestr   r   glabrixix1ix2ncmilabeldsxr   iir   rb   s                      r   r   zNested.initialize  s    	5!!!:)M A/1 1 1
 Jtz233	>Q!!!!T'*I"
#r %a(tz+,, 	  	 AuQx==D:*1-D**40C tR00HC>#c(AAA+.>#c(AAA+./03A 
 XtTl"(;;;F!$qFC:!$qFC:NN6""" (CHHfqj1DDDCC1IYs^^ % %^C1H--#$B!a%K  NN3~gA666immDL!,,QQQr   c                    | j         j        }| j         j        }t          |          }| j        |                     | j                    | j         j        }| j         j        j        }g }d}t          | j         j
                  D ]}	||	         \  }
}t          j         ||
                    }||	         |
z
  |z  }t          j        t          |          d          \  }}|                    ||         ||         z             |t          j        |dz            z  }t          j        |          }|||z
  z  }t          j        | j        t          j        | j        j        |          | j        z            }t          j        |dt          j                  | _        || _        | j                                        | _        d S )Nrr   r   r)   r   )r   rt   ru   rU   r   _compute_designrx   rv   rw   r5   r{   r3   r|   r   r:   r}   r   dotr   r   r   r   clipinfvcomp_coeffr   copyr   )r   r   r   ru   rX   rx   r   dvmatr   r   r<   r   r=   r   r   r   r   s                    r   r    zNested.update  s    
#z&kk<  ,,,z.*#,tz+,, 
	( 
	(A$QIFAGGGFOO,,E1X&%/Es5zz266HCLLseCj0111RVEQJ'''EEu%%$* fT^RVDN4D49.; .;=A^.L M M 7;26::
*//11r   c                    t          |          }| j        "t          j        |t          j                  dfS | j        |         }t          j        | j        t          j        | j	                  f         }||         }|| j        z  }|dfS rR   )
rU   r   r3   rV   rW   r   r_r   cumsumr   )r   r<   r#   rX   r   r   r?   s          r   r$   zNested.covariance_matrix   s     &kk ?"6#RZ000$66e$E$*bi(8999:y
Tzr   c                 *   dg}t          | j        d          r |                    | j        j                   nA|                    d t	          t          | j                  dz
            D                        t          | j        d          r| j        j        |d<   |                    d           | j        	                                }|                    | j
        t          j        |          z
             t          j        d|i|	          }|S )
zd
        Returns a summary string describing the state of the
        dependence structure.
        Groups_dep_data_namesc                     g | ]
}d |dz   z  S )zComponent %d:r*   r,   )r/   r   s     r   r2   z"Nested.summary.<locals>.<listcomp>:  s!    bbbAoQ7bbbr   r*   _groups_namer   ResidualVariance)r#   )rm   r   extendr   r5   rU   r   r   r:   tolistr   r3   r}   pd	DataFrame)r   	dep_namesvcsmrys       r   rH   zNested.summary0  s    J	4:011 	dTZ78888bbs4K[G\G\_`G`AaAabbbccc4:~.. 	3:2IaL$$$$$&&
		$*rvbzz)***|Z,I>>>r   )rJ   rK   rL   rM   r   r   r   r    r$   rH   r   r   s   @r   r   r     s        % %NA" A" A" A" A"F Xi&'''2 '2 (''2R Xi)122  32      r   r   c                        e Zd ZdZd fd	Z fdZ eej        j                  d             Zd Z	d Z
 eej        j                  d	             Zd
 Z eej        j                   fd            Zd Z xZS )
Stationarya  
    A stationary covariance structure.

    The correlation between two observations is an arbitrary function
    of the distance between them.  Distances up to a given maximum
    value are included in the covariance model.

    Parameters
    ----------
    max_lag : float
        The largest distance that is included in the covariance model.
    grid : bool
        If True, the index positions in the data (after dropping missing
        values) are used to define distances, and the `time` variable is
        ignored.
    r*   Nc                    t                                                       t          |dd          }|t          j        dt
                     || _        t          |          | _        t          j
        |dz             | _        d S )NgridToptional1grid=True will become default in a future versionr*   )ra   r   r   r8   r9   FutureWarningmax_lagboolr   r3   rz   r   )r   r   r   rb   s      r   r   zStationary.__init__Y  s{    v555<MC  
 JJ	(7Q;//r   c                     t                                          |           | j        sT| j        j        d d df                             t          j                  }| j                            |          | _        d S d S )Nr   )	ra   r   r   r   rf   astyper3   r   cluster_list)r   r   rf   rb   s      r   r   zStationary.initializeg  sr    5!!! y 	6:?111a4(//99D
//55DIII	6 	6r   c                 l    | j         r|                     |           d S |                     |           d S r   )r   update_gridupdate_nogridr   s     r   r    zStationary.updatep  sB     9 	'V$$$$$v&&&&&r   c           
         | j         j        }| j         j        }| j         j        j        }t          j        | j        dz             }t          | j         j	                  D ]}||         \  }}t          j
         ||                    }	||         |z
  |	z  }
|dxx         t          j        |
|
z            t          |
          z  z  cc<   t          d| j        dz             D ]J}|
|d          }||xx         t          j        |
d|          |z            t          |          z  z  cc<   K||d         z  }|| _        d S )Nr*   r   )r   rt   rx   rv   rw   r3   rz   r   r5   r{   r|   r}   rU   r   )r   r   r   rx   r   r   r   r<   r   r=   r   jr[   s                r   r   zStationary.update_gridx  sH   
#z.*#,XdlQ.//
tz+,, 		B 		BA$QIFAGGGFOO,,E1X&%/EqMMMRVEEM22SZZ??MMM1dlQ.// B B!""I1ada!8!83q66!AAB 	jm#
$r   c                    | j         j        }| j         j        }| j         j        j        }t          j        | j        dz             }t          j        | j        dz             }d}d}t          | j         j	                  D ]}	||	         \  }
}t          j
         ||
                    }||	         |
z
  |z  }t          j        t          |
          d          \  }}t          j        | j        |	         |         | j        |	         |         z
            }t          j        || j        k              }||         }||         }||         }t          j        |||         ||         z  | j        dz             }t          j        || j        dz             }|t          j        |dz            z  }|t          |          z  }t          j        |dk              }t          |          dk    r/||xx         dz  cc<   ||xx         ||         ||         z  z  cc<   t          j        |dk              }||xx         ||         z  cc<   ||z  }||z  }|| _        d S )Nr*   r   r   )ry   	minlength)r   r)   )r   rt   rx   rv   rw   r3   rz   r   r5   r{   r|   r   rU   absrf   r   bincountr}   r   )r   r   r   rx   r   r   dn	resid_ssqresid_ssq_nr   r<   r   r=   r   j1j2dxr   vsvdi0	resid_msqs                         r   r   zStationary.update_nogrid  s<   
#z.*#,XdlQ.//
XdlQ&''	tz+,, 	2 	2A$QIFAGGGFOO,,E1X&%/E_S[["55FB	!R(49Q<+;;<<Bdl 233BBBBBBBRrU2Y)>'+|a'79 9 9BR4<!+;<<<Bq)))I3u::%KQ''B2ww{{2!2"R&2b6/1^BF##2"R& +	i
$r   c                    | j         r|                     ||          S t          j        t	          |          d          \  }}t          j        | j        |         |         | j        |         |         z
            }t          j        || j        k              }||         }||         }||         }t          j	        t	          |                    }| j
        |         |||f<   | j
        |         |||f<   |dfS )Nr   T)r   covariance_matrix_gridr3   r   rU   r   rf   r   r   rV   r   )r   r"   r#   r  r  r  r   cmats           r   r$   zStationary.covariance_matrix  s     9 	D..|UCCC\!2!2B77BVDIe$R(49U+;B+??@@^B$,.//VVVvc,''((r*RVr*RVTzr   c                     ddl m} t          j        t	          |                    }d|d<   | j        dd          |d| j        dz   <    ||          dfS )Nr   )toeplitzr*   T)scipy.linalgr  r3   rz   rU   r   r   )r   r"   r#   r  rs        r   r	  z!Stationary.covariance_matrix_grid  sh    ))))))HS&&''! $ 3!DL1
x{{D  r   c                    | j         s$t                                          ||||          S ddlm} t          j        t          |                    }| j        dd          |d| j	        <   g }|D ]p}|j
        dk    r(||z  }	|                     |||	          |z             5||d d d f         z  }	|                     |||	          |d d d f         z             q|S )Nr   )stationary_solver*   )r   ra   rE   statsmodels.tools.linalgr  r3   rz   rU   r   r   rZ   r:   )r   r<   r#   r=   r>   r  r  r\   r0   r   rb   s             r   rE   z"Stationary.covariance_matrix_solve  s    y 	+7722uc+ + + 	>=====HS[[!! OABB/!DL. 	E 	EAv{{I,,Q22U:;;;;aaag&,,Q22U111d7^CDDDDr   c                 r    t          j        | j        dz             }t          j        || j        d          S )Nr*   )LagCov)r3   aranger   r   r   r   )r   lags     r   rH   zStationary.summary  s2    iq())|C@@AAAr   )r*   N)rJ   rK   rL   rM   r   r   r   r   r    r   r   r$   r	  rE   rH   r   r   s   @r   r   r   G  s2        "0 0 0 0 0 06 6 6 6 6 Xi&''' ' (''% % %*'% '% '%R Xi)122  32$! ! ! Xi/788    98*B B B B B B Br   r   c                        e Zd ZdZd
 fd	Z eej        j                  d             Zd Zd Z	 eej
        j                  d             Z
 eej        j                  d             Zd	 Z xZS )Autoregressivea  
    A first-order autoregressive working dependence structure.

    The dependence is defined in terms of the `time` component of the
    parent GEE class, which defaults to the index position of each
    value within its cluster, based on the order of values in the
    input data set.  Time represents a potentially multidimensional
    index from which distances between pairs of observations can be
    determined.

    The correlation between two observations in the same cluster is
    dep_params^distance, where `dep_params` contains the (scalar)
    autocorrelation parameter to be estimated, and `distance` is the
    distance between the two observations, calculated from their
    corresponding time values.  `time` is stored as an n_obs x k
    matrix, where `k` represents the number of dimensions in the time
    index.

    The autocorrelation parameter is estimated using weighted
    nonlinear least squares, regressing each value within a cluster on
    each preceding value in the same cluster.

    Parameters
    ----------
    dist_func : function from R^k x R^k to R^+, optional
        A function that computes the distance between the two
        observations based on their `time` values.

    References
    ----------
    B Rosner, A Munoz.  Autoregressive modeling for the analysis of
    longitudinal data with unequally spaced examinations.  Statistics
    in medicine. Vol 7, 59-71, 1988.
    Nc                    t                                                       t          |dd          }|	d | _        n|| _        |t	          j        dt                     t          |          | _        | j        sd | _	        d| _
        d S )Nr   Tr   c                 T    t          j        | |z
                                            S r   )r3   r   r}   )r0   r   s     r   <lambda>z)Autoregressive.__init__.<locals>.<lambda>  s    "&Q--*;*;*=*= r   r   rr   )ra   r   r   	dist_funcr8   r9   r   r   r   r   r   )r   r  r   rb   s      r   r   zAutoregressive.__init__  s    v555==DNN&DN<MC   JJ	y 	 DL r   c                     | j         j        t          j        dt                     | j        r|                     |           d S |                     |           d S )Nz[weights not implemented for autoregressive cov_struct, using unweighted covariance estimate)r   ry   r8   r9   r	   r   _update_grid_update_nogridr   s     r   r    zAutoregressive.update'  sl     :)M M/1 1 1 9 	(f%%%%%'''''r   c                    | j         j        }| j                                         }| j         j        j        }| j         j        }d\  }}t          | j         j                  D ]}||         \  }	}
t          j	        | ||	          z            }||         |	z
  |z  }t          |          }|dk    rM|t          j        |dd         |dd          z            |dz
  z  z  }|t          j        |dz            |z  z  }||z  | _        d S )N)rr   rr   r*   r   r   r)   )r   rx   estimate_scalerv   rw   rt   r5   r{   r3   r|   rU   r}   r   )r   r   rx   r   r   r   lag0lag1r   r<   r   r=   r   ns                 r   r  zAutoregressive._update_grid4  s
   z.
))++*#,
#
dtz+,, 		- 		-A$QIFAGEGGFOO344E1X&%/EE

A1uuuQrT{U122Y6771q5AAuax((1,,+r   c                    | j         j        }| j         j        }| j        | j        ng t	          | j         j                  D ]}t          ||                   }|dk    rt	          |          D ]]}t	          |          D ]K}                    |                     ||         |d d f         ||         |d d f                              L^t          j
                  | _        | j                                         }| j         j        j        }	| j         j        }
d| j        dz  z  z
  }|d| j        dz  z
  z  }d|z                                  z  g t	          | j         j                  D ]}|
|         \  }}t          j        | |	|          z            }||         |z
  |z  }t          |          }t	          |          D ]7}t	          |          D ]%}                    ||         ||         g           &8t          j
                  fd}d |d          }}d |d          }}||k    r%|dz  } ||          }|dk     r	d| _        d S ||k    %d |d          }}||k     r1|d|z
  dz  z   } ||          }|d	k    rt%          d
          ||k     1ddlm}  |||||g          | _        d S )Nr   rs   r)   c                 r    d d df         | z  d d df         z  z
  }t          j        |dz            S )Nr   r*   r)   )r3   r   )adifr   residmatwtss     r   fitfuncz.Autoregressive._update_nogrid.<locals>.fitfunc{  sD    111a4.ALHQQQTN#BBC6#(C(((r   rr   r   g:0yE>g      ?g!?z,Autoregressive: unable to find right bracket)brent)brack)r   rt   rn   r   r5   r{   rU   r:   r  r3   arrayr!  rv   rw   rx   r   r}   r|   ri   scipy.optimizer,  )r   r   r   rf   r   r   r  r  r   r   rx   varr<   r   r=   r   r+  b_lftf_lftb_ctrf_ctrb_rgtf_rgtr,  r   r)  r*  s                           @@@r   r  zAutoregressive._update_nogridI  s|   
#z! <#lGGG4:/00 
G 
G58}}199  ++ G GB#Bii G Gt~~d1gb!!!en6:1gb!!!en(F (F G G G GGG
 hw''G"DL
))++*#,z. 4?q7{33rDOq(((3hswwyytz+,, 		< 		<A$QIFAGEGGFOO344E1X&%/Eu::DDkk < <)) < <BOOU2Yb	$:;;;;<< 8H%%	) 	) 	) 	) 	) 	) 	)
 772;;u GGCLLuemmQJEGENNEt||"# emm WWT]]uemmR%Z1,,EGENNEy   BD D D	 emm 	)(((((%ue/DEEEr   c                    t          |          }| j        dk    r"t          j        |t          j                  dfS t          j        |          }| j        t          j        |d d d f         |d d d f         z
            z  }|dfS )Nr   rS   T)rU   r   r3   rV   rW   r  r   )r   r"   r#   r   idxr
  s         r   r$   z Autoregressive.covariance_matrix  s    <  ?a6$bj111477ioo"&QQQWD!!!G)D"E"EETzr   c                 R   t          |          }| j        }g }|dk    rfd|D             S |dk    rt          j        d| g| dgg          }|d|dz  z
  z  }|D ]l}	|	j        dk    r|	z  }
n|	d d d f         z  }
t          j        ||
          }
|	j        dk    r|
z  }
n|
d d d f         z  }
|                    |
           m|S d|dz  z   d|dz  z
  z  }dd|dz  z
  z  }| d|dz  z
  z  }g }|D ]-}	d}|	j        dk    r|	d d d f         }	d}|	d d d f         z  }
t          j        d|
j        d         f          }t          j	        |
dd d d f         |fd          }t          j	        ||
dd	d d f         fd          }||
z  ||z  z   ||z  z   }||
dd d f         z  ||
dd d f         z  z   |dd d f<   ||
d	d d f         z  ||
d
d d f         z  z   |d	d d f<   |d d d f         z  }|rt          j
        |          }|                    |           /|S )Nr*   c                      g | ]
}|d z  z  S )r)   r,   )r/   r0   r=   s     r   r2   z:Autoregressive.covariance_matrix_solve.<locals>.<listcomp>  s!    000qA
N000r   r)   rs   FTr   r   r   )rU   r   r3   r.  rZ   r   r:   rz   r   r   squeeze)r   r<   r#   r=   r>   r   r  rD   matr0   r   c0c1c2flattenz0rhs1rhs2r   s      `               r   rE   z&Autoregressive.covariance_matrix_solve  s    KKO 660000C0000 66(QGqb!W-..CBaK C 
  
 6Q;;UBBU111d7^+BVC__6Q;;%KBB%4.(BBK 16kb16k*2Q;R2Q; 	 	AGv{{aaagJU111d7^#B1bhqk*++B>2abb!!!e9b/:::D>2r!B$'{"3!<<<DR"t)#b4i/A2ad8mb2ad8m3AadGBr111uI~RAAAY6Ab!!!eHqqq$wA "JqMMKKNNNNr   c                     d| j         z  S )Nz-Autoregressive(1) dependence parameter: %.3f
r   rG   s    r   rH   zAutoregressive.summary  s    @  	!r   )NN)rJ   rK   rL   rM   r   r   r   r    r  r  r$   rE   rH   r   r   s   @r   r  r    s       ! !F     , Xi&''
( 
( ('
(& & &*LF LF LF\ Xi)122  32 Xi/788: : 98:x! ! ! ! ! ! !r   r  c                   "     e Zd ZdZ fdZ xZS )CategoricalCovStructa  
    Parent class for covariance structure for categorical data models.

    Attributes
    ----------
    nlevel : int
        The number of distinct levels for the outcome variable.
    ibd : list
        A list whose i^th element ibd[i] is an array whose rows
        contain integer pairs (a,b), where endog_li[i][a:b] is the
        subvector of binary indicators derived from the same ordinal
        value.
    c                    t                                          |           t          |j                  | _        | j        dz
  | _        ddlm} t          j	        t          j
                  j        }g }|j        D ]p}t          j        dt          |          dz   | j        t          j
                  } ||t          |          dz
  df||f          }|                    |           q|| _        d S )Nr*   r   )
as_stridedrS   r)   )r   strides)ra   r   rU   endog_valuesnlevel_ncutnumpy.lib.stride_tricksrI  r3   rT   int64itemsizert   r  r:   ibd)r   r   rI  brQ  r[   jjrb   s          r   r   zCategoricalCovStruct.initialize  s    5!!!%,--[1_
666666HRX' 	 	A1c!ffqj$*BHEEEBBs2ww{A&6AGGGBJJrNNNNr   )rJ   rK   rL   rM   r   r   r   s   @r   rG  rG    sB                 r   rG  c                        e Zd ZdZ fdZ fdZd Z eej	        j                  d             Z	d Z
d Z eej        j                  d             Zd	 Z xZS )
GlobalOddsRatioa  
    Estimate the global odds ratio for a GEE with ordinal or nominal
    data.

    References
    ----------
    PJ Heagerty and S Zeger. "Marginal Regression Models for Clustered
    Ordinal Measurements". Journal of the American Statistical
    Association Vol. 91, Issue 435 (1996).

    Thomas Lumley. Generalized Estimating Equations for Ordinal Data:
    A Note on Working Correlation Structures. Biometrics Vol. 52,
    No. 1 (Mar., 1996), pp. 354-361
    http://www.jstor.org/stable/2533173

    Notes
    -----
    The following data structures are calculated in the class:

    'ibd' is a list whose i^th element ibd[i] is a sequence of integer
    pairs (a,b), where endog_li[i][a:b] is the subvector of binary
    indicators derived from the same ordinal value.

    `cpp` is a dictionary where cpp[group] is a map from cut-point
    pairs (c,c') to the indices of all between-subject pairs derived
    from the given cut points.
    c                 d    t                                                       || _        d| _        d S r   )ra   r   
endog_typer   )r   rW  rb   s     r   r   zGlobalOddsRatio.__init__"  s+    $r   c                    t                                          |           | j        j        t	          j        dt                     g }|j        D ]}t          t          |          | j
        z            }t          j        |d          \  }}i }t          | j
                  D ]u}t          |dz             D ]`}	t          j        t          |          dft          j                  }
|| j
        z  |z   |
d d df<   || j
        z  |	z   |
d d df<   |
||	|f<   av|                    |           || _        |                                 | _        | j        j        r| j        | _        d S d S )Nz\weights not implemented for GlobalOddsRatio cov_struct, using unweighted covariance estimater   r*   r)   rS   r   )ra   r   r   ry   r8   r9   r	   rt   intrU   rM  r3   r   r5   rz   rO  r:   cppobserved_crude_oddsratiocrude_or
update_depr   )r   r   rZ  r[   r   i1i2cpp1k1k2rS  rb   s              r   r   zGlobalOddsRatio.initialize'  s   5!!!:)M M/1 1 1
  	 	A CFFTZ'((A_Q++FBDDJ'' ( (Q-- ( (B3r77A,bh???B!DJ3Bqqq!tH!DJ3Bqqq!tH%'D"bNN	( JJt 5577:  	,"mDOOO	, 	,r   c                    t          |          dk    rdS g g }}|D ]}t          j        |d                   t          j        |d                   z   t          j        |d                   z
  t          j        |d                   z
  }|                    |           |                    d|                    t          j                  z                                             d |D             }t          |          fd	|D             }t          d
 t          ||          D                       }t          j        |          S )z
        Returns the pooled odds ratio for a list of 2x2 tables.

        The pooled odds ratio is the inverse variance weighted average
        of the sample odds ratios of the tables.
        r   rs   r*   r*   r   r   r*   r*   r   r*   c                     g | ]}d |z  S )r*   r,   )r/   r[   s     r   r2   z5GlobalOddsRatio.pooled_odds_ratio.<locals>.<listcomp>]  s    """q1u"""r   c                     g | ]}|z  S r,   r,   )r/   r   wtsums     r   r2   z5GlobalOddsRatio.pooled_odds_ratio.<locals>.<listcomp>_  s    &&&Qq5y&&&r   c                     g | ]
\  }}||z  S r,   r,   )r/   r   es      r   r2   z5GlobalOddsRatio.pooled_odds_ratio.<locals>.<listcomp>`  s     GGGtq!QUGGGr   )	rU   r3   logr:   r   rW   r}   zipexp)	r   tableslog_oddsratior0  tablelorr*  log_pooled_orri  s	           @r   pooled_odds_ratioz!GlobalOddsRatio.pooled_odds_ratioI  s>    v;;!2  s 	= 	=E&t%%uT{(;(;;uT{##$&(fU4[&9&9:C  %%%JJELL44499;;<<<< #"c"""C&&&&#&&&GGs3/F/FGGGHHvm$$$r   c                 f    |                      ||          }|t          j        ||          z  }|dfS )NF)get_eyyr3   r4   )r   expected_valuer#   r?   s       r   r$   z!GlobalOddsRatio.covariance_matrixd  s7     ||NE22888U{r   c           
         | j         }| j        j        }i }|d                                         D ]%}t	          j        dt          j                  ||<   &t          t          |                    D ]}||         }t	          j	        ||          }t	          j	        |d|z
            }t	          j	        d|z
  |          }	t	          j	        d|z
  d|z
            }
||         }|                                D ]}||         }||         dxx         ||dddf         |dddf         f         
                                z  cc<   ||         dxx         ||dddf         |dddf         f         
                                z  cc<   ||         d	xx         |	|dddf         |dddf         f         
                                z  cc<   ||         d
xx         |
|dddf         |dddf         f         
                                z  cc<   |                     t          |                                                    S )a  
        To obtain the crude (global) odds ratio, first pool all binary
        indicators corresponding to a given pair of cut points (c,c'),
        then calculate the odds ratio for this 2x2 table.  The crude
        odds ratio is the inverse variance weighted average of these
        odds ratios.  Since the covariate effects are ignored, this OR
        will generally be greater than the stratified OR.
        r   r)   r)   rS   rs   rd  Nr*   rf  re  r   )rZ  r   rt   keysr3   rz   rW   r5   rU   r4   r}   rt  listvalues)r   rZ  r   ro  r   r   yvecendog_11endog_10endog_01endog_00r`  kyrp   s                 r   r[  z(GlobalOddsRatio.observed_crude_oddsratiok  se    h
# a&++-- 	< 	<B&
;;;F2JJ s5zz"" 	G 	GA 8Dxd++Hxb4i00HxT	400HxT	2955Hq6Diikk G G"Xr
4   HR1Xr!!!Q$x-?$@$D$D$F$FF   r
4   HR1Xr!!!Q$x-?$@$D$D$F$FF   r
4   HR1Xr!!!Q$x-?$@$D$D$F$FF   r
4   HR1Xr!!!Q$x-?$@$D$D$F$FF    G %%d6==??&;&;<<<r   c                    | j         }| j        |         }|dk    rt          j        ||          }nz|dddf         |dddf         z   }|dddf         |dddf         z  }t          j        d||dz
  z  z   dz  d|z  d|z
  z  |z  z             }d||dz
  z  z   |z
  }|d|dz
  z  z  }|D ]}	||	d         |	d                  }
| j        dk    rBt          j                            |
|
          ||	d         |	d         |	d         |	d         f<   et          j        |
          ||	d         |	d         |	d         |	d         f<   |S )	z
        Returns a matrix V such that V[i,j] is the joint probability
        that endog[i] = 1 and endog[j] = 1, based on the marginal
        probabilities of endog and the global odds ratio `current_or`.
        rs   Nr)      g       @r*   r   ordinal)r   rQ  r3   r4   r|   rW  minimumr;   )r   r"   r#   
current_orrQ  r?   psumpprodpfacbdlevys              r   rv  zGlobalOddsRatio.get_eyy  s    _
huo 8L,77DD4(<aaa+@@D D)Lqqq,AAE7Bb!99a?z>R*_=EF G GD
R0047DB*q.))D  	B 	BCs1vc!f}-C)++J$$S#.. SVCF]CF3q6M122 68WS\\SVCF]CF3q6M122r   c           
         | j         }| j        j        }t          |d                   dk    rdS i }|d         D ]%}t	          j        dt          j                  ||<   &t          | j        j                  D ]}}||         \  }}| 	                    ||          }	|dddf         |	z
  }
|	 |z   }d|	|
z   |z   z
  }||         }|
                                D ]}||         }||         dxx         |	|dddf         |dddf         f                                         z  cc<   ||         dxx         |
|dddf         |dddf         f                                         z  cc<   ||         d	xx         ||dddf         |dddf         f                                         z  cc<   ||         d
xx         ||dddf         |dddf         f                                         z  cc<   |                     t          |                                                    }| xj        | j        |z  z  c_        t	          j        | j                  s#d| _        t%          j        dt(                     dS dS )z\
        Update the global odds ratio based on the current value of
        params.
        r   Nry  rS   rs   rd  r*   rf  re  r   z%dep_params became inf, resetting to 1)rZ  r   rx   rU   r3   rz   rW   r5   r{   rv  rz  r}   rt  r{  r|  r   r\  isfiniter8   r9   r   )r   r   rZ  rx   ro  r   r   r"   r   emat_11emat_10emat_01emat_00r`  r  rp   
cor_expvals                    r   r    zGlobalOddsRatio.update  s    hz. s1v;;!Fa& 	< 	<B&
;;;F2JJtz+,, 	F 	FA*1oOL!ll<33G"111d7+g5Gh-GGg-78Gq6Diikk F F"Xr
4   GBqqq!tHbAh,>$?$C$C$E$EE   r
4   GBqqq!tHbAh,>$?$C$C$E$EE   r
4   GBqqq!tHbAh,>$?$C$C$E$EE   r
4   GBqqq!tHbAh,>$?$C$C$E$EE    F ++D,A,ABB
4=:55{4?++ 	. DOMA,. . . . .	. 	.r   c                     d| j         z  S )NzGlobal odds ratio: %.3f
r   rG   s    r   rH   zGlobalOddsRatio.summary  s    *T_<<r   )rJ   rK   rL   rM   r   r   rt  r   r   r$   r[  rv  r    rH   r   r   s   @r   rU  rU    s         8    
 ,  ,  ,  ,  ,D% % %6 Xi)122  32$= $= $=L  @ Xi&''). ). (').V= = = = = = =r   rU  c                       e Zd ZdZd Zd ZdS )OrdinalIndependenceal  
    An independence covariance structure for ordinal models.

    The working covariance between indicators derived from different
    observations is zero.  The working covariance between indicators
    derived form a common observation is determined from their current
    mean values.

    There are no parameters to estimate in this covariance structure.
    c                 R   | j         |         }t          |          }t          j        ||f          }|D ]o}||d         |d                  }t          j                            ||          t          j        ||          z
  ||d         |d         |d         |d         f<   p|dfS Nr   r*   F)rQ  rU   r3   rz   r  r4   r   rw  r#   rQ  r$  r?   r  evs           r   r$   z%OrdinalIndependence.covariance_matrix  s    huoxA 	< 	<CAs1v.B
  R((28B+;+;; QAAs1v-.. U{r   c                     d S r   r,   r   s     r   r    zOrdinalIndependence.update      r   NrJ   rK   rL   rM   r$   r    r,   r   r   r  r    <        	 	      r   r  c                       e Zd ZdZd Zd ZdS )NominalIndependenceal  
    An independence covariance structure for nominal models.

    The working covariance between indicators derived from different
    observations is zero.  The working covariance between indicators
    derived form a common observation is determined from their current
    mean values.

    There are no parameters to estimate in this covariance structure.
    c                 :   | j         |         }t          |          }t          j        ||f          }|D ]c}||d         |d                  }t          j        |          t          j        ||          z
  ||d         |d         |d         |d         f<   d|dfS r  )rQ  rU   r3   rz   r;   r4   r  s           r   r$   z%NominalIndependence.covariance_matrix  s    huoxA 	/ 	/CAs1v.Bbhr2... QAAs1v-.. U{r   c                     d S r   r,   r   s     r   r    zNominalIndependence.update  r  r   Nr  r,   r   r   r  r    r  r   r  c                        e Zd ZdZd
 fd	Zd Zd Z fdZ ee	j
        j                  d             Z
 ee	j        j                  d	             Z xZS )Equivalencea	  
    A covariance structure defined in terms of equivalence classes.

    An 'equivalence class' is a set of pairs of observations such that
    the covariance of every pair within the equivalence class has a
    common value.

    Parameters
    ----------
    pairs : dict-like
      A dictionary of dictionaries, where `pairs[group][label]`
      provides the indices of all pairs of observations in the group
      that have the same covariance value.  Specifically,
      `pairs[group][label]` is a tuple `(j1, j2)`, where `j1` and `j2`
      are integer arrays of the same length.  `j1[i], j2[i]` is one
      index pair that belongs to the `label` equivalence class.  Only
      one triangle of each covariance matrix should be included.
      Positions where j1 and j2 have the same value are variance
      parameters.
    labels : array_like
      An array of labels such that every distinct pair of labels
      defines an equivalence class.  Either `labels` or `pairs` must
      be provided.  When the two labels in a pair are equal two
      equivalence classes are defined: one for the diagonal elements
      (corresponding to variances) and one for the off-diagonal
      elements (corresponding to covariances).
    return_cov : bool
      If True, `covariance_matrix` returns an estimate of the
      covariance matrix, otherwise returns an estimate of the
      correlation matrix.

    Notes
    -----
    Using `labels` to define the class is much easier than using
    `pairs`, but is less general.

    Any pair of values not contained in `pairs` will be assigned zero
    covariance.

    The index values in `pairs` are row indices into the `exog`
    matrix.  They are not updated if missing data are present.  When
    using this covariance structure, missing data should be removed
    before constructing the model.

    If using `labels`, after a model is defined using the covariance
    structure it is possible to remove a label pair from the second
    level of the `pairs` dictionary to force the corresponding
    covariance to be zero.

    Examples
    --------
    The following sets up the `pairs` dictionary for a model with two
    groups, equal variance for all observations, and constant
    covariance for all pairs of observations within each group.

    >> pairs = {0: {}, 1: {}}
    >> pairs[0][0] = (np.r_[0, 1, 2], np.r_[0, 1, 2])
    >> pairs[0][1] = np.tril_indices(3, -1)
    >> pairs[1][0] = (np.r_[3, 4, 5], np.r_[3, 4, 5])
    >> pairs[1][2] = 3 + np.tril_indices(3, -1)
    NFc                    t                                                       ||t          d          ||t          d          |dd l}|                    |          | _        |t          j        |          | _        || _	        d S )Nz:Equivalence cov_struct requires either `pairs` or `labels`z?Equivalence cov_struct accepts only one of `pairs` and `labels`r   )
ra   r   ri   r   deepcopypairsr3   r   labels
return_cov)r   r  r  r  r   rb   s        r   r   zEquivalence.__init__\  s    MLN N N F$6      KKKu--DJ*V,,DK$r   c                 F   t          j        t          |          t          |          z  dft           j                  }t          j        t          |                    }t          j        ||                              t           j                  |dddf<   t          j        t          |                    }t          j        ||                              t           j                  |dddf<   |                    d           	 t          j        t           j	        |j        j
        |j        d         z  f          }t          j        |                              |          }t          j        |d          \  }}n|# t          $ ro t           j                            d          }	t          j        ||	                    |j        d         	                    }t          j        |d          \  }}Y nw xY w||ddf         }|dddf         |dddf         fS )
z
        Create arrays containing all unique ordered pairs of i, j.

        The arrays i and j must be one-dimensional containing non-negative
        integers.
        r)   rS   Nr   r*   T)return_indexi  )size)r3   rz   rU   r   r   kronr   sortrT   voidrP  r   ascontiguousarrayviewr   	TypeErrorrandomRandomStater   uniform)
r   r   r   r=  r   rT   bmatr   r8  rss
             r   _make_pairszEquivalence._make_pairsr  s    hAQ+28<<< GCFFOOGAqMM((22AAAqD	GCFFOOGAqMM((22AAAqD			8Hbgsy'9CIaL'HIJJE',,11%88DYt$777FAss 	8 	8 	8 &&t,,B6#rzzsy|z<<==DYt$777FAsss	8 #qqq&k111a4y#aaad)##s   
A8F A6G<;G<c                    ddl m  fd          }| j        }t          j        | j        |j        d          }|                    ddg          }t          j	        | j                  }t          |j                  D ]J\  }}t          t          |                    D ]&}t          |dz             D ]}	||         }
||	         }	 |j        ||
f         }|j        ||f         }n# t          $ r Y ?w xY w|                     ||          \  }}t!          |
          dz   t!          |          z   }t          j        ||k              }t          |          dk    r|d	z   }||         ||         f||         |<   t          j        ||k              }t          |          dk    r||         }||         }||f||         |<   (L|| _        d S )
Nr   r   c                        d           S )Nc                      d S r   r,   r,   r   r   r  zBEquivalence._pairs_from_labels.<locals>.<lambda>.<locals>.<lambda>  s     r   r,   r   s   r   r  z0Equivalence._pairs_from_labels.<locals>.<lambda>  s    KK$=$= r   )r  groupsr  r  r*   /z/v)collectionsr   r   r   r   r  r  groupbyr3   r   	enumerater   r5   rU   KeyErrorr  strr   r  )r   r  r   dfgbulabelsg_ixg_lblx1lx2lb1lb2r^  r_  clabelrS  clabelvr   s                    @r   _pairs_from_labelszEquivalence._pairs_from_labels  s   ++++++====>>
\T[ELIIJJZZ8,--)DK((#E$677 	7 	7JD$ S\\** 7 7 q>> 7 7C!#,C!#,C!Yc{3Yc{3# ! ! ! ! "--b"55FB XX^c#hh6F b11B2ww{{"(4-0222/?dG, b11B2ww{{VV/12hdF+577: 


s   C,,
C9	8C9	c                 |   t                                          |           | j        j        t	          j        dt                     t          | d          s|                                  t          t                    | _        t                      | _        | j        j        D ]}| j        |         D ]}| j        |         |         \  }}t!          j        ||k              rVt!          j        ||k              st	          j        dt&                     | j                            |           d| j        |<   dt!          j        t-          | j        j                  t           j                  z  }t3          | j        j                  D ]G\  }}| j        j        |         }	t!          j        t-          |	          t           j                  ||	<   H| j        j        D ]X}| j        |                                         D ]6}| j        |         |         \  }
}||
         ||         f| j        |         |<   7Yd S )NzVweights not implemented for equalence cov_struct, using unweighted covariance estimater  zBequivalence class contains both variance and covariance parametersr*   r   rS   )ra   r   r   ry   r8   r9   r	   rm   r  r   r   r   set_var_classesr   r  r3   anyallr
   addr   rU   r   r   r  r   r  rz  )r   r   gplbr  r  rxr  r  r   r'  rR  rb   s               r   r   zEquivalence.initialize  s.   5!!!:)M A/1 1 1 tW%% 	&##%%% &e,,EE*) 		, 		,Bjn , ,B+B6"(## ,6"(++ H 89FH H H %))"---*+DOB', "'#dj.//rx@@@@#DJ$;<< 	8 	8JD$)$/BYs2wwbh777BrFF *) 	4 	4Bjn))++ 4 4z"~b)1&(eRU^
2r""4	4 	4r   c                    | j         j        }| j         j        j        }| j         j        }t          d           }t          t                    }t          |          }t          | j         j	                  D ]_\  }}	||         \  }
}t          j         ||
                    }||         |
z
  |z  }| j        |	                                         D ]}| j        s
|| j        v r| j        |	         |         }||         dxx         t          j        ||d                  ||d                  z            z  cc<   | j        sn||         dxx         t          j        ||d                  dz            z  cc<   ||         dxx         t          j        ||d                  dz            z  cc<   ||xx         t          |d                   z  cc<   a| j        r5|                                D ]}||         d         ||         |z
  z  ||<    ni|                                D ]E}t          j        ||         d         ||         d         z            }||         d         |z  ||<   F| j        D ]}d||<   || _        || _        d S )Nc                  
    g dS )Nr   r,   r,   r   r   r  z$Equivalence.update.<locals>.<lambda>  s
     r   r   r*   r)   rs   )r   rt   rv   rw   rx   r   rY  rU   r  r   r3   r|   r  rz  r  r  r}   r   r   )r   r   r   r   rx   r   r   rX   r   r  r<   r   r=   r   r  rS  dens                    r   r    zEquivalence.update  s    
#*#,z. !5!566
c""&kktz677 	* 	*EAr$QIFAGGGFOO,,E1X&%/Ejn))++ * * R43D-D-DZ^B'2q!!!RVE"Q%L5A<,G%H%HH!!! CrN1%%%be0A)B)BB%%%rN1%%%be0A)B)BB%%%s2a5zz)* ? 	$ oo'' I I!+B!2gbkC6G!H
2I !oo'' 9 9gjnQ/*R.2CCDD!+B!2S!8
2' $ $!#
2$r   c                    t          |          }t          j        ||f          }| j        j        |         }| j        |                                         D ]*}| j        |         |         \  }}| j        |         |||f<   +||j        z   }t          j	        ||
                                dz             || j         fS )Nr)   )rU   r3   rz   r   r   r  rz  r   r   r   diagonalr  )	r   r<   r#   rX   r
  r  r  r  r  s	            r   r$   zEquivalence.covariance_matrix  s    &kkxc
##z&u-*T"'')) 	/ 	/BZ%b)FB?2.DRLLdf}
t}}2333(((r   )NNF)rJ   rK   rL   rM   r   r  r  r   r   r   r    r$   r   r   s   @r   r  r    s        < <|% % % % % %,$ $ $@, , ,\&4 &4 &4 &4 &4P Xi&''" " ('"H Xi)122) ) 32) ) ) ) )r   r  )!rM   statsmodels.compat.pandasr   r  r   r8   numpyr3   pandasr   scipyr   r-   #statsmodels.stats.correlation_toolsr   statsmodels.tools.sm_exceptionsr   r	   r
   statsmodels.tools.validationr   r   rO   r_   r   r   r   r  rG  rU  r  r  r  r,   r   r   <module>r     s    / . . . . . # # # # # #                ; ; ; ; ; ;         
 3 2 2 2 2 2e" e" e" e" e" e" e" e"P) ) ) ) )9 ) ) )<R R R R R9 R R RjR: R: R: R: R:9 R: R: R:jy y y y yY y y yxcB cB cB cB cB cB cB cBLs! s! s! s! s!Y s! s! s!l    9   DY= Y= Y= Y= Y=* Y= Y= Y=x    .   <    .   <}) }) }) }) })) }) }) }) }) })r   