
:>"^  ã               @   s·   d  Z  d d l m Z d d l m Z d d l Z y d d l m Z Wn5 e k
 r} Z	 z d d l m Z WYd d Z	 [	 Xn Xd d d	 d
 „ Z
 e d k r³ d d l m Z e ƒ  d S)aP  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.
é    )Úprint_function)ÚrangeN)Úmediang       @g      @é   c                s$  t  ˆ ƒ } t t j | | ƒ ƒ ‰  ‡  ‡ f d d †  t | ƒ Dƒ } t j t ˆ g t j ˆ g ƒ | ƒ d d ƒ } d | | | } | | | } t j | ƒ } t j	 | ƒ } xft | ƒ D]X}	 xà t | ƒ D]Ò }
 | | d d … |
 f } | ˆ } t j
 | | ƒ } t j
 | | ƒ } t | ƒ } t | ƒ } | } t j
 | ˆ ƒ } | | | | } | | | | | } | | | | | } | | ˆ |
 | |
 <q× W| | } t t | ƒ ƒ } t j | d | d d ƒ | d d … <d | | | d d … <| | | d d … <qÄ W| S)	a¿  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]
    c                s1   g  |  ]' } t  j t ˆ ˆ | ƒ ƒ ˆ  ‘ q S© )ÚnumpyÚsortÚabs)Ú.0Úi)ÚrÚxr   ú:/tmp/pip-build-ww9dw3qa/biopython/Bio/Statistics/lowess.pyú
<listcomp>I   s   	 zlowess.<locals>.<listcomp>g        g      ð?é   Né   éÿÿÿÿ)ÚlenÚintr   Úceilr   Zclipr	   Z	transposeZzerosZonesÚdotÚsumr   )r   ÚyÚfÚiterÚnÚhÚwZyestÚdeltaÚ	iterationr   ZweightsZweights_mul_xZb1Zb2ZA11ZA12ZA21ZA22ZdeterminantZbeta1Zbeta2Z	residualsÚsr   )r   r   r   Úlowess'   s8     "2

'r!   Ú__main__)Úrun_doctestgUUUUUUå?)Ú__doc__Ú
__future__r   Z	Bio._py3kr   r   ZBio.Clusterr   ÚImportErrorr   r!   Ú__name__Z
Bio._utilsr#   r   r   r   r   Ú<module>   s   #>