{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
module Statistics.Distribution.CauchyLorentz (
CauchyDistribution
, cauchyDistribMedian
, cauchyDistribScale
, 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
data CauchyDistribution = CD {
CauchyDistribution -> Double
cauchyDistribMedian :: {-# UNPACK #-} !Double
, CauchyDistribution -> Double
cauchyDistribScale :: {-# UNPACK #-} !Double
}
deriving (CauchyDistribution -> CauchyDistribution -> Bool
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
CauchyDistribution -> DataType
CauchyDistribution -> Constr
(forall b. Data b => b -> b)
-> CauchyDistribution -> 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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
Monad m =>
(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 :: forall u.
Int -> (forall d. Data d => d -> u) -> CauchyDistribution -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> CauchyDistribution -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> CauchyDistribution -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CauchyDistribution -> [u]
gmapQr :: forall r r'.
(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 :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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
Data, 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 Int
i (CD Double
m Double
s) = forall a b. (Show a, Show b) => [Char] -> a -> b -> Int -> ShowS
defaultShow2 [Char]
"cauchyDistribution" Double
m Double
s Int
i
instance Read CauchyDistribution where
readPrec :: ReadPrec CauchyDistribution
readPrec = forall a b r.
(Read a, Read b) =>
[Char] -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 [Char]
"cauchyDistribution" Double -> Double -> Maybe CauchyDistribution
cauchyDistributionE
instance ToJSON CauchyDistribution
instance FromJSON CauchyDistribution where
parseJSON :: Value -> Parser CauchyDistribution
parseJSON (Object Object
v) = do
Double
m <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"cauchyDistribMedian"
Double
s <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"cauchyDistribScale"
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsg Double
m Double
s) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe CauchyDistribution
cauchyDistributionE Double
m Double
s
parseJSON Value
_ = forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary CauchyDistribution where
put :: CauchyDistribution -> Put
put (CD Double
m Double
s) = forall t. Binary t => t -> Put
put Double
m forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall t. Binary t => t -> Put
put Double
s
get :: Get CauchyDistribution
get = do
Double
m <- forall t. Binary t => Get t
get
Double
s <- forall t. Binary t => Get t
get
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsg Double
m Double
s) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe CauchyDistribution
cauchyDistributionE Double
m Double
s
cauchyDistribution :: Double
-> Double
-> CauchyDistribution
cauchyDistribution :: Double -> Double -> CauchyDistribution
cauchyDistribution Double
m Double
s
= forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsg Double
m Double
s)
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe CauchyDistribution
cauchyDistributionE Double
m Double
s
cauchyDistributionE :: Double
-> Double
-> Maybe CauchyDistribution
cauchyDistributionE :: Double -> Double -> Maybe CauchyDistribution
cauchyDistributionE Double
m Double
s
| Double
s forall a. Ord a => a -> a -> Bool
> Double
0 = forall a. a -> Maybe a
Just (Double -> Double -> CauchyDistribution
CD Double
m Double
s)
| Bool
otherwise = forall a. Maybe a
Nothing
errMsg :: Double -> Double -> String
errMsg :: Double -> Double -> [Char]
errMsg Double
_ Double
s
= [Char]
"Statistics.Distribution.CauchyLorentz.cauchyDistribution: FWHM must be positive. Got "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Double
s
standardCauchy :: CauchyDistribution
standardCauchy :: CauchyDistribution
standardCauchy = Double -> Double -> CauchyDistribution
CD Double
0 Double
1
instance D.Distribution CauchyDistribution where
cumulative :: CauchyDistribution -> Double -> Double
cumulative (CD Double
m Double
s) Double
x
| Double
y forall a. Ord a => a -> a -> Bool
< -Double
1 = forall a. Floating a => a -> a
atan (-Double
1forall a. Fractional a => a -> a -> a
/Double
y) forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a
pi
| Bool
otherwise = Double
0.5 forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
atan Double
y forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a
pi
where
y :: Double
y = (Double
x forall a. Num a => a -> a -> a
- Double
m) forall a. Fractional a => a -> a -> a
/ Double
s
complCumulative :: CauchyDistribution -> Double -> Double
complCumulative (CD Double
m Double
s) Double
x
| Double
y forall a. Ord a => a -> a -> Bool
> Double
1 = forall a. Floating a => a -> a
atan (Double
1forall a. Fractional a => a -> a -> a
/Double
y) forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a
pi
| Bool
otherwise = Double
0.5 forall a. Num a => a -> a -> a
- forall a. Floating a => a -> a
atan Double
y forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a
pi
where
y :: Double
y = (Double
x forall a. Num a => a -> a -> a
- Double
m) forall a. Fractional a => a -> a -> a
/ Double
s
instance D.ContDistr CauchyDistribution where
density :: CauchyDistribution -> Double -> Double
density (CD Double
m Double
s) Double
x = (Double
1 forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a
pi) forall a. Fractional a => a -> a -> a
/ (Double
s forall a. Num a => a -> a -> a
* (Double
1 forall a. Num a => a -> a -> a
+ Double
yforall a. Num a => a -> a -> a
*Double
y))
where y :: Double
y = (Double
x forall a. Num a => a -> a -> a
- Double
m) forall a. Fractional a => a -> a -> a
/ Double
s
quantile :: CauchyDistribution -> Double -> Double
quantile (CD Double
m Double
s) Double
p
| Double
p forall a. Eq a => a -> a -> Bool
== Double
0 = -Double
1 forall a. Fractional a => a -> a -> a
/ Double
0
| Double
p forall a. Eq a => a -> a -> Bool
== Double
1 = Double
1 forall a. Fractional a => a -> a -> a
/ Double
0
| Double
p forall a. Eq a => a -> a -> Bool
== Double
0.5 = Double
m
| Double
p forall a. Ord a => a -> a -> Bool
< Double
0 = forall {a}. a
err
| Double
p forall a. Ord a => a -> a -> Bool
< Double
0.5 = Double
m forall a. Num a => a -> a -> a
- Double
s forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
tan( forall a. Floating a => a
pi forall a. Num a => a -> a -> a
* Double
p )
| Double
p forall a. Ord a => a -> a -> Bool
< Double
1 = Double
m forall a. Num a => a -> a -> a
+ Double
s forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
tan( forall a. Floating a => a
pi forall a. Num a => a -> a -> a
* (Double
1 forall a. Num a => a -> a -> a
- Double
p) )
| Bool
otherwise = forall {a}. a
err
where
err :: a
err = forall a. HasCallStack => [Char] -> a
error
forall a b. (a -> b) -> a -> b
$ [Char]
"Statistics.Distribution.CauchyLorentz.quantile: p must be in [0,1] range. Got: "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show Double
p
complQuantile :: CauchyDistribution -> Double -> Double
complQuantile (CD Double
m Double
s) Double
p
| Double
p forall a. Eq a => a -> a -> Bool
== Double
0 = Double
1 forall a. Fractional a => a -> a -> a
/ Double
0
| Double
p forall a. Eq a => a -> a -> Bool
== Double
1 = -Double
1 forall a. Fractional a => a -> a -> a
/ Double
0
| Double
p forall a. Eq a => a -> a -> Bool
== Double
0.5 = Double
m
| Double
p forall a. Ord a => a -> a -> Bool
< Double
0 = forall {a}. a
err
| Double
p forall a. Ord a => a -> a -> Bool
< Double
0.5 = Double
m forall a. Num a => a -> a -> a
+ Double
s forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
tan( forall a. Floating a => a
pi forall a. Num a => a -> a -> a
* Double
p )
| Double
p forall a. Ord a => a -> a -> Bool
< Double
1 = Double
m forall a. Num a => a -> a -> a
- Double
s forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
tan( forall a. Floating a => a
pi forall a. Num a => a -> a -> a
* (Double
1 forall a. Num a => a -> a -> a
- Double
p) )
| Bool
otherwise = forall {a}. a
err
where
err :: a
err = forall a. HasCallStack => [Char] -> a
error
forall a b. (a -> b) -> a -> b
$ [Char]
"Statistics.Distribution.CauchyLorentz.quantile: p must be in [0,1] range. Got: "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show Double
p
instance D.ContGen CauchyDistribution where
genContVar :: forall g (m :: * -> *).
StatefulGen g m =>
CauchyDistribution -> g -> m Double
genContVar = forall d g (m :: * -> *).
(ContDistr d, StatefulGen g m) =>
d -> g -> m Double
D.genContinuous
instance D.Entropy CauchyDistribution where
entropy :: CauchyDistribution -> Double
entropy (CD Double
_ Double
s) = forall a. Floating a => a -> a
log Double
s forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
log (Double
4forall a. Num a => a -> a -> a
*forall a. Floating a => a
pi)
instance D.MaybeEntropy CauchyDistribution where
maybeEntropy :: CauchyDistribution -> 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