{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, OverloadedStrings #-}
module Statistics.Distribution.DiscreteUniform
(
DiscreteUniform
, discreteUniform
, discreteUniformAB
, rangeFrom
, rangeTo
) where
import Control.Applicative (empty)
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 DiscreteUniform = U {
DiscreteUniform -> Int
rangeFrom :: {-# UNPACK #-} !Int
, DiscreteUniform -> Int
rangeTo :: {-# UNPACK #-} !Int
} deriving (DiscreteUniform -> DiscreteUniform -> Bool
(DiscreteUniform -> DiscreteUniform -> Bool)
-> (DiscreteUniform -> DiscreteUniform -> Bool)
-> Eq DiscreteUniform
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DiscreteUniform -> DiscreteUniform -> Bool
$c/= :: DiscreteUniform -> DiscreteUniform -> Bool
== :: DiscreteUniform -> DiscreteUniform -> Bool
$c== :: DiscreteUniform -> DiscreteUniform -> Bool
Eq, Typeable, Typeable DiscreteUniform
DataType
Constr
Typeable DiscreteUniform
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DiscreteUniform -> c DiscreteUniform)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DiscreteUniform)
-> (DiscreteUniform -> Constr)
-> (DiscreteUniform -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DiscreteUniform))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c DiscreteUniform))
-> ((forall b. Data b => b -> b)
-> DiscreteUniform -> DiscreteUniform)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r)
-> (forall u.
(forall d. Data d => d -> u) -> DiscreteUniform -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> DiscreteUniform -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform)
-> Data DiscreteUniform
DiscreteUniform -> DataType
DiscreteUniform -> Constr
(forall b. Data b => b -> b) -> DiscreteUniform -> DiscreteUniform
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DiscreteUniform -> c DiscreteUniform
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DiscreteUniform
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) -> DiscreteUniform -> u
forall u. (forall d. Data d => d -> u) -> DiscreteUniform -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DiscreteUniform
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DiscreteUniform -> c DiscreteUniform
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DiscreteUniform)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c DiscreteUniform)
$cU :: Constr
$tDiscreteUniform :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
gmapMp :: (forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
gmapM :: (forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> DiscreteUniform -> m DiscreteUniform
gmapQi :: Int -> (forall d. Data d => d -> u) -> DiscreteUniform -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> DiscreteUniform -> u
gmapQ :: (forall d. Data d => d -> u) -> DiscreteUniform -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> DiscreteUniform -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DiscreteUniform -> r
gmapT :: (forall b. Data b => b -> b) -> DiscreteUniform -> DiscreteUniform
$cgmapT :: (forall b. Data b => b -> b) -> DiscreteUniform -> DiscreteUniform
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c DiscreteUniform)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c DiscreteUniform)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c DiscreteUniform)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DiscreteUniform)
dataTypeOf :: DiscreteUniform -> DataType
$cdataTypeOf :: DiscreteUniform -> DataType
toConstr :: DiscreteUniform -> Constr
$ctoConstr :: DiscreteUniform -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DiscreteUniform
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DiscreteUniform
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DiscreteUniform -> c DiscreteUniform
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DiscreteUniform -> c DiscreteUniform
$cp1Data :: Typeable DiscreteUniform
Data, (forall x. DiscreteUniform -> Rep DiscreteUniform x)
-> (forall x. Rep DiscreteUniform x -> DiscreteUniform)
-> Generic DiscreteUniform
forall x. Rep DiscreteUniform x -> DiscreteUniform
forall x. DiscreteUniform -> Rep DiscreteUniform x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DiscreteUniform x -> DiscreteUniform
$cfrom :: forall x. DiscreteUniform -> Rep DiscreteUniform x
Generic)
instance Show DiscreteUniform where
showsPrec :: Int -> DiscreteUniform -> ShowS
showsPrec Int
i (U Int
a Int
b) = String -> Int -> Int -> Int -> ShowS
forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
defaultShow2 String
"discreteUniformAB" Int
a Int
b Int
i
instance Read DiscreteUniform where
readPrec :: ReadPrec DiscreteUniform
readPrec = String
-> (Int -> Int -> Maybe DiscreteUniform)
-> ReadPrec DiscreteUniform
forall a b r.
(Read a, Read b) =>
String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 String
"discreteUniformAB" (\Int
a Int
b -> DiscreteUniform -> Maybe DiscreteUniform
forall a. a -> Maybe a
Just (Int -> Int -> DiscreteUniform
discreteUniformAB Int
a Int
b))
instance ToJSON DiscreteUniform
instance FromJSON DiscreteUniform where
parseJSON :: Value -> Parser DiscreteUniform
parseJSON (Object Object
v) = do
Int
a <- Object
v Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"uniformA"
Int
b <- Object
v Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"uniformB"
DiscreteUniform -> Parser DiscreteUniform
forall (m :: * -> *) a. Monad m => a -> m a
return (DiscreteUniform -> Parser DiscreteUniform)
-> DiscreteUniform -> Parser DiscreteUniform
forall a b. (a -> b) -> a -> b
$ Int -> Int -> DiscreteUniform
discreteUniformAB Int
a Int
b
parseJSON Value
_ = Parser DiscreteUniform
forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary DiscreteUniform where
put :: DiscreteUniform -> Put
put (U Int
a Int
b) = Int -> Put
forall t. Binary t => t -> Put
put Int
a Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Put
forall t. Binary t => t -> Put
put Int
b
get :: Get DiscreteUniform
get = Int -> Int -> DiscreteUniform
discreteUniformAB (Int -> Int -> DiscreteUniform)
-> Get Int -> Get (Int -> DiscreteUniform)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int
forall t. Binary t => Get t
get Get (Int -> DiscreteUniform) -> Get Int -> Get DiscreteUniform
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int
forall t. Binary t => Get t
get
instance D.Distribution DiscreteUniform where
cumulative :: DiscreteUniform -> Double -> Double
cumulative (U Int
a Int
b) Double
x
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
a = Double
0
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b = Double
1
| Bool
otherwise = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
instance D.DiscreteDistr DiscreteUniform where
probability :: DiscreteUniform -> Int -> Double
probability (U Int
a Int
b) Int
k
| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
a Bool -> Bool -> Bool
&& Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
b = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = Double
0
instance D.Mean DiscreteUniform where
mean :: DiscreteUniform -> Double
mean (U Int
a Int
b) = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
aInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
b)Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2
instance D.Variance DiscreteUniform where
variance :: DiscreteUniform -> Double
variance (U Int
a Int
b) = (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)Double -> Int -> Double
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
2::Int) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
12
instance D.MaybeMean DiscreteUniform where
maybeMean :: DiscreteUniform -> Maybe Double
maybeMean = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (DiscreteUniform -> Double) -> DiscreteUniform -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiscreteUniform -> Double
forall d. Mean d => d -> Double
D.mean
instance D.MaybeVariance DiscreteUniform where
maybeStdDev :: DiscreteUniform -> Maybe Double
maybeStdDev = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (DiscreteUniform -> Double) -> DiscreteUniform -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiscreteUniform -> Double
forall d. Variance d => d -> Double
D.stdDev
maybeVariance :: DiscreteUniform -> Maybe Double
maybeVariance = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (DiscreteUniform -> Double) -> DiscreteUniform -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiscreteUniform -> Double
forall d. Variance d => d -> Double
D.variance
instance D.Entropy DiscreteUniform where
entropy :: DiscreteUniform -> Double
entropy (U Int
a Int
b) = Double -> Double
forall a. Floating a => a -> a
log (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Double) -> Int -> Double
forall a b. (a -> b) -> a -> b
$ Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
instance D.MaybeEntropy DiscreteUniform where
maybeEntropy :: DiscreteUniform -> Maybe Double
maybeEntropy = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (DiscreteUniform -> Double) -> DiscreteUniform -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiscreteUniform -> Double
forall d. Entropy d => d -> Double
D.entropy
instance D.ContGen DiscreteUniform where
genContVar :: DiscreteUniform -> g -> m Double
genContVar DiscreteUniform
d = (Int -> Double) -> m Int -> m Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (m Int -> m Double) -> (g -> m Int) -> g -> m Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiscreteUniform -> g -> m Int
forall d g (m :: * -> *).
(DiscreteGen d, StatefulGen g m) =>
d -> g -> m Int
D.genDiscreteVar DiscreteUniform
d
instance D.DiscreteGen DiscreteUniform where
genDiscreteVar :: DiscreteUniform -> g -> m Int
genDiscreteVar (U Int
a Int
b) = (Int, Int) -> g -> m Int
forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (Int
a,Int
b)
discreteUniform :: Int
-> DiscreteUniform
discreteUniform :: Int -> DiscreteUniform
discreteUniform Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 = String -> DiscreteUniform
forall a. HasCallStack => String -> a
error (String -> DiscreteUniform) -> String -> DiscreteUniform
forall a b. (a -> b) -> a -> b
$ String
msg String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"range must be > 0. Got " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
| Bool
otherwise = Int -> Int -> DiscreteUniform
U Int
1 Int
n
where msg :: String
msg = String
"Statistics.Distribution.DiscreteUniform.discreteUniform: "
discreteUniformAB :: Int
-> Int
-> DiscreteUniform
discreteUniformAB :: Int -> Int -> DiscreteUniform
discreteUniformAB Int
a Int
b
| Int
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
a = Int -> Int -> DiscreteUniform
U Int
b Int
a
| Bool
otherwise = Int -> Int -> DiscreteUniform
U Int
a Int
b