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
impl BayesianLogisticRegression
Sourcepub fn new(prior_precision: f32) -> Self
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);Sourcepub fn with_learning_rate(self, lr: f32) -> Self
pub fn with_learning_rate(self, lr: f32) -> Self
Sets the learning rate for MAP estimation.
Sourcepub fn with_max_iter(self, max_iter: usize) -> Self
pub fn with_max_iter(self, max_iter: usize) -> Self
Sets the maximum number of iterations.
Sourcepub fn with_tolerance(self, tol: f32) -> Self
pub fn with_tolerance(self, tol: f32) -> Self
Sets the convergence tolerance.
Sourcepub fn coefficients_map(&self) -> Option<&[f32]>
pub fn coefficients_map(&self) -> Option<&[f32]>
Returns the MAP estimate of coefficients (available after fitting).
Sourcepub fn posterior_covariance(&self) -> Option<&[Vec<f32>]>
pub fn posterior_covariance(&self) -> Option<&[Vec<f32>]>
Returns the posterior covariance matrix (available after fitting).
Sourcepub fn predict(&self, x_test: &Matrix<f32>) -> Result<Vector<f32>>
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.
Sourcepub fn predict_proba_interval(
&self,
x_test: &Matrix<f32>,
level: f32,
) -> Result<(Vector<f32>, Vector<f32>)>
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
impl Clone for BayesianLogisticRegression
Source§fn clone(&self) -> BayesianLogisticRegression
fn clone(&self) -> BayesianLogisticRegression
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for BayesianLogisticRegression
impl RefUnwindSafe for BayesianLogisticRegression
impl Send for BayesianLogisticRegression
impl Sync for BayesianLogisticRegression
impl Unpin for BayesianLogisticRegression
impl UnsafeUnpin for BayesianLogisticRegression
impl UnwindSafe for BayesianLogisticRegression
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> 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