
    -iXq                         S r SSKJr  SSKJrJr  SSKrSSKJ	r	  SSK
Jr  SSKJrJr  SS	KJr  SS
KJr  SSKJr  SSKJrJr  SSKJrJrJr   " S S\\5      r " S S\\5      rg)z
Various bayesian regression
    )log)IntegralRealN)linalg)pinvh   )RegressorMixin_fit_context)_safe_indexing)Interval)fast_logdet)_check_sample_weightvalidate_data   )LinearModel_preprocess_data_rescale_datac                   $   \ rS rSr% Sr\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/S\" \SSSS9/S\" \SSSS9/S	/S	/S	/S
/S.r\	\
S'   SSSSSSSSSSSSS.S jr\" SS9SS j5       rSS jrS rS rSrg)BayesianRidge   a\  Bayesian ridge regression.

Fit a Bayesian ridge model. See the Notes section for details on this
implementation and the optimization of the regularization parameters
lambda (precision of the weights) and alpha (precision of the noise).

Read more in the :ref:`User Guide <bayesian_regression>`.
For an intuitive visualization of how the sinusoid is approximated by
a polynomial using different pairs of initial values, see
:ref:`sphx_glr_auto_examples_linear_model_plot_bayesian_ridge_curvefit.py`.

Parameters
----------
max_iter : int, default=300
    Maximum number of iterations over the complete dataset before
    stopping independently of any early stopping criterion.

    .. versionchanged:: 1.3

tol : float, default=1e-3
    Stop the algorithm if w has converged.

alpha_1 : float, default=1e-6
    Hyper-parameter : shape parameter for the Gamma distribution prior
    over the alpha parameter.

alpha_2 : float, default=1e-6
    Hyper-parameter : inverse scale parameter (rate parameter) for the
    Gamma distribution prior over the alpha parameter.

lambda_1 : float, default=1e-6
    Hyper-parameter : shape parameter for the Gamma distribution prior
    over the lambda parameter.

lambda_2 : float, default=1e-6
    Hyper-parameter : inverse scale parameter (rate parameter) for the
    Gamma distribution prior over the lambda parameter.

alpha_init : float, default=None
    Initial value for alpha (precision of the noise).
    If not set, alpha_init is 1/Var(y).

    .. versionadded:: 0.22

lambda_init : float, default=None
    Initial value for lambda (precision of the weights).
    If not set, lambda_init is 1.

    .. versionadded:: 0.22

compute_score : bool, default=False
    If True, compute the log marginal likelihood at each iteration of the
    optimization.

fit_intercept : bool, default=True
    Whether to calculate the intercept for this model.
    The intercept is not treated as a probabilistic parameter
    and thus has no associated variance. If set
    to False, no intercept will be used in calculations
    (i.e. data is expected to be centered).

copy_X : bool, default=True
    If True, X will be copied; else, it may be overwritten.

verbose : bool, default=False
    Verbose mode when fitting the model.

Attributes
----------
coef_ : array-like of shape (n_features,)
    Coefficients of the regression model (mean of distribution)

intercept_ : float
    Independent term in decision function. Set to 0.0 if
    `fit_intercept = False`.

alpha_ : float
   Estimated precision of the noise.

lambda_ : float
   Estimated precision of the weights.

sigma_ : array-like of shape (n_features, n_features)
    Estimated variance-covariance matrix of the weights

scores_ : array-like of shape (n_iter_+1,)
    If computed_score is True, value of the log marginal likelihood (to be
    maximized) at each iteration of the optimization. The array starts
    with the value of the log marginal likelihood obtained for the initial
    values of alpha and lambda and ends with the value obtained for the
    estimated alpha and lambda.

n_iter_ : int
    The actual number of iterations to reach the stopping criterion.

X_offset_ : ndarray of shape (n_features,)
    If `fit_intercept=True`, offset subtracted for centering data to a
    zero mean. Set to np.zeros(n_features) otherwise.

X_scale_ : ndarray of shape (n_features,)
    Set to np.ones(n_features).

