rust_ml/builders/
linear_regression.rs

1/// Builder implementation for LinearRegression models.
2///
3/// This module provides a builder pattern implementation for creating LinearRegression
4/// models with customizable configurations. The builder allows for fluent API-style
5/// configuration of model parameters before construction.
6use crate::builders::builder::Builder;
7use crate::core::error::ModelError;
8use crate::core::types::{Matrix, Vector};
9use crate::model::linear_regression::LinearRegression;
10
11/// Builder for creating LinearRegression models with customizable configurations.
12///
13/// The LinearRegressionBuilder provides methods to configure the properties of a
14/// LinearRegression model before it is instantiated, following the Builder design pattern.
15///
16/// # Fields
17///
18/// * `n_x` - The number of input features for the linear regression model
19///
20/// # Examples
21///
22pub struct LinearRegressionBuilder {
23    /// Number of input features (independent variables) for the model
24    n_x: usize,
25}
26
27impl Builder<LinearRegression, Matrix, Vector> for LinearRegressionBuilder {
28    /// Builds and returns a new LinearRegression model with the configured parameters.
29    ///
30    /// # Returns
31    ///
32    /// * `Result<LinearRegression, ModelError>` - A new LinearRegression instance with the
33    ///   specified number of input features, or an error if construction fails
34    fn build(&self) -> Result<LinearRegression, ModelError> {
35        LinearRegression::new(self.n_x)
36    }
37}
38
39impl LinearRegressionBuilder {
40    /// Creates a new LinearRegressionBuilder with default parameter values.
41    ///
42    /// The default number of input features is set to 0 and must be configured
43    /// before building the model.
44    ///
45    /// # Returns
46    ///
47    /// * `Self` - A new LinearRegressionBuilder instance with default settings
48    pub fn new() -> Self {
49        Self { n_x: 0 }
50    }
51
52    /// Sets the number of input features for the linear regression model.
53    ///
54    /// # Arguments
55    ///
56    /// * `n_x` - The number of independent variables (features) in the input data
57    ///
58    /// # Returns
59    ///
60    /// * `&mut Self` - Reference to self for method chaining
61    pub fn n_input_features(&mut self, n_x: usize) -> &mut Self {
62        self.n_x = n_x;
63        self
64    }
65}
66
67impl Default for LinearRegressionBuilder {
68    /// Creates a new LinearRegressionBuilder with default parameter values.
69    ///
70    /// The default number of input features is set to 0 and must be configured
71    /// before building the model.
72    ///
73    /// # Returns
74    ///
75    /// * `Self` - A new LinearRegressionBuilder instance with default settings
76    fn default() -> Self {
77        Self::new()
78    }
79}