mirror of
https://github.com/hanzoai/jube-fork.git
synced 2026-10-30 18:33:33 +00:00
Included open-source Accord.net library code natively to the solution given the deprecated state of that project. Accord.net will be supported inside Jube for own needs (such as upgrading to .Net 7+). Accord.net libraries modified to compile to .Net 6. Modified Accord.net libraries to better support Newtonsoft Json.net serialisation. Included annotations in the Accord.net libraries to house important dependency statistics. Serialised json can be used independently of Jube to recall a Neural Network. Removal of Binary Serializer totally as a precursor to the projects support of .Net 7+. Extensive bug fixing in crucial functions of Neural Network training and recall. While this part of the system remains in Alpha, it is substantially more fit for purpose.
217 lines
8.0 KiB
C#
217 lines
8.0 KiB
C#
// Accord Statistics Library
|
|
// The Accord.NET Framework
|
|
// http://accord-framework.net
|
|
//
|
|
// Copyright © César Souza, 2009-2017
|
|
// cesarsouza at gmail.com
|
|
//
|
|
// This library is free software; you can redistribute it and/or
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
// License as published by the Free Software Foundation; either
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
// Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
// License along with this library; if not, write to the Free Software
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
//
|
|
|
|
namespace Accord.Statistics.Models.Regression
|
|
{
|
|
using Accord.MachineLearning;
|
|
using Accord.Math.Optimization;
|
|
using Accord.Statistics.Models.Regression.Fitting;
|
|
using System;
|
|
using Accord.Compat;
|
|
|
|
/// <summary>
|
|
/// Regression function delegate.
|
|
/// </summary>
|
|
///
|
|
/// <remarks>
|
|
/// This delegate represents a parameterized function that, given a set of
|
|
/// <paramref name="coefficients">model coefficients</paramref> and an input
|
|
/// vector, produces an associated output value.
|
|
/// </remarks>
|
|
///
|
|
/// <param name="coefficients">The model coefficients, also known as parameters or coefficients.</param>
|
|
/// <param name="input">An input vector.</param>
|
|
///
|
|
/// <returns>The output value produced given the
|
|
/// <paramref name="coefficients"/> and <paramref name="input"/> vector.</returns>
|
|
///
|
|
public delegate double RegressionFunction(double[] coefficients, double[] input);
|
|
|
|
/// <summary>
|
|
/// Gradient function delegate.
|
|
/// </summary>
|
|
///
|
|
/// <remarks>
|
|
/// This delegate represents the gradient of <see cref="RegressionFunction">regression
|
|
/// function</see>. A regression function is a parameterized function that, given a set
|
|
/// of <paramref name="coefficients">model coefficients</paramref> and an input vector,
|
|
/// produces an associated output value. This function should compute the gradient vector
|
|
/// in respect to the function <paramref name="coefficients"/>.
|
|
/// </remarks>
|
|
///
|
|
/// <param name="coefficients">The model coefficients, also known as parameters or coefficients.</param>
|
|
/// <param name="input">An input vector.</param>
|
|
/// <param name="result">The resulting gradient vector (w.r.t to the coefficients).</param>
|
|
///
|
|
public delegate void RegressionGradientFunction(double[] coefficients, double[] input, double[] result);
|
|
|
|
/// <summary>
|
|
/// Nonlinear Regression.
|
|
/// </summary>
|
|
///
|
|
/// <example>
|
|
/// <para>
|
|
/// The first example shows how to fit a non-linear regression with <see cref="LevenbergMarquardt"/>.</para>
|
|
/// <code source="Unit Tests\Accord.Tests.Statistics\Models\Regression\NonlinearLeastSquaresTest.cs" region="doc_learn_lm" lang="cs" />
|
|
/// <code source="Unit Tests\Accord.Tests.Statistics.VB\Models\Regression\NonlinearLeastSquaresTest.vb" region="doc_learn_lm" lang="vb" />
|
|
///
|
|
/// <para>
|
|
/// The second example shows how to fit a non-linear regression with <see cref="GaussNewton"/>.</para>
|
|
/// <code source="Unit Tests\Accord.Tests.Statistics\Models\Regression\NonlinearLeastSquaresTest.cs" region="doc_learn_gn" lang="cs" />
|
|
/// <code source="Unit Tests\Accord.Tests.Statistics.VB\Models\Regression\NonlinearLeastSquaresTest.vb" region="doc_learn_gn" lang="vb" />
|
|
/// </example>
|
|
///
|
|
/// <seealso cref="NonlinearLeastSquares"/>
|
|
///
|
|
[Serializable]
|
|
public class NonlinearRegression : TransformBase<double[], double>, ICloneable
|
|
{
|
|
double[] coefficients;
|
|
double[] standardErrors;
|
|
|
|
// TODO: implement a way to serialize the function and gradient
|
|
// functions, most likely using serializable expression trees.
|
|
|
|
RegressionFunction function;
|
|
RegressionGradientFunction gradient;
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the regression coefficients.
|
|
/// </summary>
|
|
///
|
|
public double[] Coefficients
|
|
{
|
|
get { return coefficients; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the standard errors for the regression coefficients.
|
|
/// </summary>
|
|
///
|
|
public double[] StandardErrors
|
|
{
|
|
get { return standardErrors; }
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the model function, mapping inputs to
|
|
/// outputs given a suitable parameter vector.
|
|
/// </summary>
|
|
///
|
|
public RegressionFunction Function
|
|
{
|
|
get { return function; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a function that computes the gradient of the
|
|
/// <see cref="Function"/> in respect to the <see cref="Coefficients"/>.
|
|
/// </summary>
|
|
///
|
|
public RegressionGradientFunction Gradient
|
|
{
|
|
get { return gradient; }
|
|
set { gradient = value; }
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="NonlinearRegression"/> class.
|
|
/// </summary>
|
|
///
|
|
/// <param name="parameters">The number of variables (free parameters) in the model.</param>
|
|
/// <param name="function">The regression function implementing the regression model.</param>
|
|
///
|
|
public NonlinearRegression(int parameters, RegressionFunction function)
|
|
{
|
|
this.coefficients = new double[parameters];
|
|
this.standardErrors = new double[parameters];
|
|
this.function = function;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="NonlinearRegression"/> class.
|
|
/// </summary>
|
|
///
|
|
/// <param name="parameters">The number of variables (free parameters) in the model.</param>
|
|
/// <param name="function">The regression function implementing the regression model.</param>
|
|
/// <param name="gradient">The function that computes the gradient for <paramref name="function"/> (optional).</param>
|
|
///
|
|
public NonlinearRegression(int parameters, RegressionFunction function, RegressionGradientFunction gradient)
|
|
: this(parameters, function)
|
|
{
|
|
this.gradient = gradient;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Computes the model output for the given input vector.
|
|
/// </summary>
|
|
///
|
|
/// <param name="inputs">The input vector.</param>
|
|
///
|
|
/// <returns>The output value.</returns>
|
|
///
|
|
[Obsolete("Please use Transform instead.")]
|
|
public double Compute(double[] inputs)
|
|
{
|
|
return function(coefficients, inputs);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Creates a new object that is a copy of the current instance.
|
|
/// </summary>
|
|
///
|
|
/// <returns>
|
|
/// A new object that is a copy of this instance.
|
|
/// </returns>
|
|
///
|
|
public object Clone()
|
|
{
|
|
var clone = new NonlinearRegression(Coefficients.Length, function, gradient);
|
|
clone.coefficients = (double[])this.coefficients.Clone();
|
|
clone.standardErrors = (double[])this.standardErrors.Clone();
|
|
return clone;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Applies the transformation to an input, producing an associated output.
|
|
/// </summary>
|
|
/// <param name="input">The input data to which the transformation should be applied.</param>
|
|
/// <returns>
|
|
/// The output generated by applying this transformation to the given input.
|
|
/// </returns>
|
|
public override double Transform(double[] input)
|
|
{
|
|
return function(coefficients, input);
|
|
}
|
|
}
|
|
}
|