Ë
    8Ò^ja‹  ã                   óf   — d Z ddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ d„ Z G d„ d	«      Zy)
zA
Loss functions for linear models with raw_prediction = X @ coef
é    N)Úsparse)Úget_namespaceÚget_namespace_and_deviceÚmove_to)Ú_align_api_if_sparse)Úsafe_sparse_dotÚsquared_normc           
      óú   — | j                   d   }t        j                  | «      r=t        t	        | j
                  t        j                  |df||f¬«      | z  d¬«      «      S |dd…df   | z  }| j
                  |z  S )z/Compute the sandwich product X.T @ diag(W) @ X.r   ©ÚshapeT)Údense_outputN)r   r   Úissparser   r   ÚTÚ	dia_array)ÚXÚWÚ	n_samplesÚWXs       úl/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/sklearn/linear_model/_linear_loss.pyÚsandwich_dotr      sz   € ð —‘˜‘
€IÜ‡��qÔÜ#ÜØ—‘Ü× Ñ  ! Q °	¸9Ð/EÔFÈÑJØ!ôó
ð 	
ð Šq�$ˆw‰Z˜!‰^ˆØ�s‰s�R‰xˆó    c                   ó~   — e Zd ZdZd„ Zdd„Zd„ Zd„ Zd„ Z	 	 	 	 dd„Z		 	 	 	 dd	„Z
	 	 	 	 dd
„Z	 	 	 	 	 	 dd„Z	 dd„Zy)ÚLinearModelLossa>	  General class for loss functions with raw_prediction = X @ coef + intercept.

    Note that raw_prediction is also known as linear predictor.

    The loss is the average of per sample losses and includes a term for L2
    regularization::

        loss = 1 / s_sum * sum_i s_i loss(y_i, X_i @ coef + intercept)
               + 1/2 * l2_reg_strength * ||coef||_2^2

    with sample weights s_i=1 if sample_weight=None and s_sum=sum_i s_i.

    Gradient and hessian, for simplicity without intercept, are::

        gradient = 1 / s_sum * X.T @ loss.gradient + l2_reg_strength * coef
        hessian = 1 / s_sum * X.T @ diag(loss.hessian) @ X
                  + l2_reg_strength * identity

    Conventions:
        if fit_intercept:
            n_dof =  n_features + 1
        else:
            n_dof = n_features

        if base_loss.is_multiclass:
            coef.shape = (n_classes, n_dof) or ravelled (n_classes * n_dof,)
        else:
            coef.shape = (n_dof,)

        The intercept term is at the end of the coef array:
        if base_loss.is_multiclass:
            if coef.shape (n_classes, n_dof):
                intercept = coef[:, -1]
            if coef.shape (n_classes * n_dof,)
                intercept = coef[n_classes * n_features:] = coef[(n_dof-1):]
            intercept.shape = (n_classes,)
        else:
            intercept = coef[-1]

        Shape of gradient follows shape of coef.
        gradient.shape = coef.shape

        But hessian (to make our lives simpler) are always 2-d:
        if base_loss.is_multiclass:
            hessian.shape = (n_classes * n_dof, n_classes * n_dof)
        else:
            hessian.shape = (n_dof, n_dof)

    Note: if coef has shape (n_classes * n_dof,), the classes are expected to be
    contiguous, i.e. the 2d-array can be reconstructed as

        coef.reshape((n_classes, -1), order="F")

    The option order="F" makes coef[:, i] contiguous. This, in turn, makes the
    coefficients without intercept, coef[:, :-1], contiguous and speeds up
    matrix-vector computations.

    Note: If the average loss per sample is wanted instead of the sum of the loss per
    sample, one can simply use a rescaled sample_weight such that
    sum(sample_weight) = 1.

    Parameters
    ----------
    base_loss : instance of class BaseLoss from sklearn._loss.
    fit_intercept : bool
    c                 ó    — || _         || _        y ©N)Ú	base_lossÚfit_intercept)Úselfr   r   s      r   Ú__init__zLinearModelLoss.__init__s   s   € Ø"ˆŒØ*ˆÕr   Nc                 ó
  — |j                   d   }| j                  j                  }| j                  r|dz   }n|}| j                  j                  rt        j                  ||f|d¬«      }|S t        j                  ||¬«      }|S )aâ  Allocate coef of correct shape with zeros.

        Parameters:
        -----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.
        dtype : data-type, default=None
            Overrides the data type of coef. With dtype=None, coef will have the same
            dtype as X.

        Returns
        -------
        coef : ndarray of shape (n_dof,) or (n_classes, n_dof)
            Coefficients of a linear model.
        é   ÚF)r   ÚdtypeÚorder)r   r#   )r   r   Ú	n_classesr   Úis_multiclassÚnpÚzeros)r   r   r#   Ú
