RobustFit is a .NET library for ordinary least squares (OLS) and robust regression analysis, designed to handle datasets that contain outliers and noise. It implements iteratively reweighted least squares (IRLS) methods with various robust loss functions.
____ _ _ _____ _ _
| _ \ ___ | |__ _ _ ___ | |_ | ___| (_) | |_
| |_) | / _ \ | '_ \ | | | | / __| | __| | |_ | | | __|
| _ < | (_) | | |_) | | |_| | \__ \ | |_ | _| | | | |_
|_| \_\ \___/ |_.__/ \__,_| |___/ \__| |_| |_| \__|
- RobustFit
- Ordinary Least Squares (OLS) regression for standard linear models
- Robust regression with configurable loss functions:
- Huber loss - balances efficiency and resistance to outliers
- Tukey (bisquare) loss - completely nullifies the effect of extreme outliers
- Support for custom loss functions through the
ILossFunction
interface
- Iteratively Reweighted Least Squares (IRLS) implementation for robust fitting
- Consistent API for fitting models and making predictions
- Support for single- and multi-feature datasets
- Comprehensive linear algebra utilities
- Full test coverage with MSTest
Install-Package RobustFit.Core
dotnet add package RobustFit.Core
<PackageReference Include="RobustFit.Core" Version="1.0.0" />
RobustFit/
├── artifacts/ # NuGet packages
├── samples/ # Example applications
│ └── RobustFit.Demo/ # Console demo application
├── src/ # Source code
│ └── RobustFit.Core/ # Core library
│ ├── LossFunctions/ # Loss function implementations
│ ├── Models/ # Regression model implementations
│ └── Utils/ # Helper functions and utilities
└── tests/ # Test projects
└── RobustFit.UnitTests/ # Unit and integration tests
For detailed information about each component:
- .NET 8.0 SDK or later
- Visual Studio 2022, VS Code, or any .NET-compatible IDE
Build the entire solution:
# From the root directory
dotnet build
# Build specific project
dotnet build src/RobustFit.Core
# From the src/RobustFit.Core directory
dotnet pack -c Release -o ../../artifacts
Alternatively, use the provided build script (for Linux Only):
./build.sh
# Run all tests
dotnet test
# Run tests with coverage
dotnet test --collect:"XPlat Code Coverage"
# Generate coverage report (requires ReportGenerator tool)
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:tests/RobustFit.UnitTests/TestResults/*/coverage.cobertura.xml -targetdir:coveragereport
See the Tests Documentation for more detailed information on testing.
The RobustFit.Demo project demonstrates the differences between OLS and robust regression methods when dealing with outliers.
# From the root directory
cd samples/RobustFit.Demo
dotnet run
Or using Visual Studio:
- Set
RobustFit.Demo
as the startup project - Press F5 or click "Start Debugging"
See the Demo Documentation for more details.
using RobustFit.Core.Models;
// Prepare data
double[][] X = [
[ 1.0, 2.0 ],
[ 2.0, 3.0 ],
[ 3.0, 4.0 ]
];
double[] y = [5.0, 7.0, 9.0];
// Create and fit model
var ols = new OLSRegressor();
ols.Fit(X, y);
// Make predictions
double prediction = ols.Predict([4.0, 5.0 ]);
Console.WriteLine($"OLS prediction: {prediction}");
// Access coefficients
Console.WriteLine($"Intercept: {ols.Coefficients[0]}");
Console.WriteLine($"Feature 1 coefficient: {ols.Coefficients[1]}");
Console.WriteLine($"Feature 2 coefficient: {ols.Coefficients[2]}");
using RobustFit.Core.Models;
using RobustFit.Core.LossFunctions;
// Create data with outliers
double[][] X = [
[1.0],
[2.0],
[3.0],
[4.0],
[5.0] // This will be an outlier point
];
double[] y = [2.0, 4.0, 6.0, 8.0, 100.0]; // Last point is outlier
// Create loss function
var huberLoss = new HuberLoss(c: 1.345);
// Create and fit model
var robust = new RobustRegressor();
robust.Fit(X, y, huberLoss, maxIter: 100, tol: 1e-8);
// Make prediction
double prediction = robust.Predict([6.0]);
Console.WriteLine($"Robust prediction: {prediction}");
For more usage examples and API details, see the Core Library Documentation.
Contributions are welcome! Please feel free to submit a pull request or open an issue for any enhancements, bug fixes, or questions.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.