Skip to content
Snippets Groups Projects
Commit 20f7c338 authored by Stefan Berthold's avatar Stefan Berthold
Browse files

fix: quantile as defined in math.

parent f91965cd
Branches
Tags
No related merge requests found
......@@ -33,12 +33,20 @@ intersectionWith f a b
a' = Map.fromList $ P.map toPair $ toList a
b' = Map.fromList $ P.map toPair $ toList b
-- | x%-Quantile: the events that stand (at least) for x percent likelihood
quantile :: (Ord a, Ord x, Fractional x) => x -> Distribution a x -> Distribution [a] x
quantile x = fst . foldl cut (empty,0) where
cut (new,q) p
| q + prob p <= 1-x/100 = (new, q + prob p)
| otherwise = (add (p { prob = prob p/(1-q) }) new, q)
add p = insert (p { event = [event p] })
-- | Quantile
quantile :: (Ord a, Ord x, Fractional x) => x -> Distribution a x -> Probability a x
quantile x ps
| x > 1 = findMax ps
| x < 0 = findMin ps
| otherwise = quantile' x (prob min) min ps'
where (min, ps') = deleteFindMin ps
-- | Quantile worker
quantile' :: (Ord a, Ord x, Fractional x) => x -> x -> Probability a x -> Distribution a x -> Probability a x
quantile' x p prev ps
| null ps = prev
| p + (prob min) > x = prev
| otherwise = quantile' x (p + (prob min)) min ps'
where (min, ps') = deleteFindMin ps
-- vim: ft=haskell:sts=2:sw=2:et:nu:ai
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment