Skip to main content

BayesianRidge

Struct BayesianRidge 

Source
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 (f32 or f64).

Fields§

§max_iter: usize

Maximum number of EM (evidence-maximization) iterations.

§tol: F

Convergence tolerance on sum(|coef_old - coef|) (sklearn tol).

§alpha_1: F

Shape parameter of the Gamma prior over alpha (sklearn alpha_1, default 1e-6).

§alpha_2: F

Inverse-scale (rate) parameter of the Gamma prior over alpha (sklearn alpha_2, default 1e-6).

§lambda_1: F

Shape parameter of the Gamma prior over lambda (sklearn lambda_1, default 1e-6).

§lambda_2: F

Inverse-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: bool

If true, accumulate the log marginal likelihood at each EM iteration into scores_ (sklearn compute_score, default false, _bayes.py:198).

§fit_intercept: bool

Whether to fit an intercept (bias) term.

Implementations§

Source§

impl<F: Float + FromPrimitive> BayesianRidge<F>

Source

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.

Source

pub fn with_max_iter(self, max_iter: usize) -> Self

Set the maximum number of iterations.

Source

pub fn with_tol(self, tol: F) -> Self

Set the convergence tolerance.

Source

pub fn with_alpha_1(self, alpha_1: F) -> Self

Set the Gamma-prior shape parameter alpha_1 over the noise precision.

Source

pub fn with_alpha_2(self, alpha_2: F) -> Self

Set the Gamma-prior rate parameter alpha_2 over the noise precision.

Source

pub fn with_lambda_1(self, lambda_1: F) -> Self

Set the Gamma-prior shape parameter lambda_1 over the weight precision.

Source

pub fn with_lambda_2(self, lambda_2: F) -> Self

Set the Gamma-prior rate parameter lambda_2 over the weight precision.

Source

pub fn with_alpha_init(self, alpha_init: F) -> Self

Set the initial noise precision. None restores the 1/(Var(y)+eps) default.

Source

pub fn with_lambda_init(self, lambda_init: F) -> Self

Set the initial weight precision. None restores the 1.0 default.

Source

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.

Source

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>

Source

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

Trait Implementations§

Source§

impl<F: Clone> Clone for BayesianRidge<F>

Source§

fn clone(&self) -> BayesianRidge<F>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug> Debug for BayesianRidge<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Float + FromPrimitive> Default for BayesianRidge<F>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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>

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

See BayesianRidge::fit_with_sample_weight.

Source§

type Fitted = FittedBayesianRidge<F>

The fitted model type returned by fit.
Source§

type Error = FerroError

The error type returned by fit.
Source§

impl<F> PipelineEstimator<F> for BayesianRidge<F>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V