{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
module Statistics.Distribution.Uniform
(
UniformDistribution
, uniformDistr
, uniformDistrE
, uniformA
, uniformB
) where
import Control.Applicative
import Data.Aeson (FromJSON(..), ToJSON, Value(..), (.:))
import Data.Binary (Binary(..))
import Data.Data (Data, Typeable)
import System.Random.Stateful (uniformRM)
import GHC.Generics (Generic)
import qualified Statistics.Distribution as D
import Statistics.Internal
data UniformDistribution = UniformDistribution {
UniformDistribution -> Double
uniformA :: {-# UNPACK #-} !Double
, UniformDistribution -> Double
uniformB :: {-# UNPACK #-} !Double
} deriving (UniformDistribution -> UniformDistribution -> Bool
(UniformDistribution -> UniformDistribution -> Bool)
-> (UniformDistribution -> UniformDistribution -> Bool)
-> Eq UniformDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UniformDistribution -> UniformDistribution -> Bool
== :: UniformDistribution -> UniformDistribution -> Bool
$c/= :: UniformDistribution -> UniformDistribution -> Bool
/= :: UniformDistribution -> UniformDistribution -> Bool
Eq, Typeable, Typeable UniformDistribution
Typeable UniformDistribution =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UniformDistribution
-> c UniformDistribution)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UniformDistribution)
-> (UniformDistribution -> Constr)
-> (UniformDistribution -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UniformDistribution))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UniformDistribution))
-> ((forall b. Data b => b -> b)
-> UniformDistribution -> UniformDistribution)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r)
-> (forall u.
(forall d. Data d => d -> u) -> UniformDistribution -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> UniformDistribution -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution)
-> Data UniformDistribution
UniformDistribution -> Constr
UniformDistribution -> DataType
(forall b. Data b => b -> b)
-> UniformDistribution -> UniformDistribution
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) -> UniformDistribution -> u
forall u.
(forall d. Data d => d -> u) -> UniformDistribution -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UniformDistribution
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UniformDistribution
-> c UniformDistribution
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UniformDistribution)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UniformDistribution)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UniformDistribution
-> c UniformDistribution
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UniformDistribution
-> c UniformDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UniformDistribution
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UniformDistribution
$ctoConstr :: UniformDistribution -> Constr
toConstr :: UniformDistribution -> Constr
$cdataTypeOf :: UniformDistribution -> DataType
dataTypeOf :: UniformDistribution -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UniformDistribution)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UniformDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UniformDistribution)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UniformDistribution)
$cgmapT :: (forall b. Data b => b -> b)
-> UniformDistribution -> UniformDistribution
gmapT :: (forall b. Data b => b -> b)
-> UniformDistribution -> UniformDistribution
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> UniformDistribution -> [u]
gmapQ :: forall u.
(forall d. Data d => d -> u) -> UniformDistribution -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> UniformDistribution -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> UniformDistribution -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
Data, (forall x. UniformDistribution -> Rep UniformDistribution x)
-> (forall x. Rep UniformDistribution x -> UniformDistribution)
-> Generic UniformDistribution
forall x. Rep UniformDistribution x -> UniformDistribution
forall x. UniformDistribution -> Rep UniformDistribution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. UniformDistribution -> Rep UniformDistribution x
from :: forall x. UniformDistribution -> Rep UniformDistribution x
$cto :: forall x. Rep UniformDistribution x -> UniformDistribution
to :: forall x. Rep UniformDistribution x -> UniformDistribution
Generic)
instance Show UniformDistribution where
showsPrec :: Int -> UniformDistribution -> ShowS
showsPrec Int
i (UniformDistribution Double
a Double
b) = String -> Double -> Double -> Int -> ShowS
forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
defaultShow2 String
"uniformDistr" Double
a Double
b Int
i
instance Read UniformDistribution where
readPrec :: ReadPrec UniformDistribution
readPrec = String
-> (Double -> Double -> Maybe UniformDistribution)
-> ReadPrec UniformDistribution
forall a b r.
(Read a, Read b) =>
String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 String
"uniformDistr" Double -> Double -> Maybe UniformDistribution
uniformDistrE
instance ToJSON UniformDistribution
instance FromJSON UniformDistribution where
parseJSON :: Value -> Parser UniformDistribution
parseJSON (Object Object
v) = do
Double
a <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"uniformA"
Double
b <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"uniformB"
Parser UniformDistribution
-> (UniformDistribution -> Parser UniformDistribution)
-> Maybe UniformDistribution
-> Parser UniformDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Parser UniformDistribution
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg) UniformDistribution -> Parser UniformDistribution
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe UniformDistribution -> Parser UniformDistribution)
-> Maybe UniformDistribution -> Parser UniformDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe UniformDistribution
uniformDistrE Double
a Double
b
parseJSON Value
_ = Parser UniformDistribution
forall a. Parser a
forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary UniformDistribution where
put :: UniformDistribution -> Put
put (UniformDistribution Double
x Double
y) = Double -> Put
forall t. Binary t => t -> Put
put Double
x 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
y
get :: Get UniformDistribution
get = do
Double
a <- Get Double
forall t. Binary t => Get t
get
Double
b <- Get Double
forall t. Binary t => Get t
get
Get UniformDistribution
-> (UniformDistribution -> Get UniformDistribution)
-> Maybe UniformDistribution
-> Get UniformDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Get UniformDistribution
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg) UniformDistribution -> Get UniformDistribution
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe UniformDistribution -> Get UniformDistribution)
-> Maybe UniformDistribution -> Get UniformDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe UniformDistribution
uniformDistrE Double
a Double
b
uniformDistr :: Double -> Double -> UniformDistribution
uniformDistr :: Double -> Double -> UniformDistribution
uniformDistr Double
a Double
b = UniformDistribution
-> (UniformDistribution -> UniformDistribution)
-> Maybe UniformDistribution
-> UniformDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> UniformDistribution
forall a. HasCallStack => String -> a
error String
errMsg) UniformDistribution -> UniformDistribution
forall a. a -> a
id (Maybe UniformDistribution -> UniformDistribution)
-> Maybe UniformDistribution -> UniformDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe UniformDistribution
uniformDistrE Double
a Double
b
uniformDistrE :: Double -> Double -> Maybe UniformDistribution
uniformDistrE :: Double -> Double -> Maybe UniformDistribution
uniformDistrE Double
a Double
b
| Double
b Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
a = UniformDistribution -> Maybe UniformDistribution
forall a. a -> Maybe a
Just (UniformDistribution -> Maybe UniformDistribution)
-> UniformDistribution -> Maybe UniformDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> UniformDistribution
UniformDistribution Double
b Double
a
| Double
a Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
b = UniformDistribution -> Maybe UniformDistribution
forall a. a -> Maybe a
Just (UniformDistribution -> Maybe UniformDistribution)
-> UniformDistribution -> Maybe UniformDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> UniformDistribution
UniformDistribution Double
a Double
b
| Bool
otherwise = Maybe UniformDistribution
forall a. Maybe a
Nothing
errMsg :: String
errMsg :: String
errMsg = String
"Statistics.Distribution.Uniform.uniform: wrong parameters"
instance D.Distribution UniformDistribution where
cumulative :: UniformDistribution -> Double -> Double
cumulative (UniformDistribution Double
a Double
b) Double
x
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
a = Double
0
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
b = Double
1
| Bool
otherwise = (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a)
instance D.ContDistr UniformDistribution where
density :: UniformDistribution -> Double -> Double
density (UniformDistribution Double
a Double
b) Double
x
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
a = Double
0
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
b = Double
0
| Bool
otherwise = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a)
quantile :: UniformDistribution -> Double -> Double
quantile (UniformDistribution Double
a Double
b) 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 = Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
p
| Bool
otherwise =
String -> Double
forall a. HasCallStack => String -> a
error (String -> Double) -> String -> Double
forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Uniform.quantile: p must be in [0,1] range. Got: "String -> ShowS
forall a. [a] -> [a] -> [a]
++Double -> String
forall a. Show a => a -> String
show Double
p
complQuantile :: UniformDistribution -> Double -> Double
complQuantile (UniformDistribution Double
a Double
b) 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 = Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
b) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
p
| Bool
otherwise =
String -> Double
forall a. HasCallStack => String -> a
error (String -> Double) -> String -> Double
forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Uniform.complQuantile: p must be in [0,1] range. Got: "String -> ShowS
forall a. [a] -> [a] -> [a]
++Double -> String
forall a. Show a => a -> String
show Double
p
instance D.Mean UniformDistribution where
mean :: UniformDistribution -> Double
mean (UniformDistribution Double
a Double
b) = Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
b)
instance D.Variance UniformDistribution where
stdDev :: UniformDistribution -> Double
stdDev (UniformDistribution Double
a Double
b) = Double
0.2886751345948129 Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a)
variance :: UniformDistribution -> Double
variance (UniformDistribution Double
a Double
b) = Double
d Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
d Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
12 where d :: Double
d = Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a
instance D.MaybeMean UniformDistribution where
maybeMean :: UniformDistribution -> Maybe Double
maybeMean = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (UniformDistribution -> Double)
-> UniformDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniformDistribution -> Double
forall d. Mean d => d -> Double
D.mean
instance D.MaybeVariance UniformDistribution where
maybeStdDev :: UniformDistribution -> Maybe Double
maybeStdDev = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (UniformDistribution -> Double)
-> UniformDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniformDistribution -> Double
forall d. Variance d => d -> Double
D.stdDev
instance D.Entropy UniformDistribution where
entropy :: UniformDistribution -> Double
entropy (UniformDistribution Double
a Double
b) = Double -> Double
forall a. Floating a => a -> a
log (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a)
instance D.MaybeEntropy UniformDistribution where
maybeEntropy :: UniformDistribution -> Maybe Double
maybeEntropy = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (UniformDistribution -> Double)
-> UniformDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniformDistribution -> Double
forall d. Entropy d => d -> Double
D.entropy
instance D.ContGen UniformDistribution where
genContVar :: forall g (m :: * -> *).
StatefulGen g m =>
UniformDistribution -> g -> m Double
genContVar (UniformDistribution Double
a Double
b) = (Double, Double) -> g -> m Double
forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
forall g (m :: * -> *).
StatefulGen g m =>
(Double, Double) -> g -> m Double
uniformRM (Double
a,Double
b)