{-# LANGUAGE ViewPatterns #-}
module Statistics.ConfidenceInt (
poissonCI
, poissonNormalCI
, binomialCI
, naiveBinomialCI
) where
import Statistics.Distribution
import Statistics.Distribution.ChiSquared
import Statistics.Distribution.Beta
import Statistics.Types
poissonNormalCI :: Int -> Estimate NormalErr Double
poissonNormalCI :: Int -> Estimate NormalErr Double
poissonNormalCI Int
n
| Int
n forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.ConfidenceInt.poissonNormalCI negative number of trials"
| Bool
otherwise = forall a. a -> a -> Estimate NormalErr a
estimateNormErr Double
n' (forall a. Floating a => a -> a
sqrt Double
n')
where
n' :: Double
n' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
poissonCI :: CL Double -> Int -> Estimate ConfInt Double
poissonCI :: CL Double -> Int -> Estimate ConfInt Double
poissonCI cl :: CL Double
cl@(forall a. CL a -> a
significanceLevel -> Double
p) Int
n
| Int
n forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.ConfidenceInt.poissonCI: negative number of trials"
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. Num a => a -> (a, a) -> CL Double -> Estimate ConfInt a
estimateFromInterval Double
m (Double
0 ,Double
m2) CL Double
cl
| Bool
otherwise = forall a. Num a => a -> (a, a) -> CL Double -> Estimate ConfInt a
estimateFromInterval Double
m (Double
m1,Double
m2) CL Double
cl
where
m :: Double
m = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
m1 :: Double
m1 = Double
0.5 forall a. Num a => a -> a -> a
* forall d. ContDistr d => d -> Double -> Double
quantile (Int -> ChiSquared
chiSquared (Int
2forall a. Num a => a -> a -> a
*Int
n )) (Double
pforall a. Fractional a => a -> a -> a
/Double
2)
m2 :: Double
m2 = Double
0.5 forall a. Num a => a -> a -> a
* forall d. ContDistr d => d -> Double -> Double
complQuantile (Int -> ChiSquared
chiSquared (Int
2forall a. Num a => a -> a -> a
*Int
nforall a. Num a => a -> a -> a
+Int
2)) (Double
pforall a. Fractional a => a -> a -> a
/Double
2)
naiveBinomialCI :: Int
-> Int
-> Estimate NormalErr Double
naiveBinomialCI :: Int -> Int -> Estimate NormalErr Double
naiveBinomialCI Int
n Int
k
| Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| Int
k forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.ConfidenceInt.naiveBinomialCI: negative number of events"
| Int
k forall a. Ord a => a -> a -> Bool
> Int
n = forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.ConfidenceInt.naiveBinomialCI: more successes than trials"
| Bool
otherwise = forall a. a -> a -> Estimate NormalErr a
estimateNormErr Double
p Double
σ
where
p :: Double
p = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
σ :: Double
σ = forall a. Floating a => a -> a
sqrt forall a b. (a -> b) -> a -> b
$ Double
p forall a. Num a => a -> a -> a
* (Double
1 forall a. Num a => a -> a -> a
- Double
p) forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
binomialCI :: CL Double
-> Int
-> Int
-> Estimate ConfInt Double
binomialCI :: CL Double -> Int -> Int -> Estimate ConfInt Double
binomialCI cl :: CL Double
cl@(forall a. CL a -> a
significanceLevel -> Double
p) Int
ni Int
ki
| Int
ni forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| Int
ki forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.ConfidenceInt.binomialCI: negative number of events"
| Int
ki forall a. Ord a => a -> a -> Bool
> Int
ni = forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.ConfidenceInt.binomialCI: more successes than trials"
| Int
ki forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. Num a => a -> (a, a) -> CL Double -> Estimate ConfInt a
estimateFromInterval Double
eff (Double
0, Double
ub) CL Double
cl
| Int
ni forall a. Eq a => a -> a -> Bool
== Int
ki = forall a. Num a => a -> (a, a) -> CL Double -> Estimate ConfInt a
estimateFromInterval Double
eff (Double
lb,Double
0 ) CL Double
cl
| Bool
otherwise = forall a. Num a => a -> (a, a) -> CL Double -> Estimate ConfInt a
estimateFromInterval Double
eff (Double
lb,Double
ub) CL Double
cl
where
k :: Double
k = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ki
n :: Double
n = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ni
eff :: Double
eff = Double
k forall a. Fractional a => a -> a -> a
/ Double
n
lb :: Double
lb = forall d. ContDistr d => d -> Double -> Double
quantile (Double -> Double -> BetaDistribution
betaDistr Double
k (Double
n forall a. Num a => a -> a -> a
- Double
k forall a. Num a => a -> a -> a
+ Double
1)) (Double
pforall a. Fractional a => a -> a -> a
/Double
2)
ub :: Double
ub = forall d. ContDistr d => d -> Double -> Double
complQuantile (Double -> Double -> BetaDistribution
betaDistr (Double
k forall a. Num a => a -> a -> a
+ Double
1) (Double
n forall a. Num a => a -> a -> a
- Double
k) ) (Double
pforall a. Fractional a => a -> a -> a
/Double
2)