U
    Ãmœd•&  ã                   @   sD   d Z ddlZddlZddd„Zddd„ZG dd„ dƒZddd„ZdS )zqAnova k-sample comparison without and with trimming

Created on Sun Jun 09 23:51:34 2013

Author: Josef Perktold
é    Nc                 C   sv   t  | ¡} |dkr|  ¡ } d}| j| }t|| ƒ}|| }||krLtdƒ‚tdƒg| j }t||ƒ||< | t|ƒ S )a  
    Slices off a proportion of items from both ends of an array.

    Slices off the passed proportion of items from both ends of the passed
    array (i.e., with `proportiontocut` = 0.1, slices leftmost 10% **and**
    rightmost 10% of scores).  You must pre-sort the array if you want
    'proper' trimming.  Slices off less if proportion results in a
    non-integer slice index (i.e., conservatively slices off
    `proportiontocut`).

    Parameters
    ----------
    a : array_like
        Data to trim.
    proportiontocut : float or int
        Proportion of data to trim at each end.
    axis : int or None
        Axis along which the observations are trimmed. The default is to trim
        along axis=0. If axis is None then the array will be flattened before
        trimming.

    Returns
    -------
    out : array-like
        Trimmed version of array `a`.

    Examples
    --------
    >>> from scipy import stats
    >>> a = np.arange(20)
    >>> b = stats.trimboth(a, 0.1)
    >>> b.shape
    (16,)

    Nr   úProportion too big.)	ÚnpÚasarrayZravelÚshapeÚintÚ
ValueErrorÚsliceÚndimÚtuple)ÚaÚproportiontocutÚaxisÚnobsÚlowercutÚuppercutÚsl© r   úY/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/stats/robust_compare.pyÚtrimboth   s    $

r   c                 C   s$   t t | |¡||d�}tj||d�S )aá  
    Return mean of array after trimming observations from both tails.

    If `proportiontocut` = 0.1, slices off 'leftmost' and 'rightmost' 10% of
    scores. Slices off LESS if proportion results in a non-integer slice
    index (i.e., conservatively slices off `proportiontocut` ).

    Parameters
    ----------
    a : array_like
        Input array
    proportiontocut : float
        Fraction to cut off at each tail of the sorted observations.
    axis : int or None
        Axis along which the trimmed means are computed. The default is axis=0.
        If axis is None then the trimmed mean will be computed for the
        flattened array.

    Returns
    -------
    trim_mean : ndarray
        Mean of trimmed array.

    ©r   )r   r   ÚsortÚmean)r   r   r   Znewar   r   r   Ú	trim_meanC   s    r   c                   @   s€   e Zd ZdZddd„Zedd„ ƒZedd	„ ƒZed
d„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZddd„Zdd„ ZdS )ÚTrimmedMeanaŸ  
    class for trimmed and winsorized one sample statistics

    axis is None, i.e. ravelling, is not supported

    Parameters
    ----------
    data : array-like
        The data, observations to analyze.
    fraction : float in (0, 0.5)
        The fraction of observations to trim at each tail.
        The number of observations trimmed at each tail is
        ``int(fraction * nobs)``
    is_sorted : boolean
        Indicator if data is already sorted. By default the data is sorted
        along ``axis``.
    axis : int
        The axis of reduce operations. By default axis=0, that is observations
        are along the zero dimension, i.e. rows if 2-dim.
    Fr   c                 C   sì   t  |¡| _|| _|| _| jj|  | _}t|| ƒ | _}||  | _	}||krZt
dƒ‚|d|  | _td ƒg| jj | _t| j| j	ƒ| j|< t| jƒ| _|s´t j| j|d�| _n| j| _t j| j||d�| _t j| j|d |d�| _d S )Nr   é   r   é   )r   r   Údatar   Úfractionr   r   r   r   r   r   Únobs_reducedr   r	   r   r
   r   Údata_sortedZtakeÚ
