Class RobustRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Represents a robust regression model that is resistant to outliers in the data.
public class RobustRegression<T> : RegressionBase<T>, IRegression<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
RobustRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
Robust regression provides an alternative to traditional regression methods when data contains outliers or influential observations. By using weight functions, it reduces the influence of outliers on the final model. This implementation uses an iterative reweighted least squares approach to estimate coefficients that are less affected by extreme values.
For Beginners: Traditional regression models can be heavily influenced by outliers (unusual data points that don't follow the general pattern).
Think of robust regression like a smart voting system:
- It identifies which data points are "suspicious" (potential outliers)
- It gives these points less influence (lower weight) in determining the final model
- It focuses more on the reliable data points to find the true pattern
For example, if most houses in a neighborhood cost $200,000-300,000, but one special mansion costs $2 million, robust regression would recognize this as an outlier and reduce its influence when predicting house prices based on size or features.
Constructors
RobustRegression(RobustRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the RobustRegression<T> class with the specified options and regularization method.
public RobustRegression(RobustRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsRobustRegressionOptions<T>The configuration options for the robust regression. If null, default options are used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>The regularization method to apply. If null, no regularization is applied.
Remarks
This constructor creates a new robust regression model with the specified configuration options and regularization method. If options are not provided, default values are used. Regularization helps prevent overfitting by adding penalties for model complexity.
For Beginners: This method sets up a new robust regression model with your chosen settings.
Think of it like configuring a new smartphone:
- You can use the default settings (by not specifying options)
- Or you can customize how it works (by providing specific options)
- You can also add regularization, which helps prevent the model from memorizing the data instead of learning patterns (similar to adding parental controls)
Methods
CreateNewInstance()
Creates a new instance of the robust regression model with the same options.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the robust regression model with the same configuration but no trained parameters.
Remarks
This method creates a new instance of the robust regression model with the same configuration options and regularization method as the current instance, but without copying the trained coefficients or intercept.
For Beginners: This method creates a fresh copy of the model configuration without any learned parameters.
Think of it like getting a blank template with the same settings, but without any of the values that were learned from training data. The new model has the same:
- Weight function (how outliers are handled)
- Tuning constant (how sensitive the model is to outliers)
- Maximum iterations (how many times it will try to improve)
- Tolerance (when it decides it's "good enough")
- Regularization settings (how it prevents overfitting)
But it doesn't have any of the coefficients or intercept values that were learned from data.
This is mainly used internally when doing things like cross-validation or creating multiple similar models with different training data.
Deserialize(byte[])
Deserializes the robust regression model from a byte array.
public override void Deserialize(byte[] modelData)
Parameters
modelDatabyte[]The byte array containing the serialized model data.
Remarks
This method reconstructs the model from a byte array created by the Serialize method. It restores the model's coefficients, intercept, and configuration options, allowing a previously saved model to be loaded and used for predictions.
For Beginners: This method loads a saved model from computer memory.
Think of it like restoring a model from a snapshot:
- It takes the byte array created by the Serialize method
- It reconstructs all the important values and settings
- The model is then ready to use for making predictions
This allows you to:
- Use a previously trained model without retraining it
- Load models that others have shared with you
- Use the same model across different applications
GetModelType()
Gets the type of this regression model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type enum value representing robust regression.
Remarks
This method returns the enum value that identifies this model as a robust regression model. This is used for model identification in serialization/deserialization and for logging purposes.
For Beginners: This method simply tells the system what kind of model this is.
It's like a name tag for the model that says "I am a robust regression model." This is useful when:
- Saving the model to a file
- Loading a model from a file
- Logging information about the model
GetParameters()
Gets the model parameters (coefficients and intercept) as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
A vector containing all model parameters.
Remarks
For Beginners: This method packages all the model's parameters into a single vector.
The returned vector combines:
- All the coefficients (the weights for each feature)
- The intercept (baseline value when all features are zero)
This is useful for optimization algorithms that need to work with all parameters at once.
Serialize()
Serializes the robust regression model to a byte array for storage or transmission.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model data.
Remarks
This method converts the model, including its coefficients, intercept, and configuration options, into a byte array. This enables the model to be saved to a file, stored in a database, or transmitted over a network.
For Beginners: This method saves the model to computer memory so you can use it later.
Think of it like taking a snapshot of the model:
- It captures all the important values and settings
- It converts them into a format that can be easily stored
- The resulting byte array can be saved to a file or database
This is useful when you want to:
- Train the model once and use it many times
- Share the model with others
- Use the model in a different application
Train(Matrix<T>, Vector<T>)
Trains the robust regression model using the provided input data and target values.
public override void Train(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input matrix where rows represent observations and columns represent features.
yVector<T>The target vector containing the values to predict.
Remarks
This method implements the iterative reweighted least squares algorithm for robust regression. It starts with an initial estimate using standard regression, then iteratively recalculates weights based on residuals and performs weighted regression until convergence or the maximum number of iterations is reached.
For Beginners: This method teaches the model to make predictions based on your data.
The training process works like this:
- Start with a regular regression (like finding the best straight line through your data points)
- Calculate how far each point is from this line (called "residuals")
- Give lower weights to points that are far from the line (potential outliers)
- Create a new line that pays more attention to points with higher weights
- Repeat steps 2-4 until the line stops changing significantly
This way, the final model is less influenced by outliers and better represents the true pattern in your data.
WithParameters(Vector<T>)
Creates a new model instance with the specified parameters.
public override IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
Parameters
parametersVector<T>A vector containing all model parameters (coefficients and intercept).
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new model instance with the specified parameters.
Remarks
For Beginners: This method creates a new model using a provided set of parameters.
It takes a parameter vector (which combines coefficients and intercept) and:
- Extracts the coefficients (all values except the last)
- Extracts the intercept (the last value)
- Creates a new model with these values
This allows you to try different parameter sets without changing the original model.