{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
-- |
-- Module    : Statistics.Distribution.FDistribution
-- Copyright : (c) 2011 Aleksey Khudyakov
-- License   : BSD3
--
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : portable
--
-- Fisher F distribution
module Statistics.Distribution.FDistribution (
    FDistribution
    -- * Constructors
  , fDistribution
  , fDistributionE
  , fDistributionReal
  , fDistributionRealE
    -- * Accessors
  , 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


-- | F distribution
data FDistribution = F { FDistribution -> Double
fDistributionNDF1 :: {-# UNPACK #-} !Double
                       , FDistribution -> Double
fDistributionNDF2 :: {-# UNPACK #-} !Double
                       , FDistribution -> Double
_pdfFactor        :: {-# UNPACK #-} !Double
                       }
                   deriving (FDistribution -> FDistribution -> Bool
(FDistribution -> FDistribution -> Bool)
-> (FDistribution -> FDistribution -> Bool) -> Eq FDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FDistribution -> FDistribution -> Bool
== :: FDistribution -> FDistribution -> Bool
$c/= :: FDistribution -> FDistribution -> Bool
/= :: FDistribution -> FDistribution -> Bool
Eq, Typeable, Typeable FDistribution
Typeable FDistribution =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> FDistribution -> c FDistribution)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c FDistribution)
-> (FDistribution -> Constr)
-> (FDistribution -> DataType)
-> (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))
-> ((forall b. Data b => b -> b) -> FDistribution -> FDistribution)
-> (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 u. (forall d. Data d => d -> u) -> FDistribution -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> FDistribution -> u)
-> (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 (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> FDistribution -> m FDistribution)
-> Data FDistribution
FDistribution -> Constr
FDistribution -> DataType
(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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FDistribution -> c FDistribution
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FDistribution -> c FDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FDistribution
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FDistribution
$ctoConstr :: FDistribution -> Constr
toConstr :: FDistribution -> Constr
$cdataTypeOf :: FDistribution -> DataType
dataTypeOf :: FDistribution -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FDistribution)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FDistribution)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FDistribution)
$cgmapT :: (forall b. Data b => b -> b) -> FDistribution -> FDistribution
gmapT :: (forall b. Data b => b -> b) -> FDistribution -> FDistribution
$cgmapQl :: 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
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FDistribution -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FDistribution -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> FDistribution -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FDistribution -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FDistribution -> u
$cgmapM :: forall (m :: * -> *).
Monad 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
$cgmapMp :: 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
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FDistribution -> m FDistribution
Data, (forall x. FDistribution -> Rep FDistribution x)
-> (forall x. Rep FDistribution x -> FDistribution)
-> Generic FDistribution
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
$cfrom :: forall x. FDistribution -> Rep FDistribution x
from :: forall x. FDistribution -> Rep FDistribution x
$cto :: forall x. Rep FDistribution x -> FDistribution
to :: forall x. Rep FDistribution x -> FDistribution
Generic)

instance Show FDistribution where
  showsPrec :: Int -> FDistribution -> ShowS
showsPrec Int
i (F Double
n Double
m Double
_) = [Char] -> Double -> Double -> Int -> ShowS
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 = [Char]
-> (Double -> Double -> Maybe FDistribution)
-> ReadPrec FDistribution
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 Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"fDistributionNDF1"
    Double
m <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"fDistributionNDF2"
    Parser FDistribution
-> (FDistribution -> Parser FDistribution)
-> Maybe FDistribution
-> Parser FDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> Parser FDistribution
forall a. [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Parser FDistribution) -> [Char] -> Parser FDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsgR Double
n Double
m) FDistribution -> Parser FDistribution
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe FDistribution -> Parser FDistribution)
-> Maybe FDistribution -> Parser FDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe FDistribution
fDistributionRealE Double
n Double
m
  parseJSON Value
_ = Parser FDistribution
forall a. Parser a
forall (f :: * -> *) a. Alternative f => f a
empty

instance Binary FDistribution where
  put :: FDistribution -> Put
put (F Double
n Double
m Double
_) = Double -> Put
forall t. Binary t => t -> Put
put Double
n Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Double -> Put
forall t. Binary t => t -> Put
put Double
m
  get :: Get FDistribution
get = do
    Double
n <- Get Double
forall t. Binary t => Get t
get
    Double
m <- Get Double
forall t. Binary t => Get t
get
    Get FDistribution
-> (FDistribution -> Get FDistribution)
-> Maybe FDistribution
-> Get FDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> Get FDistribution
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get FDistribution) -> [Char] -> Get FDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsgR Double
n Double
m) FDistribution -> Get FDistribution
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe FDistribution -> Get FDistribution)
-> Maybe FDistribution -> Get FDistribution
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 = FDistribution
-> (FDistribution -> FDistribution)
-> Maybe FDistribution
-> FDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> FDistribution
forall a. HasCallStack => [Char] -> a
error ([Char] -> FDistribution) -> [Char] -> FDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [Char]
errMsg Int
n Int
m) FDistribution -> FDistribution
forall a. a -> a
id (Maybe FDistribution -> FDistribution)
-> Maybe FDistribution -> FDistribution
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 = FDistribution
-> (FDistribution -> FDistribution)
-> Maybe FDistribution
-> FDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> FDistribution
forall a. HasCallStack => [Char] -> a
error ([Char] -> FDistribution) -> [Char] -> FDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsgR Double
n Double
m) FDistribution -> FDistribution
forall a. a -> a
id (Maybe FDistribution -> FDistribution)
-> Maybe FDistribution -> FDistribution
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 =
    let n' :: Double
n' = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
        m' :: Double
m' = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m
        f' :: Double
f' = Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double -> Double
forall a. Floating a => a -> a
log Double
m' Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m' Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
log Double
n' Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n') Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double
logBeta (Double
0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
n') (Double
0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
m')
    in FDistribution -> Maybe FDistribution
forall a. a -> Maybe a
Just (FDistribution -> Maybe FDistribution)
-> FDistribution -> Maybe FDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double -> FDistribution
F Double
n' Double
m' Double
f'
  | Bool
otherwise = Maybe FDistribution
forall a. Maybe a
Nothing

fDistributionRealE :: Double -> Double -> Maybe FDistribution
fDistributionRealE :: Double -> Double -> Maybe FDistribution
fDistributionRealE Double
n Double
m
  | Double
n Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
m Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 =
    let f' :: Double
f' = Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double -> Double
forall a. Floating a => a -> a
log Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
log Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double
logBeta (Double
0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
n) (Double
0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
m)
    in FDistribution -> Maybe FDistribution
forall a. a -> Maybe a
Just (FDistribution -> Maybe FDistribution)
-> FDistribution -> Maybe FDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double -> FDistribution
F Double
n Double
m Double
f'
  | Bool
otherwise = Maybe FDistribution
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 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0    = Double
0
    | Bool
otherwise = Double -> Double
forall a. Floating a => a -> a
exp (Double -> Double) -> Double -> Double
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 Double -> Double -> Bool
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 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0       = Double
0
  -- Only matches +∞
  | Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
x = Double
1
  -- NOTE: Here we rely on implementation detail of incompleteBeta. It
  --       computes using series expansion for sufficiently small x
  --       and uses following identity otherwise:
  --
  --           I(x; a, b) = 1 - I(1-x; b, a)
  --
  --       Point is we can compute 1-x as m/(m+y) without loss of
  --       precision for large x. Sadly this switchover point is
  --       implementation detail.
  | Double
n Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= (Double
nDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
m)Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
bx = Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n) (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m) Double
bx
  | Bool
otherwise     = Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m) (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n) Double
bx1
  where
    y :: Double
y   = Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x
    bx :: Double
bx  = Double
y Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
y)
    bx1 :: Double
bx1 = Double
m Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
m Double -> Double -> Double
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 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0        = Double
1
  -- Only matches +∞
  | Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
x  = Double
0
  -- See NOTE at cumulative
  | Double
m Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= (Double
nDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
m)Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
bx = Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m) (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n) Double
bx
  | Bool
otherwise     = Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double -> Double
incompleteBeta (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n) (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m) Double
bx1
  where
    y :: Double
y   = Double
nDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
x
    bx :: Double
bx  = Double
m Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
y)
    bx1 :: Double
bx1 = Double
y Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
m Double -> Double -> Double
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 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
log Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
forall a. Floating a => a -> a
log(Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
nDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
x) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
n Double -> Double -> Double
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 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
0 Bool -> Bool -> Bool
&& Double
p Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
1 =
    let x :: Double
x = Double -> Double -> Double -> Double
invIncompleteBeta (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n) (Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m) Double
p
    in Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
x))
  | Bool
otherwise =
    [Char] -> Double
forall a. HasCallStack => [Char] -> a
error ([Char] -> Double) -> [Char] -> Double
forall a b. (a -> b) -> a -> b
$ [Char]
"Statistics.Distribution.Uniform.quantile: p must be in [0,1] range. Got: "[Char] -> ShowS
forall a. [a] -> [a] -> [a]
++Double -> [Char]
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 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
2     = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double) -> Double -> Maybe Double
forall a b. (a -> b) -> a -> b
$ Double
m Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2)
                      | Bool
otherwise = Maybe Double
forall a. Maybe a
Nothing

instance D.MaybeVariance FDistribution where
  maybeStdDev :: FDistribution -> Maybe Double
maybeStdDev (F Double
n Double
m Double
_)
    | Double
m Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
4     = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double) -> Double -> Maybe Double
forall a b. (a -> b) -> a -> b
$ Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
square Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
square (Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2) Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
4))
    | Bool
otherwise = Maybe Double
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 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
n
        mHalf :: Double
mHalf = Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
m in
    Double -> Double
forall a. Floating a => a -> a
log (Double
nDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
m)
    Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double -> Double
logBeta Double
nHalf Double
mHalf
    Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
nHalf) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
digamma Double
nHalf
    Double -> Double -> Double
forall a. Num a => a -> a -> a
- (Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
mHalf) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
digamma Double
mHalf
    Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
nHalf Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
mHalf) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
digamma (Double
nHalf Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
mHalf)

instance D.MaybeEntropy FDistribution where
  maybeEntropy :: FDistribution -> Maybe Double
maybeEntropy = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (FDistribution -> Double) -> FDistribution -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FDistribution -> Double
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 = FDistribution -> g -> m Double
forall d g (m :: * -> *).
(ContDistr d, StatefulGen g m) =>
d -> g -> m Double
D.genContinuous