Skip to main content

BayesianLogisticRegression

Struct BayesianLogisticRegression 

Source
pub struct BayesianLogisticRegression { /* private fields */ }
Expand description

Bayesian Logistic Regression with Laplace approximation.

Uses a Gaussian approximation to the posterior distribution around the MAP (Maximum A Posteriori) estimate.

§Example

use aprender::bayesian::BayesianLogisticRegression;
use aprender::primitives::{Matrix, Vector};

let mut model = BayesianLogisticRegression::new(1.0); // precision = 1.0
model.fit(&x_train, &y_train).expect("fit should succeed with valid data");

let probas = model.predict_proba(&x_test).expect("prediction should succeed after fitting");
let (lower, upper) = model.predict_proba_interval(&x_test, 0.95).expect("interval prediction should succeed after fitting");

Implementations§

Source§

impl BayesianLogisticRegression

Source

pub fn new(prior_precision: f32) -> Self

Creates a new Bayesian Logistic Regression with specified prior precision.

§Arguments
  • prior_precision - Precision λ of the Gaussian prior (λ = 1/σ²)
    • Higher precision = stronger regularization
    • Typical values: 0.1 - 10.0
§Example
use aprender::bayesian::BayesianLogisticRegression;

let model = BayesianLogisticRegression::new(1.0);
Source

pub fn with_learning_rate(self, lr: f32) -> Self

Sets the learning rate for MAP estimation.

Source

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

Sets the maximum number of iterations.

Source

pub fn with_tolerance(self, tol: f32) -> Self

Sets the convergence tolerance.

Source

pub fn coefficients_map(&self) -> Option<&[f32]>

Returns the MAP estimate of coefficients (available after fitting).

Source

pub fn posterior_covariance(&self) -> Option<&[Vec<f32>]>

Returns the posterior covariance matrix (available after fitting).

Source

pub fn fit(&mut self, x: &Matrix<f32>, y: &Vector<f32>) -> Result<()>

Fits the Bayesian Logistic Regression using Laplace approximation.

Finds the MAP estimate β_MAP and computes the Hessian for uncertainty.

§Arguments
  • x - Feature matrix (n × p)
  • y - Binary labels (n), must be 0.0 or 1.0
§Returns

Ok(()) on success, error if dimensions mismatch or convergence fails

Source

pub fn predict_proba(&self, x_test: &Matrix<f32>) -> Result<Vector<f32>>

Predicts class probabilities for test data.

Returns the probability of class 1 for each sample.

§Arguments
  • x_test - Test feature matrix (n_test × p)
§Returns

Vector of probabilities P(y=1 | x)

Source

pub fn predict(&self, x_test: &Matrix<f32>) -> Result<Vector<f32>>

Predicts class labels (0 or 1) for test data.

Uses threshold 0.5: predict 1 if P(y=1|x) >= 0.5, else 0.

Source

pub fn predict_proba_interval( &self, x_test: &Matrix<f32>, level: f32, ) -> Result<(Vector<f32>, Vector<f32>)>

Predicts class probabilities with credible intervals.

Uses the Laplace approximation to compute Bayesian credible intervals for predicted probabilities.

§Arguments
  • x_test - Test feature matrix (n_test × p)
  • level - Confidence level (e.g., 0.95 for 95% credible interval)
§Returns

Tuple of (lower_bounds, upper_bounds) for P(y=1|x)

§Example
let (lower, upper) = model.predict_proba_interval(&x_test, 0.95)?;

Trait Implementations§

Source§

impl Clone for BayesianLogisticRegression

Source§

fn clone(&self) -> BayesianLogisticRegression

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BayesianLogisticRegression

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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