B
    Z                 @   s  d Z ddlZddlmZ dddZG dd deZG d	d
 d
eZdddZ	e
dkrddlmZ ddlmZ edZeeZed ZeeZe  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dd edd eedd e  dS )z
Empirical CDF Functions
    N)interp1d皙?c             C   sP   t | }ttd| d|  }t| | dd}t| | dd}||fS )a  
    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       @   r      )lennpZsqrtlogZclip)FZalphanobsepsilonlowerupper r   Olib/python3.7/site-packages/statsmodels/distributions/empirical_distribution.py	_conf_set   s
    r   c               @   s"   e Zd ZdZd
ddZdd Zd	S )StepFunctiona/  
    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
            Fleftc       
      C   s   |  dkrd}t||| _t|}t|}|j|jkrJd}t|t|jdkrdd}t|tjtj |f | _	tj||f | _
|st| j	}	t| j	|	d| _	t| j
|	d| _
| j	jd | _d S )N)rightr   z*side can take the values 'right' or 'left'z"x and y do not have the same shaper   zx and y must be 1-dimensionalr   )r   
ValueErrorsider   asarrayshaper   Zr_infxyargsortZtaken)
selfr   r   ivalsortedr   msg_xZ_yZasortr   r   r   __init__M   s&    

zStepFunction.__init__c             C   s    t | j|| jd }| j| S )Nr   )r   Zsearchsortedr   r   r   )r   ZtimeZtindr   r   r   __call__g   s    zStepFunction.__call__N)r   Fr   )__name__
__module____qualname____doc__r#   r$   r   r   r   r   r   "   s   )
r   c                   s"   e Zd ZdZd fdd	Z  ZS )ECDFa  
    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                sf   d}|rRt j|dd}|  t|}t d| d|}tt| j|||dd nt||dt	dS d S )NT)copyg      ?r   )r   r    F)Zdrop_errorsZfill_values)
r   arraysortr   Zlinspacesuperr)   r#   r   r   )r   r   r   stepr
   r   )	__class__r   r   r#      s    zECDF.__init__)r   )r%   r&   r'   r(   r#   __classcell__r   r   )r/   r   r)   l   s   r)   Tc             K   sf   t |}|r| |f|}n.g }x|D ]}|| |f| q&W t |}t |}t|| || S )z
    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   appendr+   r   r   )fnr   Z
vectorizedkeywordsr   r"   ar   r   r   monotone_fn_inverter   s    



r5   __main__)urlopenz-http://www.statsci.org/data/general/nerve.txtg      I@Zpost)whererg      ?g?)r   )T)r(   Znumpyr   Zscipy.interpolater   r   objectr   r)   r5   r%   Zstatsmodels.compat.pythonr7   Zmatplotlib.pyplotZpyplotZpltZ
nerve_dataZloadtxtr   Zcdfr,   r	   r.   r   r   ZxlimZylimZvlinesZshowr   r   r   r   <module>   s.   
J%


