ó
˛é Ic           @   s   d  Z  d d l Z y d d l m Z Wn# e k
 rK Z d d l m Z n Xd d d d  Z d   Z e d	 k r~ e   n  d S(
   s]  
This module implements the Lowess function for nonparametric regression.

Functions:
lowess        Fit a smooth nonparametric regression curve to a scatterplot.

For more information, see

William S. Cleveland: "Robust locally weighted regression and smoothing
scatterplots", Journal of the American Statistical Association, December 1979,
volume 74, number 368, pp. 829-836.

William S. Cleveland and Susan J. Devlin: "Locally weighted regression: An
approach to regression analysis by local fitting", Journal of the American
Statistical Association, September 1988, volume 83, number 403, pp. 596-610.
i˙˙˙˙N(   t   mediang       @g      @i   c         C   s!  t  |   } t t j | |   } g  t |  D]' } t j t |  |  |   | ^ q2 } t j t |  g t j |  g  |  d d  } d | | | } | | | } t j	 |  }	 t j
 |  }
 xKt |  D]=} xŕ t |  D]Ň } |
 | d d  | f } | |  } t j | |  } t j | |  } t |  } t |  } | } t j | |   } | | | | } | | | | | } | | | | | } | | |  | |	 | <qď W| |	 } t t |   } t j | d | d d  |
 (d |
 |
 |
 (|
 |
 |
 (qÜ W|	 S(   sç  lowess(x, y, f=2./3., iter=3) -> yest

    Lowess smoother: Robust locally weighted regression.
    The lowess function fits a nonparametric regression curve to a scatterplot.
    The arrays x and y contain an equal number of elements; each pair
    (x[i], y[i]) defines a data point in the scatterplot. The function returns
    the estimated (smooth) values of y.

    The smoothing span is given by f. A larger value for f will result in a
    smoother curve. The number of robustifying iterations is given by iter. The
    function will run faster with a smaller number of iterations.

    x and y should be numpy float arrays of equal length.  The return value is
    also a numpy float array of that length.

    e.g.
    >>> import numpy
    >>> x = numpy.array([4,  4,  7,  7,  8,  9, 10, 10, 10, 11, 11, 12, 12, 12,
    ...                 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16,
    ...                 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20, 20, 20, 20,
    ...                 20, 22, 23, 24, 24, 24, 24, 25], numpy.float)
    >>> y = numpy.array([2, 10,  4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24,
    ...                 28, 26, 34, 34, 46, 26, 36, 60, 80, 20, 26, 54, 32, 40,
    ...                 32, 40, 50, 42, 56, 76, 84, 36, 46, 68, 32, 48, 52, 56,
    ...                 64, 66, 54, 70, 92, 93, 120, 85], numpy.float)
    >>> result = lowess(x, y)
    >>> len(result)
    50
    >>> print "[%0.2f, ..., %0.2f]" % (result[0], result[-1])
    [4.85, ..., 84.98]
    g        g      đ?i   Ni   i˙˙˙˙(   t   lent   intt   numpyt   ceilt   ranget   sortt   abst   clipt	   transposet   zerost   onest   xranget   dott   sumR    (   t   xt   yt   ft   itert   nt   rt   it   ht   wt   yestt   deltat	   iterationt   weightst   weights_mul_xt   b1t   b2t   A11t   A12t   A21t   A22t   determinantt   beta1t   beta2t	   residualst   s(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Statistics/lowess.pyt   lowess!   s8     :2

c          C   s$   d GHd d l  }  |  j   d GHd S(   s0   Run the Bio.Statistics.lowess module's doctests.s   Running doctests...i˙˙˙˙Nt   Done(   t   doctestt   testmod(   R*   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Statistics/lowess.pyt   _test^   s    
t   __main__(	   t   __doc__R   t   Bio.ClusterR    t   ImportErrorR   R(   R,   t   __name__(    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Statistics/lowess.pyt   <module>   s   =	