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_regression
Validates input and computes the regression line (ŷ) for a givenx
. -
coefficient_determination
Calculates R², showing how well the model fits the data. -
mse
Computes the Mean Squared Error, indicating the average squared difference between actual and predicted values. -
predict_values
Predicts newy
values using the derived regression line.
These helper functions support the core calculations for linear regression:
-
std_deviation
Returns the standard deviation of a numeric vector. -
correlation
Computes the Pearson correlation coefficient between two datasets. -
slope
Calculates the slope (m) of the best-fit line. -
intercept
Determines 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 run
Running 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