`L i$dZddlmZmZddlmZddlZddlm Z m Z ddl m Z ddl mZeGd d Zej fd ZGd d eZGddeZGddeZGddeZGddeZGddeZeeeeedZy)zM Module contains classes for invertible (and differentiable) link functions. )ABCabstractmethod) dataclassN)expitlogit)gmean)softmaxcBeZdZUeed<eed<eed<eed<dZdZy)Intervallowhigh low_inclusivehigh_inclusivec|j|jkDr&td|jd|jdy)zCheck that low <= highz#One must have low <= high; got low=z, high=.N)r r ValueError)selfs X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/_loss/link.py __post_init__zInterval.__post_init__s? 88dii 5dhhZwtyykQRS  c|jr!tj||j}n tj||j}tj |sy|j r!tj||j}n tj||j}ttj |S)zTest whether all values of x are in interval range. Parameters ---------- x : ndarray Array whose elements are tested to be in interval range. Returns ------- result : bool F) rnp greater_equalr greaterallr less_equalrlessbool)rxr rs rincludeszInterval.includes s   ""1dhh/C**Q)Cvvc{   ==DII.D771dii(DBFF4L!!rN)__name__ __module__ __qualname__float__annotations__rrr!rrr r s" J K"rr cdtj|jz}|jtj k(rd}n:|jdkr|jd|z z|z}n|jd|zz|z}|j tjk(rd}||fS|j dkr|j d|zz|z }||fS|j d|z z|z }||fS)zGenerate values low and high to be within the interval range. This is used in tests only. Returns ------- low, high : tuple The returned values low and high lie within the interval. g _rg _B)rfinfoepsr infr)intervaldtyper,r rs r_inclusive_low_highr0=s rxx"" "C||w  lla#g&,lla#g&,}} 9  }}C(3. 9}}C(3. 9rcxeZdZdZdZeej ejddZe ddZ e ddZ y)BaseLinkaAbstract base class for differentiable, invertible link functions. Convention: - link function g: raw_prediction = g(y_pred) - inverse link h: y_pred = h(raw_prediction) For (generalized) linear models, `raw_prediction = X @ coef` is the so called linear predictor, and `y_pred = h(raw_prediction)` is the predicted conditional (on X) expected value of the target `y_true`. The methods are not implemented as staticmethods in case a link function needs parameters. FNcy)aXCompute the link function g(y_pred). The link function maps (predicted) target values to raw predictions, i.e. `g(y_pred) = raw_prediction`. Parameters ---------- y_pred : array Predicted target values. out : array A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. Returns ------- out : array Output array, element-wise link function. Nr'ry_predouts rlinkz BaseLink.linkorcy)aCompute the inverse link function h(raw_prediction). The inverse link function maps raw predictions to predicted target values, i.e. `h(raw_prediction) = y_pred`. Parameters ---------- raw_prediction : array Raw prediction values (in link space). out : array A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. Returns ------- out : array Output array, element-wise inverse link function. Nr'rraw_predictionr6s rinversezBaseLink.inverser8rN) r"r#r$__doc__ is_multiclassr rr-interval_y_predrr7r<r'rrr2r2YsP M wu=O  *  rr2ceZdZdZddZeZy) IdentityLinkz"The identity link function g(x)=x.Nc:|tj|||S|Sr=)rcopytor4s rr7zIdentityLink.links ? IIc6 "JMrr=)r"r#r$r>r7r<r'rrrBrBs,GrrBcJeZdZdZedej ddZddZddZ y)LogLinkz"The log link function g(x)=log(x).rFNc0tj||SNr6)rlogr4s rr7z LogLink.linksvvf#&&rc0tj||SrH)rexpr:s rr<zLogLink.inversesvvn#..rr=) r"r#r$r>r rr-r@r7r<r'rrrFrFs#,q"&&%7O'/rrFc6eZdZdZeddddZddZddZy) LogitLinkz&The logit link function g(x)=logit(x).rr*FNct||SrHrr4s rr7zLogitLink.linksV%%rct||SrHrr:s rr<zLogitLink.inverses^--rr=r"r#r$r>r r@r7r<r'rrrNrNs0q!UE2O&.rrNc6eZdZdZeddddZddZddZy) HalfLogitLinkzZHalf the logit link function g(x)=1/2 * logit(x). Used for the exponential loss. rr*FNc*t||}|dz}|S)NrIg?rPr4s rr7zHalfLogitLink.linksF$ s  rc td|z|S)Nr rRr:s rr<zHalfLogitLink.inversesQ'--rr=rSr'rrrUrUs# q!UE2O .rrUc@eZdZdZdZeddddZdZd dZd d Z y) MultinomialLogitaThe symmetric multinomial logit function. Convention: - y_pred.shape = raw_prediction.shape = (n_samples, n_classes) Notes: - The inverse link h is the softmax function. - The sum is over the second axis, i.e. axis=1 (n_classes). We have to choose additional constraints in order to make y_pred[k] = exp(raw_pred[k]) / sum(exp(raw_pred[k]), k=0..n_classes-1) for n_classes classes identifiable and invertible. We choose the symmetric side constraint where the geometric mean response is set as reference category, see [2]: The symmetric multinomial logit link function for a single data point is then defined as raw_prediction[k] = g(y_pred[k]) = log(y_pred[k]/gmean(y_pred)) = log(y_pred[k]) - mean(log(y_pred)). Note that this is equivalent to the definition in [1] and implies mean centered raw predictions: sum(raw_prediction[k], k=0..n_classes-1) = 0. For linear models with raw_prediction = X @ coef, this corresponds to sum(coef[k], k=0..n_classes-1) = 0, i.e. the sum over classes for every feature is zero. Reference --------- .. [1] Friedman, Jerome; Hastie, Trevor; Tibshirani, Robert. "Additive logistic regression: a statistical view of boosting" Ann. Statist. 28 (2000), no. 2, 337--407. doi:10.1214/aos/1016218223. https://projecteuclid.org/euclid.aos/1016218223 .. [2] Zahid, Faisal Maqbool and Gerhard Tutz. "Ridge estimation for multinomial logit models with symmetric side constraints." Computational Statistics 28 (2013): 1017-1034. http://epub.ub.uni-muenchen.de/11001/1/tr067.pdf Trr*Fc`|tj|dddtjfz S)Nr*axis)rmeannewaxis)rr;s rsymmetrize_raw_predictionz*MultinomialLogit.symmetrize_raw_predictions%Q ?2:: NNNrNczt|d}tj||ddtjfz |S)Nr*r[rI)rrrJr^)rr5r6gms rr7zMultinomialLogit.links1 6 "vvfr!RZZ-00c::rcj| t|dStj||t|d|S)NT)copyF)r rrDr:s rr<zMultinomialLogit.inverse s1 ;>5 5 IIc> * Ce $Jrr=) r"r#r$r>r?r r@r_r7r<r'rrrYrYs/+ZMq!UE2OO; rrY)identityrJr half_logitmultinomial_logit)r>abcrr dataclassesrnumpyr scipy.specialrr scipy.statsr utils.extmathr r float64r0r2rBrFrNrUrY_LINKSr'rrros$!&# '"'" '"T)+ 8@ s@ F 8  /h / . ..H."?x?F  )  r