Table of Contents

Class OrthogonalRegression<T>

Namespace
AiDotNet.Regression
Assembly
AiDotNet.dll

Implements orthogonal regression (also known as total least squares), which minimizes the perpendicular distance from data points to the fitted line or hyperplane.

public class OrthogonalRegression<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 data type used for calculations (e.g., float, double).

Inheritance
OrthogonalRegression<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

Unlike ordinary least squares regression which minimizes vertical distances, orthogonal regression minimizes the perpendicular (orthogonal) distance from each data point to the regression line or hyperplane. This approach is more appropriate when both dependent and independent variables contain measurement errors.

The algorithm works by centering the data, optionally scaling the variables, and then finding the solution using matrix decomposition methods such as SVD (Singular Value Decomposition).

For Beginners: Orthogonal regression is useful when you're not sure which variable is dependent and which is independent, or when both variables have measurement errors. Think of it as finding the line that's as close as possible to all points when measuring distance perpendicular to the line, rather than just vertically.

Constructors

OrthogonalRegression(OrthogonalRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)

Initializes a new instance of the OrthogonalRegression class with the specified options and regularization.

public OrthogonalRegression(OrthogonalRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)

Parameters

options OrthogonalRegressionOptions<T>

Configuration options for the orthogonal regression model. If null, default options will be used.

regularization IRegularization<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 orthogonal 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 Orthogonal 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 the Orthogonal Regression model.

Remarks

This method creates a deep copy of the current Orthogonal Regression model, including its options, coefficients, intercept, and regularization settings. The new instance is completely independent of the original, allowing modifications without affecting the original model.

For Beginners: This method creates an exact copy of your trained model.

Think of it like making a perfect duplicate:

  • It copies all the configuration settings (like tolerance and whether to scale variables)
  • It preserves the coefficients (the weights for each feature)
  • It maintains the intercept (the starting point of your regression line or plane)
  • It includes the same regularization settings to prevent overfitting

Creating a copy is useful when you want to:

  • Create a backup before making changes to the model
  • Create variations of the same model for different purposes
  • Share the model with others while keeping your original intact

Exceptions

InvalidOperationException

Thrown when the creation fails or required components are null.

Deserialize(byte[])

Deserializes the model from a byte array.

public override void Deserialize(byte[] modelData)

Parameters

modelData byte[]

The byte array containing the serialized model data.

Remarks

This method deserializes both the base class data and the orthogonal 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.

GetModelType()

Gets the type of the model.

protected override ModelType GetModelType()

Returns

ModelType

The model type identifier for orthogonal regression.

Remarks

This method is used for model identification and serialization purposes.

For Beginners: This method simply returns an identifier that indicates this is an orthogonal regression model. It's used internally by the library to keep track of different types of models.

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 orthogonal regression specific options, including tolerance, maximum iterations, and whether to scale variables.

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 orthogonal regression model on the provided data.

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

Parameters

x Matrix<T>

The input features matrix where each row is a training example and each column is a feature.

y Vector<T>

The target values vector corresponding to each training example.

Remarks

This method performs the following steps: 1. Applies regularization to the input matrix 2. Centers the data by subtracting the mean from each feature and the target 3. Optionally scales the variables to have unit variance 4. Computes the augmented matrix [X y] 5. Uses matrix decomposition to find the solution 6. Rescales the solution and normalizes it 7. Extracts the coefficients and intercept 8. Applies regularization to the coefficients

For Beginners: Training is the process where the model learns from your data. The algorithm first centers the data (subtracts the average from each variable) and optionally scales it (makes all variables have similar ranges). Then it uses advanced matrix math to find the best-fitting line or plane that minimizes the perpendicular distance from all points. The result is a set of coefficients that define this line or plane.