`L i 0dZddlZddlZddlZddlmZmZddlm Z Gdde Z d dZ d d Z d d Zdd Zy)a 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. N)line_search_wolfe1line_search_wolfe2)ConvergenceWarningc eZdZy)_LineSearchErrorN)__name__ __module__ __qualname__\/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/utils/optimize.pyrrsr rc |dk\} dtjtj|jjz} | r$t dt d| t dt |||||||fi|} | r| ddnd} t d | zd z| d|jd t} |||zg| }tj|| z}||z }tj||k}| r't d tj|d |d||rtjj|d}|||zg| }tjj|d}||k}| rt d|d|d||rd| ddz| ddz|||f} | d;| r t dt|||||||fi|} | r| ddnd} t d| zd z| d t| S)a 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. rz Line Searchz eps=16 * finfo.eps=z try line search wolfe1rznot z wolfe1 line search was successfulargsz2 check loss |improvement| <= eps * |loss_old|:  <=  )ordz1 check sum(|gradient|) < sum(|gradient_old|): z < ?z' last resort: try line search wolfe2z wolfe2 line search was )npfinfoasarraydtypeepsprintrgettupleabsscipylinalgnormrr)ffprimexkpkgfkold_fval old_old_fvalverbosekwargs is_verboserret_not_rfval tiny_lossloss_improvementchecksum_abs_grad_oldgrad sum_abs_grads r_line_search_wolfe12r8sNAJ rxx 8,22377 7C o 'u-. *+ QBX| Vv VCa&.b +e3lBC 1v~zz&%'*b 4 FF8c>* (?'(I5  FF+,-T)AeWF  $||00!0< "r')D)D <<,,Tq,9L #33EG#nC(8'95'CFQJFQJ  1v~  ; < vr2sHl >D  !!fnF"E /%7,F G 1v~  Jr c 6dtjtjjz}tjt ||j }tj|}| }d} tj||} | } |dk\} | |kr@tjtj||kr=| r:td| dtjtj|d|n||} tj|| }d|cxkr|| zkr#nn | rtd| d|d | d |d|| z n|dkr2| dkDr| rtd| d |nk|| |z |zz }| r td nR| |z }|||zz }||| zz }tj||}|| z }| ||zz}||dz| zz} | d z} |} | |kr@| r?| |kDr:td| d z dtjtj||S)aP 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 of shape (n_features,) or (n_features + 1,) Gradient vector. maxiter : int Number of CG iterations. tol : float Stopping criterion. Returns ------- xsupi : ndarray of shape (n_features,) or (n_features + 1,) Estimated solution. r)rrrz Inner CG solver iteration z+ stopped with sum(|residuals|) <= tol: rz2 stopped with tiny_|p| = eps * ||p||^2, eps = z, squared L2 norm ||p||^2 = z curvature <= tiny_|p|: z. stopped with negative curvature, curvature = z; Inner CG solver iteration 0 fell back to steepest descentrz+ Inner CG solver stopped reaching maxiter=z with sum(|residuals|) = ) rrfloat64rzeroslenrcopydotsumr!r)fhess_pfgradmaxitertolr,rxsupiripsupiidri0 psupi_norm2r.Apcurvalphaidri1betais r_cgrOqsY2 rxx #'' 'C HHSZu{{ 3E B CE A 66"b>DKAJ w, 66"&&*  $21#6446FF266":4F3GtC5R  U^vveR   )k) )21#6;;>%@11< >226tC+|dk(r|t|j|}n |j}|jdk7r]|d|d|jd|jd}|||k(r |d|dz }|d z }||d|zz }t j |t d |St) aCheck the OptimizeResult for successful convergence Parameters ---------- solver : str Solver name. Currently only `lbfgs` is supported. result : OptimizeResult Result of the scipy.optimize.minimize function. max_iter : int, default=None Expected maximum number of iterations. extra_warning_msg : str, default=None Extra warning message. Returns ------- n_iter : int Number of iterations. lbfgsrz failed to converge after z iteration(s) (status=z):  zH Increase the number of iterations to improve the convergence (max_iter=z).zr You might also want to scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.htmlr) stacklevel)rTnitstatusmessagerVrWrNotImplementedError)solverresultmax_iterextra_warning_msgn_iter_i warning_msgs r_check_optimize_resultrvKs.  6::x0HzzH ==A (4XJ?!==/>>""& #H(<--5Jb:  % K !,t&777 MM+'9a H O"!r )r)r g-C6?dTTr)NN)__doc__rVnumpyrr"scipy.optimize._linesearchrr exceptionsr RuntimeErrorrr8rOrgrvr r rr~s] " M+ | =>OdV|    ~B:r