diff --git a/src/Numerics/LinearRegression/MultipleRegression.cs b/src/Numerics/LinearRegression/MultipleRegression.cs index 0ef6c992..0ac8a77c 100644 --- a/src/Numerics/LinearRegression/MultipleRegression.cs +++ b/src/Numerics/LinearRegression/MultipleRegression.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2013 Math.NET +// Copyright (c) 2009-2014 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -36,6 +36,96 @@ namespace MathNet.Numerics.LinearRegression { public static class MultipleRegression { + /// + /// Find the model parameters β such that X*β with predictor X becomes as close to response Y as possible, with least squares residuals. + /// + /// Predictor matrix X + /// Response vector Y + /// The direct method to be used to compute the regression. + /// Best fitting vector for model parameters β + public static Vector DirectMethod(Matrix x, Vector y, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations) where T : struct, IEquatable, IFormattable + { + switch (method) + { + case DirectRegressionMethod.NormalEquations: + return NormalEquations(x, y); + case DirectRegressionMethod.QR: + return QR(x, y); + case DirectRegressionMethod.Svd: + return Svd(x, y); + default: + throw new NotSupportedException(method.ToString()); + } + } + + /// + /// Find the model parameters β such that X*β with predictor X becomes as close to response Y as possible, with least squares residuals. + /// + /// Predictor matrix X + /// Response matrix Y + /// The direct method to be used to compute the regression. + /// Best fitting vector for model parameters β + public static Matrix DirectMethod(Matrix x, Matrix y, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations) where T : struct, IEquatable, IFormattable + { + switch (method) + { + case DirectRegressionMethod.NormalEquations: + return NormalEquations(x, y); + case DirectRegressionMethod.QR: + return QR(x, y); + case DirectRegressionMethod.Svd: + return Svd(x, y); + default: + throw new NotSupportedException(method.ToString()); + } + } + + /// + /// Find the model parameters β such that their linear combination with all predictor-arrays in X become as close to their response in Y as possible, with least squares residuals. + /// + /// List of predictor-arrays. + /// List of responses + /// True if an intercept should be added as first artificial predictor value. Default = false. + /// The direct method to be used to compute the regression. + /// Best fitting list of model parameters β for each element in the predictor-arrays. + public static T[] DirectMethod(T[][] x, T[] y, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations) where T : struct, IEquatable, IFormattable + { + switch (method) + { + case DirectRegressionMethod.NormalEquations: + return NormalEquations(x, y, intercept); + case DirectRegressionMethod.QR: + return QR(x, y, intercept); + case DirectRegressionMethod.Svd: + return Svd(x, y, intercept); + default: + throw new NotSupportedException(method.ToString()); + } + } + + /// + /// Find the model parameters β such that their linear combination with all predictor-arrays in X become as close to their response in Y as possible, with least squares residuals. + /// Uses the cholesky decomposition of the normal equations. + /// + /// Sequence of predictor-arrays and their response. + /// True if an intercept should be added as first artificial predictor value. Default = false. + /// The direct method to be used to compute the regression. + /// Best fitting list of model parameters β for each element in the predictor-arrays. + public static T[] DirectMethod(IEnumerable> samples, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations) where T : struct, IEquatable, IFormattable + { + switch (method) + { + case DirectRegressionMethod.NormalEquations: + return NormalEquations(samples, intercept); + case DirectRegressionMethod.QR: + return QR(samples, intercept); + case DirectRegressionMethod.Svd: + return Svd(samples, intercept); + default: + throw new NotSupportedException(method.ToString()); + } + } + /// /// Find the model parameters β such that X*β with predictor X becomes as close to response Y as possible, with least squares residuals. /// Uses the cholesky decomposition of the normal equations. diff --git a/src/Numerics/LinearRegression/Options.cs b/src/Numerics/LinearRegression/Options.cs new file mode 100644 index 00000000..83d2e3dc --- /dev/null +++ b/src/Numerics/LinearRegression/Options.cs @@ -0,0 +1,39 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2014 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.LinearRegression +{ + public enum DirectRegressionMethod + { + NormalEquations = 0, + QR, + Svd + } +} diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index d7658c95..106dec4f 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -94,6 +94,7 @@ +