This project showcases how to implement Linear Regression, including key statistical metrics such as the Coefficient of Determination (Rยฒ) and Mean Squared Error (MSE), entirely in the Rust programming languageโwithout using any external crates.
Although the example dataset represents monthly water consumption based on the number of family members, this implementation is general-purpose and can be applied to any dataset where the data can be modeled as x and y values.
These are the primary functions that power the linear regression analysis:
-
linear_regressionValidates input and computes the regression line (ลท) for a givenx. -
coefficient_determinationCalculates Rยฒ, showing how well the model fits the data. -
mseComputes the Mean Squared Error, indicating the average squared difference between actual and predicted values. -
predict_valuesPredicts newyvalues using the derived regression line.
These helper functions support the core calculations for linear regression:
-
std_deviationReturns the standard deviation of a numeric vector. -
correlationComputes the Pearson correlation coefficient between two datasets. -
slopeCalculates the slope (m) of the best-fit line. -
interceptDetermines the y-intercept (b) of the regression line.
- Rust (installed and configured)
- Clone this repository to your local machine.
- Navigate into the project directory.
- Run the program with the command:
cargo runRunning cargo run will produce output similar to this:
๐ Linear Regression Analysis
------------------------------------------------------------
Input (Family Members): [4.0, 7.0, 8.0, 3.0, 11.0]
Input (Water Consumption): [13.0, 21.0, 24.5, 11.0, 34.7] mยณ
------------------------------------------------------------
==================== Regression Summary ====================
Rยฒ (Coefficient of Determination): 0.9927
โ 99.27% of water consumption is explained by family size.
MSE (Mean Squared Error): 0.5337
โ Indicates difference between observed and predicted values.
============================================================
================== Predicted Consumption ===================
Family Size | Predicted Monthly Water Consumption (mยณ)
------------------------------------------------------------
2 | 7.22
5 | 16.10
13 | 39.79
9 | 27.95
15 | 45.71
============================================================
Automated tests are included to ensure correct handling of input and core logic.
To run the tests, use:
cargo test