
    Vfd                     `    d Z ddlmZ ddlZddlmZ  G d de          Z G d d          ZdS )	zgBase classes for statistical test results

Created on Mon Apr 22 14:03:21 2013

Author: Josef Perktold
    )lzipN)Holderc                   >     e Zd ZdZd fd	Zd Zd Zd ZddZ xZ	S )	HolderTuplez Holder class with indexing

    Nc                       t          t                     j        di | |"t           fd|D                        _        d S  j         j        f _        d S )Nc              3   8   K   | ]}t          |          V  d S N)getattr).0attselfs     6lib/python3.11/site-packages/statsmodels/stats/base.py	<genexpr>z'HolderTuple.__init__.<locals>.<genexpr>   s-      DDcwtS11DDDDDD     )superr   __init__tuple	statisticpvalue)r   tuple_kwds	__class__s   `  r   r   zHolderTuple.__init__   si    )k4  )11D111DDDDVDDDDDDJJJ.$+6DJJJr   c              #   $   K   | j         E d {V  d S r	   r   r   s    r   __iter__zHolderTuple.__iter__   s&      :r   c                     | j         |         S r	   r   )r   idxs     r   __getitem__zHolderTuple.__getitem__   s    z#r   c                 *    t          | j                  S r	   )lenr   r   s    r   __len__zHolderTuple.__len__   s    4:r   c                 R    t          j        t          | j                  |          S )N)dtype)npasarraylistr   )r   r%   s     r   	__array__zHolderTuple.__array__"   s!    z$tz**%8888r   r	   )
__name__
__module____qualname____doc__r   r   r    r#   r)   __classcell__)r   s   @r   r   r      s         7 7 7 7 7 7      9 9 9 9 9 9 9 9r   r   c                   8    e Zd ZdZ	 	 d	dZd
dZd Zd Zd ZdS )AllPairsResultsa  Results class for pairwise comparisons, based on p-values

    Parameters
    ----------
    pvals_raw : array_like, 1-D
        p-values from a pairwise comparison test
    all_pairs : list of tuples
        list of indices, one pair for each comparison
    multitest_method : str
        method that is used by default for p-value correction. This is used
        as default by the methods like if the multiple-testing method is not
        specified as argument.
    levels : {list[str], None}
        optional names of the levels or groups
    n_levels : None or int
        If None, then the number of levels or groups is inferred from the
        other arguments. It can be explicitly specified, if the inferred
        number is incorrect.

    Notes
    -----
    This class can also be used for other pairwise comparisons, for example
    comparing several treatments to a control (as in Dunnet's test).

    hsNc                     || _         || _        |t          j        |          dz   | _        n|| _        || _        | _        d |D             | _        d S fd|D             | _        d S )N   c                     g | ]}|S r   r   )r   pairss     r   
<listcomp>z,AllPairsResults.__init__.<locals>.<listcomp>N   s    #K#K#KEE#K#K#Kr   c                 N    g | ]!}|d                   d|d                  "S )r   -r3   r   )r   r5   levelss     r   r6   z,AllPairsResults.__init__.<locals>.<listcomp>P   sM     $< $< $<(- 06eAh/?/?/?/5eAh/?/?%A $< $< $<r   )	pvals_raw	all_pairsr&   maxn_levelsmultitest_methodr9   all_pairs_names)r   r:   r;   r>   r9   r=   s       ` r   r   zAllPairsResults.__init__A   s    ""F9--1DMM$DM 0>#K#K#K#K#KD   $< $< $< $<1:$< $< $<D   r   c                 j    ddl mc m} || j        }|                    | j        |          d         S )zp-values corrected for multiple testing problem

        This uses the default p-value correction of the instance stored in
        ``self.multitest_method`` if method is None.

        r   N)methodr3   )statsmodels.stats.multiteststats	multitestr>   multipletestsr:   )r   rA   smts      r   pval_correctedzAllPairsResults.pval_correctedT   sL     	211111111>*F   ??BBr   c                 *    |                                  S r	   )summaryr   s    r   __str__zAllPairsResults.__str__a   s    ||~~r   c                     | j         }t          j        ||f          }|                                 |t	          | j         <   |S )zcreate a (n_levels, n_levels) array with corrected p_values

        this needs to improve, similar to R pairwise output
        )r=   r&   zerosrG   r   r;   )r   k	pvals_mats      r   
pval_tablezAllPairsResults.pval_tabled   sA    
 MHaV$$	+/+>+>+@+@	$'(r   c           	      4   ddl mc m} t          d | j        D                       }d|j        | j                 z  }|dd|dz
  dz   z  z   d	z   z  }|d
                    d t          | j        | 	                                          D                       z  }|S )zreturns text summarizing the results

        uses the default pvalue correction of the instance stored in
        ``self.multitest_method``
        r   Nc              3   4   K   | ]}t          |          V  d S r	   )r"   )r   sss     r   r   z*AllPairsResults.summary.<locals>.<genexpr>v   s(      ??BB??????r   z0Corrected p-values using %s p-value correction

Pairs    r3   z	p-values

c              3   *   K   | ]\  }}d ||fz  V  dS )z	%s  %6.4gNr   )r   r5   pvs      r   r   z*AllPairsResults.summary.<locals>.<genexpr>{   sG       L L%;%4 L L L L L Lr   )
rB   rC   rD   r<   r?   multitest_methods_namesr>   joinziprG   )r   rF   maxleveltexts       r   rI   zAllPairsResults.summaryo   s     	211111111??$*>???@@D-d.CDE3(Q,"234|CC		 L Ld2D4G4G4I4IJJL L L M M 	Mr   )r1   NNr	   )	r*   r+   r,   r-   r   rG   rJ   rO   rI   r   r   r   r0   r0   &   s         4 ?C'+< < < <&C C C C  	 	 	    r   r0   )	r-   statsmodels.compat.pythonr   numpyr&   statsmodels.tools.testingr   r   r0   r   r   r   <module>ra      s     + * * * * *     , , , , , ,9 9 9 9 9& 9 9 92W W W W W W W W W Wr   