Class QuantileRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Implements Quantile Regression, a technique that estimates the conditional quantiles of a response variable distribution in the linear model, providing a more complete view of the relationship between variables.
public class QuantileRegression<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 data type used for calculations (e.g., float, double).
- Inheritance
-
QuantileRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
Unlike ordinary least squares regression which estimates the conditional mean of the response variable, quantile regression estimates the conditional median or other quantiles of the response variable. This makes it robust to outliers and useful for modeling heterogeneous conditional distributions.
The algorithm uses gradient descent optimization to minimize the quantile loss function, which gives different weights to positive and negative errors based on the specified quantile.
For Beginners: While standard regression tells you about the average relationship between variables, quantile regression lets you explore different parts of the data distribution. For example, median regression (quantile=0.5) tells you about the middle of the distribution, while quantile=0.9 tells you about the upper end. This is useful when you suspect that the relationship between variables might be different for different ranges of the outcome.
Constructors
QuantileRegression(QuantileRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the QuantileRegression class with the specified options and regularization.
public QuantileRegression(QuantileRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsQuantileRegressionOptions<T>Configuration options for the quantile regression model. If null, default options will be used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Regularization method to prevent overfitting. If null, no regularization will be applied.
Remarks
The constructor initializes the model with either the provided options or default settings.
For Beginners: This constructor sets up the quantile regression model with your specified settings or uses default settings if none are provided. Regularization is an optional technique to prevent the model from becoming too complex and overfitting to the training data.
Methods
CreateNewInstance()
Creates a new instance of the quantile 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 quantile regression model with the same configuration but no trained parameters.
Remarks
This method creates a new instance of the quantile regression model with the same configuration options and regularization method as the current instance, but without copying the trained parameters.
For Beginners: This method creates a fresh copy of the model configuration without any learned parameters.
Think of it like getting a blank notepad with the same paper quality and size, but without any writing on it yet. The new model has the same:
- Quantile setting (which part of the distribution you're estimating)
- Learning rate (how quickly the model adjusts during training)
- Maximum iterations (how long the model will train)
- Regularization settings (safeguards against overfitting)
But it doesn't have any of the coefficient values that were learned from data.
This is mainly used internally when doing things like cross-validation or creating ensembles of similar models with different training data.
Deserialize(byte[])
Deserializes the model from a byte array.
public override void Deserialize(byte[] modelData)
Parameters
modelDatabyte[]The byte array containing the serialized model data.
Remarks
This method deserializes both the base class data and the quantile regression specific options, reconstructing the model's state from the serialized data.
For Beginners: Deserialization is the opposite of serialization - it takes the saved model data and reconstructs the model's internal state. This allows you to load a previously trained model and use it to make predictions without having to retrain it. It's like loading a saved game to continue where you left off.
GetModelMetadata()
Gets metadata about the model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetadata object containing information about the model.
Remarks
This method returns metadata about the model, including its type and the quantile being estimated.
For Beginners: Model metadata provides information about the model itself, rather than the predictions it makes. For quantile regression, this includes which quantile the model is estimating (e.g., median, 90th percentile). This information can be useful for understanding and comparing different models.
GetModelType()
Gets the type of the model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type identifier for quantile regression.
Remarks
This method is used for model identification and serialization purposes.
For Beginners: This method simply returns an identifier that indicates this is a quantile regression model. It's used internally by the library to keep track of different types of models.
Predict(Matrix<T>)
Makes predictions for the given input data.
public override Vector<T> Predict(Matrix<T> input)
Parameters
inputMatrix<T>The input features matrix where each row is an example and each column is a feature.
Returns
- Vector<T>
A vector of predicted values for each input example.
Remarks
This method predicts the specified quantile of the conditional distribution for each input example.
For Beginners: After training, this method is used to make predictions on new data. For each example in your input data, it calculates the predicted value at the quantile you specified. For instance, if you set quantile=0.5, it predicts the median value; if you set quantile=0.9, it predicts the value below which 90% of the observations would fall.
Serialize()
Serializes the model to a byte array.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model data.
Remarks
This method serializes both the base class data and the quantile regression specific options, including the quantile, learning rate, and maximum iterations.
For Beginners: Serialization converts the model's internal state into a format that can be saved to disk or transmitted over a network. This allows you to save a trained model and load it later without having to retrain it. Think of it like saving your progress in a video game.
Train(Matrix<T>, Vector<T>)
Trains the quantile regression model on the provided data.
public override void Train(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input features matrix where each row is a training example and each column is a feature.
yVector<T>The target values vector corresponding to each training example.
Remarks
This method implements gradient descent optimization to minimize the quantile loss function. The steps are: 1. Initialize coefficients and intercept 2. Apply regularization to the input matrix 3. For each iteration: a. Calculate predictions and errors for all examples b. Compute gradients based on the quantile loss function c. Update coefficients and intercept using the gradients d. Apply regularization to the coefficients
For Beginners: Training is the process where the model learns from your data. The algorithm starts with initial guesses for the coefficients and then iteratively improves them. At each step, it calculates how far off its predictions are, but unlike standard regression, it penalizes over-predictions and under-predictions differently based on the quantile you specified. It then adjusts the coefficients to reduce these errors.