lowerboundÚ
upperbound)Úselfr   r   Ú	is_sortedr   r   r   r   r   r   r   Ú__init__v   s"    zTrimmedMean.__init__c                 C   s   | j | j S )z/numpy array of trimmed and sorted data
        )r   r   ©r"   r   r   r   Údata_trimmed’   s    zTrimmedMean.data_trimmedc                 C   s0   t  | j| j¡}t  | j| j¡}t  | j||¡S )zwinsorized data
        )r   Úexpand_dimsr    r   r!   Zclipr   )r"   ZlbZubr   r   r   Údata_winsorized™   s    zTrimmedMean.data_winsorizedc                 C   s   t  | jt| jƒ | j¡S )zmean of trimmed data
        )r   r   r   r
   r   r   r%   r   r   r   Úmean_trimmed¡   s    zTrimmedMean.mean_trimmedc                 C   s   t  | j| j¡S )z mean of winsorized data
        )r   r   r(   r   r%   r   r   r   Úmean_winsorized§   s    zTrimmedMean.mean_winsorizedc                 C   s   t j| jd| jd�S )z$variance of winsorized data
        r   )Zddofr   )r   Úvarr(   r   r%   r   r   r   Úvar_winsorized­   s    zTrimmedMean.var_winsorizedc                 C   s,   t  | j| j ¡}|t  | j| j ¡9 }|S )z'standard error of trimmed mean
        )r   Úsqrtr,   r   r   )r"   Úser   r   r   Ústd_mean_trimmed´   s    zTrimmedMean.std_mean_trimmedc                 C   s.   t  | j| j ¡}|| jd | jd  9 }|S )z*standard error of winsorized mean
        r   )r   r-   r,   r   r   )r"   Ústd_r   r   r   Ústd_mean_winsorized¿   s    zTrimmedMean.std_mean_winsorizedÚtrimmedú	two-sidedc           	      C   sp   ddl m  m} | jd }|dkr2| j}| j}n|dkrH| j}| j}ntdƒ‚|j	|d||||d�}||f S )aF  
        One sample t-test for trimmed or Winsorized mean

        Parameters
        ----------
        value : float
            Value of the mean under the Null hypothesis
        transform : {'trimmed', 'winsorized'}
            Specified whether the mean test is based on trimmed or winsorized
            data.
        alternative : {'two-sided', 'larger', 'smaller'}


        Notes
        -----
        p-value is based on the approximate t-distribution of the test
        statistic. The approximation is valid if the underlying distribution
        is symmetric.
        r   Nr   r2   Z
winsorizedz/transform can only be 'trimmed' or 'winsorized')ÚalternativeÚdiff)
Zstatsmodels.stats.weightstatsÚstatsZweightstatsr   r)   r/   r*   r1   r   Z_tstat_generic)	r"   ÚvalueÚ	transformr4   ZsmwsZdfZmean_r0   Úresr   r   r   Ú
ttest_meanÍ   s    

  ÿzTrimmedMean.ttest_meanc                 C   s    t | j|d| jd�}| j|_|S )z„create a TrimmedMean instance with a new trimming fraction

        This reuses the sorted array from the current instance.
        T)r#   r   )r   r   r   r   )r"   ÚfracÚtmr   r   r   Úreset_fractionñ   s
    
ÿzTrimmedMean.reset_fractionN)Fr   )r   r2   r3   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   Úpropertyr&   r(   r)   r*   r,   r/   r1   r:   r=   r   r   r   r   r   `   s(   








  ÿ
$r   ÚmedianÚabsçš™™™™™É?c                 C   sö   t  | ¡}|dkrt j}n:|dkr,dd„ }n(|dkr>dd„ }nt|ƒrL|}ntdƒ‚|dkr|||t  t j||d	�|¡ ƒ}nv|d
kr¤||t  t j||d	�|¡ ƒ}nN|dkrÐt|||d	�}||t  ||¡ ƒ}n"t	|t
jƒrê||| ƒ}ntdƒ‚|S )a  Transform data for variance comparison for Levene type tests

    Parameters
    ----------
    data : array_like
        Observations for the data.
    center : "median", "mean", "trimmed" or float
        Statistic used for centering observations. If a float, then this
        value is used to center. Default is median.
    transform : 'abs', 'square', 'identity' or a callable
        The transform for the centered data.
    trim_frac : float in [0, 0.5)
        Fraction of observations that are trimmed on each side of the sorted
        observations. This is only used if center is `trimmed`.
    axis : int
        Axis along which the data are transformed when centering.

    Returns
    -------
    res : ndarray
        transformed data in the same shape as the original data.

    rD   Zsquarec                 S   s   | |  S ©Nr   ©Úxr   r   r   Ú<lambda>  ó    z!scale_transform.<locals>.<lambda>Úidentityc                 S   s   | S rF   r   rG   r   r   r   rI     rJ   z&transform should be abs, square or exprC   r   r   r2   z(center should be median, mean or trimmed)r   r   rD   Úcallabler   r'   rC   r   r   Ú
isinstanceÚnumbersÚNumber)r   Úcenterr8   Z	trim_fracr   rH   Ztfuncr9   r   r   r   Úscale_transformÿ   s*    


  rQ   )r   )r   )rC   rD   rE   r   )rA   rN   Únumpyr   r   r   r   rQ   r   r   r   r   Ú<module>   s   
3
    ÿ