    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
    -- |
    -- Module    : Statistics.Distribution.CauchyLorentz
    -- Copyright : (c) 2011 Aleksey Khudyakov
    -- License   : BSD3
    --
    -- Maintainer  : bos@serpentine.com
    -- Stability   : experimental
    -- Portability : portable
    --
    -- The Cauchy-Lorentz distribution. It's also known as Lorentz
    -- distribution or Breit–Wigner distribution.
    --
    -- It doesn't have mean and variance.
    module Statistics.Distribution.CauchyLorentz (
        CauchyDistribution
      , cauchyDistribMedian
      , cauchyDistribScale
        -- * Constructors
      , cauchyDistribution
      , cauchyDistributionE
      , standardCauchy
      ) where

    import Control.Applicative
    import Data.Aeson             (FromJSON(..), ToJSON, Value(..), (.:))
    import Data.Binary            (Binary(..))
    import Data.Maybe             (fromMaybe)
    import Data.Data              (Data, Typeable)
    import GHC.Generics           (Generic)
    import qualified Statistics.Distribution as D
    import Statistics.Internal

    -- | Cauchy-Lorentz distribution.
    data CauchyDistribution = CD {
        -- | Central value of Cauchy-Lorentz distribution which is its
        --   mode and median. Distribution doesn't have mean so function
        --   is named after median.
        CauchyDistribution -> Double
    cauchyDistribMedian :: {-# UNPACK #-} !Double
        -- | Scale parameter of Cauchy-Lorentz distribution. It's
        --   different from variance and specify half width at half
        --   maximum (HWHM).
      , CauchyDistribution -> Double
    cauchyDistribScale  :: {-# UNPACK #-} !Double
      }
      deriving (CauchyDistribution -> CauchyDistribution -> Bool
    (CauchyDistribution -> CauchyDistribution -> Bool)
    -> (CauchyDistribution -> CauchyDistribution -> Bool)
    -> Eq CauchyDistribution
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    /= :: CauchyDistribution -> CauchyDistribution -> Bool
    $c/= :: CauchyDistribution -> CauchyDistribution -> Bool
    == :: CauchyDistribution -> CauchyDistribution -> Bool
    $c== :: CauchyDistribution -> CauchyDistribution -> Bool
    Eq, Typeable, Typeable CauchyDistribution
    DataType
    Constr
    Typeable CauchyDistribution =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g)
     -> CauchyDistribution
     -> c CauchyDistribution)
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c CauchyDistribution)
    -> (CauchyDistribution -> Constr)
    -> (CauchyDistribution -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c CauchyDistribution))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e))
        -> Maybe (c CauchyDistribution))
    -> ((forall b. Data b => b -> b)
        -> CauchyDistribution -> CauchyDistribution)
    -> (forall r r'.
        (r -> r' -> r)
        -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r)
    -> (forall r r'.
        (r' -> r -> r)
        -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r)
    -> (forall u.
        (forall d. Data d => d -> u) -> CauchyDistribution -> [u])
    -> (forall u.
        Int -> (forall d. Data d => d -> u) -> CauchyDistribution -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d)
        -> CauchyDistribution -> m CauchyDistribution)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d)
        -> CauchyDistribution -> m CauchyDistribution)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d)
        -> CauchyDistribution -> m CauchyDistribution)
    -> Data CauchyDistribution
    CauchyDistribution -> DataType
    CauchyDistribution -> Constr
    (forall b. Data b => b -> b)
    -> CauchyDistribution -> CauchyDistribution
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> CauchyDistribution
    -> c CauchyDistribution
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c CauchyDistribution
    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) -> CauchyDistribution -> u
    forall u. (forall d. Data d => d -> u) -> CauchyDistribution -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r
    forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c CauchyDistribution
    forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> CauchyDistribution
    -> c CauchyDistribution
    forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c CauchyDistribution)
    forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c CauchyDistribution)
    $cCD :: Constr
    $tCauchyDistribution :: DataType
    gmapMo :: (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    $cgmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    gmapMp :: (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    $cgmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    gmapM :: (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    $cgmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> CauchyDistribution -> m CauchyDistribution
    gmapQi :: Int -> (forall d. Data d => d -> u) -> CauchyDistribution -> u
    $cgmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> CauchyDistribution -> u
    gmapQ :: (forall d. Data d => d -> u) -> CauchyDistribution -> [u]
    $cgmapQ :: forall u. (forall d. Data d => d -> u) -> CauchyDistribution -> [u]
    gmapQr :: (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r
    gmapQl :: (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r
    $cgmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> CauchyDistribution -> r
    gmapT :: (forall b. Data b => b -> b)
    -> CauchyDistribution -> CauchyDistribution
    $cgmapT :: (forall b. Data b => b -> b)
    -> CauchyDistribution -> CauchyDistribution
    dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c CauchyDistribution)
    $cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c CauchyDistribution)
    dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c CauchyDistribution)
    $cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c CauchyDistribution)
    dataTypeOf :: CauchyDistribution -> DataType
    $cdataTypeOf :: CauchyDistribution -> DataType
    toConstr :: CauchyDistribution -> Constr
    $ctoConstr :: CauchyDistribution -> Constr
    gunfold :: (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c CauchyDistribution
    $cgunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c CauchyDistribution
    gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> CauchyDistribution
    -> c CauchyDistribution
    $cgfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> CauchyDistribution
    -> c CauchyDistribution
    $cp1Data :: Typeable CauchyDistribution
    Data, (forall x. CauchyDistribution -> Rep CauchyDistribution x)
    -> (forall x. Rep CauchyDistribution x -> CauchyDistribution)
    -> Generic CauchyDistribution
    forall x. Rep CauchyDistribution x -> CauchyDistribution
    forall x. CauchyDistribution -> Rep CauchyDistribution x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cto :: forall x. Rep CauchyDistribution x -> CauchyDistribution
    $cfrom :: forall x. CauchyDistribution -> Rep CauchyDistribution x
    Generic)

    instance Show CauchyDistribution where
      showsPrec :: Int -> CauchyDistribution -> ShowS
    showsPrec i :: Int
    i (CD m :: Double
    m s :: Double
    s) = String -> Double -> Double -> Int -> ShowS
    forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
    defaultShow2 "cauchyDistribution" Double
    m Double
    s Int
    i
    instance Read CauchyDistribution where
      readPrec :: ReadPrec CauchyDistribution
    readPrec = String
    -> (Double -> Double -> Maybe CauchyDistribution)
    -> ReadPrec CauchyDistribution
    forall a b r.
    (Read a, Read b) =>
    String -> (a -> b -> Maybe r) -> ReadPrec r
    defaultReadPrecM2 "cauchyDistribution" Double -> Double -> Maybe CauchyDistribution
    cauchyDistributionE

    instance ToJSON   CauchyDistribution
    instance FromJSON CauchyDistribution where
      parseJSON :: Value -> Parser CauchyDistribution
    parseJSON (Object v :: Object
    v) = do
        Double
    m <- Object
    v Object -> Text -> Parser Double
    forall a. FromJSON a => Object -> Text -> Parser a
    .: "cauchyDistribMedian"
        Double
    s <- Object
    v Object -> Text -> Parser Double
    forall a. FromJSON a => Object -> Text -> Parser a
    .: "cauchyDistribScale"
        Parser CauchyDistribution
    -> (CauchyDistribution -> Parser CauchyDistribution)
    -> Maybe CauchyDistribution
    -> Parser CauchyDistribution
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe (String -> Parser CauchyDistribution
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Parser CauchyDistribution)
    -> String -> Parser CauchyDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> String
    errMsg Double
    m Double
    s) CauchyDistribution -> Parser CauchyDistribution
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Maybe CauchyDistribution -> Parser CauchyDistribution)
    -> Maybe CauchyDistribution -> Parser CauchyDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> Maybe CauchyDistribution
    cauchyDistributionE Double
    m Double
    s
      parseJSON _ = Parser CauchyDistribution
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance Binary CauchyDistribution where
        put :: CauchyDistribution -> Put
    put (CD m :: Double
    m s :: Double
    s) = Double -> Put
    forall t. Binary t => t -> Put
    put Double
    m 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
    s
        get :: Get CauchyDistribution
    get = do
          Double
    m <- Get Double
    forall t. Binary t => Get t
    get
          Double
    s <- Get Double
    forall t. Binary t => Get t
    get
          Get CauchyDistribution
    -> (CauchyDistribution -> Get CauchyDistribution)
    -> Maybe CauchyDistribution
    -> Get CauchyDistribution
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe (String -> Get CauchyDistribution
    forall a. HasCallStack => String -> a
    error (String -> Get CauchyDistribution)
    -> String -> Get CauchyDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> String
    errMsg Double
    m Double
    s) CauchyDistribution -> Get CauchyDistribution
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Maybe CauchyDistribution -> Get CauchyDistribution)
    -> Maybe CauchyDistribution -> Get CauchyDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> Maybe CauchyDistribution
    cauchyDistributionE Double
    m Double
    s

    -- | Cauchy distribution
    cauchyDistribution :: Double    -- ^ Central point
                       -> Double    -- ^ Scale parameter (FWHM)
                       -> CauchyDistribution
    cauchyDistribution :: Double -> Double -> CauchyDistribution
    cauchyDistribution m :: Double
    m s :: Double
    s
      = CauchyDistribution
    -> Maybe CauchyDistribution -> CauchyDistribution
    forall a. a -> Maybe a -> a
    fromMaybe (String -> CauchyDistribution
    forall a. HasCallStack => String -> a
    error (String -> CauchyDistribution) -> String -> CauchyDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> String
    errMsg Double
    m Double
    s)
      (Maybe CauchyDistribution -> CauchyDistribution)
    -> Maybe CauchyDistribution -> CauchyDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> Maybe CauchyDistribution
    cauchyDistributionE Double
    m Double
    s

    -- | Cauchy distribution
    cauchyDistributionE :: Double    -- ^ Central point
                        -> Double    -- ^ Scale parameter (FWHM)
                        -> Maybe CauchyDistribution
    cauchyDistributionE :: Double -> Double -> Maybe CauchyDistribution
    cauchyDistributionE m :: Double
    m s :: Double
    s
      | Double
    s Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    > 0     = CauchyDistribution -> Maybe CauchyDistribution
    forall a. a -> Maybe a
    Just (Double -> Double -> CauchyDistribution
    CD Double
    m Double
    s)
      | Bool
    otherwise = Maybe CauchyDistribution
    forall a. Maybe a
    Nothing

    errMsg :: Double -> Double -> String
    errMsg :: Double -> Double -> String
    errMsg _ s :: Double
    s
      = "Statistics.Distribution.CauchyLorentz.cauchyDistribution: FWHM must be positive. Got "
      String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Double -> String
    forall a. Show a => a -> String
    show Double
    s

    -- | Standard Cauchy distribution. It's centered at 0 and have 1 FWHM
    standardCauchy :: CauchyDistribution
    standardCauchy :: CauchyDistribution
    standardCauchy = Double -> Double -> CauchyDistribution
    CD 0 1

    instance D.Distribution CauchyDistribution where
      cumulative :: CauchyDistribution -> Double -> Double
    cumulative (CD m :: Double
    m s :: Double
    s) x :: Double
    x = 0.5 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + Double -> Double
    forall a. Floating a => a -> a
    atan( (Double
    x Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - Double
    m) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    s ) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    forall a. Floating a => a
    pi

    instance D.ContDistr CauchyDistribution where
      density :: CauchyDistribution -> Double -> Double
    density (CD m :: Double
    m s :: Double
    s) x :: Double
    x = (1 Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    forall a. Floating a => a
    pi) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / (Double
    s Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * (1 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + Double
    yDouble -> Double -> Double
    forall a. Num a => a -> a -> a
    *Double
    y))
        where y :: Double
    y = (Double
    x Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - Double
    m) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    s
      quantile :: CauchyDistribution -> Double -> Double
    quantile (CD m :: Double
    m s :: Double
    s) 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
    m Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + Double
    s Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double -> Double
    forall a. Floating a => a -> a
    tan( Double
    forall a. Floating a => a
    pi Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * (Double
    p Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - 0.5) )
        | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == 0         = -1 Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / 0
        | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1         =  1 Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / 0
        | Bool
    otherwise      =
          String -> Double
    forall a. HasCallStack => String -> a
    error (String -> Double) -> String -> Double
    forall a b. (a -> b) -> a -> b
    $ "Statistics.Distribution.CauchyLorentz.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 :: CauchyDistribution -> Double -> Double
    complQuantile (CD m :: Double
    m s :: Double
    s) 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
    m Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + Double
    s Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double -> Double
    forall a. Floating a => a -> a
    tan( Double
    forall a. Floating a => a
    pi Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * (0.5 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - Double
    p) )
        | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == 0         =  1 Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / 0
        | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1         = -1 Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / 0
        | Bool
    otherwise      =
          String -> Double
    forall a. HasCallStack => String -> a
    error (String -> Double) -> String -> Double
    forall a b. (a -> b) -> a -> b
    $ "Statistics.Distribution.CauchyLorentz.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.ContGen CauchyDistribution where
      genContVar :: CauchyDistribution -> Gen (PrimState m) -> m Double
    genContVar = CauchyDistribution -> Gen (PrimState m) -> m Double
    forall d (m :: * -> *).
    (ContDistr d, PrimMonad m) =>
    d -> Gen (PrimState m) -> m Double
    D.genContinuous

    instance D.Entropy CauchyDistribution where
      entropy :: CauchyDistribution -> Double
    entropy (CD _ s :: Double
    s) = Double -> Double
    forall a. Floating a => a -> a
    log Double
    s Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + Double -> Double
    forall a. Floating a => a -> a
    log (4Double -> Double -> Double
    forall a. Num a => a -> a -> a
    *Double
    forall a. Floating a => a
    pi)

    instance D.MaybeEntropy CauchyDistribution where
      maybeEntropy :: CauchyDistribution -> Maybe Double
    maybeEntropy = Double -> Maybe Double
    forall a. a -> Maybe a
    Just (Double -> Maybe Double)
    -> (CauchyDistribution -> Double)
    -> CauchyDistribution
    -> Maybe Double
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . CauchyDistribution -> Double
    forall d. Entropy d => d -> Double
    D.entropy