Click or drag to resize
Accord.NET (logo)

NonNegativeLeastSquares Class

Non-negative Least Squares for MultipleLinearRegression optimization.
Inheritance Hierarchy
SystemObject
  Accord.Statistics.Models.Regression.FittingNonNegativeLeastSquares

Namespace:  Accord.Statistics.Models.Regression.Fitting
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
public class NonNegativeLeastSquares : ISupervisedLearning<MultipleLinearRegression, double[], double>
Request Example View Source

The NonNegativeLeastSquares type exposes the following members.

Constructors
  NameDescription
Public methodNonNegativeLeastSquares
Initializes a new instance of the NonNegativeLeastSquares class.
Public methodNonNegativeLeastSquares(MultipleLinearRegression)
Initializes a new instance of the NonNegativeLeastSquares class.
Top
Properties
  NameDescription
Public propertyCoefficients
Gets the coefficient vector being fitted.
Public propertyMaxIterations
Gets or sets the maximum number of iterations to be performed.
Public propertyToken
Gets or sets a cancellation token that can be used to stop the learning algorithm while it is running.
Public propertyTolerance
Gets or sets the tolerance for detecting convergence. Default is 0.001.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn
Learns a model that can map the given inputs to the given outputs.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRun Obsolete.
Runs the fitting algorithm.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks

References:

  • Donghui Chen and Robert J.Plemmons, Nonnegativity Constraints in Numerical Analysis. Available on: http://users.wfu.edu/plemmons/papers/nonneg.pdf

Examples

The following example shows how to fit a multiple linear regression model with the additional constraint that none of its coefficients should be negative. For this we can use the NonNegativeLeastSquares learning algorithm instead of the OrdinaryLeastSquares used above.

// Declare training samples
var inputs = new double[][]
{
    new[] { 1.0, 1.0, 1.0 },
    new[] { 2.0, 4.0, 8.0 },
    new[] { 3.0, 9.0, 27.0 },
    new[] { 4.0, 16.0, 64.0 },
};

var outputs = new double[] { 0.23, 1.24, 3.81, 8.72 };

// Create a NN LS learning algorithm
var nnls = new NonNegativeLeastSquares()
{
    MaxIterations = 100
};

// Use the algorithm to learn a multiple linear regression
MultipleLinearRegression regression = nnls.Learn(inputs, outputs);

// None of the regression coefficients should be negative:
double[] coefficients = regression.Weights; // should be

// Check the quality of the regression:
double[] prediction = regression.Transform(inputs);

double error = new SquareLoss(expected: outputs)
    .Loss(actual: prediction); // should be 0
See Also