LinearRegression

Struct LinearRegression 

Source
pub struct LinearRegression<T = f64>
where T: Float + Debug + Default + Serialize,
{ pub slope: T, pub intercept: T, pub r_squared: T, pub standard_error: T, pub n: usize, }
Expand description

Linear regression model that fits a line to data points.

Fields§

§slope: T

Slope of the regression line (coefficient of x)

§intercept: T

Y-intercept of the regression line

§r_squared: T

Coefficient of determination (R²) - goodness of fit

§standard_error: T

Standard error of the estimate

§n: usize

Number of data points used for regression

Implementations§

Source§

impl<T> LinearRegression<T>
where T: Float + Debug + Default + NumCast + Serialize + for<'de> Deserialize<'de>,

Source

pub fn new() -> Self

Create a new linear regression model without fitting any data

Source

pub fn fit<U, V>( &mut self, x_values: &[U], y_values: &[V], ) -> Result<(), String>
where U: NumCast + Copy, V: NumCast + Copy,

Fit a linear model to the provided x and y data points

§Arguments
  • x_values - Independent variable values
  • y_values - Dependent variable values (observations)
§Returns
  • Result<(), String> - Ok if successful, Err with message if the inputs are invalid
Source

pub fn predict<U>(&self, x: U) -> T
where U: NumCast + Copy,

Predict y value for a given x using the fitted model

§Arguments
  • x - The x value to predict for
§Returns
  • The predicted y value
Source

pub fn predict_many<U>(&self, x_values: &[U]) -> Vec<T>
where U: NumCast + Copy,

Calculate predictions for multiple x values

§Arguments
  • x_values - Slice of x values to predict for
§Returns
  • Vector of predicted y values
Source

pub fn confidence_interval<U>( &self, x: U, confidence_level: f64, ) -> Option<(T, T)>
where U: NumCast + Copy,

Calculate confidence intervals for the regression line

§Arguments
  • x - The x value to calculate confidence interval for
  • confidence_level - Confidence level (0.95 for 95% confidence)
§Returns
  • Option<(T, T)> - Tuple of (lower_bound, upper_bound) or None if not enough data
Source

pub fn correlation_coefficient(&self) -> T

Get the correlation coefficient (r)

Source

pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Save the model to a file

§Arguments
  • path - Path where to save the model
§Returns
  • Result<(), io::Error> - Ok if successful, Err with IO error if saving fails
Source

pub fn save_binary<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Save the model in binary format

§Arguments
  • path - Path where to save the model
§Returns
  • Result<(), io::Error> - Ok if successful, Err with IO error if saving fails
Source

pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Load a model from a file

§Arguments
  • path - Path to the saved model file
§Returns
  • Result<Self, io::Error> - Loaded model or IO error
Source

pub fn load_binary<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Load a model from a binary file

§Arguments
  • path - Path to the saved model file
§Returns
  • Result<Self, io::Error> - Loaded model or IO error
Source

pub fn to_json(&self) -> Result<String, String>

Save the model to a string in JSON format

§Returns
  • Result<String, String> - JSON string representation or error message
Source

pub fn from_json(json: &str) -> Result<Self, String>

Load a model from a JSON string

§Arguments
  • json - JSON string containing the model data
§Returns
  • Result<Self, String> - Loaded model or error message

Trait Implementations§

Source§

impl<T> Clone for LinearRegression<T>
where T: Float + Debug + Default + Serialize + Clone,

Source§

fn clone(&self) -> LinearRegression<T>

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<T> Debug for LinearRegression<T>
where T: Float + Debug + Default + Serialize + Debug,

Source§

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

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

impl<T> Default for LinearRegression<T>
where T: Float + Debug + Default + NumCast + Serialize + for<'de> Deserialize<'de>,

Source§

fn default() -> Self

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

impl<'de, T> Deserialize<'de> for LinearRegression<T>
where T: Float + Debug + Default + Serialize + Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Serialize for LinearRegression<T>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LinearRegression<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for LinearRegression<T>
where T: RefUnwindSafe,

§

impl<T> Send for LinearRegression<T>
where T: Send,

§

impl<T> Sync for LinearRegression<T>
where T: Sync,

§

impl<T> Unpin for LinearRegression<T>
where T: Unpin,

§

impl<T> UnwindSafe for LinearRegression<T>
where T: 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> 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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,