ó
‡ˆ\c           @   s‹   d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z d e	 f d „  ƒ  YZ
 d „  Z d	 „  Z d d
 d d e e d „ Z d S(   s’  
Our own implementation of the Newton algorithm

Unlike the scipy.optimize version, this version of the Newton conjugate
gradient solver uses only one function call to retrieve the
func value, the gradient value and a callable for the Hessian matvec
product. If the function call is very expensive (e.g. for logistic
regression with large design matrix), this approach gives very
significant speedups.
iÿÿÿÿN(   t   line_search_wolfe2t   line_search_wolfe1i   (   t   ConvergenceWarningt   _LineSearchErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s5   lib/python2.7/site-packages/sklearn/utils/optimize.pyR      s   c   	   	   K   su   t  |  | | | | | | |  } | d d k rU t |  | | | | | | |  } n  | d d k rq t ƒ  ‚ n  | S(   s  
    Same as line_search_wolfe1, but fall back to line_search_wolfe2 if
    suitable step length is not found, and raise an exception if a
    suitable step length is not found.

    Raises
    ------
    _LineSearchError
        If no suitable step size is found

    i    N(   R   t   NoneR    R   (	   t   ft   fprimet   xkt   pkt   gfkt   old_fvalt   old_old_fvalt   kwargst   ret(    (    s5   lib/python2.7/site-packages/sklearn/utils/optimize.pyt   _line_search_wolfe12   s    	c         C   s`  t  j t | ƒ d | j ƒ} | } | } d } t  j | | ƒ } x| | k r[t  j t  j | ƒ ƒ | k rt Pn  |  | ƒ }	 t  j | |	 ƒ }
 d |
 k o¼ d t  j t  j ƒ j	 k n rÅ Pn2 |
 d k  r÷ | d k rá Pq÷ | | |
 | 7} Pn  | |
 } | | | 7} | | |	 } t  j | | ƒ } | | } | | | } | d } | } qF W| S(   sI  
    Solve iteratively the linear system 'fhess_p . xsupi = fgrad'
    with a conjugate gradient descent.

    Parameters
    ----------
    fhess_p : callable
        Function that takes the gradient as a parameter and returns the
        matrix product of the Hessian and gradient

    fgrad : ndarray, shape (n_features,) or (n_features + 1,)
        Gradient vector

    maxiter : int
        Number of CG iterations.

    tol : float
        Stopping criterion.

    Returns
    -------
    xsupi : ndarray, shape (n_features,) or (n_features + 1,)
        Estimated solution
    t   dtypei    i   i   (
   t   npt   zerost   lenR   t   dott   sumt   abst   finfot   float64t   eps(   t   fhess_pt   fgradt   maxitert   tolt   xsupit   rit   psupit   it   dri0t   Apt   curvt   alphait   dri1t   betai(    (    s5   lib/python2.7/site-packages/sklearn/utils/optimize.pyt   _cg7   s4    /



g-Cëâ6?id   iÈ   c
      
   C   s  t  j | ƒ j ƒ  } | }
 d } | r? | | | Œ } d
 } n  x| | k  r`|  |
 | Œ \ } } t  j | ƒ } t  j | ƒ | k  r‹ Pn  t  j | ƒ } t d t  j | ƒ g ƒ } | | } t	 | | d | d | ƒ} d } | rEy: t
 | | |
 | | | | d | ƒ\ } } } } } } WqEt k
 rAt j d ƒ PqEXn  |
 | | }
 | d 7} qB W|	 r†| | k r†t j d	 t ƒ n  |
 | f S(   sL  
    Minimization of scalar function of one or more variables using the
    Newton-CG algorithm.

    Parameters
    ----------
    grad_hess : callable
        Should return the gradient and a callable returning the matvec product
        of the Hessian.

    func : callable
        Should return the value of the function.

    grad : callable
        Should return the function value and the gradient. This is used
        by the linesearch functions.

    x0 : array of float
        Initial guess.

    args : tuple, optional
        Arguments passed to func_grad_hess, func and grad.

    tol : float
        Stopping criterion. The iteration will stop when
        ``max{|g_i | i = 1, ..., n} <= tol``
        where ``g_i`` is the i-th component of the gradient.

    maxiter : int
        Number of Newton iterations.

    maxinner : int
        Number of CG iterations.

    line_search : boolean
        Whether to use a line search or not.

    warn : boolean
        Whether to warn when didn't converge.

    Returns
    -------
    xk : ndarray of float
        Estimated minimum.
    i    g      à?R   R   g      ð?t   argss   Line Search failedi   s@   newton-cg failed to converge. Increase the number of iterations.N(   R   t   asarrayt   flattenR   R   t   maxR   t   mint   sqrtR)   R   R   t   warningst   warnR   (   t	   grad_hesst   funct   gradt   x0R*   R   R   t   maxinnert   line_searchR1   R	   t   kR   R   R   R   t   absgradt   maggradt   etat   termcondR   t   alphakt   fct   gct   gfkp1(    (    s5   lib/python2.7/site-packages/sklearn/utils/optimize.pyt	   newton_cgr   s:    /	
(	
(    (   t   __doc__t   numpyR   R0   t   scipy.optimize.linesearchR    R   t
   exceptionsR   t   RuntimeErrorR   R   R)   t   TrueRA   (    (    (    s5   lib/python2.7/site-packages/sklearn/utils/optimize.pyt   <module>
   s   		;