n_featuresr%   Ún_dofÚcoefs          r   Úinit_zero_coefzLinearModelLoss.init_zero_coefw   sy   € ð  —W‘W˜Q‘Zˆ
Ø—N‘N×,Ñ,ˆ	Ø×ÒØ ‘N‰EàˆEØ�>‰>×'Ò'Ü—8‘8 9¨eÐ"4¸EÈÔMˆDð ˆô —8‘8 %¨uÔ5ˆDØˆr   c                 ó<  — | j                   j                  s"| j                  r|d   }|dd }||fS d}|}||fS |j                  dk(  r*|j	                  | j                   j
                  dfd¬«      }n|}| j                  r|dd…df   }|dd…dd…f   }||fS d}||fS )a˜  Helper function to get coefficients and intercept.

        Parameters
        ----------
        coef : ndarray of shape (n_dof,), (n_classes, n_dof) or (n_classes * n_dof,)
            Coefficients of a linear model.
            If shape (n_classes * n_dof,), the classes of one feature are contiguous,
            i.e. one reconstructs the 2d-array via
            coef.reshape((n_classes, -1), order="F").

        Returns
        -------
        weights : ndarray of shape (n_features,) or (n_classes, n_features)
            Coefficients without intercept term.
        intercept : float or ndarray of shape (n_classes,)
            Intercept terms.
        éÿÿÿÿNç        r!   r"   ©r$   )r   r&   r   ÚndimÚreshaper%   )r   r+   Ú	interceptÚweightss       r   Úweight_interceptz LinearModelLoss.weight_intercept“   sÍ   € ð$ �~‰~×+Ò+Ø×!Ò!Ø  ™H�	Ø˜s ˜)�ð  ˜	Ð!Ð!ð  �	Ø�ð ˜	Ð!Ð!ð �y‰y˜AŠ~ØŸ,™,¨¯©×(@Ñ(@À"Ð'EÈS˜,ÓQ‘à�Ø×!Ò!Ø#¢A r E™N�	Ø!¢! S b S &™/�ð ˜	Ð!Ð!ð  �	à˜	Ð!Ð!r   c                 ó,  — | j                  |«      \  }}t        |«      \  }}}|j                  ||j                  |¬«      }|j                  ||j                  |¬«      }	| j                  j
                  s	||z  |	z   }
n||j                  z  |	z   }
|||
fS )ai  Helper function to get coefficients, intercept and raw_prediction.

        Parameters
        ----------
        coef : ndarray of shape (n_dof,), (n_classes, n_dof) or (n_classes * n_dof,)
            Coefficients of a linear model.
            If shape (n_classes * n_dof,), the classes of one feature are contiguous,
            i.e. one reconstructs the 2d-array via
            coef.reshape((n_classes, -1), order="F").
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.

        Returns
        -------
        weights : ndarray of shape (n_features,) or (n_classes, n_features)
            Coefficients without intercept term.
        intercept : float or ndarray of shape (n_classes,)
            Intercept terms.
        raw_prediction : ndarray of shape (n_samples,) or             (n_samples, n_classes)
        )r#   Údevice)r5   r   Úasarrayr#   r   r&   r   )r   r+   r   r4   r3   ÚxpÚ_Údevice_Ú
