ó
šßÈ[c           @` s¹   d  Z  d d l m Z m Z m Z m Z d d l m Z d d l Z	 d d l
 m Z d d	 d
 d g Z d d d d „ Z e d „ Z e d „ Z e e d „ Z d e f d „  ƒ  YZ d S(   ui   
Methods for selecting the bin width of histograms

Ported from the astroML project: http://astroML.org/
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   sixNi   (   t   bayesian_blocksu	   histogramu   scott_bin_widthu   freedman_bin_widthu   knuth_bin_widthi
   c      	   K` s4  t  | t j ƒ rt j |  ƒ j ƒ  }  | d k	 rB t d ƒ ‚ n  | d k	 rs |  |  | d k |  | d k @}  n  | d k rŽ t |  ƒ } q| d k r² t	 |  t
 ƒ \ } } q| d k rÖ t |  t
 ƒ \ } } q| d k rú t |  t
 ƒ \ } } qt d j | ƒ ƒ ‚ n  t j |  d	 | d
 | d | | S(   u6  Enhanced histogram function, providing adaptive binnings

    This is a histogram function that enables the use of more sophisticated
    algorithms for determining bins.  Aside from the ``bins`` argument allowing
    a string specified how bins are computed, the parameters are the same
    as ``numpy.histogram()``.

    Parameters
    ----------
    a : array_like
        array of data to be histogrammed

    bins : int or list or str (optional)
        If bins is a string, then it must be one of:

        - 'blocks' : use bayesian blocks for dynamic bin widths

        - 'knuth' : use Knuth's rule to determine bins

        - 'scott' : use Scott's rule to determine bins

        - 'freedman' : use the Freedman-Diaconis rule to determine bins

    range : tuple or None (optional)
        the minimum and maximum range for the histogram.  If not specified,
        it will be (x.min(), x.max())

    weights : array_like, optional
        Not Implemented

    other keyword arguments are described in numpy.histogram().

    Returns
    -------
    hist : array
        The values of the histogram. See ``density`` and ``weights`` for a
        description of the possible semantics.
    bin_edges : array of dtype float
        Return the bin edges ``(length(hist)+1)``.

    See Also
    --------
    numpy.histogram
    u8   weights are not yet supported for the enhanced histogrami    i   u   blocksu   knuthu   scottu   freedmanu   unrecognized bin code: '{}'t   binst   ranget   weightsN(   t
   isinstanceR   t   string_typest   npt   asarrayt   ravelt   Nonet   NotImplementedErrorR   t   knuth_bin_widtht   Truet   scott_bin_widtht   freedman_bin_widtht
   ValueErrort   formatt	   histogram(   t   aR   R   R   t   kwargst   da(    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyR      s     .%c         C` sÂ   t  j |  ƒ }  |  j d k r- t d ƒ ‚ n  |  j } t  j |  ƒ } d | | d } | rº t  j |  j ƒ  |  j ƒ  | ƒ } t d | ƒ } |  j ƒ  | t  j	 | d ƒ } | | f S| Sd S(   u'  Return the optimal histogram bin width using Scott's rule

    Scott's rule is a normal reference rule: it minimizes the integrated
    mean squared error in the bin approximation under the assumption that the
    data is approximately Gaussian.

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool (optional)
        if True, then return the bin edges

    Returns
    -------
    width : float
        optimal bin width using Scott's rule
    bins : ndarray
        bin edges: returned if ``return_bins`` is True

    Notes
    -----
    The optimal bin width is

    .. math::
        \Delta_b = \frac{3.5\sigma}{n^{1/3}}

    where :math:`\sigma` is the standard deviation of the data, and
    :math:`n` is the number of data points [1]_.

    References
    ----------
    .. [1] Scott, David W. (1979). "On optimal and data-based histograms".
       Biometricka 66 (3): 605-610

    See Also
    --------
    knuth_bin_width
    freedman_bin_width
    bayesian_blocks
    histogram
    i   u   data should be one-dimensionalg      @i   NgUUUUUUÕ?(
   R   R   t   ndimR   t   sizet   stdt   ceilt   maxt   mint   arange(   t   datat   return_binst   nt   sigmat   dxt   NbinsR   (    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyR   `   s    +	#!
c         C` s<  t  j |  ƒ }  |  j d k r- t d ƒ ‚ n  |  j } | d k  rQ t d ƒ ‚ n  t  j |  d d g ƒ \ } } d | | | d } | r4|  j ƒ  |  j ƒ  } } t d t  j | | | ƒ ƒ } y | | t  j	 | d ƒ }	 WnD t k
 r)}
 d	 t
 |
 ƒ k r#t d
 j | d ƒ ƒ ‚ q*‚  n X| |	 f S| Sd S(   u  Return the optimal histogram bin width using the Freedman-Diaconis rule

    The Freedman-Diaconis rule is a normal reference rule like Scott's
    rule, but uses rank-based statistics for results which are more robust
    to deviations from a normal distribution.

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool (optional)
        if True, then return the bin edges

    Returns
    -------
    width : float
        optimal bin width using the Freedman-Diaconis rule
    bins : ndarray
        bin edges: returned if ``return_bins`` is True

    Notes
    -----
    The optimal bin width is

    .. math::
        \Delta_b = \frac{2(q_{75} - q_{25})}{n^{1/3}}

    where :math:`q_{N}` is the :math:`N` percent quartile of the data, and
    :math:`n` is the number of data points [1]_.

    References
    ----------
    .. [1] D. Freedman & P. Diaconis (1981)
       "On the histogram as a density estimator: L2 theory".
       Probability Theory and Related Fields 57 (4): 453-476

    See Also
    --------
    knuth_bin_width
    scott_bin_width
    bayesian_blocks
    histogram
    i   u   data should be one-dimensionali   u(   data should have more than three entriesi   iK   i   i   u   Maximum allowed size exceededu’   The inter-quartile range of the data is too small: failed to construct histogram with {} bins. Please use another bin method, such as bins="scott"NgUUUUUUÕ?(   R   R   R   R   R   t
   percentileR   R   R   R    t   strR   (   R!   R"   R#   t   v25t   v75R%   t   dmint   dmaxR&   R   t   e(    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyR      s*    ,	 
c   
      C` s   d d l  m } t |  ƒ } t |  t ƒ \ } } | j | t | ƒ d | ƒd } | j | ƒ } | d | d }	 | r… |	 | f S|	 Sd S(   u1  Return the optimal histogram bin width using Knuth's rule.

    Knuth's rule is a fixed-width, Bayesian approach to determining
    the optimal bin width of a histogram.

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool (optional)
        if True, then return the bin edges
    quiet : bool (optional)
        if True (default) then suppress stdout output from scipy.optimize

    Returns
    -------
    dx : float
        optimal bin width. Bins are measured starting at the first data point.
    bins : ndarray
        bin edges: returned if ``return_bins`` is True

    Notes
    -----
    The optimal number of bins is the value M which maximizes the function

    .. math::
        F(M|x,I) = n\log(M) + \log\Gamma(\frac{M}{2})
        - M\log\Gamma(\frac{1}{2})
        - \log\Gamma(\frac{2n+M}{2})
        + \sum_{k=1}^M \log\Gamma(n_k + \frac{1}{2})

    where :math:`\Gamma` is the Gamma function, :math:`n` is the number of
    data points, :math:`n_k` is the number of measurements in bin :math:`k`
    [1]_.

    References
    ----------
    .. [1] Knuth, K.H. "Optimal Data-Based Binning for Histograms".
       arXiv:0605197, 2006

    See Also
    --------
    freedman_bin_width
    scott_bin_width
    bayesian_blocks
    histogram
    i    (   t   optimizet   dispi   N(   t   scipyR.   t   _KnuthFR   R   t   fmint   lenR   (
   R!   R"   t   quietR.   t   knuthFt   dx0t   bins0t   MR   R%   (    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyR   ç   s    1#
R1   c           B` s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   ug  Class which implements the function minimized by knuth_bin_width

    Parameters
    ----------
    data : array-like, one dimension
        data to be histogrammed

    Notes
    -----
    the function F is given by

    .. math::
        F(M|x,I) = n\log(M) + \log\Gamma(\frac{M}{2})
        - M\log\Gamma(\frac{1}{2})
        - \log\Gamma(\frac{2n+M}{2})
        + \sum_{k=1}^M \log\Gamma(n_k + \frac{1}{2})

    where :math:`\Gamma` is the Gamma function, :math:`n` is the number of
    data points, :math:`n_k` is the number of measurements in bin :math:`k`.

    See Also
    --------
    knuth_bin_width
    c         C` su   t  j | d t ƒ|  _ |  j j d k r9 t d ƒ ‚ n  |  j j ƒ  |  j j |  _ d d l	 m
 } | j |  _ d  S(   Nt   copyi   u   data should be 1-dimensionali    (   t   special(   R   t   arrayR   R!   R   R   t   sortR   R#   R0   R:   t   gammaln(   t   selfR!   R:   (    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyt   __init__?  s    c         C` s+   t  j |  j d |  j d t | ƒ d ƒ S(   u%   Return the bin edges given a width dxi    iÿÿÿÿi   (   R   t   linspaceR!   t   int(   R>   R8   (    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyR   N  s    c         C` s   |  j  | ƒ S(   N(   t   eval(   R>   R8   (    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyt   __call__R  s    c         C` s²   t  | ƒ } | d k r t j S|  j | ƒ } t j |  j | ƒ \ } } |  j t j | ƒ |  j d | ƒ | |  j d ƒ |  j |  j d | ƒ t j	 |  j | d ƒ ƒ S(   u  Evaluate the Knuth function

        Parameters
        ----------
        dx : float
            Width of bins

        Returns
        -------
        F : float
            evaluation of the negative Knuth likelihood function:
            smaller values indicate a better fit.
        i    g      à?(
   RA   R   t   infR   R   R!   R#   t   logR=   t   sum(   R>   R8   R   t   nk(    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyRB   U  s    M(   t   __name__t
   __module__t   __doc__R?   R   RC   RB   (    (    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyR1   &  s
   			(   RJ   t
   __future__R    R   R   R   t   externR   t   numpyR   t    R   t   __all__R   R   t   FalseR   R   R   R   t   objectR1   (    (    (    s6   lib/python2.7/site-packages/astropy/stats/histogram.pyt   <module>   s   "		K=J?