    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
    -- |
    -- Module    : Statistics.Distribution.Uniform
    -- Copyright : (c) 2011 Aleksey Khudyakov
    -- License   : BSD3
    --
    -- Maintainer  : bos@serpentine.com
    -- Stability   : experimental
    -- Portability : portable
    --
    -- Variate distributed uniformly in the interval.
    module Statistics.Distribution.Uniform
        (
          UniformDistribution
        -- * Constructors
        , uniformDistr
        , uniformDistrE
        -- ** Accessors
        , uniformA
        , uniformB
        ) where

    import Control.Applicative
    import Data.Aeson          (FromJSON(..), ToJSON, Value(..), (.:))
    import Data.Binary         (Binary(..))
    import Data.Data           (Data, Typeable)
    import GHC.Generics        (Generic)
    import qualified System.Random.MWC       as MWC

    import qualified Statistics.Distribution as D
    import Statistics.Internal

    -- | Uniform distribution from A to B
    data UniformDistribution = UniformDistribution {
          UniformDistribution -> Double
    uniformA :: {-# UNPACK #-} !Double -- ^ Low boundary of distribution
        , UniformDistribution -> Double
    uniformB :: {-# UNPACK #-} !Double -- ^ Upper boundary of distribution
        } deriving (UniformDistribution -> UniformDistribution -> Bool
    (UniformDistribution -> UniformDistribution -> Bool)
    -> (UniformDistribution -> UniformDistribution -> Bool)
    -> Eq UniformDistribution
    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
    DataType
    Constr
    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 -> DataType
    UniformDistribution -> Constr
    (forall b. Data b => b -> b)
    -> UniformDistribution -> UniformDistribution
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> UniformDistribution
    -> c UniformDistribution
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c 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)
    $cUniformDistribution :: Constr
    $tUniformDistribution :: DataType
    gmapMo :: (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 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 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 :: Int -> (forall d. Data d => d -> u) -> UniformDistribution -> u
    $cgmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> UniformDistribution -> u
    gmapQ :: (forall d. Data d => d -> u) -> UniformDistribution -> [u]
    $cgmapQ :: forall u.
    (forall d. Data d => d -> u) -> UniformDistribution -> [u]
    gmapQr :: (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 :: (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 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 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 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 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
    $cp1Data :: Typeable 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
    $cto :: forall x. Rep UniformDistribution x -> UniformDistribution
    $cfrom :: forall x. UniformDistribution -> Rep UniformDistribution x
    Generic)

    instance Show UniformDistribution where
      showsPrec :: Int -> UniformDistribution -> ShowS
    showsPrec i :: Int
    i (UniformDistribution a :: Double
    a b :: Double
    b) = String -> Double -> Double -> Int -> ShowS
    forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
    defaultShow2 "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 "uniformDistr" Double -> Double -> Maybe UniformDistribution
    uniformDistrE

    instance ToJSON UniformDistribution
    instance FromJSON UniformDistribution where
      parseJSON :: Value -> Parser UniformDistribution
    parseJSON (Object v :: Object
    v) = do
        Double
    a <- Object
    v Object -> Text -> Parser Double
    forall a. FromJSON a => Object -> Text -> Parser a
    .: "uniformA"
        Double
    b <- Object
    v Object -> Text -> Parser Double
    forall a. FromJSON a => Object -> Text -> Parser a
    .: "uniformB"
        Parser UniformDistribution
    -> (UniformDistribution -> Parser UniformDistribution)
    -> Maybe UniformDistribution
    -> Parser UniformDistribution
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe (String -> Parser UniformDistribution
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail String
    errMsg) UniformDistribution -> Parser UniformDistribution
    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 _ = Parser UniformDistribution
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance Binary UniformDistribution where
      put :: UniformDistribution -> Put
    put (UniformDistribution x :: Double
    x y :: Double
    y) = Double -> Put
    forall t. Binary t => t -> Put
    put Double
    x Put -> Put -> Put
    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 (m :: * -> *) a. MonadFail m => String -> m a
    fail String
    errMsg) UniformDistribution -> Get UniformDistribution
    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

    -- | Create uniform distribution.
    uniformDistr :: Double -> Double -> UniformDistribution
    uniformDistr :: Double -> Double -> UniformDistribution
    uniformDistr a :: Double
    a b :: 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

    -- | Create uniform distribution.
    uniformDistrE :: Double -> Double -> Maybe UniformDistribution
    uniformDistrE :: Double -> Double -> Maybe UniformDistribution
    uniformDistrE a :: Double
    a b :: 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
    -- NOTE: failure is in default branch to guard against NaNs.

    errMsg :: String
    errMsg :: String
    errMsg = "Statistics.Distribution.Uniform.uniform: wrong parameters"

    instance D.Distribution UniformDistribution where
      cumulative :: UniformDistribution -> Double -> Double
    cumulative (UniformDistribution a :: Double
    a b :: Double
    b) x :: Double
    x
        | Double
    x Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    < Double
    a     = 0
        | Double
    x Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    > Double
    b     = 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 a :: Double
    a b :: Double
    b) x :: Double
    x
        | Double
    x Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    < Double
    a     = 0
        | Double
    x Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    > Double
    b     = 0
        | Bool
    otherwise = 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 a :: Double
    a b :: Double
    b) p :: Double
    p
        | Double
    p Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Double
    p Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 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
    $ "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 a :: Double
    a b :: Double
    b) p :: Double
    p
        | Double
    p Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Double
    p Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 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
    $ "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 a :: Double
    a b :: Double
    b) = 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
      -- NOTE: 1/sqrt 12 is not constant folded (#4101) so it's written as
      --       numerical constant. (Also FIXME!)
      stdDev :: UniformDistribution -> Double
    stdDev   (UniformDistribution a :: Double
    a b :: Double
    b) = 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 a :: Double
    a b :: 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
    / 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 a :: Double
    a b :: 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 :: UniformDistribution -> Gen (PrimState m) -> m Double
    genContVar (UniformDistribution a :: Double
    a b :: Double
    b) gen :: Gen (PrimState m)
    gen = (Double, Double) -> Gen (PrimState m) -> m Double
    forall a (m :: * -> *).
    (Variate a, PrimMonad m) =>
    (a, a) -> Gen (PrimState m) -> m a
    MWC.uniformR (Double
    a,Double
    b) Gen (PrimState m)
    gen