{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
module Statistics.Distribution.FDistribution (
FDistribution
, fDistribution
, fDistributionE
, fDistributionReal
, fDistributionRealE
, fDistributionNDF1
, fDistributionNDF2
) where
import Control.Applicative
import Data.Aeson (FromJSON(..), ToJSON, Value(..), (.:))
import Data.Binary (Binary(..))
import Data.Data (Data, Typeable)
import GHC.Generics (Generic)
import Numeric.SpecFunctions (
logBeta, incompleteBeta, invIncompleteBeta, digamma)
import Numeric.MathFunctions.Constants (m_neg_inf)
import qualified Statistics.Distribution as D
import Statistics.Function (square)
import Statistics.Internal
data FDistribution = F { FDistribution -> Double
fDistributionNDF1 :: {-# UNPACK #-} !Double
, FDistribution -> Double
fDistributionNDF2 :: {-# UNPACK #-} !Double
, FDistribution -> Double
_pdfFactor :: {-# UNPACK #-} !Double
}
deriving (FDistribution -> FDistribution -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FDistribution -> FDistribution -> Bool
$c/= :: FDistribution -> FDistribution -> Bool
== :: FDistribution -> FDistribution -> Bool
$c== :: FDistribution -> FDistribution -> Bool
Eq, Typeable, Typeable FDistribution
FDistribution -> DataType
FDistribution -> Constr
(forall b. Data b => b -> b) -> FDistribution -> FDistribution
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> FDistribution -> u
forall u. (forall d. Data d => d -> u) -> FDistribution -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FDistribution
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FDistribution -> c FDistribution
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FDistribution)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FDistribution)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FDistribution -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FDistribution -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> FDistribution -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FDistribution -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
gmapT :: (forall b. Data b => b -> b) -> FDistribution -> FDistribution
$cgmapT :: (forall b. Data b => b -> b) -> FDistribution -> FDistribution
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FDistribution)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FDistribution)
dataTypeOf :: FDistribution -> DataType
$cdataTypeOf :: FDistribution -> DataType
toConstr :: FDistribution -> Constr
$ctoConstr :: FDistribution -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FDistribution
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FDistribution -> c FDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FDistribution -> c FDistribution
Data, forall x. Rep FDistribution x -> FDistribution
forall x. FDistribution -> Rep FDistribution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep FDistribution x -> FDistribution
$cfrom :: forall x. FDistribution -> Rep FDistribution x
Generic)
instance Show FDistribution where
showsPrec :: Int -> FDistribution -> ShowS
showsPrec Int
i (F Double
n Double
m Double
_) = forall a b. (Show a, Show b) => [Char] -> a -> b -> Int -> ShowS
defaultShow2 [Char]
"fDistributionReal" Double
n Double
m Int
i
instance Read FDistribution where
readPrec :: ReadPrec FDistribution
readPrec = forall a b r.
(Read a, Read b) =>
[Char] -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 [Char]
"fDistributionReal" Double -> Double -> Maybe FDistribution
fDistributionRealE
instance ToJSON FDistribution
instance FromJSON FDistribution where
parseJSON :: Value -> Parser FDistribution
parseJSON (Object Object
v) = do
Double
n <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"fDistributionNDF1"
Double
m <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"fDistributionNDF2"
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsgR Double
n Double
m) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe FDistribution
fDistributionRealE Double
n Double
m
parseJSON Value
_ = forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary FDistribution where
put :: FDistribution -> Put
put (F Double
n Double
m Double
_) = forall t. Binary t => t -> Put
put Double
n forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall t. Binary t => t -> Put
put Double
m
get :: Get FDistribution
get = do
Double
n <- forall t. Binary t => Get t
get
Double
m <- forall t. Binary t => Get t
get
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsgR Double
n Double
m) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe FDistribution
fDistributionRealE Double
n Double
m
fDistribution :: Int -> Int -> FDistribution
fDistribution :: Int -> Int -> FDistribution
fDistribution Int
n Int
m = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ Int -> Int -> [Char]
errMsg Int
n Int
m) forall a. a -> a
id forall a b. (a -> b) -> a -> b
$ Int -> Int -> Maybe FDistribution
fDistributionE Int
n Int
m
fDistributionReal :: Double -> Double -> FDistribution
fDistributionReal :: Double -> Double -> FDistribution
fDistributionReal Double
n Double
m = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsgR Double
n Double
m) forall a. a -> a
id forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe FDistribution
fDistributionRealE Double
n Double
m
fDistributionE :: Int -> Int -> Maybe FDistribution
fDistributionE :: Int -> Int -> Maybe FDistribution
fDistributionE Int
n Int
m
| Int
n forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
m forall a. Ord a => a -> a -> Bool
> Int
0 =
let n' :: Double
n' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
m' :: Double
m' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m
f' :: Double
f' = Double
0.5 forall a. Num a => a -> a -> a
* (forall a. Floating a => a -> a
log Double
m' forall a. Num a => a -> a -> a
* Double
m' forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
log Double
n' forall a. Num a => a -> a -> a
* Double
n') forall a. Num a => a -> a -> a
- Double -> Double -> Double
logBeta (Double
0.5forall a. Num a => a -> a -> a
*Double
n') (Double
0.5forall a. Num a => a -> a -> a
*Double
m')
in forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double -> FDistribution
F Double
n' Double
m' Double
f'
| Bool
otherwise = forall a. Maybe a
Nothing
fDistributionRealE :: Double -> Double -> Maybe FDistribution
fDistributionRealE :: Double -> Double -> Maybe FDistribution
fDistributionRealE Double
n Double
m
| Double
n forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
m forall a. Ord a => a -> a -> Bool
> Double
0 =
let f' :: Double
f' = Double
0.5 forall a. Num a => a -> a -> a
* (forall a. Floating a => a -> a
log Double
m forall a. Num a => a -> a -> a
* Double
m forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
log Double
n forall a. Num a => a -> a -> a
* Double
n) forall a. Num a => a -> a -> a
- Double -> Double -> Double
logBeta (Double
0.5forall a. Num a => a -> a -> a
*Double
n) (Double
0.5forall a. Num a => a -> a -> a
*Double
m)
in forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double -> FDistribution
F Double
n Double
m Double
f'
| Bool
otherwise = forall a. Maybe a
Nothing
errMsg :: Int -> Int -> String
errMsg :: Int -> Int -> [Char]
errMsg Int
_ Int
_ = [Char]
"Statistics.Distribution.FDistribution.fDistribution: non-positive number of degrees of freedom"
errMsgR :: Double -> Double -> String
errMsgR :: Double -> Double -> [Char]
errMsgR Double
_ Double
_ = [Char]
"Statistics.Distribution.FDistribution.fDistribution: non-positive number of degrees of freedom"
instance D.Distribution FDistribution where
cumulative :: FDistribution -> Double -> Double
cumulative = FDistribution -> Double -> Double
cumulative
complCumulative :: FDistribution -> Double -> Double
complCumulative = FDistribution -> Double -> Double
complCumulative
instance D.ContDistr FDistribution where
density :: FDistribution -> Double -> Double
density FDistribution
d Double
x
| Double
x forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
0
| Bool
otherwise = forall a. Floating a => a -> a
exp forall a b. (a -> b) -> a -> b
$ FDistribution -> Double -> Double
logDensity FDistribution
d Double
x
logDensity :: FDistribution -> Double -> Double
logDensity FDistribution
d Double
x
| Double
x forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
m_neg_inf
| Bool
otherwise = FDistribution -> Double -> Double
logDensity FDistribution
d Double
x
quantile :: FDistribution -> Double -> Double
quantile = FDistribution -> Double -> Double
quantile
cumulative :: FDistribution -> Double -> Double
cumulative :: FDistribution -> Double -> Double
cumulative (F Double
n Double
m Double
_) Double
x
| Double
x forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
0
| forall a. RealFloat a => a -> Bool
isInfinite Double
x = Double
1
| Double
n forall a. Ord a => a -> a -> Bool
>= (Double
nforall a. Num a => a -> a -> a
+Double
m)forall a. Num a => a -> a -> a
*Double
bx = Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 forall a. Num a => a -> a -> a
* Double
n) (Double
0.5 forall a. Num a => a -> a -> a
* Double
m) Double
bx
| Bool
otherwise = Double
1 forall a. Num a => a -> a -> a
- Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 forall a. Num a => a -> a -> a
* Double
m) (Double
0.5 forall a. Num a => a -> a -> a
* Double
n) Double
bx1
where
y :: Double
y = Double
n forall a. Num a => a -> a -> a
* Double
x
bx :: Double
bx = Double
y forall a. Fractional a => a -> a -> a
/ (Double
m forall a. Num a => a -> a -> a
+ Double
y)
bx1 :: Double
bx1 = Double
m forall a. Fractional a => a -> a -> a
/ (Double
m forall a. Num a => a -> a -> a
+ Double
y)
complCumulative :: FDistribution -> Double -> Double
complCumulative :: FDistribution -> Double -> Double
complCumulative (F Double
n Double
m Double
_) Double
x
| Double
x forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
1
| forall a. RealFloat a => a -> Bool
isInfinite Double
x = Double
0
| Double
m forall a. Ord a => a -> a -> Bool
>= (Double
nforall a. Num a => a -> a -> a
+Double
m)forall a. Num a => a -> a -> a
*Double
bx = Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 forall a. Num a => a -> a -> a
* Double
m) (Double
0.5 forall a. Num a => a -> a -> a
* Double
n) Double
bx
| Bool
otherwise = Double
1 forall a. Num a => a -> a -> a
- Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 forall a. Num a => a -> a -> a
* Double
n) (Double
0.5 forall a. Num a => a -> a -> a
* Double
m) Double
bx1
where
y :: Double
y = Double
nforall a. Num a => a -> a -> a
*Double
x
bx :: Double
bx = Double
m forall a. Fractional a => a -> a -> a
/ (Double
m forall a. Num a => a -> a -> a
+ Double
y)
bx1 :: Double
bx1 = Double
y forall a. Fractional a => a -> a -> a
/ (Double
m forall a. Num a => a -> a -> a
+ Double
y)
logDensity :: FDistribution -> Double -> Double
logDensity :: FDistribution -> Double -> Double
logDensity (F Double
n Double
m Double
fac) Double
x
= Double
fac forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
log Double
x forall a. Num a => a -> a -> a
* (Double
0.5 forall a. Num a => a -> a -> a
* Double
n forall a. Num a => a -> a -> a
- Double
1) forall a. Num a => a -> a -> a
- forall a. Floating a => a -> a
log(Double
m forall a. Num a => a -> a -> a
+ Double
nforall a. Num a => a -> a -> a
*Double
x) forall a. Num a => a -> a -> a
* Double
0.5 forall a. Num a => a -> a -> a
* (Double
n forall a. Num a => a -> a -> a
+ Double
m)
quantile :: FDistribution -> Double -> Double
quantile :: FDistribution -> Double -> Double
quantile (F Double
n Double
m Double
_) Double
p
| Double
p forall a. Ord a => a -> a -> Bool
>= Double
0 Bool -> Bool -> Bool
&& Double
p forall a. Ord a => a -> a -> Bool
<= Double
1 =
let x :: Double
x = Double -> Double -> Double -> Double
invIncompleteBeta (Double
0.5 forall a. Num a => a -> a -> a
* Double
n) (Double
0.5 forall a. Num a => a -> a -> a
* Double
m) Double
p
in Double
m forall a. Num a => a -> a -> a
* Double
x forall a. Fractional a => a -> a -> a
/ (Double
n forall a. Num a => a -> a -> a
* (Double
1 forall a. Num a => a -> a -> a
- Double
x))
| Bool
otherwise =
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Statistics.Distribution.Uniform.quantile: p must be in [0,1] range. Got: "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show Double
p
instance D.MaybeMean FDistribution where
maybeMean :: FDistribution -> Maybe Double
maybeMean (F Double
_ Double
m Double
_) | Double
m forall a. Ord a => a -> a -> Bool
> Double
2 = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Double
m forall a. Fractional a => a -> a -> a
/ (Double
m forall a. Num a => a -> a -> a
- Double
2)
| Bool
otherwise = forall a. Maybe a
Nothing
instance D.MaybeVariance FDistribution where
maybeStdDev :: FDistribution -> Maybe Double
maybeStdDev (F Double
n Double
m Double
_)
| Double
m forall a. Ord a => a -> a -> Bool
> Double
4 = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Double
2 forall a. Num a => a -> a -> a
* Double -> Double
square Double
m forall a. Num a => a -> a -> a
* (Double
m forall a. Num a => a -> a -> a
+ Double
n forall a. Num a => a -> a -> a
- Double
2) forall a. Fractional a => a -> a -> a
/ (Double
n forall a. Num a => a -> a -> a
* Double -> Double
square (Double
m forall a. Num a => a -> a -> a
- Double
2) forall a. Num a => a -> a -> a
* (Double
m forall a. Num a => a -> a -> a
- Double
4))
| Bool
otherwise = forall a. Maybe a
Nothing
instance D.Entropy FDistribution where
entropy :: FDistribution -> Double
entropy (F Double
n Double
m Double
_) =
let nHalf :: Double
nHalf = Double
0.5 forall a. Num a => a -> a -> a
* Double
n
mHalf :: Double
mHalf = Double
0.5 forall a. Num a => a -> a -> a
* Double
m in
forall a. Floating a => a -> a
log (Double
nforall a. Fractional a => a -> a -> a
/Double
m)
forall a. Num a => a -> a -> a
+ Double -> Double -> Double
logBeta Double
nHalf Double
mHalf
forall a. Num a => a -> a -> a
+ (Double
1 forall a. Num a => a -> a -> a
- Double
nHalf) forall a. Num a => a -> a -> a
* Double -> Double
digamma Double
nHalf
forall a. Num a => a -> a -> a
- (Double
1 forall a. Num a => a -> a -> a
+ Double
mHalf) forall a. Num a => a -> a -> a
* Double -> Double
digamma Double
mHalf
forall a. Num a => a -> a -> a
+ (Double
nHalf forall a. Num a => a -> a -> a
+ Double
mHalf) forall a. Num a => a -> a -> a
* Double -> Double
digamma (Double
nHalf forall a. Num a => a -> a -> a
+ Double
mHalf)
instance D.MaybeEntropy FDistribution where
maybeEntropy :: FDistribution -> Maybe Double
maybeEntropy = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Entropy d => d -> Double
D.entropy
instance D.ContGen FDistribution where
genContVar :: forall g (m :: * -> *).
StatefulGen g m =>
FDistribution -> g -> m Double
genContVar = forall d g (m :: * -> *).
(ContDistr d, StatefulGen g m) =>
d -> g -> m Double
D.genContinuous