§
    ¥˜ºhCz  ã                   óŽ  — d Z ddlmZmZ ddlZddlmZmZ ddl	m
Z
mZ ddlmZ ddlmZ  G d	„ d
e¬¦  «        Z G d„ dee¬¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ dee¬¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZeeeededœZdS )zZLosses and corresponding default initial estimators for gradient boosting
decision trees.
é    )ÚABCMetaÚabstractmethodN)ÚexpitÚ	logsumexpé   )ÚDummyClassifierÚDummyRegressor)Ú	TREE_LEAF)Ú_weighted_percentilec                   ó˜   — e Zd ZdZdZd„ Zed„ ¦   «         Zedd„¦   «         Zed„ ¦   «         Z		 	 dd
„Z
ed„ ¦   «         Zed„ ¦   «         ZdS )ÚLossFunctionaM  Abstract base class for various loss functions.

    Parameters
    ----------
    n_classes : int
        Number of classes.

    Attributes
    ----------
    K : int
        The number of regression trees to be induced;
        1 for regression and binary classification;
        ``n_classes`` for multi-class classification.
    Fc                 ó   — || _         d S ©N)ÚK)ÚselfÚ	n_classess     úc/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/sklearn/ensemble/_gb_losses.pyÚ__init__zLossFunction.__init__!   s   € ØˆŒˆˆó    c                 ó   — dS )z-Default ``init`` estimator for loss function.N© ©r   s    r   Úinit_estimatorzLossFunction.init_estimator$   ó   € € € r   Nc                 ó   — dS )ad  Compute the loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves).

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nr   ©r   ÚyÚraw_predictionsÚsample_weights       r   Ú__call__zLossFunction.__call__(   r   r   c                 ó   — dS )áN  Compute the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        Nr   ©r   r   r   Úkargss       r   Únegative_gradientzLossFunction.negative_gradient8   r   r   çš™™™™™¹?r   c
                 ó~  — |                      |¦  «        }
|
                     ¦   «         }d|| <   t          j        |j        t
          k    ¦  «        d         D ](}|                      |||||||dd…|	f         |¦  «         Œ)|dd…|	fxx         ||j        dd…ddf                              |
d¬¦  «        z  z  cc<   dS )az  Update the terminal regions (=leaves) of the given tree and
        updates the current predictions of the model. Traverses tree
        and invokes template method `_update_terminal_region`.

        Parameters
        ----------
        tree : tree.Tree
            The tree object.
        X : ndarray of shape (n_samples, n_features)
            The data array.
        y : ndarray of shape (n_samples,)
            The target labels.
        residual : ndarray of shape (n_samples,)
            The residuals (usually the negative gradient).
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        sample_weight : ndarray of shape (n_samples,)
            The weight of each sample.
        sample_mask : ndarray of shape (n_samples,)
            The sample mask to be used.
        learning_rate : float, default=0.1
            Learning rate shrinks the contribution of each tree by
             ``learning_rate``.
        k : int, default=0
            The index of the estimator being updated.

        éÿÿÿÿr   N©Úaxis)	ÚapplyÚcopyÚnpÚwhereÚchildren_leftr
   Ú_update_terminal_regionÚvalueÚtake)r   ÚtreeÚXr   Úresidualr   r   Úsample_maskÚlearning_rateÚkÚterminal_regionsÚmasked_terminal_regionsÚleafs                r   Úupdate_terminal_regionsz$LossFunction.update_terminal_regionsF   s  € ðR  Ÿ:š: a™=œ=Ðð #3×"7Ò"7Ñ"9Ô"9ÐØ02Ð  Ñ-õ ”H˜TÔ/µ9Ò<Ñ=Ô=¸aÔ@ð 
	ð 
	ˆDØ×(Ò(ØØ'ØØØØØ    1 Ô%Øñ	ô 	ð 	ð 	ð 	˜˜˜˜1˜ÐÐÔ °´¸A¸A¸A¸qÀ!¸GÔ1D×1IÒ1IØ 1ð 2Jñ 2
ô 2
ñ "
ñ 	
ÐÐÑÐÐr   c	                 ó   — dS )z=Template method for updating terminal regions (i.e., leaves).Nr   ©	r   r3   r9   r;   r4   r   r5   r   r   s	            r   r0   z$LossFunction._update_terminal_region‡   r   r   c                 ó   — dS )aL  Return the initial raw predictions.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features)
            The data array.
        estimator : object
            The estimator to use to compute the predictions.

        Returns
        -------
        raw_predictions : ndarray of shape (n_samples, K)
            The initial raw predictions. K is equal to 1 for binary
            classification and regression, and equal to the number of classes
            for multiclass classification. ``raw_predictions`` is casted
            into float64.
        Nr   )r   r4   Ú	estimators      r   Úget_init_raw_predictionsz%LossFunction.get_init_raw_predictions•   s	   € ð& 	ˆr   r   ©r&   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úis_multi_classr   r   r   r    r%   r<   r0   rA   r   r   r   r   r      sà   € € € € € ðð ð €Nðð ð ð ð<ð <ñ „^ð<ð ðð ð ñ „^ðð ðð ñ „^ðð, Ø
ð?
ð ?
ð ?
ð ?
ðB ðLð Lñ „^ðLð ðð ñ „^ðð ð r   r   )Ú	metaclassc                   ó.   ‡ — e Zd ZdZˆ fd„Zd„ Zd„ Zˆ xZS )ÚRegressionLossFunctionz)Base class for regression loss functions.c                 óL   •— t          ¦   «                              d¬¦  «         d S )Né   ©r   )Úsuperr   )r   Ú	__class__s    €r   r   zRegressionLossFunction.__init__®   s$   ø€ Ý‰Œ×Ò 1ÐÑ%Ô%Ð%Ð%Ð%r   c                 ód   — t          |d¦  «        rt          |d¦  «        st          d¦  «        ‚dS )z±Make sure estimator has the required fit and predict methods.

        Parameters
        ----------
        estimator : object
            The init estimator to check.
        ÚfitÚpredictzNThe init parameter must be a valid estimator and support both fit and predict.N©ÚhasattrÚ
