{-# 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UniformDistribution -> UniformDistribution -> Bool
$c/= :: UniformDistribution -> UniformDistribution -> Bool
== :: UniformDistribution -> UniformDistribution -> Bool
$c== :: UniformDistribution -> UniformDistribution -> Bool
Eq, Typeable, Typeable UniformDistribution
UniformDistribution -> DataType
UniformDistribution -> Constr
(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)
gmapMo :: 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
gmapMp :: forall (m :: * -> *).
MonadPlus 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
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UniformDistribution -> m UniformDistribution
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> UniformDistribution -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> UniformDistribution -> u
gmapQ :: forall u.
(forall d. Data d => d -> u) -> UniformDistribution -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> UniformDistribution -> [u]
gmapQr :: 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
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniformDistribution -> r
gmapT :: (forall b. Data b => b -> b)
-> UniformDistribution -> UniformDistribution
$cgmapT :: (forall b. Data b => b -> b)
-> UniformDistribution -> UniformDistribution
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UniformDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UniformDistribution)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UniformDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UniformDistribution)
dataTypeOf :: UniformDistribution -> DataType
$cdataTypeOf :: UniformDistribution -> DataType
toConstr :: UniformDistribution -> Constr
$ctoConstr :: UniformDistribution -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UniformDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UniformDistribution
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UniformDistribution
-> c UniformDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UniformDistribution
-> c UniformDistribution
Data, 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
$cto :: forall x. Rep UniformDistribution x -> UniformDistribution
$cfrom :: forall x. UniformDistribution -> Rep UniformDistribution x
Generic)
instance Show UniformDistribution where
showsPrec :: Int -> UniformDistribution -> ShowS
showsPrec Int
i (UniformDistribution Double
a Double
b) = 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 = 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 forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"uniformA"
Double
b <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"uniformB"
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe UniformDistribution
uniformDistrE Double
a Double
b
parseJSON Value
_ = forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary UniformDistribution where
put :: UniformDistribution -> Put
put (UniformDistribution Double
x Double
y) = forall t. Binary t => t -> Put
put Double
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall t. Binary t => t -> Put
put Double
y
get :: Get UniformDistribution
get = do
Double
a <- forall t. Binary t => Get t
get
Double
b <- forall t. Binary t => Get t
get
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg) forall (m :: * -> *) a. Monad m => a -> m a
return 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 = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => String -> a
error String
errMsg) forall a. a -> a
id 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 forall a. Ord a => a -> a -> Bool
< Double
a = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Double -> Double -> UniformDistribution
UniformDistribution Double
b Double
a
| Double
a forall a. Ord a => a -> a -> Bool
< Double
b = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Double -> Double -> UniformDistribution
UniformDistribution Double
a Double
b
| Bool
otherwise = 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 forall a. Ord a => a -> a -> Bool
< Double
a = Double
0
| Double
x forall a. Ord a => a -> a -> Bool
> Double
b = Double
1
| Bool
otherwise = (Double
x forall a. Num a => a -> a -> a
- Double
a) forall a. Fractional a => a -> a -> a
/ (Double
b 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 forall a. Ord a => a -> a -> Bool
< Double
a = Double
0
| Double
x forall a. Ord a => a -> a -> Bool
> Double
b = Double
0
| Bool
otherwise = Double
1 forall a. Fractional a => a -> a -> a
/ (Double
b forall a. Num a => a -> a -> a
- Double
a)
quantile :: UniformDistribution -> Double -> Double
quantile (UniformDistribution Double
a Double
b) 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 = Double
a forall a. Num a => a -> a -> a
+ (Double
b forall a. Num a => a -> a -> a
- Double
a) forall a. Num a => a -> a -> a
* Double
p
| Bool
otherwise =
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Uniform.quantile: p must be in [0,1] range. Got: "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show Double
p
complQuantile :: UniformDistribution -> Double -> Double
complQuantile (UniformDistribution Double
a Double
b) 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 = Double
b forall a. Num a => a -> a -> a
+ (Double
a forall a. Num a => a -> a -> a
- Double
b) forall a. Num a => a -> a -> a
* Double
p
| Bool
otherwise =
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Uniform.complQuantile: p must be in [0,1] range. Got: "forall a. [a] -> [a] -> [a]
++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 forall a. Num a => a -> a -> a
* (Double
a 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 forall a. Num a => a -> a -> a
* (Double
b forall a. Num a => a -> a -> a
- Double
a)
variance :: UniformDistribution -> Double
variance (UniformDistribution Double
a Double
b) = Double
d forall a. Num a => a -> a -> a
* Double
d forall a. Fractional a => a -> a -> a
/ Double
12 where d :: Double
d = Double
b forall a. Num a => a -> a -> a
- Double
a
instance D.MaybeMean UniformDistribution where
maybeMean :: UniformDistribution -> Maybe Double
maybeMean = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Mean d => d -> Double
D.mean
instance D.MaybeVariance UniformDistribution where
maybeStdDev :: UniformDistribution -> Maybe Double
maybeStdDev = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Variance d => d -> Double
D.stdDev
instance D.Entropy UniformDistribution where
entropy :: UniformDistribution -> Double
entropy (UniformDistribution Double
a Double
b) = forall a. Floating a => a -> a
log (Double
b forall a. Num a => a -> a -> a
- Double
a)
instance D.MaybeEntropy UniformDistribution where
maybeEntropy :: UniformDistribution -> 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 UniformDistribution where
genContVar :: forall g (m :: * -> *).
StatefulGen g m =>
UniformDistribution -> g -> m Double
genContVar (UniformDistribution Double
a Double
b) = forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (Double
a,Double
b)