pub struct BayesianRidge<F> {
pub max_iter: usize,
pub tol: F,
pub alpha_1: F,
pub alpha_2: F,
pub lambda_1: F,
pub lambda_2: F,
pub alpha_init: Option<F>,
pub lambda_init: Option<F>,
pub compute_score: bool,
pub fit_intercept: bool,
}Expand description
Bayesian Ridge Regression with automatic regularization tuning.
Estimates weight precision (lambda) and noise precision (alpha)
iteratively using evidence maximization. The intercept, if requested,
is fit by centering.
§Type Parameters
F: The floating-point type (f32orf64).
Fields§
§max_iter: usizeMaximum number of EM (evidence-maximization) iterations.
tol: FConvergence tolerance on sum(|coef_old - coef|) (sklearn tol).
alpha_1: FShape parameter of the Gamma prior over alpha (sklearn alpha_1,
default 1e-6).
alpha_2: FInverse-scale (rate) parameter of the Gamma prior over alpha
(sklearn alpha_2, default 1e-6).
lambda_1: FShape parameter of the Gamma prior over lambda (sklearn lambda_1,
default 1e-6).
lambda_2: FInverse-scale (rate) parameter of the Gamma prior over lambda
(sklearn lambda_2, default 1e-6).
alpha_init: Option<F>Initial noise precision (alpha). None (the default) means
1 / (Var(y) + eps), matching sklearn’s alpha_init=None. Must be
positive when set.
lambda_init: Option<F>Initial weight precision (lambda). None (the default) means 1.0,
matching sklearn’s lambda_init=None. Must be positive when set.
compute_score: boolIf true, accumulate the log marginal likelihood at each EM iteration
into scores_ (sklearn compute_score, default false,
_bayes.py:198).
fit_intercept: boolWhether to fit an intercept (bias) term.
Implementations§
Source§impl<F: Float + FromPrimitive> BayesianRidge<F>
impl<F: Float + FromPrimitive> BayesianRidge<F>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new BayesianRidge with default settings.
Defaults mirror sklearn.linear_model.BayesianRidge.__init__
(sklearn/linear_model/_bayes.py:187-202): max_iter = 300,
tol = 1e-3, alpha_1 = alpha_2 = lambda_1 = lambda_2 = 1e-6,
alpha_init = None (⇒ 1/(Var(y)+eps) at fit time),
lambda_init = None (⇒ 1.0), fit_intercept = true.
Sourcepub fn with_max_iter(self, max_iter: usize) -> Self
pub fn with_max_iter(self, max_iter: usize) -> Self
Set the maximum number of iterations.
Sourcepub fn with_alpha_1(self, alpha_1: F) -> Self
pub fn with_alpha_1(self, alpha_1: F) -> Self
Set the Gamma-prior shape parameter alpha_1 over the noise precision.
Sourcepub fn with_alpha_2(self, alpha_2: F) -> Self
pub fn with_alpha_2(self, alpha_2: F) -> Self
Set the Gamma-prior rate parameter alpha_2 over the noise precision.
Sourcepub fn with_lambda_1(self, lambda_1: F) -> Self
pub fn with_lambda_1(self, lambda_1: F) -> Self
Set the Gamma-prior shape parameter lambda_1 over the weight precision.
Sourcepub fn with_lambda_2(self, lambda_2: F) -> Self
pub fn with_lambda_2(self, lambda_2: F) -> Self
Set the Gamma-prior rate parameter lambda_2 over the weight precision.
Sourcepub fn with_alpha_init(self, alpha_init: F) -> Self
pub fn with_alpha_init(self, alpha_init: F) -> Self
Set the initial noise precision. None restores the 1/(Var(y)+eps)
default.
Sourcepub fn with_lambda_init(self, lambda_init: F) -> Self
pub fn with_lambda_init(self, lambda_init: F) -> Self
Set the initial weight precision. None restores the 1.0 default.
Sourcepub fn with_compute_score(self, compute_score: bool) -> Self
pub fn with_compute_score(self, compute_score: bool) -> Self
Set whether to compute the log marginal likelihood at each iteration
(sklearn compute_score, _bayes.py:198). When true, the converged
model’s FittedBayesianRidge::scores holds the per-iteration LML
sequence (length n_iter_ + 1); when false it is empty.
Sourcepub fn with_fit_intercept(self, fit_intercept: bool) -> Self
pub fn with_fit_intercept(self, fit_intercept: bool) -> Self
Set whether to fit an intercept term.
Source§impl<F: LinalgFloat + ScalarOperand + FromPrimitive> BayesianRidge<F>
impl<F: LinalgFloat + ScalarOperand + FromPrimitive> BayesianRidge<F>
Sourcepub fn fit_with_sample_weight(
&self,
x: &Array2<F>,
y: &Array1<F>,
sample_weight: Option<&Array1<F>>,
) -> Result<FittedBayesianRidge<F>, FerroError>
pub fn fit_with_sample_weight( &self, x: &Array2<F>, y: &Array1<F>, sample_weight: Option<&Array1<F>>, ) -> Result<FittedBayesianRidge<F>, FerroError>
Fit the Bayesian Ridge model with optional per-sample weights, mirroring
sklearn.linear_model.BayesianRidge.fit(X, y, sample_weight=None)
(sklearn/linear_model/_bayes.py:217-341).
When sample_weight is Some, X and y are rescaled by
sqrt(sample_weight) AFTER centering, exactly as sklearn applies
_rescale_data after _preprocess_data (_bayes.py:246-256); a
weighted least-squares fit is then an ordinary fit on the rescaled data.
Passing None is byte-identical to Fit::fit.
After centering (when fit_intercept), the (thin) SVD X = U S Vᵀ
gives eigen_vals_ = S² (_bayes.py:287-288). Each iteration updates
the posterior mean coef_ (_bayes.py:294, _update_coef_), then the
effective degrees of freedom and the Gamma-prior precision updates
(_bayes.py:305-307):
gamma_ = sum((alpha_ * eigen_vals_) / (lambda_ + alpha_ * eigen_vals_))
lambda_ = (gamma_ + 2*lambda_1) / (sum(coef_²) + 2*lambda_2)
alpha_ = (n_samples - gamma_ + 2*alpha_1) / (rmse_ + 2*alpha_2)converging when sum(|coef_old - coef_|) < tol (_bayes.py:310).
n_iter_ is set to iter_ + 1 (_bayes.py:316); when compute_score
is set, the log marginal likelihood is accumulated per iteration plus
once after the loop (_bayes.py:283/302/330).
§Errors
FerroError::ShapeMismatch— sample count mismatch (yorsample_weight).FerroError::InvalidParameter— non-positivealpha_init/lambda_init.FerroError::InsufficientSamples— fewer than 2 samples.FerroError::NumericalInstability— SVD or numerical failure.
Trait Implementations§
Source§impl<F: Clone> Clone for BayesianRidge<F>
impl<F: Clone> Clone for BayesianRidge<F>
Source§fn clone(&self) -> BayesianRidge<F>
fn clone(&self) -> BayesianRidge<F>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<F: Debug> Debug for BayesianRidge<F>
impl<F: Debug> Debug for BayesianRidge<F>
Source§impl<F: Float + FromPrimitive> Default for BayesianRidge<F>
impl<F: Float + FromPrimitive> Default for BayesianRidge<F>
Source§impl<F: LinalgFloat + ScalarOperand + FromPrimitive> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<F>, Dim<[usize; 1]>>> for BayesianRidge<F>
impl<F: LinalgFloat + ScalarOperand + FromPrimitive> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<F>, Dim<[usize; 1]>>> for BayesianRidge<F>
Source§fn fit(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<FittedBayesianRidge<F>, FerroError>
fn fit( &self, x: &Array2<F>, y: &Array1<F>, ) -> Result<FittedBayesianRidge<F>, FerroError>
Fit the Bayesian Ridge model by MacKay (1992) evidence maximization,
mirroring sklearn.linear_model.BayesianRidge.fit
(sklearn/linear_model/_bayes.py:217-341). This delegates to
BayesianRidge::fit_with_sample_weight with sample_weight = None.
§Errors
Source§type Fitted = FittedBayesianRidge<F>
type Fitted = FittedBayesianRidge<F>
fit.Source§type Error = FerroError
type Error = FerroError
fit.Source§impl<F> PipelineEstimator<F> for BayesianRidge<F>
impl<F> PipelineEstimator<F> for BayesianRidge<F>
Source§fn fit_pipeline(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError>
fn fit_pipeline( &self, x: &Array2<F>, y: &Array1<F>, ) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError>
Fit the model and return it as a boxed pipeline estimator.
§Errors
Propagates any FerroError from fit.
Auto Trait Implementations§
impl<F> Freeze for BayesianRidge<F>where
F: Freeze,
impl<F> RefUnwindSafe for BayesianRidge<F>where
F: RefUnwindSafe,
impl<F> Send for BayesianRidge<F>where
F: Send,
impl<F> Sync for BayesianRidge<F>where
F: Sync,
impl<F> Unpin for BayesianRidge<F>where
F: Unpin,
impl<F> UnsafeUnpin for BayesianRidge<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for BayesianRidge<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more