weights_xpÚintercept_xpÚraw_predictions              r   Úweight_intercept_rawz$LinearModelLoss.weight_intercept_rawº   s˜   € ð, "×2Ñ2°4Ó8Ñˆ�Ü1°!Ó4‰ˆˆAˆwð —Z‘Z ¨q¯w©w¸w�ZÓGˆ
Ø—z‘z )°1·7±7À7�zÓKˆØ�~‰~×+Ò+Ø ™^¨lÑ:‰Nð  §¡Ñ-°Ñ<ˆNà˜	 >Ð1Ð1r   c                 ób   — |j                   dk(  r||z  n
t        |«      }t        d|z  |z  «      S )z5Compute L2 penalty term l2_reg_strength/2 *||w||_2^2.r!   g      à?)r1   r	   Úfloat)r   r4   Úl2_reg_strengthÚnorm2_ws       r   Ú
l2_penaltyzLinearModelLoss.l2_penaltyâ   s3   € à'.§|¡|°qÒ'8�'˜GÒ#¼lÈ7Ó>SˆÜ�S˜?Ñ*¨WÑ4Ó5Ð5r   c                 ót  — |j                   d   }|€| j                  ||«      \  }	}
}n| j                  |«      \  }	}
| j                  j	                  ||||¬«      }t        |||«      \  }}|€|n|j                  |«      }t        |j                  |«      |z  «      }|dkD  r|| j                  |	|«      z  }|S )a  Compute the loss as weighted average over point-wise losses.

        Parameters
        ----------
        coef : ndarray of shape (n_dof,), (n_classes, n_dof) or (n_classes * n_dof,)
            Coefficients of a linear model.
            If shape (n_classes * n_dof,), the classes of one feature are contiguous,
            i.e. one reconstructs the 2d-array via
            coef.reshape((n_classes, -1), order="F").
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.
        y : contiguous array of shape (n_samples,)
            Observed, true target values.
        sample_weight : None or contiguous array of shape (n_samples,), default=None
            Sample weights.
        l2_reg_strength : float, default=0.0
            L2 regularization strength
        n_threads : int, default=1
            Number of OpenMP threads to use.
        raw_prediction : C-contiguous array of shape (n_samples,) or array of             shape (n_samples, n_classes)
            Raw prediction values (in link space). If provided, these are used. If
            None, then raw_prediction = X @ coef + intercept is calculated.

        Returns
        -------
        loss : float
            Weighted average of losses per sample, plus penalty.
        r   ©Úy_truer>   Úsample_weightÚ	n_threads)	r   r?   r5   r   Úlossr   ÚsumrA   rD   )r   r+   r   ÚyrH   rB   rI   r>   r   r4   r3   rJ   r9   r:   Úsw_sums                  r   rJ   zLinearModelLoss.lossç   sÎ   € ðN —G‘G˜A‘Jˆ	ØÐ!Ø15×1JÑ1JÈ4ÐQRÓ1SÑ.ˆG�Y¡à!%×!6Ñ!6°tÓ!<ÑˆG�Yà�~‰~×"Ñ"ØØ)Ø'Øð	 #ó 
ˆô ˜a  MÓ2‰ˆˆAØ+Ð3‘¸¿¹ÀÓ9NˆÜ�R—V‘V˜D“\ FÑ*Ó+ˆà˜QÒØ�D—O‘O G¨_Ó=Ñ=ˆDàˆr   c                 ó<  — |j                   | j                  j                  c\  }}	}
|	t        | j                  «      z   }|€| j                  ||«      \  }}}n| j                  |«      \  }}| j                  j                  ||||¬«      \  }}t        |||«      \  }}|€|n|j                  |«      }t        |j                  |«      |z  «      }|| j                  ||«      z  }||z  }| j                  j                  sot        j                  ||j                  ¬«      }|j                   |z  }t#        |t        d¬«      ||z  z   |d|	 | j                  r|j                  |«      |d<   ||fS t        j$                  |
|f|j                  d¬«      }|j                   |z  }t#        |t        d¬«      ||z  z   |dd…d|	…f<   | j                  r*t#        |j                  |d	¬
«      t        d¬«      |dd…df<   |j&                  dk(  r|j)                  d¬«      }||fS )a\  Computes the sum of loss and gradient w.r.t. coef.

        Parameters
        ----------
        coef : ndarray of shape (n_dof,), (n_classes, n_dof) or (n_classes * n_dof,)
            Coefficients of a linear model.
            If shape (n_classes * n_dof,), the classes of one feature are contiguous,
            i.e. one reconstructs the 2d-array via
            coef.reshape((n_classes, -1), order="F").
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.
        y : contiguous array of shape (n_samples,)
            Observed, true target values.
        sample_weight : None or contiguous array of shape (n_samples,), default=None
            Sample weights.
        l2_reg_strength : float, default=0.0
            L2 regularization strength
        n_threads : int, default=1
            Number of OpenMP threads to use.
        raw_prediction : C-contiguous array of shape (n_samples,) or array of             shape (n_samples, n_classes)
            Raw prediction values (in link space). If provided, these are used. If
            None, then raw_prediction = X @ coef + intercept is calculated.

        Returns
        -------
        loss : float
            Weighted average of losses per sample, plus penalty.

        gradient : ndarray of shape coef.shape
             The gradient of the loss.
        NrF   ©r#   Úcpu)r9   r7   r.   r"   ©r#   r$   r   ©Úaxisr!   r0   )r   r   r%   Úintr   r?   r5   Úloss_gradientr   rK   rA   rD   r&   r'   Ú
