U
    Kvf&                     @   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   D/tmp/pip-unpacked-wheel-2v6byqio/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| _|st 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')alternativeZ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 )zcreate 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   r9   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rt|||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.

    rC   Zsquarec                 S   s   | |  S Nr   xr   r   r   <lambda>      z!scale_transform.<locals>.<lambda>identityc                 S   s   | S rE   r   rF   r   r   r   rH     rI   z&transform should be abs, square or exprB   r   r   r2   z(center should be median, mean or trimmed)r   r   rC   callabler   r'   rB   r   r   
isinstancenumbersNumber)r   centerr7   Z	trim_fracr   rG   Ztfuncr8   r   r   r   scale_transform   s*    


  rP   )r   )r   )rB   rC   rD   r   )r@   rM   Znumpyr   r   r   r   rP   r   r   r   r   <module>   s   
3
    