ó
‡ˆ\c           @   sß   d  d l  Z d  d l m Z d  d l m Z d d l m Z m Z m	 Z	 d d l
 m Z m Z m Z d d l m Z m Z d  d l Z d  d l Z d d	 d
 g Z d „  Z d d d e d „ Z d
 e e e	 f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   interpolate(   t	   spearmanri   (   t   BaseEstimatort   TransformerMixint   RegressorMixin(   t   as_float_arrayt   check_arrayt   check_consistent_length(   t'   _inplace_contiguous_isotonic_regressiont   _make_uniquet   check_increasingt   isotonic_regressiont   IsotonicRegressionc   	      C   sÞ   t  |  | ƒ \ } } | d k } | d	 k rÚ t |  ƒ d k rÚ d t j d | d | ƒ } d t j t |  ƒ d ƒ } t j | d | ƒ } t j | d | ƒ } t j | ƒ t j | ƒ k rÚ t j	 d ƒ qÚ n  | S(
   sC  Determine whether y is monotonically correlated with x.

    y is found increasing or decreasing with respect to x based on a Spearman
    correlation test.

    Parameters
    ----------
    x : array-like, shape=(n_samples,)
            Training data.

    y : array-like, shape=(n_samples,)
        Training target.

    Returns
    -------
    increasing_bool : boolean
        Whether the relationship is increasing or decreasing.

    Notes
    -----
    The Spearman correlation coefficient is estimated from the data, and the
    sign of the resulting estimate is used as the result.

    In the event that the 95% confidence interval based on Fisher transform
    spans zero, a warning is raised.

    References
    ----------
    Fisher transformation. Wikipedia.
    https://en.wikipedia.org/wiki/Fisher_transformation
    i    g      ð¿g      ð?i   g      à?i   g\Âõ(\ÿ?sw   Confidence interval of the Spearman correlation coefficient spans zero. Determination of ``increasing`` may be suspect.(   g      ð¿g      ð?(
   R   t   lent   matht   logt   sqrtt   tanht   npt   signt   warningst   warn(	   t   xt   yt   rhot   _t   increasing_boolt   Ft   F_set   rho_0t   rho_1(    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyR
      s    "c         C   s  | r t  j n t  j d d d … } t  j |  | d t  j ƒ}  | d k ro t  j t |  ƒ d t  j ƒ} n t  j | | d t  j ƒ} t |  | ƒ | d k	 s° | d k	 rú | d k rÉ t  j } n  | d k rá t  j } n  t  j	 |  | | |  ƒ n  |  | S(   s¨  Solve the isotonic regression model::

        min sum w[i] (y[i] - y_[i]) ** 2

        subject to y_min = y_[1] <= y_[2] ... <= y_[n] = y_max

    where:
        - y[i] are inputs (real numbers)
        - y_[i] are fitted
        - w[i] are optional strictly positive weights (default to 1.0)

    Read more in the :ref:`User Guide <isotonic>`.

    Parameters
    ----------
    y : iterable of floats
        The data.

    sample_weight : iterable of floats, optional, default: None
        Weights on each point of the regression.
        If None, weight is set to 1 (equal weights).

    y_min : optional, default: None
        If not None, set the lowest value of the fit to y_min.

    y_max : optional, default: None
        If not None, set the highest value of the fit to y_max.

    increasing : boolean, optional, default: True
        Whether to compute ``y_`` is increasing (if set to True) or decreasing
        (if set to False)

    Returns
    -------
    y_ : list of floats
        Isotonic fit of y.

    References
    ----------
    "Active set algorithms for isotonic regression; A unifying framework"
    by Michael J. Best and Nilotpal Chakravarti, section 3.
    Niÿÿÿÿt   dtype(
   R   t   s_t   arrayt   float64t   Nonet   onesR   R   t   inft   clip(   R   t   sample_weightt   y_mint   y_maxt
   increasingt   order(    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyR   M   s    ,&!c           B   st   e  Z d  Z d d e d d „ Z d d „ Z d „  Z e d „ Z d d „ Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z RS(   sI	  Isotonic regression model.

    The isotonic regression optimization problem is defined by::

        min sum w_i (y[i] - y_[i]) ** 2

        subject to y_[i] <= y_[j] whenever X[i] <= X[j]
        and min(y_) = y_min, max(y_) = y_max

    where:
        - ``y[i]`` are inputs (real numbers)
        - ``y_[i]`` are fitted
        - ``X`` specifies the order.
          If ``X`` is non-decreasing then ``y_`` is non-decreasing.
        - ``w[i]`` are optional strictly positive weights (default to 1.0)

    Read more in the :ref:`User Guide <isotonic>`.

    Parameters
    ----------
    y_min : optional, default: None
        If not None, set the lowest value of the fit to y_min.

    y_max : optional, default: None
        If not None, set the highest value of the fit to y_max.

    increasing : boolean or string, optional, default: True
        If boolean, whether or not to fit the isotonic regression with y
        increasing or decreasing.

        The string value "auto" determines whether y should
        increase or decrease based on the Spearman correlation estimate's
        sign.

    out_of_bounds : string, optional, default: "nan"
        The ``out_of_bounds`` parameter handles how x-values outside of the
        training domain are handled.  When set to "nan", predicted y-values
        will be NaN.  When set to "clip", predicted y-values will be
        set to the value corresponding to the nearest train interval endpoint.
        When set to "raise", allow ``interp1d`` to throw ValueError.


    Attributes
    ----------
    X_min_ : float
        Minimum value of input array `X_` for left bound.

    X_max_ : float
        Maximum value of input array `X_` for right bound.

    f_ : function
        The stepwise interpolating function that covers the input domain ``X``.

    Notes
    -----
    Ties are broken using the secondary method from Leeuw, 1977.

    References
    ----------
    Isotonic Median Regression: A Linear Programming Approach
    Nilotpal Chakravarti
    Mathematics of Operations Research
    Vol. 14, No. 2 (May, 1989), pp. 303-308

    Isotone Optimization in R : Pool-Adjacent-Violators
    Algorithm (PAVA) and Active Set Methods
    Leeuw, Hornik, Mair
    Journal of Statistical Software 2009

    Correctness of Kruskal's algorithms for monotone regression with ties
    Leeuw, Psychometrica, 1977
    t   nanc         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R(   R)   R*   t   out_of_bounds(   t   selfR(   R)   R*   R-   (    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   __init__Ô   s    			c         C   s(   t  | j ƒ d k r$ t d ƒ ‚ n  d  S(   Ni   s   X should be a 1d array(   R   t   shapet
   ValueError(   R.   t   XR   R'   (    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   _check_fit_dataÛ   s    c            s…   |  j  d k r* t d j |  j  ƒ ƒ ‚ n  |  j  d k } t ˆ  ƒ d k r` ‡  f d †  |  _ n! t j | ˆ  d d d	 | ƒ|  _ d
 S(   s   Build the f_ interp1d function.t   raiseR,   R&   sI   The argument ``out_of_bounds`` must be in 'nan', 'clip', 'raise'; got {0}i   c            s   ˆ  j  |  j ƒ S(   N(   t   repeatR0   (   R   (   R   (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   <lambda>ë   s    t   kindt   lineart   bounds_errorN(   R4   R,   R&   (   R-   R1   t   formatR   t   f_R    t   interp1d(   R.   R2   R   R9   (    (   R   s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   _build_fß   s    	c         C   sK  t  | | | ƒ g  | | g D] } t | d t ƒ^ q \ } } t | ƒ } |  j | | | ƒ |  j d k r„ t | | ƒ |  _ n |  j |  _ | d k	 rÝ t | d t ƒ} | d k } | | | | | | } } } n t	 j
 t | ƒ ƒ } t	 j | | f ƒ } g  | | | g D]" } | | j t	 j d t ƒ^ q\ } } } t | | | ƒ \ }	 }
 } |	 |  _ } t |
 | |  j |  j d |  j ƒ|  _ } t	 j | ƒ t	 j | ƒ |  _ |  _ | r=t	 j
 t | ƒ f d t ƒ} t	 j t	 j | d d !| d	  ƒ t	 j | d d !| d
 ƒ ƒ | d d +| | | | f S| | f Sd S(   s    Build the y_ IsotonicRegression.t	   ensure_2dt   autoi    t   copyR*   R   i   iÿÿÿÿiþÿÿÿi   N(   R   R   t   FalseR   R3   R*   R
   t   increasing_R#   R   R$   R   t   lexsortt   astypeR"   R	   t   _X_R   R(   R)   t   _y_t   mint   maxt   X_min_t   X_max_t   boolt
   logical_ort	   not_equal(   R.   R2   R   R'   t   trim_duplicatesR   t   maskR+   R!   t   unique_Xt   unique_yt   unique_sample_weightt	   keep_data(    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   _build_yð   s:    1#>	%'c         C   sB   |  j  | | | ƒ \ } } | | |  _ |  _ |  j | | ƒ |  S(   sx  Fit the model using X, y as training data.

        Parameters
        ----------
        X : array-like, shape=(n_samples,)
            Training data.

        y : array-like, shape=(n_samples,)
            Training target.

        sample_weight : array-like, shape=(n_samples,), optional, default: None
            Weights. If set to None, all weights will be set to 1 (equal
            weights).

        Returns
        -------
        self : object
            Returns an instance of self.

        Notes
        -----
        X is stored for future use, as `transform` needs X to interpolate
        new input data.
        (   RT   t   _necessary_X_t   _necessary_y_R=   (   R.   R2   R   R'   (    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   fit(  s    c         C   s”   t  | ƒ } t | j ƒ d k r0 t d ƒ ‚ n  |  j d k rZ t d j |  j ƒ ƒ ‚ n  |  j d k r‡ t j | |  j |  j	 ƒ } n  |  j
 | ƒ S(   s  Transform new data by linear interpolation

        Parameters
        ----------
        T : array-like, shape=(n_samples,)
            Data to transform.

        Returns
        -------
        T_ : array, shape=(n_samples,)
            The transformed data
        i   s.   Isotonic regression input should be a 1d arrayR4   R,   R&   sI   The argument ``out_of_bounds`` must be in 'nan', 'clip', 'raise'; got {0}(   R4   R,   R&   (   R   R   R0   R1   R-   R:   R   R&   RI   RJ   R;   (   R.   t   T(    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt	   transformO  s    	c         C   s   |  j  | ƒ S(   s	  Predict new data by linear interpolation.

        Parameters
        ----------
        T : array-like, shape=(n_samples,)
            Data to transform.

        Returns
        -------
        T_ : array, shape=(n_samples,)
            Transformed data.
        (   RY   (   R.   RX   (    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   predictj  s    c         C   s)   t  t |  ƒ j ƒ  } | j d d ƒ | S(   s1   Pickle-protocol - return state of the estimator. R;   N(   t   superR   t   __getstate__t   popR#   (   R.   t   state(    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyR\   y  s    c         C   sQ   t  t |  ƒ j | ƒ t |  d ƒ rM t |  d ƒ rM |  j |  j |  j ƒ n  d S(   sn   Pickle-protocol - set state of the estimator.

        We need to rebuild the interpolation function.
        RU   RV   N(   R[   R   t   __setstate__t   hasattrR=   RU   RV   (   R.   R^   (    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyR_   €  s    N(   t   __name__t
   __module__t   __doc__R#   t   TrueR/   R3   R=   RT   RW   RY   RZ   R\   R_   (    (    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyR   ‹   s   H		8'			(   t   numpyR   t   scipyR    t   scipy.statsR   t   baseR   R   R   t   utilsR   R   R   t	   _isotonicR   R	   R   R   t   __all__R
   R#   Rd   R   R   (    (    (    s/   lib/python2.7/site-packages/sklearn/isotonic.pyt   <module>   s   		9	=