
    !g}&                     H    d Z ddlZddlZddZddZ G d d      Z	 	 d	dZy)
zqAnova k-sample comparison without and with trimming

Created on Sun Jun 09 23:51:34 2013

Author: Josef Perktold
    Nc                 $   t        j                  |       } || 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/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/stats/robust_compare.pytrimbothr      s    H 	

1A|GGI774=D?T)*HhHH.//
+	BXx(BtHU2Y<    c                 t    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   s.    2 BGGAt$oDAD774d##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y)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.
    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
  |      | _        y )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   s*   JJt$	 	 99??400	D#&x$#77#'(?2 233 1x</;-$))..0dmmT]];.!wwtyyt<D#yyD ''$"2"2H4H''$"2"2HqLtLr   c                 4    | 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   sM     ^^DOOTYY7^^DOOTYY7wwt''R00r   c                     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,     wwt''dgg7CCr   c                 V    t        j                  | j                  | j                        S )z mean of winsorized data
        )r   r   r5   r   r.   s    r   mean_winsorizedzTrimmedMean.mean_winsorized   s     wwt++TYY77r   c                 Z    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!    
 vvd**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   sL     WWT((4+<+<<= 	bggdii$"3"3344	r   c                     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   sJ    
 wwt**TYY67Q4#4#4q#899 r   c                     ddl mc m} | j                  dz
  }|dk(  r| j                  }| j
                  }n)|dk(  r| j                  }| j                  }nt        d      |j                  |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"   trimmed
winsorizedz/transform can only be 'trimmed' or 'winsorized')alternativediff)
statsmodels.stats.weightstatsstatsweightstatsr%   r7   rA   r9   rD   r
   _tstat_generic)	r*   value	transformrH   smwsdfmean_rC   ress	            r   
ttest_meanzTrimmedMean.ttest_mean   s    * 	54"	!%%E((D,&((E++DNOO!!%D"$+E " KbU{r   c                 n    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   s3    
 ))44"ii))) 	r   N)Fr   )r   rF   z	two-sided)__name__
__module____qualname____doc__r,   propertyr/   r5   r7   r9   r=   rA   rD   rT   rX    r   r   r   r   _   s    *M8 ) ) 1 1 D D
 8 8
 D D     -6*"Hr   r   c           	      f   t        j                  |       }|dk(  rt         j                  }n+|dk(  rd }n"|dk(  rd }nt        |      r|}nt	        d      |dk(  r6 ||t        j
                  t        j                  ||      |      z
        }|S |d	k(  r6 ||t        j
                  t        j                  ||      |      z
        }|S |d
k(  r/t        |||      } ||t        j
                  ||      z
        }|S t        |t        j                        r |||z
        }|S t	        d      )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.

    abssquarec                     | | z  S Nr^   xs    r   <lambda>z!scale_transform.<locals>.<lambda>  s
    !a% r   identityc                     | S rc   r^   rd   s    r   rf   z!scale_transform.<locals>.<lambda>  s    ! r   z&transform should be abs, square or expmedianr   r   rF   z(center should be median, mean or trimmed)r   r   r`   callabler
   r1   ri   r   r   
isinstancenumbersNumber)r#   centerrO   	trim_fracr   re   tfuncrS   s           r   scale_transformrq      s,   2 	

4AE	h		j	 	)	ABBAryy'>EEF J 
6	Arwwqt'<dCCD J 
9	1id3Avt445 J 
FGNN	+AJ J CDDr   )r   )ri   r`   g?r   )r\   rl   numpyr   r   r   r   rq   r^   r   r   <module>rs      s9     0f$:\ \~ GJ2r   