ó
î&]\c        
   @` s’  d  Z  d d l m Z m Z m Z d d d d d d d	 d
 d d g
 Z d d l Z d d l m Z m	 Z	 m
 Z
 d d l j Z d d l m Z d d l m Z d d l m Z m Z m Z m Z e d d d g ƒ d e d „ Z d e d „ Z e d d d g ƒ d d „ Z d# e e f d d d „ Z d d d g d d „ Z d d d g d d d „ Z d d d „ Z  d d  „ Z! d d! „ Z" d d" „ Z# d S($   sB   
Additional statistics functions with support for masked arrays.

i    (   t   divisiont   print_functiont   absolute_importt   compare_medians_mst   hdquantilest   hdmediant   hdquantiles_sdt   idealfourthst   median_cihst   mjcit   mquantiles_cimjt   rsht   trimmed_mean_ciN(   t   float_t   int_t   ndarray(   t   MaskedArrayi   (   t   mstats_basic(   t   normt   betat   tt   binomg      Ð?g      à?g      è?c         C` sÂ   d „  } t  j |  d t d t ƒ}  t j | d t d d ƒ} | d k sZ |  j d k ro | |  | | ƒ } n@ |  j d k r” t d |  j ƒ ‚ n  t  j | | |  | | ƒ } t  j	 | d t ƒS(	   s  
    Computes quantile estimates with the Harrell-Davis method.

    The quantile estimates are calculated as a weighted linear combination
    of order statistics.

    Parameters
    ----------
    data : array_like
        Data array.
    prob : sequence, optional
        Sequence of quantiles to compute.
    axis : int or None, optional
        Axis along which to compute the quantiles. If None, use a flattened
        array.
    var : bool, optional
        Whether to return the variance of the estimate.

    Returns
    -------
    hdquantiles : MaskedArray
        A (p,) array of quantiles (if `var` is False), or a (2,p) array of
        quantiles and variances (if `var` is True), where ``p`` is the
        number of quantiles.

    See Also
    --------
    hdquantiles_sd

    c         S` s¬  t  j t  j |  j ƒ  j t ƒ ƒ ƒ } | j } t  j d t | ƒ f t	 ƒ } | d k  rx t  j
 | _ | rp | S| d St  j | d ƒ t | ƒ } t j } x˜ t | ƒ D]Š \ } }	 | | | d |	 | d d |	 ƒ }
 |
 d |
 d  } t  j | | ƒ } | | d | f <t  j | | | d ƒ | d | f <q« W| d | d | d k f <| d | d | d k f <| r¤t  j
 | d | d k f <| d | d k f <| S| d S(   sG   Computes the HD quantiles for a 1D array. Returns nan for invalid data.i   i    i   iÿÿÿÿ(   t   npt   squeezet   sortt
   compressedt   viewR   t   sizet   emptyt   lenR   t   nant   flatt   aranget   floatR   t   cdft	   enumeratet   dot(   t   datat   probt   vart   xsortedt   nt   hdt   vt   betacdft   it   pt   _wt   wt   hd_mean(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyt   _hd_1D>   s,    '		&(-t   copyt   dtypet   ndmini   i   sD   Array 'data' must be at most two dimensional, but got data.ndim = %dN(
   t   mat   arrayt   FalseR   R   t   Nonet   ndimt
   ValueErrort   apply_along_axist   fix_invalid(   R%   R&   t   axisR'   R2   R.   t   result(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR      s    	iÿÿÿÿc         C` s(   t  |  d g d | d | ƒ} | j ƒ  S(   s9  
    Returns the Harrell-Davis estimate of the median along the given axis.

    Parameters
    ----------
    data : ndarray
        Data array.
    axis : int, optional
        Axis along which to compute the quantiles. If None, use a flattened
        array.
    var : bool, optional
        Whether to return the variance of the estimate.

    Returns
    -------
    hdmedian : MaskedArray
        The median values.  If ``var=True``, the variance is returned inside
        the masked array.  E.g. for a 1-D array the shape change from (1,) to
        (2,).

    g      à?R>   R'   (   R   R   (   R%   R>   R'   R?   (    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR   j   s    c         C` s³   d „  } t  j |  d t d t ƒ}  t j | d t d d ƒ} | d k r] | |  | ƒ } n= |  j d k r‚ t d |  j ƒ ‚ n  t  j | | |  | ƒ } t  j	 | d t ƒj
 ƒ  S(	   sý  
    The standard error of the Harrell-Davis quantile estimates by jackknife.

    Parameters
    ----------
    data : array_like
        Data array.
    prob : sequence, optional
        Sequence of quantiles to compute.
    axis : int, optional
        Axis along which to compute the quantiles. If None, use a flattened
        array.

    Returns
    -------
    hdquantiles_sd : MaskedArray
        Standard error of the Harrell-Davis quantile estimates.

    See Also
    --------
    hdquantiles

    c         S` s¶  t  j |  j ƒ  ƒ } t | ƒ } t  j t | ƒ t ƒ } | d k  rT t  j | _ n  t  j | ƒ t	 | d ƒ } t
 j } x5t | ƒ D]'\ } } | | | d | | d d | ƒ }	 |	 d |	 d  }
 t  j g  t | ƒ D]T } t  j |
 | t  j t t d | ƒ ƒ t t | d | ƒ ƒ f j t ƒ ƒ ^ qÞ d t ƒ} t  j | j ƒ  d t d d ƒ| t	 | d ƒ } t	 | d ƒ t  j t  j | ƒ j ƒ  t	 | ƒ ƒ | | <q‡ W| S(   s%   Computes the std error for 1D arrays.i   i   iÿÿÿÿi    R4   R3   R5   (   R   R   R   R   R   R   R   R   R    R!   R   R"   R#   t   fromitert   rangeR$   t   r_t   listt   astypeR   R7   R'   R8   t   sqrtt   diagt   diagonal(   R%   R&   R(   R)   t   hdsdt   vvR,   R-   R.   R/   R0   t   kt   mx_t   mx_var(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyt   _hdsd_1Dœ   s    	&	m3>R3   R4   R5   i   i   sD   Array 'data' must be at most two dimensional, but got data.ndim = %dN(   R6   R7   R8   R   R   R9   R:   R;   R<   R=   t   ravel(   R%   R&   R>   RM   R.   R?   (    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR   „   s    	gš™™™™™É?gš™™™™™©?c   
      C` s¶   t  j |  d t ƒ}  t j |  d | d | d | ƒ} | j | ƒ } t j |  d | d | d | ƒ} | j | ƒ d } t j	 d | d | ƒ }	 t
 j | |	 | | |	 | f ƒ S(   s³  
    Selected confidence interval of the trimmed mean along the given axis.

    Parameters
    ----------
    data : array_like
        Input data.
    limits : {None, tuple}, optional
        None or a two item tuple.
        Tuple of the percentages to cut on each side of the array, with respect
        to the number of unmasked data, as floats between 0. and 1. If ``n``
        is the number of unmasked data before trimming, then
        (``n * limits[0]``)th smallest data and (``n * limits[1]``)th
        largest data are masked.  The total number of unmasked data after
        trimming is ``n * (1. - sum(limits))``.
        The value of one limit can be set to None to indicate an open interval.

        Defaults to (0.2, 0.2).
    inclusive : (2,) tuple of boolean, optional
        If relative==False, tuple indicating whether values exactly equal to
        the absolute limits are allowed.
        If relative==True, tuple indicating whether the number of data being
        masked on each side should be rounded (True) or truncated (False).

        Defaults to (True, True).
    alpha : float, optional
        Confidence level of the intervals.

        Defaults to 0.05.
    axis : int, optional
        Axis along which to cut. If None, uses a flattened version of `data`.

        Defaults to None.

    Returns
    -------
    trimmed_mean_ci : (2,) ndarray
        The lower and upper confidence intervals of the trimmed data.

    R3   t   limitst	   inclusiveR>   i   g       @(   R6   R7   R8   t   mstatst   trimrt   meant   trimmed_stdet   countR   t   ppfR   (
   R%   RO   RP   t   alphaR>   t   trimmedt   tmeant   tstdet   dft   tppf(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR   Á   s    *!!c         C` s‘   d „  } t  j |  d t ƒ}  |  j d k rC t d |  j ƒ ‚ n  t j | d t d d ƒ} | d k rw | |  | ƒ St  j | | |  | ƒ Sd S(   s„  
    Returns the Maritz-Jarrett estimators of the standard error of selected
    experimental quantiles of the data.

    Parameters
    ----------
    data : ndarray
        Data array.
    prob : sequence, optional
        Sequence of quantiles to compute.
    axis : int or None, optional
        Axis along which to compute the quantiles. If None, use a flattened
        array.

    c         S` s#  t  j |  j ƒ  ƒ }  |  j } t  j | ƒ | d j t ƒ } t j } t  j	 t
 | ƒ t ƒ } t  j d | d d t ƒ| } | d | } x t | ƒ D] \ } }	 | | |	 d | |	 ƒ | | |	 d | |	 ƒ }
 t  j |
 |  ƒ } t  j |
 |  d ƒ } t  j | | d ƒ | | <qš W| S(   Ng      à?i   R4   g      ð?i   (   R   R   R   R   R7   RD   R   R   R"   R   R   R   R    R#   R$   RE   (   R%   R.   R)   R&   R,   t   mjt   xt   yR-   t   mt   Wt   C1t   C2(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyt   _mjci_1D  s    	 	 2R3   i   sD   Array 'data' must be at most two dimensional, but got data.ndim = %dR5   i   N(   R6   R7   R8   R:   R;   R   R9   R<   (   R%   R&   R>   Rd   R.   (    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR	   ô   s    	c      	   C` s}   t  | d | ƒ } t j d | d ƒ } t j |  | d d d d d | ƒ} t |  | d | ƒ} | | | | | | f S(   sÕ  
    Computes the alpha confidence interval for the selected quantiles of the
    data, with Maritz-Jarrett estimators.

    Parameters
    ----------
    data : ndarray
        Data array.
    prob : sequence, optional
        Sequence of quantiles to compute.
    alpha : float, optional
        Confidence level of the intervals.
    axis : int or None, optional
        Axis along which to compute the quantiles.
        If None, use a flattened array.

    Returns
    -------
    ci_lower : ndarray
        The lower boundaries of the confidence interval.  Of the same length as
        `prob`.
    ci_upper : ndarray
        The upper boundaries of the confidence interval.  Of the same length as
        `prob`.

    i   g       @t   alphapi    t   betapR>   (   t   minR   RV   RQ   t
   mquantilesR	   (   R%   R&   RW   R>   t   zt   xqt   smj(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR
   !  s
    $c         C` s}   d „  } t  j |  d t ƒ}  | d k r< | |  | ƒ } n= |  j d k ra t d |  j ƒ ‚ n  t  j | | |  | ƒ } | S(   sA  
    Computes the alpha-level confidence interval for the median of the data.

    Uses the Hettmasperger-Sheather method.

    Parameters
    ----------
    data : array_like
        Input data. Masked values are discarded. The input should be 1D only,
        or `axis` should be set to None.
    alpha : float, optional
        Confidence level of the intervals.
    axis : int or None, optional
        Axis along which to compute the quantiles. If None, use a flattened
        array.

    Returns
    -------
    median_cihs
        Alpha level confidence interval.

    c   	      S` sŽ  t  j |  j ƒ  ƒ }  t |  ƒ } t | d | ƒ } t t j | d | d ƒ ƒ } t j | | | d ƒ t j | d | d ƒ } | d | k  rÐ | d 8} t j | | | d ƒ t j | d | d ƒ } n  t j | | d | d ƒ t j | | d ƒ } | d | | | } | | | t	 | | d | | ƒ } | |  | d | |  | d | |  | | d d | |  | | f } | S(   Ni   g       @g      à?i   (
   R   R   R   R   Rg   t   intR   t   _ppfR"   R!   (	   R%   RW   R)   RJ   t   gkt   gkkt   It   lambdt   lims(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyt   _cihs_1DZ  s    0
30(-R3   i   sD   Array 'data' must be at most two dimensional, but got data.ndim = %dN(   R6   R7   R8   R9   R:   R;   R<   (   R%   RW   R>   Rs   R?   (    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR   C  s    	c         C` s“   t  j |  d | ƒt  j | d | ƒ} } t j |  d | ƒt j | d | ƒ} } t j | | ƒ t  j | d | d ƒ } d t j | ƒ S(   s+  
    Compares the medians from two independent groups along the given axis.

    The comparison is performed using the McKean-Schrader estimate of the
    standard error of the medians.

    Parameters
    ----------
    group_1 : array_like
        First dataset.  Has to be of size >=7.
    group_2 : array_like
        Second dataset.  Has to be of size >=7.
    axis : int, optional
        Axis along which the medians are estimated. If None, the arrays are
        flattened.  If `axis` is not None, then `group_1` and `group_2`
        should have the same shape.

    Returns
    -------
    compare_medians_ms : {float, ndarray}
        If `axis` is None, then returns a float, otherwise returns a 1-D
        ndarray of floats with a length equal to the length of `group_1`
        along `axis`.

    R>   i   i   (	   R6   t   medianRQ   t   stde_medianR   t   absRE   R   R"   (   t   group_1t   group_2R>   t   med_1t   med_2t   std_1t   std_2Ra   (    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR   v  s
    +,c         C` sT   d „  } t  j |  d | ƒj t ƒ }  | d k r= | |  ƒ St  j | | |  ƒ Sd S(   sC  
    Returns an estimate of the lower and upper quartiles.

    Uses the ideal fourths algorithm.

    Parameters
    ----------
    data : array_like
        Input array.
    axis : int, optional
        Axis along which the quartiles are estimated. If None, the arrays are
        flattened.

    Returns
    -------
    idealfourths : {list of floats, masked array}
        Returns the two internal values that divide `data` into four parts
        using the ideal fourths algorithm either along the flattened array
        (if `axis` is None) or along `axis` of `data`.

    c         S` sµ   |  j  ƒ  } t | ƒ } | d k  r4 t j t j g St | d d d ƒ \ } } t | ƒ } d | | | d | | | } | | } d | | | | | | d } | | g S(   Ni   g      @i   g      (@i   g«ªªªªªÚ?(   R   R   R   R   t   divmodRl   (   R%   R^   R)   t   jt   ht   qloRJ   t   qup(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyt   _idf­  s    "
"R>   N(   R6   R   R   R   R9   R<   (   R%   R>   R‚   (    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR   —  s
    	
c         C` s%  t  j |  d t ƒ}  | d k r* |  } n t j | d t d d ƒ} |  j d k rc t d ƒ ‚ n  |  j ƒ  } t |  d d ƒ} d | d | d | d } |  d d … d f | d d d … f | k j	 d ƒ } |  d d … d f | d d d … f | k  j	 d ƒ } | | d | | S(   sé  
    Evaluates Rosenblatt's shifted histogram estimators for each data point.

    Rosenblatt's estimator is a centered finite-difference approximation to the
    derivative of the empirical cumulative distribution function.

    Parameters
    ----------
    data : sequence
        Input data, should be 1-D. Masked values are ignored.
    points : sequence or None, optional
        Sequence of points where to evaluate Rosenblatt shifted histogram.
        If None, use the data.

    R3   R5   i   s#   The input array should be 1D only !R>   g333333ó?iÿÿÿÿi    g      ð?i   Ng       @gš™™™™™É?(
   R6   R7   R8   R9   R   R:   t   AttributeErrorRU   R   t   sum(   R%   t   pointsR)   t   rR   t   nhit   nlo(    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyR   ¿  s    	99(   gš™™™™™É?gš™™™™™É?($   t   __doc__t
   __future__R    R   R   t   __all__t   numpyR   R   R   R   t   numpy.maR6   R   t    R   RQ   t   scipy.stats.distributionsR   R   R   R   RC   R9   R8   R   R   R   t   TrueR   R	   R
   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/mstats_extras.pyt   <module>   s0   			"!K=2-"3!(