summaryrefslogtreecommitdiff
path: root/tests/Tests/KDE.hs
blob: 10c871108d4aa97582edbec0abc3150d0130488c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- | Tests for Kernel density estimates.
module Tests.KDE (
  tests
  )where

import Data.Vector.Unboxed             ((!))
import Numeric.Sum                     (kbn, sumVector)
import Statistics.Sample.KernelDensity
import Test.Tasty                      (TestTree, testGroup)
import Test.Tasty.QuickCheck           (testProperty)
import Test.QuickCheck                 (Property, (==>), counterexample)
import Text.Printf                     (printf)
import qualified Data.Vector.Unboxed as U


tests :: TestTree
tests = testGroup "KDE"
  [ testProperty "integral(PDF) == 1" t_densityIsPDF
  ]

t_densityIsPDF :: [Double] -> Property
t_densityIsPDF vec
  = not (null vec) ==> test
  where
    (xs,ys)  = kde 4096 (U.fromList vec)
    step     = (xs ! 1) - (xs ! 0)
    integral = integratePDF step ys
    --
    test = counterexample (printf "Integral %f" integral)
         $ abs (1 - integral) <= 1e-3



integratePDF :: Double -> U.Vector Double -> Double
integratePDF step vec
  = step * sumVector kbn (U.zipWith (*) vec weights)
  where
    n       = U.length vec
    weights = U.generate n go
      where
        go i | i == 0    = 0.5
             | i == n-1  = 0.5
             | otherwise = 1