o
    0Gf!                     @   sF   d Z ddlmZ ddlZddlmZ G dd deZG d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                       sD   e Zd ZdZd fdd	Zdd Zdd Zd	d
 ZdddZ  Z	S )HolderTuplez Holder class with indexing

    Nc                    sH   t  jdi | |d urt fdd|D  _d S  j jf _d S )Nc                 3   s    | ]}t  |V  qd S N)getattr).0Zattself 6lib/python3.10/site-packages/statsmodels/stats/base.py	<genexpr>   s    z'HolderTuple.__init__.<locals>.<genexpr>r
   )super__init__tupleZ	statisticZpvalue)r	   Ztuple_kwds	__class__r   r   r      s   zHolderTuple.__init__c                 c   s    | j E d H  d S r   r   r   r
   r
   r   __iter__   s   zHolderTuple.__iter__c                 C   s
   | j | S r   r   )r	   idxr
   r
   r   __getitem__      
zHolderTuple.__getitem__c                 C   s
   t | jS r   )lenr   r   r
   r
   r   __len__   r   zHolderTuple.__len__Tc                 C   s   t jt| j||dS )N)dtypecopy)npZarraylistr   )r	   r   r   r
   r
   r   	__array__!   s   zHolderTuple.__array__r   )NT)
__name__
__module____qualname____doc__r   r   r   r   r   __classcell__r
   r
   r   r   r      s    r   c                   @   s@   e Zd ZdZ		dddZdddZdd	 Zd
d Zd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                    sl   || _ || _|d u rt|d | _n|| _|| _ | _ d u r*dd |D | _d S  fdd|D | _d S )N   c                 S   s   g | ]}d  |qS )z{!r}formatr   pairsr
   r
   r   
<listcomp>M   s    
z,AllPairsResults.__init__.<locals>.<listcomp>c                    s(   g | ]}d   |d   |d  qS )z{}-{}r   r&   r'   r)   levelsr
   r   r+   Q   s    )	pvals_raw	all_pairsr   maxn_levelsmultitest_methodr-   all_pairs_names)r	   r.   r/   r2   r-   r1   r
   r,   r   r   @   s   
zAllPairsResults.__init__c                 C   s4   ddl m  m} |du r| j}|j| j|d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)methodr&   )statsmodels.stats.multiteststats	multitestr2   Zmultipletestsr.   )r	   r4   smtr
   r
   r   pval_correctedW   s   zAllPairsResults.pval_correctedc                 C   s   |   S r   )summaryr   r
   r
   r   __str__d   s   zAllPairsResults.__str__c                 C   s*   | j }t||f}|  |t| j < |S )zcreate a (n_levels, n_levels) array with corrected p_values

        this needs to improve, similar to R pairwise output
        )r1   r   Zzerosr9   r   r/   )r	   kZ	pvals_matr
   r
   r   
pval_tableg   s   zAllPairsResults.pval_tablec                 C   sz   ddl m  m} tdd | jD }d|j| j  }|dd|d d	   d
 7 }|ddd t| j| 	 D 7 }|S )zreturns text summarizing the results

        uses the default pvalue correction of the instance stored in
        ``self.multitest_method``
        r   Nc                 s   s    | ]}t |V  qd S r   )r   )r   ssr
   r
   r   r   y   s    z*AllPairsResults.summary.<locals>.<genexpr>z0Corrected p-values using %s p-value correction

ZPairs    r&   z	p-values

c                 s   s     | ]\}}d  ||V  qdS )z{}  {:6.4g}Nr'   )r   r*   Zpvr
   r
   r   r   ~   s    )
r5   r6   r7   r0   r3   Zmultitest_methods_namesr2   joinzipr9   )r	   r8   maxleveltextr
   r
   r   r:   r   s   

zAllPairsResults.summary)r%   NNr   )	r   r    r!   r"   r   r9   r;   r=   r:   r
   r
   r
   r   r$   %   s    

r$   )	r"   Zstatsmodels.compat.pythonr   Znumpyr   Zstatsmodels.tools.testingr   r   r$   r
   r
   r
   r   <module>   s    