n_features_in_ : int
    Number of features seen during :term:`fit`.

    .. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
    Names of features seen during :term:`fit`. Defined only when `X`
    has feature names that are all strings.

    .. versionadded:: 1.0

See Also
--------
ARDRegression : Bayesian ARD regression.

Notes
-----
There exist several strategies to perform Bayesian ridge regression. This
implementation is based on the algorithm described in Appendix A of
(Tipping, 2001) where updates of the regularization parameters are done as
suggested in (MacKay, 1992). Note that according to A New
View of Automatic Relevance Determination (Wipf and Nagarajan, 2008) these
update rules do not guarantee that the marginal likelihood is increasing
between two consecutive iterations of the optimization.

References
----------
D. J. C. MacKay, Bayesian Interpolation, Computation and Neural Systems,
Vol. 4, No. 3, 1992.

M. E. Tipping, Sparse Bayesian Learning and the Relevance Vector Machine,
Journal of Machine Learning Research, Vol. 1, 2001.

Examples
--------
>>> from sklearn import linear_model
>>> clf = linear_model.BayesianRidge()
>>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])
BayesianRidge()
>>> clf.predict([[1, 1]])
array([1.])
r   Nleftclosedr   neitherbooleanverbosemax_itertolalpha_1alpha_2lambda_1lambda_2
alpha_initlambda_initcompute_scorefit_interceptcopy_Xr   _parameter_constraints,  MbP?ư>FTc                    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        Xl        Xl	        Xl
        Xl        g Nr   )selfr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r   s                N/var/www/html/venv/lib/python3.13/site-packages/sklearn/linear_model/_bayes.py__init__BayesianRidge.__init__   sC      !  $&**    prefer_skip_nested_validationc                    [        U UU[        R                  [        R                  /SSS9u  pUR                  nUR
                  u  pVUnUR                  5       nUbH  [        X1US9nUR                  5       n[        R                  " X#S9n	[        R                  " X)-
  S-  US9n[        UUU R                  U R                  US9u  ppnUb  [        XU5      u  pnXl        Xl        [        R                   " [        R                  5      R"                  nU R$                  nU R&                  nUc  SX-   -  nUc  Sn[        R(                  " XS9n[        R(                  " UUS9nU R*                  nU R,                  nU R.                  nU R0                  nU R2                  n[5        5       U l        Sn[        R8                  " UR:                  U5      n[<        R>                  " XU:  S	9u  nnn[A        U5      nUS-  n[        RB                  " XdS9nUUS
U& US
U2SS24   n[E        U RF                  5       GH7  nU RI                  XXVUUUUUU5
      u  n n!U RJ                  (       a3  U RM                  UUUUUUU U!5      n"U R6                  RO                  U"5        [        R                  " UU-  UUU-  -   -  5      n#U#SU-  -   [        R                  " U S-  5      SU-  -   -  nUU#-
  SU-  -   U!SU-  -   -  nUS
:w  aZ  [        R                  " [        RP                  " UU -
  5      5      U RR                  :  a  U(       a  [U        S[W        U5      S5          O[        RX                  " U 5      nGM:     WS-   U l-        Xl.        UU l/        U RI                  XXVUUUUUU5
      u  U l0        n!U RJ                  (       aX  U RM                  UUUUUUW U!5      n"U R6                  RO                  U"5        [        Rb                  " U R6                  5      U l        [        R8                  " UR:                  UUU-  U-   SS2[        Rd                  4   -  5      U l3        U Ri                  XU5        U $ )a  Fit the model.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
    Training data.
y : ndarray of shape (n_samples,)
    Target values. Will be cast to X's dtype if necessary.

sample_weight : ndarray of shape (n_samples,), default=None
    Individual weights for each sample.

    .. versionadded:: 0.20
       parameter *sample_weight* support to BayesianRidge.

Returns
-------
self : object
    Returns the instance itself.
T)dtypeforce_writeable	y_numericNr7   )weightsr   )r'   copysample_weight      ?)full_matricesr   zConvergence after z iterationsr   )5r   npfloat64float32r7   shapevarr   sumaverager   r'   r(   r   	X_offset_X_scale_finfoepsr$   r%   asarrayr   r"   r#   r    r!   listscores_dotTr   svdlenzerosranger   _update_coef_r&   _log_marginal_likelihoodappendabsr   printstrr<   n_iter_alpha_lambda_coef_arraynewaxissigma__set_intercept)$r/   Xyr=   r7   	n_samples
n_featuressw_sumy_vary_meanrG   	y_offset_rH   _rJ   r[   r\   r   r"   r#   r    r!   	coef_old_XT_yUSVh_fullKeigen_vals_eigen_vals_fullVhiter_r]   sse_sgamma_s$                                       r0   fitBayesianRidge.fit   s"   , ::rzz* 
  !	$0OM"&&(FZZ9FJJ
q0-HE/?,,'0
,iH $#A-8GA!"  hhrzz"&&"">EK(F?G F0**WE2,,====,,,,v	vvacc1~ 

15KM1gFd((:;*!QqS!V_ 4==)E ,,iT1b+vwKE4 !!11	 ##A& VVVk1g@T6TUVFH,q1AAL1PQGvoG3q7{8JKF zbffRVVI,=%>?$((J.E
MJI= *@ qy --)q"k67

D --	A LL"88DLL1DL ffIIw&?":W"Dam!TT
 	I(;r3   c                     U R                  U5      nU(       d  U$ [        R                  " XR                  5      U-  R	                  SS9n[        R
                  " USU R                  -  -   5      nX54$ )0  Predict using the linear model.

In addition to the mean of the predictive distribution, also its
standard deviation can be returned.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Samples.

return_std : bool, default=False
    Whether to return the standard deviation of posterior prediction.

Returns
-------
y_mean : array-like of shape (n_samples,)
    Mean of predictive distribution of query points.

y_std : array-like of shape (n_samples,)
    Standard deviation of predictive distribution of query points.
r   axisr>   )_decision_functionr@   rN   r`   rE   sqrtr[   )r/   rb   
return_stdrh   sigmas_squared_datay_stds         r0   predictBayesianRidge.predictx  sg    , ((+M#%66![[#9A#="B"B"B"JGG/33DEFE= r3   c                    X4:  aI  [         R                  R                  UR                  XxX-  -   SS2[         R                  4   -  U/5      nOE[         R                  R                  UR                  XhX-  -   SSS24   -  UR                  U/5      n[         R
                  " U[         R                  " X5      -
  S-  5      nX4$ )zUpdate posterior mean and compute corresponding sse (sum of squared errors).

Posterior mean is given by coef_ = scaled_sigma_ * X.T * y where
scaled_sigma_ = (lambda_/alpha_ * np.eye(n_features)
                 + np.dot(X.T, X))^-1
Nr   )r@   r   	multi_dotrO   r_   rE   rN   )r/   rb   rc   rd   re   rl   rm   rs   rq   r[   r\   r]   ru   s                r0   rT   BayesianRidge._update_coef_  s     !II''r7+;;Q

]KKTRE II''a)9947CCQSS!LE vvq266!++12{r3   c	                    U R                   n	U R                  n
U R                  nU R                  nX:  a1  [        R
                  " [        R                  " XeU-  -   5      5      * nOn[        R                  " X&[        R                  " U5      R                  S9nUSU=== XT-  -  sss& [        R
                  " [        R                  " U5      5      * nU[        U5      -  X-  -
  nX[        U5      -  X-  -
  -  nUSU[        U5      -  U[        U5      -  -   XX-  -
  U[        R
                  " US-  5      -  -
  U-   U[        S[        R                  -  5      -  -
  -  -  nU$ )zLog marginal likelihood.r:   N      ?r   )r    r!   r"   r#   r@   rE   r   fullr^   r7   pi)r/   rd   re   rf   
eigen_valsr[   r\   coefsser    r!   r"   r#   logdet_sigmascores                  r0   rU   &BayesianRidge._log_marginal_likelihood  sN    ,,,,====
 !FF266'Z4G*G#HIIL77:bhhw>O>U>UVL)$(;;$FF266,#788L3w<'(*<<3v;&)999W%s6{"#l tQw'( 	
 s1ruu9~%&
 	
 r3   )rG   rH   r[   r    r!   r$   r]   r&   r(   r'   r\   r"   r#   r%   r   rZ   rM   r`   r   r   r.   F)__name__
__module____qualname____firstlineno____doc__r   r   r   r)   dict__annotations__r1   r
   rx   r   rT   rU   __static_attributes__ r3   r0   r   r      s   Od h4?@q$y9:T1d6:;T1d6:;dAtF;<dAtF;<XdAtFCDhtQVDE##+;$D $ : 5] 6]~!<2r3   r   c                      \ rS rSr% Sr\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/S/\" \SSSS9/S/S/S	/S
.r\	\
S'   SSSSSSSSSSSS
.S jr\" SS9S 5       rS rS rSS jrSrg)ARDRegressioni  a  Bayesian ARD regression.

Fit the weights of a regression model, using an ARD prior. The weights of
the regression model are assumed to be in Gaussian distributions.
Also estimate the parameters lambda (precisions of the distributions of the
weights) and alpha (precision of the distribution of the noise).
The estimation is done by an iterative procedures (Evidence Maximization)

Read more in the :ref:`User Guide <bayesian_regression>`.

Parameters
----------
max_iter : int, default=300
    Maximum number of iterations.

    .. versionchanged:: 1.3

tol : float, default=1e-3
    Stop the algorithm if w has converged.

alpha_1 : float, default=1e-6
    Hyper-parameter : shape parameter for the Gamma distribution prior
    over the alpha parameter.

alpha_2 : float, default=1e-6
    Hyper-parameter : inverse scale parameter (rate parameter) for the
    Gamma distribution prior over the alpha parameter.

lambda_1 : float, default=1e-6
    Hyper-parameter : shape parameter for the Gamma distribution prior
    over the lambda parameter.

lambda_2 : float, default=1e-6
    Hyper-parameter : inverse scale parameter (rate parameter) for the
    Gamma distribution prior over the lambda parameter.

compute_score : bool, default=False
    If True, compute the objective function at each step of the model.

threshold_lambda : float, default=10 000
    Threshold for removing (pruning) weights with high precision from
    the computation.

fit_intercept : bool, default=True
    Whether to calculate the intercept for this model. If set
    to false, no intercept will be used in calculations
    (i.e. data is expected to be centered).

copy_X : bool, default=True
    If True, X will be copied; else, it may be overwritten.

verbose : bool, default=False
    Verbose mode when fitting the model.

Attributes
----------
coef_ : array-like of shape (n_features,)
    Coefficients of the regression model (mean of distribution)

alpha_ : float
   estimated precision of the noise.

lambda_ : array-like of shape (n_features,)
   estimated precisions of the weights.

sigma_ : array-like of shape (n_features, n_features)
    estimated variance-covariance matrix of the weights

scores_ : float
    if computed, value of the objective function (to be maximized)

n_iter_ : int
    The actual number of iterations to reach the stopping criterion.

    .. versionadded:: 1.3

intercept_ : float
    Independent term in decision function. Set to 0.0 if
    ``fit_intercept = False``.

X_offset_ : float
    If `fit_intercept=True`, offset subtracted for centering data to a
    zero mean. Set to np.zeros(n_features) otherwise.

X_scale_ : float
    Set to np.ones(n_features).

n_features_in_ : int
    Number of features seen during :term:`fit`.

    .. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
    Names of features seen during :term:`fit`. Defined only when `X`
    has feature names that are all strings.

    .. versionadded:: 1.0

See Also
--------
BayesianRidge : Bayesian ridge regression.

References
----------
D. J. C. MacKay, Bayesian nonlinear modeling for the prediction
competition, ASHRAE Transactions, 1994.

R. Salakhutdinov, Lecture notes on Statistical Machine Learning,
http://www.utstat.toronto.edu/~rsalakhu/sta4273/notes/Lecture2.pdf#page=15
Their beta is our ``self.alpha_``
Their alpha is our ``self.lambda_``
ARD is a little different than the slide: only dimensions/features for
which ``self.lambda_ < self.threshold_lambda`` are kept and the rest are
discarded.

Examples
--------
>>> from sklearn import linear_model
>>> clf = linear_model.ARDRegression()
>>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])
ARDRegression()
>>> clf.predict([[1, 1]])
array([1.])

-   :ref:`sphx_glr_auto_examples_linear_model_plot_ard.py` demonstrates ARD
    Regression.
-   :ref:`sphx_glr_auto_examples_linear_model_plot_lasso_and_elasticnet.py`
    showcases ARD Regression alongside Lasso and Elastic-Net for sparse,
    correlated signals, in the presence of noise.
r   Nr   r   r   r   r   )r   r   r    r!   r"   r#   r&   threshold_lambdar'   r(   r   r)   r*   r+   r,   Fg     @Tc                    Xl         X l        Xl        X0l        X@l        XPl        X`l        Xpl        Xl        Xl	        Xl
        g r.   )r   r   r'   r    r!   r"   r#   r&   r   r(   r   )r/   r   r   r    r!   r"   r#   r&   r   r'   r(   r   s               r0   r1   ARDRegression.__init__f  s>     !*  * 0r3   r4   c           
         [        U UU[        R                  [        R                  /SSSS9u  pUR                  nUR
                  u  pE[        R                  " XSS9n[        XU R                  U R                  S9u  ppxn	Xpl
        Xl        [        R                  " U[        S9n
U R                  nU R                  nU R                   nU R"                  nU R$                  n[        R&                  " [        R                  5      R(                  n[        R*                  " S[        R,                  " U5      U-   -  US9n[        R                  " XSS9n[/        5       U l        SnS nXE:  a  U R2                  OU R4                  n[7        U R8                  5       GH  nU" UUUU
5      nU" XUUU
U5      n[        R:                  " U[        R<                  " X5      -
  S-  5      nSUU
   [        R>                  " U5      -  -
  nUS	U-  -   Xj   S-  S	U-  -   -  UU
'   UUR;                  5       -
  S	U-  -   US	U-  -   -  nUU R@                  :  n
S
Xj) '   U RB                  (       a  U[        RD                  " U5      -  UU-  -
  R;                  5       nUU[E        U5      -  UU-  -
  -  nUS[G        U5      U[E        U5      -  -   [        R:                  " [        RD                  " U5      5      -   -  -  nUSUU-  UUS-  -  R;                  5       -   -  -  nU R0                  RI                  U5        US
:  aR  [        R:                  " [        RJ                  " UU-
  5      5      U RL                  :  a  U(       a  [O        SU-  5          O2[        RP                  " U5      nU
RS                  5       (       a  GM    O   WS-   U l*        U
RS                  5       (       a  U" UUUU
5      nU" XUUU
U5      nO&[        RV                  " / 5      RY                  S
S
5      nX`l-        UU l.        UU l/        UU l0        U Rc                  XxU	5        U $ )a  Fit the model according to the given training data and parameters.

Iterative procedure to maximize the evidence

Parameters
----------
X : array-like of shape (n_samples, n_features)
    Training vector, where `n_samples` is the number of samples and
    `n_features` is the number of features.
y : array-like of shape (n_samples,)
    Target values (integers). Will be cast to X's dtype if necessary.

Returns
-------
self : object
    Fitted estimator.
Tr   )r7   r8   r9   ensure_min_samplesr:   )r'   r<   r>   Nc                 t    U[         R                  R                  XPS S 2U4   R                  U/5      -  X$'   U$ r.   )r@   r   r   rO   )rb   rc   r]   r[   keep_lambdar`   s         r0   update_coeff'ARDRegression.fit.<locals>.update_coeff  s>    !'"))*=*=1k>*,,a0+ "E Lr3   g       @r   r   zConverged after %s iterationsr   )2r   r@   rA   rB   r7   rC   rR   r   r'   r(   rG   rH   onesboolr"   r#   r    r!   r   rI   rJ   rK   rD   rL   rM   _update_sigma_update_sigma_woodburyrS   r   rE   rN   diagr   r&   r   r   rV   rW   r   rX   r<   anyrZ   r^   reshaper]   r[   r`   r\   ra   )r/   rb   rc   r7   rd   re   r]   rG   ri   rH   r   r"   r#   r    r!   r   rJ   r[   r\   rk   r   update_sigmart   r`   ru   rw   rv   s                              r0   rx   ARDRegression.fit  s   & ::rzz*  
  !	1/? 2 20
,iH #  ggj5====,,,,,, hhrzz"&& C266!9s?35A''*2v		 & ,, 	 4==)E!!VWkBF ufk6JE 661rvva//A56D7;/"''&/AAF$*S8^$;#)C(N:$GK   &**,.w>4#PW-CWXF "D$9$99K"#E, !!w/(W2DDIIKWs6{*Wv-===S'#f+-.ffRVVG_-. 
 SFTMWuax-?,D,D,FFGG##A& qyRVVBFF9u+<$=>I9EABI??$$I *L qy??!!VWkBF ufk6JEXXb\))!Q/F
I(;r3   c                    UR                   S   nUS S 2U4   nSX4   R                  SS5      -  n[        [        R                  " XQR
                  S9U-  [        R                  " Xg-  UR                  5      -   5      n[        R                  " XU-  5      n[        R                  " UR                  SS5      UR                  -  U5      * nU[        R                  " UR                   S   5      ==   SX4   -  -  ss'   U$ )Nr   r   r:   r>   )	rC   r   r   r@   eyer7   rN   rO   diag_indices)	r/   rb   r[   r\   r   rd   X_keep
inv_lambdar`   s	            r0   r   $ARDRegression._update_sigma_woodbury  s     GGAJ	1k>"-55a<<
FF9GG,v5ffV(&((34
  34&&++B2VXX=vFFrv||A/0C':N4NN0r3   c                     US S 2U4   n[         R                  " UR                  U5      n[         R                  " UR                  S   UR
                  S9nX4   U-  X&-  -   n[        U5      n	U	$ )Nr   r:   )r@   rN   rO   r   rC   r7   r   )
r/   rb   r[   r\   r   r   gramr   	sigma_invr`   s
             r0   r   ARDRegression._update_sigma  se     1k>"vvfhh'ffTZZ]!''2(3.>	y!r3   c                 *   U R                  U5      nUSL a  U$ U R                  U R                  :  n[        XSS9n[        R
                  " XR                  5      U-  R                  SS9n[        R                  " USU R                  -  -   5      nX64$ )r{   Fr   )indicesr}   r|   r>   )
r~   r\   r   r   r@   rN   r`   rE   r   r[   )r/   rb   r   rh   	col_indexr   r   s          r0   r   ARDRegression.predict  s    , ((+Mt'<'<<Iq!<A#%66![[#9A#="B"B"B"JGG/33DEFE= r3   )rG   rH   r[   r    r!   r]   r&   r(   r'   r\   r"   r#   r   rZ   rM   r`   r   r   r   r   )r   r   r   r   r   r   r   r   r)   r   r   r1   r
   rx   r   r   r   r   r   r3   r0   r   r     s    AH h4?@q$v67T1d6:;T1d6:;dAtF;<dAtF;<#%dAtFCD#+;$D " 6 5{ 6{z$	!r3   r   )r   mathr   numbersr   r   numpyr@   scipyr   scipy.linalgr   baser	   r
   utilsr   utils._param_validationr   utils.extmathr   utils.validationr   r   _baser   r   r   r   r   r   r3   r0   <module>r      sQ     "    / " . ' B ? ?sNK stf!NK f!r3   