From d7e58a02a12f2fcdd327fb663a739a1c1d6769b3 Mon Sep 17 00:00:00 2001 From: David Falkner Date: Thu, 29 Dec 2016 05:09:36 -0800 Subject: [PATCH] Calculate Standard Error of the Regression for a linear model (#464) * Addd code to calculated Standard Error of the Regression for a linear model * Added distinction between Standard Error calculations for populations vs. samples. * Added copyright blurb to StandardErrorTest.cs * Fixed filename in copyright blurb in StandardErrorTest.cs --- src/Numerics/GoodnessOfFit.cs | 57 +++++++++++- src/Numerics/Properties/Resources.Designer.cs | 9 ++ src/Numerics/Properties/Resources.resx | 57 ++++++------ .../GoodnessOfFit/StandardErrorTest.cs | 86 +++++++++++++++++++ src/UnitTests/UnitTests.csproj | 1 + 5 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 src/UnitTests/GoodnessOfFit/StandardErrorTest.cs diff --git a/src/Numerics/GoodnessOfFit.cs b/src/Numerics/GoodnessOfFit.cs index 0760fc42..eb135905 100644 --- a/src/Numerics/GoodnessOfFit.cs +++ b/src/Numerics/GoodnessOfFit.cs @@ -25,7 +25,9 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using System; using System.Collections.Generic; +using MathNet.Numerics.Properties; using MathNet.Numerics.Statistics; namespace MathNet.Numerics @@ -33,7 +35,7 @@ namespace MathNet.Numerics public static class GoodnessOfFit { /// - /// Calculated the R-Squared value, also known as coefficient of determination, + /// Calculates the R-Squared value, also known as coefficient of determination, /// given modelled and observed values /// /// The values expected from the modelled @@ -46,7 +48,7 @@ namespace MathNet.Numerics } /// - /// Calculated the R value, also known as linear correlation coefficient, + /// Calculates the R value, also known as linear correlation coefficient, /// given modelled and observed values /// /// The values expected from the modelled @@ -56,5 +58,54 @@ namespace MathNet.Numerics { return Correlation.Pearson(modelledValues, observedValues); } + + /// + /// Calculates the Standard Error of the regression, given a sequence of + /// modeled/predicted values, and a sequence of actual/observed values + /// + /// The modelled/predicted values + /// The observed/actual values + /// The Standard Error of the regression + public static double PopulationStandardError(IEnumerable modelledValues, IEnumerable observedValues) + { + return SampleStandardError(modelledValues, observedValues, 0); + } + + /// + /// Calculates the Standard Error of the regression, given a sequence of + /// modeled/predicted values, and a sequence of actual/observed values + /// + /// The modelled/predicted values + /// The observed/actual values + /// The degrees of freedom by which the + /// number of samples is reduced for performing the Standard Error calculation + /// The Standard Error of the regression + public static double SampleStandardError(IEnumerable modelledValues, IEnumerable observedValues, int degreesOfFreedom) + { + using (IEnumerator ieM = modelledValues.GetEnumerator()) + using (IEnumerator ieO = observedValues.GetEnumerator()) + { + double n = 0; + double accumulator = 0; + while (ieM.MoveNext()) + { + if (!ieO.MoveNext()) + { + throw new ArgumentOutOfRangeException("modelledValues", Resources.ArgumentArraysSameLength); + } + double currentM = ieM.Current; + double currentO = ieO.Current; + var diff = currentM - currentO; + accumulator += diff * diff; + n++; + } + + if (degreesOfFreedom >= n) + { + throw new ArgumentOutOfRangeException("degreesOfFreedom", Resources.DegreesOfFreedomMustBeLessThanSampleSize); + } + return Math.Sqrt(accumulator / (n - degreesOfFreedom)); + } + } } -} +} \ No newline at end of file diff --git a/src/Numerics/Properties/Resources.Designer.cs b/src/Numerics/Properties/Resources.Designer.cs index d35d15d6..0aa64c61 100644 --- a/src/Numerics/Properties/Resources.Designer.cs +++ b/src/Numerics/Properties/Resources.Designer.cs @@ -602,6 +602,15 @@ namespace MathNet.Numerics.Properties { } } + /// + /// Looks up a localized string similar to The sample size must be larger than the given degrees of freedom.. + /// + public static string DegreesOfFreedomMustBeLessThanSampleSize { + get { + return ResourceManager.GetString("DegreesOfFreedomMustBeLessThanSampleSize", resourceCulture); + } + } + /// /// Looks up a localized string similar to This feature is not implemented yet (but is planned).. /// diff --git a/src/Numerics/Properties/Resources.resx b/src/Numerics/Properties/Resources.resx index fe831bfe..14692289 100644 --- a/src/Numerics/Properties/Resources.resx +++ b/src/Numerics/Properties/Resources.resx @@ -1,17 +1,17 @@  - @@ -445,4 +445,7 @@ All sample vectors must have the same length. However, vectors with disagreeing length {0} and {1} have been provided. A sample with index i is given by the value at index i of each provided vector. + + The sample size must be larger than the given degrees of freedom. + \ No newline at end of file diff --git a/src/UnitTests/GoodnessOfFit/StandardErrorTest.cs b/src/UnitTests/GoodnessOfFit/StandardErrorTest.cs new file mode 100644 index 00000000..60245133 --- /dev/null +++ b/src/UnitTests/GoodnessOfFit/StandardErrorTest.cs @@ -0,0 +1,86 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// +// Copyright (c) 2009-2013 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. +// + +using System; +using System.Linq; +using NUnit.Framework; + +namespace MathNet.Numerics.UnitTests.GoodnessOfFit +{ + [TestFixture, Category("Regression")] + public class StandardErrorTest + { + [Test] + public void ComputesPopulationStandardErrorOfTheRegression() + { + // Definition as described at: http://onlinestatbook.com/lms/regression/accuracy.html + var xes = new[] { 1.0, 2, 3, 4, 5 }; + var ys = new[] { 1, 2, 1.3, 3.75, 2.25 }; + var fit = Fit.Line(xes, ys); + var a = fit.Item1; + var b = fit.Item2; + var predictedYs = xes.Select(x => a + b * x); + var standardError = Numerics.GoodnessOfFit.PopulationStandardError(predictedYs, ys); + + Assert.AreEqual(0.747, standardError, 1e-3); + } + + [Test] + public void ComputesSampleStandardErrorOfTheRegression() + { + // Definition as described at: http://onlinestatbook.com/lms/regression/accuracy.html + var xes = new[] { 1.0, 2, 3, 4, 5 }; + var ys = new[] { 1, 2, 1.3, 3.75, 2.25 }; + var fit = Fit.Line(xes, ys); + var a = fit.Item1; + var b = fit.Item2; + var predictedYs = xes.Select(x => a + b * x); + var standardError = Numerics.GoodnessOfFit.SampleStandardError(predictedYs, ys, degreesOfFreedom: 2); + + Assert.AreEqual(0.964, standardError, 1e-3); + } + + [Test] + public void PopulationStandardErrorShouldThrowIfInputsSequencesDifferInLength() + { + var y1 = new[] { 0.0, 1 }; + var y2 = new[] { 1.0 }; + + Assert.Throws(() => Numerics.GoodnessOfFit.PopulationStandardError(y1, y2)); + } + + [Test] + public void SampleStandardErrorShouldThrowIfSampleSizeIsSmallerThanGivenDegreesOfFreedom() + { + var modelled = new[] { 1.0 }; + var observed = new[] { 1.0 }; + Assert.Throws(() => Numerics.GoodnessOfFit.SampleStandardError(modelled, observed, 2)); + } + } +} diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 9f2fdcd0..77a3231b 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -142,6 +142,7 @@ +