Skip to content
Browse files

Check for samples with zero and one element

KDE is not defined for empty sample and automatically determined
range is not sensible for 1 element range
  • Loading branch information...
1 parent 5f4f3e5 commit ab424a4430efb5dff4b5ed15ae77152c2c888215 @Shimuuar Shimuuar committed
Showing with 3 additions and 1 deletion.
  1. +3 −1 Statistics/Sample/KernelDensity.hs
View
4 Statistics/Sample/KernelDensity.hs
@@ -52,7 +52,8 @@ kde :: Int
kde n0 xs = kde_ n0 (lo - range / 10) (hi + range / 10) xs
where
(lo,hi) = minMax xs
- range = hi - lo
+ range | U.length xs <= 1 = 1 -- Unreasonable guess
+ | otherwise = hi - lo
-- | Gaussian kernel density estimator for one-dimensional data, using
-- the method of Botev et al.
@@ -72,6 +73,7 @@ kde_ :: Int
-- ^ Upper bound (@max@) of the mesh range.
-> U.Vector Double -> (U.Vector Double, U.Vector Double)
kde_ n0 min max xs
+ | U.null xs = error "Statistics.KernelDensity.kde: empty sample"
| n0 < 1 = error "Statistics.KernelDensity.kde: invalid number of points"
| otherwise = (mesh, density)
where

0 comments on commit ab424a4

Please sign in to comment.
Something went wrong with that request. Please try again.