Commits

Aleksey Khudyakov committed 3cda81d

Small fixes

* Documentation
* Function in LinearTransform are renamed
* Readbility fixes

  • Participants
  • Parent commits ce53212

Comments (0)

Files changed (3)

Statistics/Distribution/StudentT.hs

 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 
     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
 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

Statistics/Distribution/Transform.hs

     LinearTransform (..)
   ) where
 
+import Data.Typeable         (Typeable)
+import Data.Functor          ((<$>))
 import qualified Statistics.Distribution as D
-import Data.Typeable         (Typeable)
 
+-- | 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)
+  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
-
+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

tests/Tests/Distribution.hs

 
 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