empty_liker#   r   r   Úemptyr1   Úravel)r   r+   r   rL   rH   rB   rI   r>   r   r)   r%   r*   r4   r3   rJ   Úgrad_pointwiser9   r:   rM   ÚgradÚX_gradÚgrad_Xs                         r   rU   zLinearModelLoss.loss_gradient#  s  € ðT ./¯W©W°d·n±n×6NÑ6NÐ*Ñˆ�J ØœS ×!3Ñ!3Ó4Ñ4ˆàÐ!Ø15×1JÑ1JÈ4ÐQRÓ1SÑ.ˆG�Y¡à!%×!6Ñ!6°tÓ!<ÑˆG�Yà#Ÿ~™~×;Ñ;ØØ)Ø'Øð	  <ó  
Ñˆˆnô ˜a  MÓ2‰ˆˆAØ+Ð3‘¸¿¹ÀÓ9NˆÜ�R—V‘V˜D“\ FÑ*Ó+ˆØ�—‘ ¨Ó9Ñ9ˆà˜&Ñ ˆà�~‰~×+Ò+Ü—=‘= ¨W¯]©]Ô;ˆDØ—S‘S˜>Ñ)ˆFä˜¤2¨eÔ4°ÈÑ7PÑPð ��*Ðð ×!Ò!ØŸ6™6 .Ó1��R‘ð$ �TˆzÐô —8‘8˜Y¨Ð.°g·m±mÈ3ÔOˆDà#×%Ñ%¨Ñ)ˆFä˜¤2¨eÔ4°ÈÑ7PÑPð ’�K�Z�K�Ñ ð ×!Ò!Ü%Ø—F‘F˜>°�FÓ2´rÀ%ô�’Q˜�U‘ð �y‰y˜AŠ~Ø—z‘z¨�zÓ,�à�TˆzÐr   c                 óF  — |j                   | j                  j                  c\  }}	}
|	t        | j                  «      z   }|€| j                  ||«      \  }}}n| j                  |«      \  }}| j                  j                  ||||¬«      }|€|nt        j                  |«      }||z  }| j                  j                  sZt        j                  ||j                  ¬«      }|j                  |z  ||z  z   |d|	 | j                  r|j                  «       |d<   |S t        j                  |
|f|j                  d¬«      }|j                  |z  ||z  z   |dd…d|	…f<   | j                  r|j                  d¬«      |dd…df<   |j                  d	k(  r|j!                  d¬
«      S |S )aõ  Computes the gradient w.r.t. coef.

        Parameters
        ----------
        coef : ndarray of shape (n_dof,), (n_classes, n_dof) or (n_classes * n_dof,)
            Coefficients of a linear model.
            If shape (n_classes * n_dof,), the classes of one feature are contiguous,
            i.e. one reconstructs the 2d-array via
            coef.reshape((n_classes, -1), order="F").
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.
        y : contiguous array of shape (n_samples,)
            Observed, true target values.
        sample_weight : None or contiguous array of shape (n_samples,), default=None
            Sample weights.
        l2_reg_strength : float, default=0.0
            L2 regularization strength
        n_threads : int, default=1
            Number of OpenMP threads to use.
        raw_prediction : C-contiguous array of shape (n_samples,) or array of             shape (n_samples, n_classes)
            Raw prediction values (in link space). If provided, these are used. If
            None, then raw_prediction = X @ coef + intercept is calculated.

        Returns
        -------
        gradient : ndarray of shape coef.shape
             The gradient of the loss.
        NrF   rO   r.   r"   rQ   r   rR   r!   r0   )r   r   r%   rT   r   r?   r5   Úgradientr'   rK   r&   rV   r#   r   rW   r1   rX   )r   r+   r   rL   rH   rB   rI   r>   r   r)   r%   r*   r4   r3   rY   rM   rZ   s                    r   r^   zLinearModelLoss.gradient}  s›  € ðN ./¯W©W°d·n±n×6NÑ6NÐ*Ñˆ�J ØœS ×!3Ñ!3Ó4Ñ4ˆàÐ!Ø15×1JÑ1JÈ4ÐQRÓ1SÑ.ˆG�Y¡à!%×!6Ñ!6°tÓ!<ÑˆG�YàŸ™×0Ñ0ØØ)Ø'Øð	 1ó 