ValueError©r   r@   s     r   Úcheck_init_estimatorz+RegressionLossFunction.check_init_estimator±   sH   € õ ˜	 5Ñ)Ô)ð 	­g°iÀÑ.KÔ.Kð 	Ýð0ñô ð ð	ð 	r   c                 ó’   — |                      |¦  «        }|                     dd¦  «                             t          j        ¦  «        S )Nr(   rL   )rR   ÚreshapeÚastyper-   Úfloat64)r   r4   r@   Úpredictionss       r   rA   z/RegressionLossFunction.get_init_raw_predictions¿   s;   € Ø×'Ò'¨Ñ*Ô*ˆØ×"Ò" 2 qÑ)Ô)×0Ò0µ´Ñ<Ô<Ð<r   )rC   rD   rE   rF   r   rW   rA   Ú__classcell__©rO   s   @r   rJ   rJ   «   s\   ø€ € € € € Ø3Ð3ð&ð &ð &ð &ð &ðð ð ð=ð =ð =ð =ð =ð =ð =r   rJ   c                   ó8   — e Zd ZdZd„ Zd
d„Zd„ Z	 	 dd„Zd	„ ZdS )ÚLeastSquaresErrorzÅLoss function for least squares (LS) estimation.
    Terminal regions do not need to be updated for least squares.

    Parameters
    ----------
    n_classes : int
        Number of classes.
    c                 ó"   — t          d¬¦  «        S )NÚmean©Ústrategy©r	   r   s    r   r   z LeastSquaresError.init_estimatorÎ   s   € Ý vÐ.Ñ.Ô.Ð.r   Nc                 óì   — |€,t          j        ||                     ¦   «         z
  dz  ¦  «        S d|                     ¦   «         z  t          j        |||                     ¦   «         z
  dz  z  ¦  «        z  S )ar  Compute the least squares loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves).

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nr   rL   )r-   rb   ÚravelÚsumr   s       r   r    zLeastSquaresError.__call__Ñ   s|   € ð Ð Ý”7˜A × 5Ò 5Ñ 7Ô 7Ñ7¸AÑ=Ñ>Ô>Ð>ð Ø×#Ò#Ñ%Ô%ñ&å”&˜¨1¨×/DÒ/DÑ/FÔ/FÑ+FÈ1Ñ*LÑMÑNÔNñOðr   c                 ó0   — ||                      ¦   «         z
  S )aT  Compute half of the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples,)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        ©rg   r#   s       r   r%   z#LeastSquaresError.negative_gradientè   s   € ð �?×(Ò(Ñ*Ô*Ñ*Ð*r   r&   r   c
                 ó~   — |dd…|	fxx         ||                      |¦  «                             ¦   «         z  z  cc<   dS )a  Least squares does not need to update terminal regions.

        But it has to update the predictions.

        Parameters
        ----------
        tree : tree.Tree
            The tree object.
        X : ndarray of shape (n_samples, n_features)
            The data array.
        y : ndarray of shape (n_samples,)
            The target labels.
        residual : ndarray of shape (n_samples,)
            The residuals (usually the negative gradient).
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        sample_weight : ndarray of shape (n,)
            The weight of each sample.
        sample_mask : ndarray of shape (n,)
            The sample mask to be used.
        learning_rate : float, default=0.1
            Learning rate shrinks the contribution of each tree by
             ``learning_rate``.
        k : int, default=0
            The index of the estimator being updated.
        N)rR   rg   )
r   r3   r4   r   r5   r   r   r6   r7   r8   s
             r   r<   z)LeastSquaresError.update_terminal_regionsö   sJ   € ðP 	˜˜˜˜1˜ÐÐÔ °·²¸a±´×1FÒ1FÑ1HÔ1HÑ!HÑHÐÐÑÐÐr   c	                 ó   — d S r   r   r>   s	            r   r0   z)LeastSquaresError._update_terminal_region   s	   € ð 	ˆr   r   rB   )	rC   rD   rE   rF   r   r    r%   r<   r0   r   r   r   r`   r`   Ä   s€   € € € € € ðð ð/ð /ð /ðð ð ð ð.+ð +ð +ð. Ø
