Commits

Bryan O'Sullivan  committed 9655d2f Merge

Merge

  • Participants
  • Parent commits 1c0eaaa, 4a9a514

Comments (0)

Files changed (21)

File Statistics/Distribution/Beta.hs

   incompleteBeta, invIncompleteBeta, logBeta, digamma)
 import Numeric.MathFunctions.Constants (m_NaN)
 import qualified Statistics.Distribution as D
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 -- | The beta distribution
 data BetaDistribution = BD
    -- ^ Beta shape parameter
  } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary BetaDistribution
+instance Binary BetaDistribution where
+    put (BD x y) = put x >> put y
+    get = BD <$> get <*> get
 
 -- | Create beta distribution. Both shape parameters must be positive.
 betaDistr :: Double             -- ^ Shape parameter alpha

File Statistics/Distribution/Binomial.hs

 import qualified Statistics.Distribution.Poisson.Internal as I
 import Numeric.SpecFunctions (choose,incompleteBeta)
 import Numeric.MathFunctions.Constants (m_epsilon)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 
 -- | The binomial distribution.
     -- ^ Probability.
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary BinomialDistribution
+instance Binary BinomialDistribution where
+    put (BD x y) = put x >> put y
+    get = BD <$> get <*> get
 
 instance D.Distribution BinomialDistribution where
     cumulative = cumulative

File Statistics/Distribution/CauchyLorentz.hs

 import Data.Data (Data, Typeable)
 import GHC.Generics (Generic)
 import qualified Statistics.Distribution as D
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 -- | Cauchy-Lorentz distribution.
 data CauchyDistribution = CD {
   }
   deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance Binary CauchyDistribution
+instance Binary CauchyDistribution where
+    put (CD x y) = put x >> put y
+    get = CD <$> get <*> get
 
 -- | Cauchy distribution
 cauchyDistribution :: Double    -- ^ Central point

File Statistics/Distribution/ChiSquared.hs

 
 import qualified Statistics.Distribution         as D
 import qualified System.Random.MWC.Distributions as MWC
+import Data.Binary (put, get)
 
 
 -- | Chi-squared distribution
 newtype ChiSquared = ChiSquared Int
                      deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary ChiSquared
+instance Binary ChiSquared where
+    get = fmap ChiSquared get
+    put (ChiSquared x) = put x
 
 -- | Get number of degrees of freedom
 chiSquaredNDF :: ChiSquared -> Int

File Statistics/Distribution/Exponential.hs

 import qualified Statistics.Sample               as S
 import qualified System.Random.MWC.Distributions as MWC
 import Statistics.Types (Sample)
+import Data.Binary (put, get)
 
 
 newtype ExponentialDistribution = ED {
       edLambda :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary ExponentialDistribution
+instance Binary ExponentialDistribution where
+    put = put . edLambda
+    get = fmap ED get
 
 instance D.Distribution ExponentialDistribution where
     cumulative      = cumulative

File Statistics/Distribution/FDistribution.hs

 import qualified Statistics.Distribution as D
 import Numeric.SpecFunctions (
   logBeta, incompleteBeta, invIncompleteBeta, digamma)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 
 
                        }
                    deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance Binary FDistribution
+instance Binary FDistribution where
+    get = F <$> get <*> get <*> get
+    put (F x y z) = put x >> put y >> put z
 
 fDistribution :: Int -> Int -> FDistribution
 fDistribution n m