ˆð ,Ð3‘¼¿¹ÀÓ9NˆØ˜&Ñ ˆà�~‰~×+Ò+Ü—=‘= ¨W¯]©]Ô;ˆDØ !§¡ nÑ 4°ÈÑ7PÑ PˆD��*ÐØ×!Ò!Ø)×-Ñ-Ó/��R‘ØˆKä—8‘8˜Y¨Ð.°g·m±mÈ3ÔOˆDà#1×#3Ñ#3°aÑ#7¸/ÈGÑ:SÑ#SˆD’�K�Z�K�Ñ Ø×!Ò!Ø,×0Ñ0°aÐ0Ó8�’Q˜�U‘Ø�y‰y˜AŠ~Ø—z‘z¨�zÓ,Ð,à�r   c
                 ó$  — |j                   | j                  j                  c\  }
}}|t        | j                  «      z   }|	€| j                  ||«      \  }}}	n| j                  |«      \  }}|€|
nt        j                  |«      }|€#t        j                  ||j                  d¬«      }nx|j                   |j                   k7  r&t        d|j                   › d|j                   › d�«      ‚| j                  j                  r!|j                  j                  st        d«      ‚|}|j                  }|€$t        j                   ||f|j                  ¬«      }n~|j                   ||fk7  rt        d	||f› d
|j                   ›d�«      ‚| j                  j                  r7|j                  j"                  s!|j                  j                  st        d«      ‚|}| j                  j                  �s:| j                  j%                  ||	||¬«      \  }}||z  }||z  }t        j&                  |dk  |¬«      dkD  }t        j(                  |«      }|j*                  |z  ||z  z   |d| | j                  r|j                  «       |d<   |r|||fS t-        ||«      |d|…d|…f<   |dkD  rA|j                  j"                  rdnd}|j/                  d|¬«      d||z  |dz   …xx   |z  cc<   | j                  �r |j*                  |z  }||dd…df<   ||ddd…f<   |j                  «       |d<   �nj| j                  j1                  ||	||¬«      \  }}||z  }|j/                  ||fd¬«      }|j*                  |z  ||z  z   |dd…d|…f<   | j                  r|j                  d¬«      |dd…df<   |j2                  dk(  r|j5                  d¬«      }|�||z  }nd|z  }t7        |«      D �]]  }|dd…|f   d|dd…|f   z
  z  |z  }t-        ||«      ||||z  |…|||z  |…f<   | j                  rV|j*                  |z  }|||||z  |…||z  |z   f<   ||||z  |z   |||z  |…f<   |j                  «       |||z  |z   ||z  |z   f<   t7        |dz   |«      D ]°  }|dd…|f    |dd…|f   z  |z  }t-        ||«      ||||z  |…|||z  |…f<   | j                  rV|j*                  |z  }|||||z  |…||z  |z   f<   ||||z  |z   |||z  |…f<   |j                  «       |||z  |z   ||z  |z   f<   ||d|…|d|…f   ||d|…|d|…f<   Œ² �Œ` |dkD  rJ|j                  j"                  rdnd}|j/                  d|¬«      d|dz  |z  |z  ||z  dz   …xx   |z  cc<   d}|||fS )a~  Computes gradient and hessian w.r.t. coef.

        Parameters
        ----------
        coef : ndarray of shape (n_dof,), (n_classes, n_dof) or (n_classes * n_dof,)
            Coefficients of a linear model.
            If shape (n_classes * n_dof,), the classes of one feature are contiguous,
            i.e. one reconstructs the 2d-array via
            coef.reshape((n_classes, -1), order="F").
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.
        y : contiguous array of shape (n_samples,)
            Observed, true target values.
        sample_weight : None or contiguous array of shape (n_samples,), default=None
            Sample weights.
        l2_reg_strength : float, default=0.0
            L2 regularization strength
        n_threads : int, default=1
            Number of OpenMP threads to use.
        gradient_out : None or ndarray of shape coef.shape
            A location into which the gradient is stored. If None, a new array
            might be created.
        hessian_out : None or ndarray of shape (n_dof, n_dof) or             (n_classes * n_dof, n_classes * n_dof)
            A location into which the hessian is stored. If None, a new array
            might be created.
        raw_prediction : C-contiguous array of shape (n_samples,) or array of             shape (n_samples, n_classes)
            Raw prediction values (in link space). If provided, these are used. If
            None, then raw_prediction = X @ coef + intercept is calculated.

        Returns
        -------
        gradient : ndarray of shape coef.shape
             The gradient of the loss.

        hessian : ndarray of shape (n_dof, n_dof) or             (n_classes, n_dof, n_dof, n_classes)
            Hessian matrix.

        hessian_warning : bool
            True if pointwise hessian has more than 25% of its elements non-positive.
        Nr"   rQ   z4gradient_out is required to have shape coef.shape = z; got ú.z"gradient_out must be F-contiguous.rO   z'hessian_out is required to have shape (z); got hessian_out.shape=zhessian_out must be contiguous.rF   r   )r4   g      Ð?r.   ÚCr0   r!   )r.   r.   rR   g      ð?é   F)r   r   r%   rT   r   r?   r5   r'   rK   rV   r#   Ú
ValueErrorr&   ÚflagsÚf_contiguousÚsizerW   Úc_contiguousÚgradient_hessianÚaverageÚabsr   r   r2   Úgradient_probar1   rX   Úrange)r   r+   r   rL   rH   rB   rI   Úgradient_outÚhessian_outr>   r   r)   r%   r*   r4   r3   rM   rZ   ÚnÚhessrY   Úhess_pointwiseÚhessian_warningr$   ÚXhÚprobaÚswÚkÚhÚls                                 r   rh   z LinearModelLoss.gradient_hessianÆ  sô  € ðn ./¯W©W°d·n±n×6NÑ6NÐ*Ñˆ�J ØœS ×!3Ñ!3Ó4Ñ4ˆØÐ!Ø15×1JÑ1JÈ4ÐQRÓ1SÑ.ˆG�Y¡à!%×!6Ñ!6°tÓ!<ÑˆG�YØ+Ð3‘¼¿¹ÀÓ9Nˆð ÐÜ—=‘= ¨W¯]©]À#ÔF‰DØ×Ñ 4§:¡:Ò-ÜØFÀtÇzÁzÀlð SØ#×)Ñ)Ð*¨!ð-óð ð �^‰^×)Ò)°,×2DÑ2D×2QÒ2QÜÐAÓBÐBàˆDà�I‰IˆØÐÜ—8‘8˜Q ˜F¨'¯-©-Ô8‰DØ×Ñ 1 a &Ò(ÜØ9¸!¸Q¸$¸ð @&Ø×$Ñ$Ð& að)óð ð �^‰^×)Ò)Ø×!Ñ!×.Ò.°{×7HÑ7H×7UÒ7UäÐ>Ó?Ð?àˆDà�~‰~×+Ó+Ø-1¯^©^×-LÑ-LØØ-Ø+Ø#ð	 .Mó .Ñ*ˆN˜Nð ˜fÑ$ˆNØ˜fÑ$ˆNô —
‘
˜>¨QÑ.¸ÔFÈÑMð ô  ŸV™V NÓ3ˆNà !§¡ nÑ 4°ÈÑ7PÑ PˆD��*ÐØ×!Ò!Ø)×-Ñ-Ó/��R‘áà˜T ?Ð2Ð2ä-9¸!¸^Ó-LˆD��*�˜k˜z˜kÐ)Ñ*à Ò"ð  $Ÿz™z×6Ò6™¸C�Ø—‘˜R u�Ó-Ð.R°¸eÑ1CÈÐPQÉ	Ð.RÓSØ#ñÓSð ×!Ó!ð —S‘S˜>Ñ)�Ø "��S�b�S˜"�W‘Ø "��R˜˜"˜�W‘Ø-×1Ñ1Ó3��V“ð %)§N¡N×$AÑ$AØØ-Ø+Ø#ð	 %Bó %Ñ!ˆN˜Eð ˜fÑ$ˆNØ—<‘< ¨EÐ 2¸#�<Ó>ˆDØ#1×#3Ñ#3°aÑ#7¸/ÈGÑ:SÑ#SˆD’�K�Z�K�Ñ Ø×!Ò!Ø,×0Ñ0°aÐ0Ó8�’Q˜�U‘Ø�y‰y˜AŠ~Ø—z‘z¨�zÓ,�ðL Ð(Ø" VÑ+‘à˜6‘\�ä˜9Ó%ó ,X�ð š!˜Q˜$‘K 1 uªQ°¨T¡{¡?Ñ3°bÑ8�ô !  AÓ&ð Ø˜	 JÑ.°Ð:Ø˜	 JÑ.°Ð:ð<ñð ×%Ò%àŸ™˜q™�Bð ð Ø˜I¨
Ñ2°YÐ>Ø! JÑ.°Ñ2ð4ñð ð Ø! JÑ.°Ñ2Ø˜I¨
Ñ2°YÐ>ð@ñð
 Ÿ™›ð ˜ ZÑ/°!Ñ3°YÀÑ5KÈaÑ5OÐOÑPô ˜q 1™u iÓ0ò X�Aàšq !˜t™˜ uªQ°¨T¡{Ñ2°RÑ7�Aô % Q¨Ó*ð Ø˜I¨
Ñ2°YÐ>Ø˜I¨
Ñ2°YÐ>ð@ñð ×)Ò)ØŸS™S 1™W˜ð ð Ø 	¨JÑ 6¸ÐBØ%¨
Ñ2°QÑ6ð8ñð ð Ø%¨
Ñ2°QÑ6Ø 	¨JÑ 6¸ÐBðDñð
 ŸE™E›Gð ˜Y¨Ñ3°aÑ7¸ÀZÑ9OÐRSÑ9SÐSÑTð 8<¸A¸L¸y¸LÈ!È,ÈYÈ,Ð<VÑ7W�D˜˜˜I˜ q |¨) |Ð3Ò4ò+Xð/,Xð\  Ò"à#Ÿz™z×6Ò6™¸C�Ø—‘˜R u�Ó-ØS�y !‘| jÑ0°5Ñ8¸YÈÑ=NÐQRÑ=RÐSóà$ñ%ó ð
 $ˆOà�T˜?Ð*Ð*r   c                 óB  ‡ ‡‡‡‡‡‡‡‡‡‡‡‡‡— ‰j                   ‰ j                  j                  c\  }ŠŠ‰t        ‰ j                  «      z   Š‰ j                  ‰‰«      \  Š}}	‰€|nt        j                  ‰«      Š‰ j                  j                  �sJ‰ j                  j                  ||	‰|¬«      \  }
}|
‰z  }
|‰z  }t        j                  ‰‰j                  ¬«      }‰j                  |
z  ‰‰z  z   |d‰ ‰ j                  r|
j                  «       |d<   |j                  «       Št        j                  ‰«      rt        j                  |df||f¬«      ‰z  Šn|dd…t        j                   f   ‰z  Š‰ j                  rMt        j"                  t        j$                  ‰j                  d¬«      «      «      Št        j&                  ‰«      Šˆˆˆˆˆˆˆ fd„}||fS ‰ j                  j)                  ||	‰|¬«      \  }
Š|
‰z  }
t        j*                  ‰‰f‰j                  d	¬
«      }|
j                  ‰z  ‰‰z  z   |dd…d‰…f<   ‰ j                  r|
j                  d¬«      |dd…df<   ˆˆˆˆˆˆˆˆˆ ˆˆfd„}‰j,                  dk(  r|j/                  d	¬«      |fS ||fS )a¡  Computes gradient and hessp (hessian product function) w.r.t. coef.

        Parameters
        ----------
        coef : ndarray of shape (n_dof,), (n_classes, n_dof) or (n_classes * n_dof,)
            Coefficients of a linear model.
            If shape (n_classes * n_dof,), the classes of one feature are contiguous,
            i.e. one reconstructs the 2d-array via
            coef.reshape((n_classes, -1), order="F").
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.
        y : contiguous array of shape (n_samples,)
            Observed, true target values.
        sample_weight : None or contiguous array of shape (n_samples,), default=None
            Sample weights.
        l2_reg_strength : float, default=0.0
            L2 regularization strength
        n_threads : int, default=1
            Number of OpenMP threads to use.

        Returns
        -------
        gradient : ndarray of shape coef.shape
             The gradient of the loss.

        hessp : callable
            Function that takes in a vector input of shape of gradient and
            and returns matrix-vector product with hessian.
        NrF   rO   r.   r   r   rR   c                 ó‚  •— t        j                  | «      }t        j                  ‰«      r‰j                  ‰| d ‰ z  z  |d ‰ n2t         j
                  j                  ‰j                  ‰| d ‰ g«      |d ‰ |d ‰xxx ‰| d ‰ z  z  ccc ‰j                  r(|d ‰xxx | d   ‰z  z  ccc ‰| d ‰ z  ‰| d   z  z   |d<   |S )Nr.   )r'   rV   r   r   r   ÚlinalgÚ	multi_dotr   )	ÚsÚretr   ÚhXÚhX_sumÚhessian_sumrB   r)   r   s	     €€€€€€€r   Úhesspz7LinearModelLoss.gradient_hessian_product.<locals>.hessp  sÐ   ø€ Ü—m‘m AÓ&�Ü—?‘? 1Ô%Ø'(§s¡s¨b°1°[°j°>Ñ.AÑ'B�C˜˜Ñ$ä')§y¡y×':Ñ':¸A¿C¹CÀÀQÀ{È
À^Ð;TÓ'U�C˜˜Ð$Ø�K�ZÓ  O°a¸¸°nÑ$DÑDÓ à×%Ò%Ø˜˜Ó$¨¨"©°©Ñ6Ó$Ø$ q¨¨* ~Ñ5¸ÀaÈÁeÑ8KÑK�C˜‘GØ�
r   r"   rQ   c                 óT  •— | j                  ‰dfd¬«      } ‰j                  r| d d …df   }| d d …d d…f   } nd}‰| j                  z  |z   }|‰
|z  j                  d¬«      d d …t        j
                  f   z  }|‰
z  }‰�|‰d d …t        j
                  f   z  }t	        j                  ‰‰f‰j                  d¬«      }|j                  ‰z  ‰z  ‰| z  z   |d d …d ‰	…f<   ‰j                  r|j                  d¬«      ‰z  |d d …df<   ‰j                  dk(  r|j                  d¬«      S |S )Nr.   r"   r0   r   r!   rR   rQ   )
r2   r   r   rK   r'   ÚnewaxisrW   r#   r1   rX   )r}   Ús_interceptÚtmpÚ	hess_prodr   r+   rB   r%   r*   r)   rt   rH   r   rM   r4   s       €€€€€€€€€€€r   r‚   z7LinearModelLoss.gradient_hessian_product.<locals>.hesspD  s6  ø€ Ø—I‘I˜y¨"˜o°S�IÓ9�Ø×%Ò%Ø"#¢A r E¡(�KØš!˜S˜b˜S˜&™	‘Aà"#�KØ˜!Ÿ#™#‘g Ñ+�Ø˜ ™×(Ñ(¨aÐ(Ó0²´B·J±J°Ñ?Ñ?�Ø�u‘�Ø Ð,Ø˜=ª¬B¯J©J¨Ñ7Ñ7�Cô ŸH™H i°Ð%7¸w¿}¹}ÐTWÔX�	Ø-0¯U©U°Q©Y¸&Ñ,@À?ÐUVÑCVÑ,V�	š!˜[˜j˜[˜.Ñ)Ø×%Ò%Ø'*§w¡w°A w£¸Ñ'?�Iša ˜eÑ$Ø—9‘9 ’>Ø$Ÿ?™?°˜?Ó5Ð5à$Ð$r   r!   r0   )r   r   r%   rT   r   r?   r'   rK   r&   rh   rV   r#   r   r   r   r   r„   Úsqueezer8   Ú
atleast_1drk   rW   r1   rX   )r   r+   r   rL   rH   rB   rI   r   r3   r>   rY   rq   rZ   r‚   r   r€   r�   r%   r*   r)   rt   rM   r4   s   ``` ``        @@@@@@@@@r   Úgradient_hessian_productz(LinearModelLoss.gradient_hessian_productÇ  s‰  ÿý€ ð@ ./¯W©W°d·n±n×6NÑ6NÐ*Ñˆ�J ØœS ×!3Ñ!3Ó4Ñ4ˆØ-1×-FÑ-FÀtÈQÓ-OÑ*ˆ�˜NØ+Ð3‘¼¿¹ÀÓ9Nˆà�~‰~×+Ó+Ø-1¯^©^×-LÑ-LØØ-Ø+Ø#ð	 .Mó .Ñ*ˆN˜Nð ˜fÑ$ˆNØ˜fÑ$ˆNÜ—=‘= ¨W¯]©]Ô;ˆDØ !§¡ nÑ 4°ÈÑ7PÑ PˆD��*ÐØ×!Ò!Ø)×-Ñ-Ó/��R‘ð )×,Ñ,Ó.ˆKÜ�‰˜qÔ!ä×$Ñ$ n°aÐ%8ÀÈIÐ@VÔWØññ ð
 $¢A¤r§z¡z MÑ2°QÑ6�à×!Ò!ô Ÿ™¤B§J¡J¨r¯v©v¸1¨v«~Ó$>Ó?�äŸ™ vÓ.�÷ò ð\ �Uˆ{Ððw %)§N¡N×$AÑ$AØØ-Ø+Ø#ð	 %Bó %Ñ!ˆN˜Eð ˜fÑ$ˆNÜ—8‘8˜Y¨Ð.°g·m±mÈ3ÔOˆDØ#1×#3Ñ#3°aÑ#7¸/ÈGÑ:SÑ#SˆD’�K�Z�K�Ñ Ø×!Ò!Ø,×0Ñ0°aÐ0Ó8�’Q˜�U‘÷.%ö %ð. �y‰y˜AŠ~Ø—z‘z¨�zÓ,¨eÐ3Ð3à�Uˆ{Ðr   r   )Nr/   r!   N)Nr/   r!   NNN)Nr/   r!   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r,   r5   r?   rD   rJ   rU   r^   rh   rŠ   © r   r   r   r   /   sŠ   „ ñAòF+óò8%"òN&2òP6ð ØØØó:ðB ØØØóXð~ ØØØóGð\ ØØØØØó+ðD NOôWr   r   )rŽ   Únumpyr'   Úscipyr   Úsklearn.utils._array_apir   r   r   Úsklearn.utils._sparser   Úsklearn.utils.extmathr   r	   r   r   r�   r   r   ú<module>r•      s5   ðñó Ý ÷ñ õ
 7ß ?ò÷6oò or   