ð(Ið (Ið (Ið (IðTð ð ð ð r   r`   c                   ó,   — e Zd ZdZd„ Zdd„Zd„ Zd„ ZdS )ÚLeastAbsoluteErrorzŽLoss function for least absolute deviation (LAD) regression.

    Parameters
    ----------
    n_classes : int
        Number of classes
    c                 ó$   — t          dd¬¦  «        S ©NÚquantileç      à?©rd   rq   re   r   s    r   r   z!LeastAbsoluteError.init_estimator7  ó   € Ý z¸CÐ@Ñ@Ô@Ð@r   Nc           	      ó(  — |€;t          j        ||                     ¦   «         z
  ¦  «                             ¦   «         S d|                     ¦   «         z  t          j        |t          j        ||                     ¦   «         z
  ¦  «        z  ¦  «        z  S )at  Compute the least absolute error.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves).

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        NrL   )r-   Úabsrg   rb   rh   r   s       r   r    zLeastAbsoluteError.__call__:  sˆ   € ð Ð Ý”6˜!˜o×3Ò3Ñ5Ô5Ñ5Ñ6Ô6×;Ò;Ñ=Ô=Ð=ð Ø×#Ò#Ñ%Ô%ñ&å”&˜­¬°°O×4IÒ4IÑ4KÔ4KÑ0KÑ)LÔ)LÑLÑMÔMñNðr   c                 óH   — |                      ¦   «         }d||z
  dk    z  dz
  S )a‚  Compute the negative gradient.

        1.0 if y - raw_predictions > 0.0 else -1.0

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        r   r   rL   rj   r#   s       r   r%   z$LeastAbsoluteError.negative_gradientQ  s/   € ð *×/Ò/Ñ1Ô1ˆØ�A˜Ñ'¨!Ò+Ñ,¨qÑ0Ð0r   c	                 ó  — t          j        ||k    ¦  «        d         }	|                     |	d¬¦  «        }|                     |	d¬¦  «        |                     |	d¬¦  «        z
  }
t          |
|d¬¦  «        |j        |ddf<   dS )z1LAD updates terminal regions to median estimates.r   r)   é2   ©Ú
percentileN)r-   r.   r2   r   r1   )r   r3   r9   r;   r4   r   r5   r   r   Úterminal_regionÚdiffs              r   r0   z*LeastAbsoluteError._update_terminal_regionb  s�   € õ œ(Ð#3°tÒ#;Ñ<Ô<¸QÔ?ˆØ%×*Ò*¨?ÀÐ*ÑCÔCˆØ�vŠv�o¨AˆvÑ.Ô.°×1EÒ1EØ !ð 2Fñ 2
ô 2
ñ 
ˆõ "6Ø�-¨Bð"
ñ "
ô "
ˆŒ
�4˜˜A�:ÑÐÐr   r   )rC   rD   rE   rF   r   r    r%   r0   r   r   r   rn   rn   .  sb   € € € € € ðð ðAð Að Aðð ð ð ð.1ð 1ð 1ð"
ð 
ð 
ð 
ð 
r   rn   c                   ó@   ‡ — e Zd ZdZd	ˆ fd„	Zd„ Zd
d„Zd
d„Zd„ Zˆ xZ	S )ÚHuberLossFunctionah  Huber loss function for robust regression.

    M-Regression proposed in Friedman 2001.

    Parameters
    ----------
    alpha : float, default=0.9
        Percentile at which to extract score.

    References
    ----------
    J. Friedman, Greedy Function Approximation: A Gradient Boosting
    Machine, The Annals of Statistics, Vol. 29, No. 5, 2001.
    çÍÌÌÌÌÌì?c                 ód   •— t          ¦   «                              ¦   «          || _        d | _        d S r   )rN   r   ÚalphaÚgamma©r   r‚   rO   s     €r   r   zHuberLossFunction.__init__ˆ  s+   ø€ Ý‰Œ×ÒÑÔÐØˆŒ
ØˆŒ
ˆ
ˆ
r   c                 ó$   — t          dd¬¦  «        S rp   re   r   s    r   r   z HuberLossFunction.init_estimator�  rt   r   Nc                 ó  — |                      ¦   «         }||z
  }| j        }|€]|€0t          j        t          j        |¦  «        | j        dz  ¦  «        }n+t          t          j        |¦  «        || j        dz  ¦  «        }t          j        |¦  «        |k    }|€jt          j        d||         dz  z  ¦  «        }t          j        |t          j        ||          ¦  «        |dz  z
  z  ¦  «        }||z   |j        d         z  }	nƒt          j        d||         z  ||         dz  z  ¦  «        }t          j        |||          z  t          j        ||          ¦  «        |dz  z
  z  ¦  «        }||z   |                     ¦   «         z  }	|	S )a‹  Compute the Huber loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Néd   rr   r   r   )	rg   rƒ   r-   r{   rv   r‚   r   rh   Úshape)
r   r   r   r   r}   rƒ   Ú
gamma_maskÚsq_lossÚlin_lossÚlosss
             r   r    zHuberLossFunction.__call__�  s…  € ð *×/Ò/Ñ1Ô1ˆØ�?Ñ"ˆØ”
ˆØˆ=ØÐ$Ýœ¥b¤f¨T¡l¤l°D´JÀÑ4DÑEÔE��å,Ý”F˜4‘L”L -°´¸cÑ1Añô �õ ”V˜D‘\”\ UÒ*ˆ
ØÐ Ý”f˜S 4¨
Ô#3°qÑ#8Ñ8Ñ9Ô9ˆGÝ”v˜e¥r¤v¨d°J°;Ô.?Ñ'@Ô'@À5È1Á9Ñ'LÑMÑNÔNˆHØ˜hÑ&¨!¬'°!¬*Ñ4ˆDˆDå”f˜S =°Ô#<Ñ<¸tÀJÔ?OÐSTÑ?TÑTÑUÔUˆGÝ”vØØ  Ô,ñ-å”6˜$ 
˜{Ô+Ñ,Ô,¨u°q©yÑ8ñ:ñô ˆHð
 ˜hÑ&¨-×*;Ò*;Ñ*=Ô*=Ñ=ˆDØˆr   c                 óâ  — |                      ¦   «         }||z
  }|€0t          j        t          j        |¦  «        | j        dz  ¦  «        }n+t          t          j        |¦  «        || j        dz  ¦  «        }t          j        |¦  «        |k    }t          j        |j        d         ft          j        ¬¦  «        }||         ||<   |t          j	        ||          ¦  «        z  || <   || _
        |S )a¯  Compute the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nr‡   r   ©Údtype)rg   r-   r{   rv   r‚   r   Úzerosrˆ   r[   Úsignrƒ   )	r   r   r   r   r$   r}   rƒ   r‰   r5   s	            r   r%   z#HuberLossFunction.negative_gradient¹  sÕ   € ð *×/Ò/Ñ1Ô1ˆØ�?Ñ"ˆØÐ Ý”M¥"¤&¨¡,¤,°´
¸SÑ0@ÑAÔAˆEˆEå(­¬°©¬°}ÀdÄjÐSVÑFVÑWÔWˆEÝ”V˜D‘\”\ UÒ*ˆ
Ý”8˜QœW QœZ˜Mµ´Ð<Ñ<Ô<ˆØ# JÔ/ˆ�ÑØ %­¬°°j°[Ô0AÑ(BÔ(BÑ Bˆ�*�ÑØˆŒ
Øˆr   c	           	      óÀ  — t          j        ||k    ¦  «        d         }	|                     |	d¬¦  «        }| j        }
|                     |	d¬¦  «        |                     |	d¬¦  «        z
  }t	          ||d¬¦  «        }||z
  }|t          j        t          j        |¦  «        t          j        t          j        |¦  «        |
¦  «        z  ¦  «        z   |j	        |df<   d S )Nr   r)   ry   rz   )
r-   r.   r2   rƒ   r   rb   r‘   Úminimumrv   r1   )r   r3   r9   r;   r4   r   r5   r   r   r|   rƒ   r}   ÚmedianÚdiff_minus_medians                 r   r0   z)HuberLossFunction._update_terminal_regionÕ  sç   € õ œ(Ð#3°tÒ#;Ñ<Ô<¸QÔ?ˆØ%×*Ò*¨?ÀÐ*ÑCÔCˆØ”
ˆØ�vŠv�o¨AˆvÑ.Ô.°×1EÒ1EØ !ð 2Fñ 2
ô 2
ñ 
ˆõ & d¨MÀbÐIÑIÔIˆØ  6™MÐØ$¥r¤wÝŒGÐ%Ñ&Ô&­¬µB´FÐ;LÑ4MÔ4MÈuÑ)UÔ)UÑUñ(
ô (
ñ 
ˆŒ
�4˜�7ÑÐÐr   ©r€   r   ©
rC   rD   rE   rF   r   r   r    r%   r0   r]   r^   s   @r   r   r   x  s‘   ø€ € € € € ðð ðð ð ð ð ð ð
Að Að Að'ð 'ð 'ð 'ðRð ð ð ð8
ð 
ð 
ð 
ð 
ð 
ð 
r   r   c                   ó>   ‡ — e Zd ZdZd	ˆ fd„	Zd„ Zd
d„Zd„ Zd„ Zˆ xZ	S )ÚQuantileLossFunctionzðLoss function for quantile regression.

    Quantile regression allows to estimate the percentiles
    of the conditional distribution of the target.

    Parameters
    ----------
    alpha : float, default=0.9
        The percentile.
    r€   c                 ój   •— t          ¦   «                              ¦   «          || _        |dz  | _        d S )Nr‡   )rN   r   r‚   r{   r„   s     €r   r   zQuantileLossFunction.__init__ù  s/   ø€ Ý‰Œ×ÒÑÔÐØˆŒ
Ø #™+ˆŒˆˆr   c                 ó.   — t          d| j        ¬¦  «        S )Nrq   rs   )r	   r‚   r   s    r   r   z#QuantileLossFunction.init_estimatorþ  s   € Ý z¸D¼JÐGÑGÔGÐGr   Nc                 óÀ  — |                      ¦   «         }||z
  }| j        }||k    }|€N|||                              ¦   «         z  d|z
  ||                               ¦   «         z  z
  |j        d         z  }ng|t	          j        ||         ||         z  ¦  «        z  d|z
  t	          j        ||          ||          z  ¦  «        z  z
  |                     ¦   «         z  }|S )aŽ  Compute the Quantile loss.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        NrL   r   )rg   r‚   rh   rˆ   r-   )r   r   r   r   r}   r‚   ÚmaskrŒ   s           r   r    zQuantileLossFunction.__call__  sé   € ð *×/Ò/Ñ1Ô1ˆØ�?Ñ"ˆØ”
ˆà�?Ò"ˆØÐ à˜˜Tœ
ŸšÑ(Ô(Ñ(¨A°©I¸¸t¸e¼¿ºÑ9JÔ9JÑ+JÑJØ”˜”
ñˆDˆDð
 �œ˜}¨TÔ2°T¸$´ZÑ?Ñ@Ô@Ñ@Ø�u‘9¥¤ }°d°UÔ';¸dÀDÀ5¼kÑ'IÑ JÔ JÑJñKà×!Ò!Ñ#Ô#ñ$ˆDð ˆr   c                 ób   — | j         }|                     ¦   «         }||k    }||z  d|z
  | z  z
  S )r"   rL   )r‚   rg   )r   r   r   r$   r‚   r�   s         r   r%   z&QuantileLossFunction.negative_gradient   s@   € ð ”
ˆØ)×/Ò/Ñ1Ô1ˆØ�?Ò"ˆØ˜‘ ! e¡)°¨uÑ!4Ñ5Ð5r   c	                 ó  — t          j        ||k    ¦  «        d         }	|                     |	d¬¦  «        |                     |	d¬¦  «        z
  }
|                     |	d¬¦  «        }t          |
|| j        ¦  «        }||j        |df<   d S )Nr   r)   )r-   r.   r2   r   r{   r1   )r   r3   r9   r;   r4   r   r5   r   r   r|   r}   Úvals               r   r0   z,QuantileLossFunction._update_terminal_region1  s˜   € õ œ(Ð#3°tÒ#;Ñ<Ô<¸QÔ?ˆØ�vŠv�o¨AˆvÑ.Ô.°×1EÒ1EØ !ð 2Fñ 2
ô 2
ñ 
ˆð &×*Ò*¨?ÀÐ*ÑCÔCˆå" 4¨¸¼ÑHÔHˆØ!ˆŒ
�4˜�7ÑÐÐr   r–   r   r—   r^   s   @r   r™   r™   í  s‹   ø€ € € € € ð	ð 	ð&ð &ð &ð &ð &ð &ð
Hð Hð Hðð ð ð ð>6ð 6ð 6ð""ð "ð "ð "ð "ð "ð "r   r™   c                   óD   — e Zd ZdZed„ ¦   «         Zed„ ¦   «         Zd„ ZdS )ÚClassificationLossFunctionz-Base class for classification loss functions.c                 ó   — dS )aˆ  Template method to convert raw predictions into probabilities.

        Parameters
        ----------
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        Returns
        -------
        probas : ndarray of shape (n_samples, K)
            The predicted probabilities.
        Nr   ©r   r   s     r   Ú_raw_prediction_to_probaz3ClassificationLossFunction._raw_prediction_to_probaI  r   r   c                 ó   — dS )a�  Template method to convert raw predictions to decisions.

        Parameters
        ----------
        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        Returns
        -------
        encoded_predictions : ndarray of shape (n_samples, K)
            The predicted encoded labels.
        Nr   r¤   s     r   Ú_raw_prediction_to_decisionz6ClassificationLossFunction._raw_prediction_to_decisionY  r   r   c                 ód   — t          |d¦  «        rt          |d¦  «        st          d¦  «        ‚dS )zªMake sure estimator has fit and predict_proba methods.

        Parameters
        ----------
        estimator : object
            The init estimator to check.
        rQ   Úpredict_probazTThe init parameter must be a valid estimator and support both fit and predict_proba.NrS   rV   s     r   rW   z/ClassificationLossFunction.check_init_estimatori  sH   € õ ˜	 5Ñ)Ô)ð 	­g°iÀÑ.QÔ.Qð 	Ýð:ñô ð ð	ð 	r   N)rC   rD   rE   rF   r   r¥   r§   rW   r   r   r   r¢   r¢   F  s]   € € € € € Ø7Ð7àðð ñ „^ðð ðð ñ „^ððð ð ð ð r   r¢   c                   óN   ‡ — e Zd ZdZˆ fd„Zd„ Zdd„Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zˆ xZS )ÚBinomialDeviancea  Binomial deviance loss function for binary classification.

    Binary classification is a special case; here, we only need to
    fit one tree instead of ``n_classes`` trees.

    Parameters
    ----------
    n_classes : int
        Number of classes.
    c                 ó²   •— |dk    r-t          d                     | j        j        |¦  «        ¦  «        ‚t	          ¦   «                              d¬¦  «         d S ©Nr   z-{0:s} requires 2 classes; got {1:d} class(es)rL   rM   ©rU   ÚformatrO   rC   rN   r   ©r   r   rO   s     €r   r   zBinomialDeviance.__init__„  ó^   ø€ Ø˜Š>ˆ>ÝØ?×FÒFØ”NÔ+¨Yñô ñô ð õ 	‰Œ×Ò 1ÐÑ%Ô%Ð%Ð%Ð%r   c                 ó"   — t          d¬¦  «        S ©NÚpriorrc   ©r   r   s    r   r   zBinomialDeviance.init_estimatorŽ  s   € õ ¨Ð0Ñ0Ô0Ð0r   Nc           	      ó  — |                      ¦   «         }|€0dt          j        ||z  t          j        d|¦  «        z
  ¦  «        z  S d|                     ¦   «         z  t          j        |||z  t          j        d|¦  «        z
  z  ¦  «        z  S )a©  Compute the deviance (= 2 * negative log-likelihood).

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Néþÿÿÿr   )rg   r-   rb   Ú	logaddexprh   r   s       r   r    zBinomialDeviance.__call__“  s£   € ð  *×/Ò/Ñ1Ô1ˆØÐ Ø�œØ�_Ñ$­¬°Q¸Ñ(HÔ(HÑHñô ñ ð ð
 Ø×#Ò#Ñ%Ô%ñ&å”&Ø!Ø˜OÑ+­r¬|¸A¸Ñ/OÔ/OÑOñQñô ñðr   c                 óJ   — |t          |                     ¦   «         ¦  «        z
  S )aP  Compute half of the negative gradient.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        )r   rg   r#   s       r   r%   z"BinomialDeviance.negative_gradient²  s#   € ð •5˜×.Ò.Ñ0Ô0Ñ1Ô1Ñ1Ð1r   c	                 ó¤  — t          j        ||k    ¦  «        d         }	|                     |	d¬¦  «        }|                     |	d¬¦  «        }|                     |	d¬¦  «        }t          j        ||z  ¦  «        }
t          j        |||z
  z  d|z
  |z   z  ¦  «        }t	          |¦  «        dk     rd|j        |ddf<   dS |
|z  |j        |ddf<   dS )zÆMake a single Newton-Raphson step.

        our node estimate is given by:

            sum(w * (y - prob)) / sum(w * prob * (1 - prob))

        we take advantage that: y - prob = residual
        r   r)   rL   çu”?jç/Ê ç        N)r-   r.   r2   rh   rv   r1   ©r   r3   r9   r;   r4   r   r5   r   r   r|   Ú	numeratorÚdenominators               r   r0   z(BinomialDeviance._update_terminal_regionÀ  sä   € õ& œ(Ð#3°tÒ#;Ñ<Ô<¸QÔ?ˆØ—=’= °q�=Ñ9Ô9ˆØ�FŠF�?¨ˆFÑ+Ô+ˆØ%×*Ò*¨?ÀÐ*ÑCÔCˆå”F˜=¨8Ñ3Ñ4Ô4ˆ	Ý”f˜]¨a°(©lÑ;¸qÀ1¹uÀxÑ?OÑPÑQÔQˆõ ˆ{ÑÔ˜fÒ$Ð$Ø%(ˆDŒJ�t˜Q �zÑ"Ð"Ð"à%.°Ñ%<ˆDŒJ�t˜Q �zÑ"Ð"Ð"r   c                 óì   — t          j        |j        d         dft           j        ¬¦  «        }t	          |                     ¦   «         ¦  «        |d d …df<   |d d …dfxx         |d d …df         z  cc<   |S )Nr   r   rŽ   rL   ©r-   Úonesrˆ   r[   r   rg   ©r   r   Úprobas      r   r¥   z)BinomialDeviance._raw_prediction_to_probaá  sy   € Ý”˜Ô.¨qÔ1°1Ð5½R¼ZÐHÑHÔHˆÝ˜O×1Ò1Ñ3Ô3Ñ4Ô4ˆˆaˆaˆa�ˆd‰Øˆaˆaˆa�ˆdˆˆŒ�u˜Q˜Q˜Q ˜T”{Ñ"ˆˆ‰Øˆr   c                 óX   — |                       |¦  «        }t          j        |d¬¦  «        S ©NrL   r)   ©r¥   r-   ÚargmaxrÃ   s      r   r§   z,BinomialDeviance._raw_prediction_to_decisionç  ó*   € Ø×-Ò-¨oÑ>Ô>ˆÝŒy˜ QÐ'Ñ'Ô'Ð'r   c                 óV  — |                      |¦  «        }|d d …df         }t          j        t          j        ¦  «        j        }t          j        ||d|z
  ¦  «        }t          j        |d|z
  z  ¦  «        }|                     dd¦  «                             t          j	        ¦  «        S )NrL   r(   ©
r©   r-   ÚfinfoÚfloat32ÚepsÚclipÚlogrY   rZ   r[   ©r   r4   r@   ÚprobasÚproba_pos_classrÎ   r   s          r   rA   z)BinomialDeviance.get_init_raw_predictionsë  s�   € Ø×(Ò(¨Ñ+Ô+ˆØ     A œ,ˆÝŒh•r”zÑ"Ô"Ô&ˆÝœ' /°3¸¸C¹Ñ@Ô@ˆåœ& °A¸Ñ4GÑ!HÑIÔIˆØ×&Ò& r¨1Ñ-Ô-×4Ò4µR´ZÑ@Ô@Ð@r   r   ©rC   rD   rE   rF   r   r   r    r%   r0   r¥   r§   rA   r]   r^   s   @r   r«   r«   x  s¸   ø€ € € € € ð	ð 	ð&ð &ð &ð &ð &ð1ð 1ð 1ð
ð ð ð ð>2ð 2ð 2ð=ð =ð =ðBð ð ð(ð (ð (ðAð Að Að Að Að Að Ar   r«   c                   óT   ‡ — e Zd ZdZdZˆ fd„Zd„ Zdd„Zdd„Zd	„ Z	d
„ Z
d„ Zd„ Zˆ xZS )ÚMultinomialDeviancezïMultinomial deviance loss function for multi-class classification.

    For multi-class classification we need to fit ``n_classes`` trees at
    each stage.

    Parameters
    ----------
    n_classes : int
        Number of classes.
    Tc                 ó®   •— |dk     r,t          d                     | j        j        ¦  «        ¦  «        ‚t	          ¦   «                              |¦  «         d S )Né   z#{0:s} requires more than 2 classes.r®   r°   s     €r   r   zMultinomialDeviance.__init__  sT   ø€ Ø�qŠ=ˆ=ÝØ5×<Ò<¸T¼^Ô=TÑUÔUñô ð õ 	‰Œ×Ò˜Ñ#Ô#Ð#Ð#Ð#r   c                 ó"   — t          d¬¦  «        S r³   rµ   r   s    r   r   z"MultinomialDeviance.init_estimator
  ó   € Ý¨Ð0Ñ0Ô0Ð0r   Nc                 ó2  — t          j        |j        d         | j        ft           j        ¬¦  «        }t          | j        ¦  «        D ]}||k    |dd…|f<   Œt          j        d||z                       d¬¦  «        z  t          |d¬¦  «        z   |¬¦  «        S )a•  Compute the Multinomial deviance.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        r   rŽ   Nr(   rL   r)   )Úweights)	r-   r�   rˆ   r   r[   ÚrangeÚaveragerh   r   )r   r   r   r   ÚYr8   s         r   r    zMultinomialDeviance.__call__  s¢   € õ  ŒH�a”g˜a”j $¤&Ð)µ´Ð<Ñ<Ô<ˆÝ�t”v‘”ð 	ð 	ˆAØ˜1’fˆAˆaˆaˆa�ˆd‰GˆGåŒzØ�!�oÑ%×*Ò*°Ð*Ñ2Ô2Ñ2µY¸ÐUVÐ5WÑ5WÔ5WÑWØ!ð
ñ 
ô 
ð 	
r   r   c           
      óŒ   — |t          j        t          j        |dd…|f         t          |d¬¦  «        z
  ¦  «        ¦  «        z
  S )a¡  Compute negative gradient for the ``k``-th class.

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            The target labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.

        k : int, default=0
            The index of the class.
        NrL   r)   )r-   Ú
nan_to_numÚexpr   )r   r   r   r8   Úkwargss        r   r%   z%MultinomialDeviance.negative_gradient&  sK   € ð •2”=ÝŒF�? 1 1 1 a 4Ô(­9°_È1Ð+MÑ+MÔ+MÑMÑNÔNñ
ô 
ñ 
ð 	
r   c	                 óÎ  — t          j        ||k    ¦  «        d         }	|                     |	d¬¦  «        }|                     |	d¬¦  «        }|                     |	d¬¦  «        }t          j        ||z  ¦  «        }
|
| j        dz
  | j        z  z  }
t          j        |||z
  z  d|z
  |z   z  ¦  «        }t          |¦  «        dk     rd|j        |ddf<   dS |
|z  |j        |ddf<   dS )z"Make a single Newton-Raphson step.r   r)   rL   r»   r¼   N)r-   r.   r2   rh   r   rv   r1   r½   s               r   r0   z+MultinomialDeviance._update_terminal_region9  sû   € õ œ(Ð#3°tÒ#;Ñ<Ô<¸QÔ?ˆØ—=’= °q�=Ñ9Ô9ˆØ�FŠF�?¨ˆFÑ+Ô+ˆØ%×*Ò*¨?ÀÐ*ÑCÔCˆå”F˜=¨8Ñ3Ñ4Ô4ˆ	Ø�d”f˜q‘j D¤FÑ*Ñ*ˆ	å”f˜]¨a°(©lÑ;¸qÀ1¹uÀxÑ?OÑPÑQÔQˆõ ˆ{ÑÔ˜fÒ$Ð$Ø%(ˆDŒJ�t˜Q �zÑ"Ð"Ð"à%.°Ñ%<ˆDŒJ�t˜Q �zÑ"Ð"Ð"r   c           	      óš   — t          j        t          j        |t          |d¬¦  «        d d …t           j        f         z
  ¦  «        ¦  «        S rÆ   )r-   rá   râ   r   Únewaxisr¤   s     r   r¥   z,MultinomialDeviance._raw_prediction_to_probaU  sI   € ÝŒ}ÝŒFØ¥9¨_À1Ð#EÑ#EÔ#EÀaÀaÀaÍÌÀmÔ#TÑUñô ñ
ô 
ð 	
r   c                 óX   — |                       |¦  «        }t          j        |d¬¦  «        S rÆ   rÇ   rÃ   s      r   r§   z/MultinomialDeviance._raw_prediction_to_decision\  rÉ   r   c                 ó
  — |                      |¦  «        }t          j        t          j        ¦  «        j        }t          j        ||d|z
  ¦  «        }t          j        |¦  «                             t          j        ¦  «        }|S )NrL   )	r©   r-   rÌ   rÍ   rÎ   rÏ   rÐ   rZ   r[   )r   r4   r@   rÒ   rÎ   r   s         r   rA   z,MultinomialDeviance.get_init_raw_predictions`  sc   € Ø×(Ò(¨Ñ+Ô+ˆÝŒh•r”zÑ"Ô"Ô&ˆÝ”˜  a¨#¡gÑ.Ô.ˆÝœ& ™.œ.×/Ò/µ´
Ñ;Ô;ˆØÐr   r   )r   )rC   rD   rE   rF   rG   r   r   r    r%   r0   r¥   r§   rA   r]   r^   s   @r   rÖ   rÖ   õ  s¼   ø€ € € € € ð	ð 	ð €Nð$ð $ð $ð $ð $ð1ð 1ð 1ð
ð 
ð 
ð 
ð2
ð 
ð 
ð 
ð&=ð =ð =ð8
ð 
ð 
ð(ð (ð (ðð ð ð ð ð ð r   rÖ   c                   óN   ‡ — e Zd ZdZˆ fd„Zd„ Zdd„Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zˆ xZS )ÚExponentialLossa  Exponential loss function for binary classification.

    Same loss as AdaBoost.

    Parameters
    ----------
    n_classes : int
        Number of classes.

    References
    ----------
    Greg Ridgeway, Generalized Boosted Models: A guide to the gbm package, 2007
    c                 ó²   •— |dk    r-t          d                     | j        j        |¦  «        ¦  «        ‚t	          ¦   «                              d¬¦  «         d S r­   r®   r°   s     €r   r   zExponentialLoss.__init__w  r±   r   c                 ó"   — t          d¬¦  «        S r³   rµ   r   s    r   r   zExponentialLoss.init_estimator�  rÚ   r   Nc                 ó$  — |                      ¦   «         }|€0t          j        t          j        d|z  dz
   |z  ¦  «        ¦  «        S d|                     ¦   «         z  t          j        |t          j        d|z  dz
   |z  ¦  «        z  ¦  «        z  S )a�  Compute the exponential loss

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble.

        sample_weight : ndarray of shape (n_samples,), default=None
            Sample weights.
        Nç       @ç      ð?r   rL   )rg   r-   rb   râ   rh   r   s       r   r    zExponentialLoss.__call__„  s”   € ð *×/Ò/Ñ1Ô1ˆØÐ Ý”7�2œ6 C¨!¡G¨c¡MÐ"2°_Ñ"DÑEÔEÑFÔFÐFð Ø×#Ò#Ñ%Ô%ñ&å”&˜­¬°!°a±%¸!±)°¸Ñ0NÑ)OÔ)OÑOÑPÔPñQðr   c                 ól   — d|z  dz
  }|t          j        | |                     ¦   «         z  ¦  «        z  S )aU  Compute the residual (= negative gradient).

        Parameters
        ----------
        y : ndarray of shape (n_samples,)
            True labels.

        raw_predictions : ndarray of shape (n_samples, K)
            The raw predictions (i.e. values from the tree leaves) of the
            tree ensemble at iteration ``i - 1``.
        rî   rï   )r-   râ   rg   )r   r   r   r$   Úy_s        r   r%   z!ExponentialLoss.negative_gradient�  s9   € ð �1‰W�s‰]ˆØ•B”F˜B˜3 ×!6Ò!6Ñ!8Ô!8Ñ8Ñ9Ô9Ñ9Ð9r   c	                 óú  — t          j        ||k    ¦  «        d         }	|                     |	d¬¦  «        }|                     |	d¬¦  «        }|                     |	d¬¦  «        }d|z  dz
  }
