From 542abe1c9e351b92f3965ee755e6678ac1f14cf2 Mon Sep 17 00:00:00 2001 From: Mark Wotton Date: Fri, 22 May 2015 11:15:05 +0700 Subject: [PATCH 1/2] add test for regressions --- statistics.cabal | 1 + tests/Tests/Regression.hs | 36 ++++++++++++++++++++++++++++++++++++ tests/tests.hs | 17 ++++++++++------- 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 tests/Tests/Regression.hs diff --git a/statistics.cabal b/statistics.cabal index 6f12ab70..ce17fe7c 100644 --- a/statistics.cabal +++ b/statistics.cabal @@ -124,6 +124,7 @@ test-suite tests Tests.ApproxEq Tests.Correlation Tests.Distribution + Tests.Regression Tests.Function Tests.Helpers Tests.KDE diff --git a/tests/Tests/Regression.hs b/tests/Tests/Regression.hs new file mode 100644 index 00000000..1619f9ae --- /dev/null +++ b/tests/Tests/Regression.hs @@ -0,0 +1,36 @@ +module Tests.Regression + ( tests ) where + +import Data.Vector.Unboxed (Vector) +import qualified Data.Vector.Unboxed as U +import Statistics.Regression +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.QuickCheck2 (testProperty) +import Test.QuickCheck + +data Predictor = Predictor [Vector Double] (Vector Double) + deriving Show + +instance Arbitrary Predictor where + arbitrary = do + -- we pick an arbitrary length N - this is the length the + -- predictor vectors and the responder vector will have + n <- arbitrary `suchThat` (>0) + + matrix <- listOf1 (vectorOf n $ elements [1,2,3]) + -- the matrix must have more rows than columns + `suchThat` ((>n) . length) + + responder <- vectorOf n (elements [4,5,6]) + + return (Predictor (map U.fromList matrix) (U.fromList responder)) + +tests :: Test +tests = testGroup "Regression" + [ testProperty "OLS test - shape" testOLS ] + + +testOLS :: Predictor -> Bool +testOLS (Predictor matrix responder) = + let (v,double) = olsRegress matrix responder in + U.length v == 1 + U.length responder && double >= 0 && double <= 1 diff --git a/tests/tests.hs b/tests/tests.hs index 128ec2ef..025c3ccb 100644 --- a/tests/tests.hs +++ b/tests/tests.hs @@ -1,11 +1,13 @@ -import Test.Framework (defaultMain) -import qualified Tests.Distribution as Distribution -import qualified Tests.Function as Function -import qualified Tests.KDE as KDE -import qualified Tests.Matrix as Matrix +import Test.Framework (defaultMain) +import qualified Tests.Correlation as Correlation +import qualified Tests.Distribution as Distribution +import qualified Tests.Function as Function +import qualified Tests.KDE as KDE +import qualified Tests.Matrix as Matrix import qualified Tests.NonParametric as NonParametric -import qualified Tests.Transform as Transform -import qualified Tests.Correlation as Correlation +import qualified Tests.Regression as Regression +import qualified Tests.Transform as Transform + main :: IO () main = defaultMain [ Distribution.tests @@ -15,4 +17,5 @@ main = defaultMain [ Distribution.tests , NonParametric.tests , Transform.tests , Correlation.tests + , Regression.tests ] From de5148146f6460c263a0560cdeea629408b0a6cf Mon Sep 17 00:00:00 2001 From: Mark Wotton Date: Fri, 22 May 2015 12:25:00 +0700 Subject: [PATCH 2/2] updated tests --- tests/Tests/Regression.hs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/Tests/Regression.hs b/tests/Tests/Regression.hs index 1619f9ae..d3981515 100644 --- a/tests/Tests/Regression.hs +++ b/tests/Tests/Regression.hs @@ -3,6 +3,7 @@ module Tests.Regression import Data.Vector.Unboxed (Vector) import qualified Data.Vector.Unboxed as U +import Debug.Trace (trace) import Statistics.Regression import Test.Framework (Test, testGroup) import Test.Framework.Providers.QuickCheck2 (testProperty) @@ -15,11 +16,11 @@ instance Arbitrary Predictor where arbitrary = do -- we pick an arbitrary length N - this is the length the -- predictor vectors and the responder vector will have - n <- arbitrary `suchThat` (>0) + n <- arbitrary `suchThat` (>1) matrix <- listOf1 (vectorOf n $ elements [1,2,3]) -- the matrix must have more rows than columns - `suchThat` ((>n) . length) + `suchThat` (( Bool testOLS (Predictor matrix responder) = let (v,double) = olsRegress matrix responder in - U.length v == 1 + U.length responder && double >= 0 && double <= 1 + let vlen = U.length v + mlen = length matrix in + if (vlen == mlen+1 && double >=0 && double <= 1) + then True + else trace (show ("ols", vlen,mlen,U.length (head matrix), double)) False