File Statistics/Distribution/Gamma.hs

 import Statistics.Distribution.Poisson.Internal  as Poisson
 import qualified Statistics.Distribution         as D
 import qualified System.Random.MWC.Distributions as MWC
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 -- | The gamma distribution.
 data GammaDistribution = GD {
     , gdScale :: {-# UNPACK #-} !Double -- ^ Scale parameter, &#977;.
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary GammaDistribution
+instance Binary GammaDistribution where
+    put (GD x y) = put x >> put y
+    get = GD <$> get <*> get
 
 -- | Create gamma distribution. Both shape and scale parameters must
 -- be positive.

File Statistics/Distribution/Geometric.hs

 import Numeric.MathFunctions.Constants(m_pos_inf,m_neg_inf)
 import qualified Statistics.Distribution         as D
 import qualified System.Random.MWC.Distributions as MWC
+import Data.Binary (put, get)
+import Control.Applicative ((<$>))
 
 ----------------------------------------------------------------
 -- Distribution over [1..]
       gdSuccess :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary GeometricDistribution
+instance Binary GeometricDistribution where
+    get = GD <$> get
+    put (GD x) = put x
 
 instance D.Distribution GeometricDistribution where
     cumulative = cumulative
       gdSuccess0 :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary GeometricDistribution0
+instance Binary GeometricDistribution0 where
+    get = GD0 <$> get
+    put (GD0 x) = put x
 
 instance D.Distribution GeometricDistribution0 where
     cumulative (GD0 s) x = cumulative (GD s) (x + 1)

File Statistics/Distribution/Hypergeometric.hs

 import Numeric.MathFunctions.Constants (m_epsilon)
 import Numeric.SpecFunctions (choose)
 import qualified Statistics.Distribution as D
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 data HypergeometricDistribution = HD {
       hdM :: {-# UNPACK #-} !Int
     , hdK :: {-# UNPACK #-} !Int
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary HypergeometricDistribution
+instance Binary HypergeometricDistribution where
+    get = HD <$> get <*> get <*> get
+    put (HD x y z) = put x >> put y >> put z
 
 instance D.Distribution HypergeometricDistribution where
     cumulative = cumulative

File Statistics/Distribution/Normal.hs

 import qualified Statistics.Distribution as D
 import qualified Statistics.Sample       as S
 import qualified System.Random.MWC.Distributions as MWC
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 
 
     , ndCdfDenom :: {-# UNPACK #-} !Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary NormalDistribution
+instance Binary NormalDistribution where
+    put (ND w x y z) = put w >> put x >> put y >> put z
+    get = ND <$> get <*> get <*> get <*> get
 
 instance D.Distribution NormalDistribution where
     cumulative      = cumulative

File Statistics/Distribution/Poisson.hs

 import qualified Statistics.Distribution.Poisson.Internal as I
 import Numeric.SpecFunctions (incompleteGamma,logFactorial)
 import Numeric.MathFunctions.Constants (m_neg_inf)
+import Data.Binary (put, get)
 
 
 newtype PoissonDistribution = PD {
       poissonLambda :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary PoissonDistribution
+instance Binary PoissonDistribution where
+    get = fmap PD get
+    put = put . poissonLambda
 
 instance D.Distribution PoissonDistribution where
     cumulative (PD lambda) x
 poisson :: Double -> PoissonDistribution
 poisson l
   | l >=  0   = PD l
-  | otherwise = error $ "Statistics.Distribution.Poisson.poisson:\
-                        \ lambda must be non-negative. Got " ++ show l
+  | otherwise = error $
+    "Statistics.Distribution.Poisson.poisson: lambda must be non-negative. Got "
+    ++ show l
 {-# INLINE poisson #-}
 
 -- $references

File Statistics/Distribution/StudentT.hs

 import Statistics.Distribution.Transform (LinearTransform (..))
 import Numeric.SpecFunctions (
   logBeta, incompleteBeta, invIncompleteBeta, digamma)
+import Data.Binary (put, get)
 
 -- | Student-T distribution
 newtype StudentT = StudentT { studentTndf :: Double }
                    deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance Binary StudentT
+instance Binary StudentT where
+    put = put . studentTndf
+    get = fmap StudentT get
 
 -- | Create Student-T distribution. Number of parameters must be positive.
 studentT :: Double -> StudentT

File Statistics/Distribution/Transform.hs

 import GHC.Generics (Generic)
 import Data.Functor          ((<$>))
 import qualified Statistics.Distribution as D
+import Data.Binary (put, get)
+import Control.Applicative ((<*>))
 
 -- | Linear transformation applied to distribution.
 --
     -- ^ Distribution being transformed.
   } deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance (Binary d) => Binary (LinearTransform d)
+instance (Binary d) => Binary (LinearTransform d) where
+    get = LinearTransform <$> get <*> get <*> get
+    put (LinearTransform x y z) = put x >> put y >> put z
 
 -- | Apply linear transformation to distribution.
 scaleAround :: Double           -- ^ Fixed point

File Statistics/Distribution/Uniform.hs

 import GHC.Generics (Generic)
 import qualified Statistics.Distribution as D
 import qualified System.Random.MWC       as MWC
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 
 -- | Uniform distribution from A to B
     , uniformB :: {-# UNPACK #-} !Double -- ^ Upper boundary of distribution
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary UniformDistribution
+instance Binary UniformDistribution where
+    put (UniformDistribution x y) = put x >> put y
+    get = UniformDistribution <$> get <*> get
 
 -- | Create uniform distribution.
 uniformDistr :: Double -> Double -> UniformDistribution

File Statistics/Math/RootFinding.hs

 import Control.Monad       (MonadPlus(..), ap)
 import Data.Data (Data, Typeable)
 import GHC.Generics (Generic)
+import Data.Binary (put, get)
+import Data.Binary.Put (putWord8)
+import Data.Binary.Get (getWord8)
 
 
 -- | The result of searching for a root of a mathematical function.
             -- ^ A root was successfully found.
               deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance (Binary a) => Binary (Root a)
+instance (Binary a) => Binary (Root a) where
+    put NotBracketed = putWord8 0
+    put SearchFailed = putWord8 1
+    put (Root a) = putWord8 2 >> put a
+
+    get = do
+        i <- getWord8
+        case i of
+            0 -> return NotBracketed
+            1 -> return SearchFailed
+            2 -> fmap Root get
+            _ -> fail $ "Root.get: Invalid value: " ++ show i
 
 instance Functor Root where
     fmap _ NotBracketed = NotBracketed

File Statistics/Resampling.hs

       fromResample :: U.Vector Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Resample
+instance Binary Resample where
+    put = put . fromResample
+    get = fmap Resample get
 
 -- | /O(e*r*s)/ Resample a data set repeatedly, with replacement,
 -- computing each estimate over the resampled data.

File Statistics/Resampling/Bootstrap.hs

 import Statistics.Types (Estimator, Sample)
 import qualified Data.Vector.Unboxed as U
 import qualified Statistics.Resampling as R
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
 
 -- | A point and interval estimate computed via an 'Estimator'.
 data Estimate = Estimate {
     -- ^ Confidence level of the confidence intervals.
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Estimate
+instance Binary Estimate where
+    put (Estimate w x y z) = put w >> put x >> put y >> put z
+    get = Estimate <$> get <*> get <*> get <*> get
 instance NFData Estimate
 
 -- | Multiply the point, lower bound, and upper bound in an 'Estimate'

File Statistics/Sample/KernelDensity/Simple.hs

       fromPoints :: U.Vector Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Points
+instance Binary Points where
+    get = fmap Points get
+    put = put . fromPoints
 
 -- | Bandwidth estimator for an Epanechnikov kernel.
 epanechnikovBW :: Double -> Bandwidth

File Statistics/Sample/Powers.hs

 newtype Powers = Powers (U.Vector Double)
     deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Powers
+instance Binary Powers where
+    put (Powers v) = put v
+    get = fmap Powers get
 
 -- | O(/n/) Collect the /n/ simple powers of a sample.
 --

File statistics.cabal

     Statistics.Test.Internal
   build-depends:
     base < 5,
-    binary >= 0.6.3.0,
+    binary >= 0.5.1.0,
     deepseq >= 1.1.0.2,
     erf,
     monad-par         >= 0.3.4,
 
   build-depends:
     base,
+    binary,
     ieee754 >= 0.7.3,
     HUnit,
     QuickCheck >= 2,

File tests/Tests/Distribution.hs

 
 import Data.List     (find)
 import Data.Typeable (Typeable)
+import Data.Binary
 
 import qualified Numeric.IEEE    as IEEE
 
 ----------------------------------------------------------------
 
 -- Tests for continous distribution
-contDistrTests :: (Param d, ContDistr d, QC.Arbitrary d, Typeable d, Show d) => T d -> Test
+contDistrTests :: (Param d, ContDistr d, QC.Arbitrary d, Typeable d, Show d, Binary d, Eq d) => T d -> Test
 contDistrTests t = testGroup ("Tests for: " ++ typeName t) $
   cdfTests t ++
   [ testProperty "PDF sanity"              $ pdfSanityCheck     t
   ]
 
 -- Tests for discrete distribution
-discreteDistrTests :: (Param d, DiscreteDistr d, QC.Arbitrary d, Typeable d, Show d) => T d -> Test
+discreteDistrTests :: (Param d, DiscreteDistr d, QC.Arbitrary d, Typeable d, Show d, Binary d, Eq d) => T d -> Test
 discreteDistrTests t = testGroup ("Tests for: " ++ typeName t) $
   cdfTests t ++
   [ testProperty "Prob. sanity"         $ probSanityCheck       t
   ]
 
 -- Tests for distributions which have CDF
-cdfTests :: (Param d, Distribution d, QC.Arbitrary d, Show d) => T d -> [Test]
+cdfTests :: (Param d, Distribution d, QC.Arbitrary d, Show d, Binary d, Eq d) => T d -> [Test]
 cdfTests t =
   [ testProperty "C.D.F. sanity"        $ cdfSanityCheck         t
   , testProperty "CDF limit at +inf"    $ cdfLimitAtPosInfinity  t
   , testProperty "CDF at -inf = 1"      $ cdfAtNegInfinity       t
   , testProperty "CDF is nondecreasing" $ cdfIsNondecreasing     t
   , testProperty "1-CDF is correct"     $ cdfComplementIsCorrect t
+  , testProperty "Binary OK"            $ p_binary t
   ]
+
+
 ----------------------------------------------------------------
 
 -- CDF is in [0,1] range
     logP = logProbability d x
 
 
-    
+p_binary :: (Eq a, Show a, Binary a) => T a -> a -> Bool
+p_binary _ a = a == (decode . encode) a
+
+
+
 ----------------------------------------------------------------
 -- Arbitrary instances for ditributions
 ----------------------------------------------------------------