Permalink
Browse files

Small fixes

 * Documentation
 * Function in LinearTransform are renamed
 * Readbility fixes
  • Loading branch information...
1 parent 90185e2 commit 979391aedc33d6a0da07c9ba3bf0e729465c8ada @Shimuuar Shimuuar committed Mar 19, 2013
Showing with 37 additions and 24 deletions.
  1. +11 −8 Statistics/Distribution/StudentT.hs
  2. +25 −15 Statistics/Distribution/Transform.hs
  3. +1 −1 tests/Tests/Distribution.hs
@@ -30,8 +30,7 @@ newtype StudentT = StudentT { studentTndf :: Double }
studentT :: Double -> StudentT
studentT ndf
| ndf > 0 = StudentT ndf
- | otherwise =
- error "Statistics.Distribution.StudentT.studentT: non-positive number of degrees of freedom"
+ | otherwise = modErr "studentT" "non-positive number of degrees of freedom"
instance D.Distribution StudentT where
cumulative = cumulative
@@ -58,8 +57,7 @@ quantile (StudentT ndf) p
in case sqrt $ ndf * (1 - x) / x of
r | p < 0.5 -> -r
| otherwise -> r
- | otherwise =
- error $ "Statistics.Distribution.Uniform.quantile: p must be in [0,1] range. Got: "++show p
+ | otherwise = modErr "quantile" $ "p must be in [0,1] range. Got: "++show p
instance D.MaybeMean StudentT where
@@ -73,9 +71,14 @@ instance D.MaybeVariance StudentT where
instance D.ContGen StudentT where
genContVar = D.genContinous
--- | Create an unstandardized Student-t distribution
-studentTUnstandardized :: Double -> Double -> Double -> LinearTransform StudentT
+-- | Create an unstandardized Student-t distribution.
+studentTUnstandardized :: Double -- ^ Number of degrees of freedom
+ -> Double -- ^ Central value (0 for standard Student T distribution)
+ -> Double -- ^ Scale parameter
+ -> LinearTransform StudentT
studentTUnstandardized ndf mu sigma
- | sigma <= 0 = error $ "Statistics.Distribution.StudentT.studentTGeneral: sigma must be > 0. Got: " ++ (show sigma)
- | otherwise = LinearTransform mu sigma (StudentT ndf)
+ | sigma > 0 = LinearTransform mu sigma $ studentT ndf
+ | otherwise = modErr "studentTUnstandardized" $ "sigma must be > 0. Got: " ++ show sigma
+modErr :: String -> String -> a
+modErr fun msg = error $ "Statistics.Distribution.StudentT." ++ fun ++ ": " ++ msg
@@ -14,39 +14,49 @@ module Statistics.Distribution.Transform (
LinearTransform (..)
) where
-import qualified Statistics.Distribution as D
import Data.Typeable (Typeable)
+import Data.Functor ((<$>))
+import qualified Statistics.Distribution as D
+-- | Linear transformation applied to distribution.
+--
+-- > LinearTransform μ σ _
+-- > x' = μ + σ·x
data LinearTransform d = LinearTransform
- { location :: {-# UNPACK #-} !Double
- , scale :: {-# UNPACK #-} !Double
- , distr :: d
+ { linTransLocation :: {-# UNPACK #-} !Double
+ -- | Location parameter.
+ , linTransScale :: {-# UNPACK #-} !Double
+ -- | Scale parameter.
+ , linTransDistr :: d
+ -- | Distribution being transformed.
} deriving (Eq,Show,Read,Typeable)
+
instance Functor LinearTransform where
fmap f (LinearTransform loc sc dist) = LinearTransform loc sc (f dist)
instance D.Distribution d => D.Distribution (LinearTransform d) where
- cumulative (LinearTransform loc sc dist) x = D.cumulative dist ((x-loc)/sc)
+ cumulative (LinearTransform loc sc dist) x = D.cumulative dist $ (x-loc) / sc
instance D.ContDistr d => D.ContDistr (LinearTransform d) where
- density (LinearTransform loc sc dist) x = (/sc) $ D.density dist ((x-loc)/sc)
+ density (LinearTransform loc sc dist) x = D.density dist ((x-loc) / sc) / sc
quantile (LinearTransform loc sc dist) p = loc + sc * D.quantile dist p
instance D.MaybeMean d => D.MaybeMean (LinearTransform d) where
- maybeMean (LinearTransform loc _ dist) = fmap (+loc) (D.maybeMean dist)
+ maybeMean (LinearTransform loc _ dist) = (+loc) <$> D.maybeMean dist
instance (D.Mean d) => D.Mean (LinearTransform d) where
- mean (LinearTransform loc _ dist) = loc + (D.mean dist)
+ mean (LinearTransform loc _ dist) = loc + D.mean dist
instance D.MaybeVariance d => D.MaybeVariance (LinearTransform d) where
- maybeVariance (LinearTransform _ sc dist) = fmap ((*sc).(*sc)) (D.maybeVariance dist)
- maybeStdDev (LinearTransform _ sc dist) = fmap (*sc) (D.maybeStdDev dist)
+ maybeVariance (LinearTransform _ sc dist) = (*(sc*sc)) <$> D.maybeVariance dist
+ maybeStdDev (LinearTransform _ sc dist) = (*sc) <$> D.maybeStdDev dist
instance (D.Variance d) => D.Variance (LinearTransform d) where
- variance (LinearTransform _ sc dist) = sc * sc * (D.variance dist)
- stdDev (LinearTransform _ sc dist) = sc * (D.stdDev dist)
-
-instance D.ContDistr d => D.ContGen (LinearTransform d) where
- genContVar = D.genContinous
+ variance (LinearTransform _ sc dist) = sc * sc * D.variance dist
+ stdDev (LinearTransform _ sc dist) = sc * D.stdDev dist
+instance D.ContGen d => D.ContGen (LinearTransform d) where
+ genContVar (LinearTransform loc sc d) g = do
+ x <- D.genContVar d g
+ return $! loc + sc * x
@@ -282,7 +282,7 @@ instance Param StudentT where
instance Param (LinearTransform StudentT) where
invQuantilePrec _ = 1e-13
- okForInfLimit d = (studentTndf . distr) d > 0.75
+ okForInfLimit d = (studentTndf . linTransDistr) d > 0.75
instance Param FDistribution where
invQuantilePrec _ = 1e-12

0 comments on commit 979391a

Please sign in to comment.