t          j        |
|z  t          j        |
 |z  ¦  «        z  ¦  «        }t          j        |t          j        |
 |z  ¦  «        z  ¦  «        }t          |¦  «        dk     rd|j        |ddf<   d S ||z  |j        |ddf<   d S )Nr   r)   rî   rï   r»   r¼   )r-   r.   r2   rh   râ   rv   r1   )r   r3   r9   r;   r4   r   r5   r   r   r|   rñ   r¾   r¿   s                r   r0   z'ExponentialLoss._update_terminal_region¬  s	  € õ œ(Ð#3°tÒ#;Ñ<Ô<¸QÔ?ˆØ)×.Ò.¨ÀQÐ.ÑGÔGˆØ�FŠF�?¨ˆFÑ+Ô+ˆØ%×*Ò*¨?ÀÐ*ÑCÔCˆà�1‰W�s‰]ˆå”F˜2 Ñ-µ´¸°s¸_Ñ7LÑ0MÔ0MÑMÑNÔNˆ	Ý”f˜]­R¬V°R°C¸/Ñ4IÑ-JÔ-JÑJÑKÔKˆõ ˆ{ÑÔ˜fÒ$Ð$Ø%(ˆDŒJ�t˜Q �zÑ"Ð"Ð"à%.°Ñ%<ˆDŒJ�t˜Q �zÑ"Ð"Ð"r   c                 óò   — t          j        |j        d         dft           j        ¬¦  «        }t	          d|                     ¦   «         z  ¦  «        |d d …df<   |d d …dfxx         |d d …df         z  cc<   |S )Nr   r   rŽ   rî   rL   rÁ   rÃ   s      r   r¥   z(ExponentialLoss._raw_prediction_to_probaÇ  s~   € Ý”˜Ô.¨qÔ1°1Ð5½R¼ZÐHÑHÔHˆÝ˜C /×"7Ò"7Ñ"9Ô"9Ñ9Ñ:Ô:ˆˆaˆaˆa�ˆd‰Øˆaˆaˆa�ˆdˆˆŒ�u˜Q˜Q˜Q ˜T”{Ñ"ˆˆ‰Øˆr   c                 ób   — |                      ¦   «         dk                         t          ¦  «        S )Nr   )rg   rZ   Úintr¤   s     r   r§   z+ExponentialLoss._raw_prediction_to_decisionÍ  s'   € Ø×%Ò%Ñ'Ô'¨1Ò,×4Ò4µSÑ9Ô9Ð9r   c                 ó\  — |                      |¦  «        }|d d …df         }t          j        t          j        ¦  «        j        }t          j        ||d|z
  ¦  «        }dt          j        |d|z
  z  ¦  «        z  }|                     dd¦  «                             t          j	        ¦  «        S )NrL   rr   r(   rË   rÑ   s          r   rA   z(ExponentialLoss.get_init_raw_predictionsÐ  s—   € Ø×(Ò(¨Ñ+Ô+ˆØ     A œ,ˆÝŒh•r”zÑ"Ô"Ô&ˆÝœ' /°3¸¸C¹Ñ@Ô@ˆð ¥¤ ¸!¸oÑ:MÑ'NÑ OÔ OÑOˆØ×&Ò& r¨1Ñ-Ô-×4Ò4µR´ZÑ@Ô@Ð@r   r   rÔ   r^   s   @r   rê   rê   h  s·   ø€ € € € € ðð ð&ð &ð &ð &ð &ð1ð 1ð 1ðð ð ð ð2:ð :ð :ð=ð =ð =ð6ð ð ð:ð :ð :ð	Að 	Að 	Að 	Að 	Að 	Að 	Ar   rê   )Úsquared_errorÚabsolute_errorÚhuberrq   Úlog_lossÚexponential)rF   Úabcr   r   Únumpyr-   Úscipy.specialr   r   Údummyr   r	   Ú
tree._treer
   Úutils.statsr   r   rJ   r`   rn   r   r™   r¢   r«   rÖ   rê   ÚLOSS_FUNCTIONSr   r   r   ú<module>r     sÅ  ððð ð (Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø "Ð "Ð "Ð "Ð "Ð "Ø .Ð .Ð .Ð .Ð .Ð .ðYð Yð Yð Yð Y˜Wð Yñ Yô Yð Yðx=ð =ð =ð =ð =˜\°Wð =ñ =ô =ð =ð2gð gð gð gð gÐ.ñ gô gð gðTG
ð G
ð G
ð G
ð G
Ð/ñ G
ô G
ð G
ðTr
ð r
ð r
ð r
ð r
Ð.ñ r
ô r
ð r
ðjV"ð V"ð V"ð V"ð V"Ð1ñ V"ô V"ð V"ðr/ð /ð /ð /ð / ¸ð /ñ /ô /ð /ðdzAð zAð zAð zAð zAÐ1ñ zAô zAð zAðzpð pð pð pð pÐ4ñ pô pð pðfqAð qAð qAð qAð qAÐ0ñ qAô qAð qAðj 'Ø(ØØ$ØØ"ðð €€€r   