diff --git a/src/Numerics/LinearRegression/WeightedRegression.cs b/src/Numerics/LinearRegression/WeightedRegression.cs index f9aafa1f..071e2033 100644 --- a/src/Numerics/LinearRegression/WeightedRegression.cs +++ b/src/Numerics/LinearRegression/WeightedRegression.cs @@ -42,7 +42,7 @@ namespace MathNet.Numerics.LinearRegression /// public static Vector Weighted(Matrix x, Vector y, Matrix w) where T : struct, IEquatable, IFormattable { - return x.TransposeThisAndMultiply(w * x).Cholesky().Solve(x.Transpose() * (w * y)); + return x.TransposeThisAndMultiply(w*x).Cholesky().Solve(x.Transpose()*(w*y)); } /// @@ -50,7 +50,7 @@ namespace MathNet.Numerics.LinearRegression /// public static Matrix Weighted(Matrix x, Matrix y, Matrix w) where T : struct, IEquatable, IFormattable { - return x.TransposeThisAndMultiply(w * x).Cholesky().Solve(x.Transpose() * (w * y)); + return x.TransposeThisAndMultiply(w*x).Cholesky().Solve(x.Transpose()*(w*y)); } /// @@ -66,7 +66,7 @@ namespace MathNet.Numerics.LinearRegression } var response = Matrix.Build.DenseVector(y); var weights = Matrix.Build.DiagonalMatrix(new DiagonalMatrixStorage(predictor.RowCount, predictor.RowCount, w)); - return predictor.TransposeThisAndMultiply(weights * predictor).Cholesky().Solve(predictor.Transpose() * (weights * response)).ToArray(); + return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.Transpose()*(weights*response)).ToArray(); } /// @@ -82,15 +82,13 @@ namespace MathNet.Numerics.LinearRegression /// /// Locally-Weighted Linear Regression using normal equations. /// - public static Vector Local(Matrix x, Vector y, Vector t, Func, Vector, T> kernel) where T : struct, IEquatable, IFormattable + public static Vector Local(Matrix x, Vector y, Vector t, double radius, Func kernel) where T : struct, IEquatable, IFormattable { - // TODO: Kernel definition is a bit weird as it includes computing the difference norm - // We can make this more common once we change the norm to always be of type double around LA. - + // TODO: Weird kernel definition var w = Matrix.Build.DenseMatrix(x.RowCount, x.RowCount); for (int i = 0; i < x.RowCount; i++) { - w.At(i, i, kernel(t, x.Row(i))); + w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius)); } return Weighted(x, y, w); } @@ -98,24 +96,20 @@ namespace MathNet.Numerics.LinearRegression /// /// Locally-Weighted Linear Regression using normal equations. /// - public static Matrix Local(Matrix x, Matrix y, Vector t, Func, Vector, T> kernel) where T : struct, IEquatable, IFormattable + public static Matrix Local(Matrix x, Matrix y, Vector t, double radius, Func kernel) where T : struct, IEquatable, IFormattable { - // TODO: Kernel definition is a bit weird as it includes computing the difference norm - // We can make this more common once we change the norm to always be of type double around LA. - + // TODO: Weird kernel definition var w = Matrix.Build.DenseMatrix(x.RowCount, x.RowCount); for (int i = 0; i < x.RowCount; i++) { - w.At(i, i, kernel(t, x.Row(i))); + w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius)); } return Weighted(x, y, w); } - public static Func, Vector, double> GaussianKernel(double radius) + public static double GaussianKernel(double normalizedDistance) { - // TODO: see above... - var d = -2.0*radius*radius; - return (t, x) => Math.Exp(Distance.SSD(x, t)/d); + return Math.Exp(-0.5*normalizedDistance*normalizedDistance); } } }