ó
áp7]c           @   sv  d  Z  d d l Z d d l m Z d d „ Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ e	 d
 „ Z
 e d k rrd d l m Z d d l j Z e d ƒ Z e j e ƒ Z e d Z e e ƒ Z e j ƒ  e e ƒ Z e j e e d d ƒe e ƒ \ Z Z e j e e d d d ƒe j e e d d d ƒe j d d ƒ e j d d ƒ e j e d d ƒ e j ƒ  n  d S(   s   
Empirical CDF Functions
iÿÿÿÿN(   t   interp1dgš™™™™™©?c         C   sl   t  |  ƒ } t j t j d | ƒ d | ƒ } t j |  | d d ƒ } t j |  | d d ƒ } | | f S(   sõ  
    Constructs a Dvoretzky-Kiefer-Wolfowitz confidence band for the eCDF.

    Parameters
    ----------
    F : array-like
        The empirical distributions
    alpha : float
        Set alpha for a (1 - alpha) % confidence band.

    Notes
    -----
    Based on the DKW inequality.

    .. math:: P \left( \sup_x \left| F(x) - \hat(F)_n(X) \right| > \epsilon \right) \leq 2e^{-2n\epsilon^2}

    References
    ----------
    Wasserman, L. 2006. `All of Nonparametric Statistics`. Springer.
    g       @i   i    i   (   t   lent   npt   sqrtt   logt   clip(   t   Ft   alphat   nobst   epsilont   lowert   upper(    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyt	   _conf_set   s
    $t   StepFunctionc           B   s)   e  Z d  Z d e d d „ Z d „  Z RS(   s/  
    A basic step function.

    Values at the ends are handled in the simplest way possible:
    everything to the left of x[0] is set to ival; everything
    to the right of x[-1] is set to y[-1].

    Parameters
    ----------
    x : array-like
    y : array-like
    ival : float
        ival is the value given to the values to the left of x[0]. Default
        is 0.
    sorted : bool
        Default is False.
    side : {'left', 'right'}, optional
        Default is 'left'. Defines the shape of the intervals constituting the
        steps. 'right' correspond to [a, b) intervals and 'left' to (a, b].

    Examples
    --------
    >>> import numpy as np
    >>> from statsmodels.distributions.empirical_distribution import StepFunction
    >>>
    >>> x = np.arange(20)
    >>> y = np.arange(20)
    >>> f = StepFunction(x, y)
    >>>
    >>> print(f(3.2))
    3.0
    >>> print(f([[3.2,4.5],[24,-3.1]]))
    [[  3.   4.]
     [ 19.   0.]]
    >>> f2 = StepFunction(x, y, side='right')
    >>>
    >>> print(f(3.0))
    2.0
    >>> print(f2(3.0))
    3.0
    g        t   leftc   
      C   s7  | j  ƒ  d k r' d } t | ƒ ‚ n  | |  _ t j | ƒ } t j | ƒ } | j | j k ru d } t | ƒ ‚ n  t | j ƒ d k rŸ d } t | ƒ ‚ n  t j t j | f |  _	 t j | | f |  _
 | s t j |  j	 ƒ }	 t j |  j	 |	 d ƒ |  _	 t j |  j
 |	 d ƒ |  _
 n  |  j	 j d |  _ d  S(	   Nt   rightR   s*   side can take the values 'right' or 'left's"   x and y do not have the same shapei   s   x and y must be 1-dimensionali    (   R   R   (   R
   t
   ValueErrort   sideR   t   asarrayt   shapeR   t   r_t   inft   xt   yt   argsortt   taket   n(
   t   selfR   R   t   ivalt   sortedR   t   msgt   _xt   _yt   asort(    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyt   __init__M   s&    	c         C   s*   t  j |  j | |  j ƒ d } |  j | S(   Ni   (   R   t   searchsortedR   R   R   (   R   t   timet   tind(    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyt   __call__g   s    (   t   __name__t
   __module__t   __doc__t   FalseR"   R&   (    (    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyR   "   s   )t   ECDFc           B   s   e  Z d  Z d d „ Z RS(   s…  
    Return the Empirical CDF of an array as a step function.

    Parameters
    ----------
    x : array-like
        Observations
    side : {'left', 'right'}, optional
        Default is 'right'. Defines the shape of the intervals constituting the
        steps. 'right' correspond to [a, b) intervals and 'left' to (a, b].

    Returns
    -------
    Empirical CDF as a step function.

    Examples
    --------
    >>> import numpy as np
    >>> from statsmodels.distributions.empirical_distribution import ECDF
    >>>
    >>> ecdf = ECDF([3, 3, 1, 4])
    >>>
    >>> ecdf([3, 55, 0.5, 1.5])
    array([ 0.75,  1.  ,  0.  ,  0.25])
    R   c         C   sm   t  j | d t ƒ} | j ƒ  t | ƒ } t  j d | d | ƒ } t t |  ƒ j | | d | d t ƒd  S(   Nt   copyg      ð?i   R   R   (	   R   t   arrayt   Truet   sortR   t   linspacet   superR+   R"   (   R   R   R   R   R   (    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyR"   †   s
    
(   R'   R(   R)   R"   (    (    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyR+   l   s   c         K   s‡   t  j | ƒ } | r' |  | |  } n< g  } x$ | D] } | j |  | |  ƒ q4 Wt  j | ƒ } t  j | ƒ } t | | | | ƒ S(   sÅ   
    Given a monotone function fn (no checking is done to verify monotonicity)
    and a set of x values, return an linearly interpolated approximation
    to its inverse from its values on x.
    (   R   R   t   appendR-   R   R    (   t   fnR   t
   vectorizedt   keywordsR   R   t   a(    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyt   monotone_fn_inverter”   s    t   __main__(   t   urlopens-   http://www.statsci.org/data/general/nerve.txtg      I@t   wheret   postt   ri    g      ø?gÍÌÌÌÌÌð?(   R)   t   numpyR   t   scipy.interpolateR    R   t   objectR   R+   R.   R7   R'   t   statsmodels.compat.pythonR9   t   matplotlib.pyplott   pyplott   pltt
   nerve_datat   loadtxtR   t   cdfR/   R   t   stepR
   R   t   xlimt   ylimt   vlinest   show(    (    (    sO   lib/python2.7/site-packages/statsmodels/distributions/empirical_distribution.pyt   <module>   s.   J(

