Table of Contents

Class WeightedRegression<T>

Namespace
AiDotNet.Regression
Assembly
AiDotNet.dll

Implements weighted regression, a variation of linear regression where each data point has a different level of importance (weight) in determining the model parameters.

public class WeightedRegression<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

T

The numeric type used for calculations (e.g., float, double).

Inheritance
WeightedRegression<T>
Implements
IFullModel<T, Matrix<T>, Vector<T>>
IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>
IParameterizable<T, Matrix<T>, Vector<T>>
ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>
IGradientComputable<T, Matrix<T>, Vector<T>>
Inherited Members
Extension Methods

Remarks

Weighted regression extends standard regression by allowing each data point to have a different level of influence on the model. This is particularly useful in scenarios where data points have varying reliability, importance, or error variance.

For Beginners: Weighted regression is like giving different voting power to different data points.

Think of it like this:

  • Regular regression treats all data points equally - each point gets one "vote" on where the line should go
  • Weighted regression lets some points have more "votes" than others
  • Points with higher weights have more influence on the final model
  • Points with lower weights have less influence

For example, if you're predicting house prices:

  • Recent sales might get higher weights because they reflect current market conditions better
  • Unusual properties might get lower weights to prevent them from skewing the model
  • More reliable measurements might get higher weights than less reliable ones

This helps you build models that focus more on the data points you trust or care about most.

Constructors

WeightedRegression(WeightedRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)

Creates a new instance of the weighted regression model.

public WeightedRegression(WeightedRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)

Parameters

options WeightedRegressionOptions<T>

Configuration options for the weighted regression model, including the weights for each data point and the polynomial order. The weights determine how much influence each data point has on the model.

regularization IRegularization<T, Matrix<T>, Vector<T>>

Optional regularization method to prevent overfitting. Regularization adds a penalty for large coefficient values, which helps create a more generalizable model.

Remarks

This constructor initializes a new weighted regression model with the specified options and regularization. The weights vector must be provided in the options, as it's a fundamental component of weighted regression.

For Beginners: This sets up your weighted regression model with your chosen settings.

When creating a weighted regression model:

  • You must provide weights for each data point (how important each point is)
  • You can specify the order (complexity) of the model
  • You can add regularization to prevent overfitting (making the model too specific to training data)

If you don't provide weights, you'll get an error because weights are essential to weighted regression.

Exceptions

ArgumentNullException

Thrown when the options parameter is null or doesn't contain weights.

Methods

CreateNewInstance()

Creates a new instance of the weighted regression model with the same configuration.

protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new instance of WeightedRegression<T> with the same configuration as the current instance.

Remarks

This method creates a new weighted regression model that has the same configuration as the current instance. It's used for model persistence, cloning, and transferring the model's configuration to new instances.

For Beginners: This method makes a fresh copy of the current model with the same settings.

It's like making a blueprint copy of your model that can be used to:

  • Save your model's settings
  • Create a new identical model
  • Transfer your model's configuration to another system

This is useful when you want to:

  • Create multiple similar models
  • Save a model's configuration for later use
  • Reset a model while keeping its settings

GetModelType()

Gets the type of regression model.

protected override ModelType GetModelType()

Returns

ModelType

The model type identifier for weighted regression.

Remarks

This method identifies the type of this regression model as weighted regression, which helps with model type checking and serialization.

For Beginners: This method simply identifies what kind of model this is.

It returns a label that identifies this as a weighted regression model, which helps the system recognize and handle it correctly when saving, loading, or processing models.

Predict(Matrix<T>)

Makes predictions using the trained weighted regression model.

public override Vector<T> Predict(Matrix<T> input)

Parameters

input Matrix<T>

The input feature matrix where each row represents a data point to predict and each column represents a feature.

Returns

Vector<T>

A vector of predicted values, one for each row in the input matrix.

Remarks

This method generates predictions for new data points using the coefficients learned during training. It first expands the input features to the same polynomial order used during training, then applies the base class prediction method to calculate the output values.

For Beginners: Once the model is trained, this method uses the discovered patterns to predict outcomes for new data.

When making predictions:

  • The new data is transformed the same way as during training (expanded to polynomial features)
  • The model applies the equation it learned to generate predictions
  • Each input row gets one predicted value

Note that the weights only matter during training - when predicting, all we need are the patterns (coefficients) the model discovered.

Train(Matrix<T>, Vector<T>)

Trains the weighted regression model using the provided input features and target values.

public override void Train(Matrix<T> x, Vector<T> y)

Parameters

x Matrix<T>

The input feature matrix where each row represents a data point and each column represents a feature.

y Vector<T>

The target values vector where each element corresponds to a row in the input matrix.

Remarks

This method implements the weighted least squares algorithm to find the optimal coefficients for the model. It first expands the features to the specified polynomial order, then applies the weights to give different importance to each data point, and finally solves the weighted normal equations to find the coefficients.

For Beginners: This method teaches the model to recognize patterns in your data, respecting the importance of each point.

During training:

  • The features are expanded based on the order (to capture more complex patterns)
  • The weights are applied to each data point (giving more influence to higher-weighted points)
  • The model solves a complex equation to find the best fit considering these weights
  • If you included an intercept, it finds both the slope(s) and the y-intercept

After training is complete, the model can make predictions for new data points.