
    M/Ph}&                     P    d Z ddlZddlZddZddZ G d d          Z	 	 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                 L   t          j        |           } ||                                 } d}| j        |         }t	          ||z            }||z
  }||k    rt          d          t          d          g| j        z  }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asarrayravelshapeint
ValueErrorslicendimtuple)aproportiontocutaxisnobslowercutuppercutsls          `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/robust_compare.pytrimbothr      s    H 	
1A|GGII74=D?T)**HhHH.///
++	BXx((BtHU2YY<    c                 x    t          t          j        | |          ||          }t          j        ||          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   newas       r   	trim_meanr   B   s9    2 BGAt$$oDAAAD74d####r   c                       e Zd ZdZddZed             Zed             Zed             Zed             Z	ed	             Z
ed
             Zed             Z	 	 ddZ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                    t          j        |          | _        || _        || _        | j        j        |         x| _        }t          ||z            x| _        }||z
  x| _	        }||k    rt          d          |d|z  z
  | _        t          d           g| j        j        z  | _        t          | j        | j	                  | j        |<   t          | j                  | _        |s!t          j        | j        |          | _        n| j        | _        t          j        | j        ||          | _        t          j        | j        |dz
  |          | _        d S )Nr      r      )r   r   datar   fractionr   r   r	   r   r   r
   nobs_reducedr   r   r   r   r   data_sortedtake
lowerbound
upperbound)selfr#   r$   	is_sortedr   r   r   r   s           r   __init__zTrimmedMean.__init__u   s9   Jt$$	 	 9?400	D#&x$#7#77#'(?2  2333 1x</;;-$).0dmT];;.. 	)!wtyt<<<D#yD '$"2H4HHH'$"2HqLtLLLr   c                 &    | j         | j                 S )z/numpy array of trimmed and sorted data
        )r&   r   r*   s    r   data_trimmedzTrimmedMean.data_trimmed   s    
 ((r   c                     t          j        | j        | j                  }t          j        | j        | j                  }t          j        | j        ||          S )zwinsorized data
        )r   expand_dimsr(   r   r)   clipr&   )r*   lbubs      r   data_winsorizedzTrimmedMean.data_winsorized   sE     ^DOTY77^DOTY77wt'R000r   c                 p    t          j        | j        t          | j                           | j                  S )zmean of trimmed data
        )r   r   r&   r   r   r   r.   s    r   mean_trimmedzTrimmedMean.mean_trimmed   s(     wt'dg7CCCr   c                 @    t          j        | j        | j                  S )z mean of winsorized data
        )r   r   r5   r   r.   s    r   mean_winsorizedzTrimmedMean.mean_winsorized   s     wt+TY777r   c                 D    t          j        | j        d| j                  S )z$variance of winsorized data
        r"   )ddofr   )r   varr5   r   r.   s    r   var_winsorizedzTrimmedMean.var_winsorized   s     
 vd*CCCCr   c                     t          j        | j        | j        z            }|t          j        | j        | j        z            z  }|S )z'standard error of trimmed mean
        )r   sqrtr=   r%   r   )r*   ses     r   std_mean_trimmedzTrimmedMean.std_mean_trimmed   sC     WT(4+<<== 	bgdi$"33444	r   c                 x    t          j        | j        | j        z            }|| j        dz
  | j        dz
  z  z  }|S )z*standard error of winsorized mean
        r"   )r   r?   r=   r   r%   )r*   std_s     r   std_mean_winsorizedzTrimmedMean.std_mean_winsorized   sA    
 wt*TY677Q4#4q#899 r   trimmed	two-sidedc                     ddl mc m} | j        dz
  }|dk    r| j        }| j        }n$|dk    r| j        }| j        }nt          d          |	                    |d||||          }||fz   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"   rE   
winsorizedz/transform can only be 'trimmed' or 'winsorized')alternativediff)
statsmodels.stats.weightstatsstatsweightstatsr%   r7   rA   r9   rD   r
   _tstat_generic)	r*   value	transformrI   smwsdfmean_rC   ress	            r   
ttest_meanzTrimmedMean.ttest_mean   s    * 	544444444"	!!%E(DD,&&(E+DDNOOO!!%D"$+E " K KbU{r   c                 X    t          | j        |d| j                  }| 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tms      r   reset_fractionzTrimmedMean.reset_fraction   s6    
 )44"i) ) )) 	r   N)Fr   )r   rE   rF   )__name__
__module____qualname____doc__r,   propertyr/   r5   r7   r9   r=   rA   rD   rU   rY    r   r   r   r   _   s%        *M M M M8 ) ) X) 1 1 X1 D D XD
 8 8 X8
 D D XD   X   X -6*" " " "H    r   r   medianabs皙?c           	         t          j        |           }|dk    rt           j        }n5|dk    rd }n+|dk    rd }n!t          |          r|}nt	          d          |dk    r6 ||t          j        t          j        ||          |          z
            }n|d	k    r6 ||t          j        t          j        ||          |          z
            }nr|d
k    r4t          |||          } ||t          j        ||          z
            }n8t          |t          j                  r |||z
            }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.

    ra   squarec                     | | z  S Nr_   xs    r   <lambda>z!scale_transform.<locals>.<lambda>  s
    !a% r   identityc                     | S rf   r_   rg   s    r   ri   z!scale_transform.<locals>.<lambda>  s    ! r   z&transform should be abs, square or expr`   r   r   rE   z(center should be median, mean or trimmed)r   r   ra   callabler
   r1   r`   r   r   
isinstancenumbersNumber)r#   centerrP   	trim_fracr   rh   tfuncrT   s           r   scale_transformrs      sm   2 	
4AE	h			j	 	 	)		 CABBBeAry'>'>'>EEEFF	6		eArwqt'<'<'<dCCCDD	9		1id333eAvt44455	FGN	+	+ EeAJCDDDJr   )r   )r`   ra   rb   r   )r]   rn   numpyr   r   r   r   rs   r_   r   r   <module>ru      s         0 0 0 0f$ $ $ $:\ \ \ \ \ \ \ \~ GJ2 2 2 2 2 2r   