Click or drag to resize
Accord.NET (logo)

NonlinearRegression Class

Nonlinear Regression.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningTransformBaseDouble, Double
    Accord.Statistics.Models.RegressionNonlinearRegression

Namespace:  Accord.Statistics.Models.Regression
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class NonlinearRegression : TransformBase<double[], double>, 
	ICloneable
Request Example View Source

The NonlinearRegression type exposes the following members.

Constructors
  NameDescription
Public methodNonlinearRegression(Int32, RegressionFunction)
Initializes a new instance of the NonlinearRegression class.
Public methodNonlinearRegression(Int32, RegressionFunction, RegressionGradientFunction)
Initializes a new instance of the NonlinearRegression class.
Top
Properties
  NameDescription
Public propertyCoefficients
Gets the regression coefficients.
Public propertyFunction
Gets the model function, mapping inputs to outputs given a suitable parameter vector.
Public propertyGradient
Gets or sets a function that computes the gradient of the Function in respect to the Coefficients.
Public propertyNumberOfInputs
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertyNumberOfOutputs
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertyStandardErrors
Gets the standard errors for the regression coefficients.
Top
Methods
  NameDescription
Public methodClone
Creates a new object that is a copy of the current instance.
Public methodCompute Obsolete.
Computes the model output for the given input vector.
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.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTransform(TInput)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.)
Public methodTransform(Double)
Applies the transformation to an input, producing an associated output.
(Overrides TransformBaseTInput, TOutputTransform(TInput).)
Public methodTransform(TInput, TOutput)
Applies the transformation to an input, producing an associated output.
(Inherited from TransformBaseTInput, TOutput.)
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
Examples

The first example shows how to fit a non-linear regression with LevenbergMarquardt.

// Suppose we would like to map the continuous values in the
// second column to the integer values in the first column.
double[,] data =
{
    { -40,    -21142.1111111111 },
    { -30,    -21330.1111111111 },
    { -20,    -12036.1111111111 },
    { -10,      7255.3888888889 },
    {   0,     32474.8888888889 },
    {  10,     32474.8888888889 },
    {  20,      9060.8888888889 },
    {  30,    -11628.1111111111 },
    {  40,    -15129.6111111111 },
};

// Extract inputs and outputs
double[][] inputs = data.GetColumn(0).ToJagged();
double[] outputs = data.GetColumn(1);

// Create a Nonlinear regression using 
var nls = new NonlinearLeastSquares()
{
    NumberOfParameters = 3,

    // Initialize to some random values
    StartValues = new[] { 4.2, 0.3, 1 },

    // Let's assume a quadratic model function: ax² + bx + c
    Function = (w, x) => w[0] * x[0] * x[0] + w[1] * x[0] + w[2],

    // Derivative in respect to the weights:
    Gradient = (w, x, r) =>
    {
        r[0] = w[0]* w[0]; // w.r.t a: a²  // https://www.wolframalpha.com/input/?i=diff+ax²+%2B+bx+%2B+c+w.r.t.+a
        r[1] = w[0];       // w.r.t b: b   // https://www.wolframalpha.com/input/?i=diff+ax²+%2B+bx+%2B+c+w.r.t.+b
        r[2] = 1;          // w.r.t c: 1   // https://www.wolframalpha.com/input/?i=diff+ax²+%2B+bx+%2B+c+w.r.t.+c
    },

    Algorithm = new LevenbergMarquardt()
    {
        MaxIterations = 100,
        Tolerance = 0
    }
};


var regression = nls.Learn(inputs, outputs);

// Use the function to compute the input values
double[] predict = regression.Transform(inputs);

The second example shows how to fit a non-linear regression with GaussNewton.

// Suppose we would like to map the continuous values in the
// second row to the integer values in the first row.
double[,] data =
{
    { 0.03, 0.1947, 0.425, 0.626, 1.253, 2.500, 3.740 },
    { 0.05, 0.127, 0.094, 0.2122, 0.2729, 0.2665, 0.3317}
};

// Extract inputs and outputs
double[][] inputs = data.GetRow(0).ToJagged();
double[] outputs = data.GetRow(1);

// Create a Nonlinear regression using 
var nls = new NonlinearLeastSquares()
{
    // Initialize to some random values
    StartValues = new[] { 0.9, 0.2 },

    // Let's assume a quadratic model function: ax² + bx + c
    Function = (w, x) => (w[0] * x[0]) / (w[1] + x[0]),

    // Derivative in respect to the weights:
    Gradient = (w, x, r) =>
    {
        r[0] = -((-x[0]) / (w[1] + x[0]));
        r[1] = -((w[0] * x[0]) / Math.Pow(w[1] + x[0], 2));
    },

    Algorithm = new GaussNewton()
    {
        MaxIterations = 0,
        Tolerance = 1e-5
    }
};


var regression = nls.Learn(inputs, outputs);

// Use the function to compute the input values
double[] predict = regression.Transform(inputs);
See Also