Skip to content

Commit

Permalink
Response to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
idontgetoutmuch committed May 12, 2024
1 parent b57a2eb commit bb03f54
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
44 changes: 20 additions & 24 deletions System/Random/MWC/Distributions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -376,20 +376,18 @@ binomial :: forall g m . StatefulGen g m
-> g -- ^ Generator
-> m Int
{-# INLINE binomial #-}
binomial nTrials prob gen =
if prob == 0.0
then return 0
else if prob == 1.0
then return nTrials
else do let (p', flipped) = if prob > 0.5
then (1.0 - prob, True)
else (prob, False)
ix <- if fromIntegral nTrials * p' < bInvThreshold
then binomialInv nTrials p' gen
else binomialTPE nTrials p' gen
if flipped
then return $ nTrials - ix
else return ix
binomial nTrials prob gen
| nTrials <= 0 = pkgError "binomial" "number of trials must be positive"
| prob < 0.0 || prob > 1.0 = pkgError "binomial" "probability must be >= 0 and <= 1"
| prob == 0.0 = return 0
| prob == 1.0 = return nTrials
| otherwise = do let (p', flipped) = if prob > 0.5 then (1.0 - prob, True) else (prob, False)
ix <- if fromIntegral nTrials * p' < bInvThreshold
then binomialInv nTrials p' gen
else binomialTPE nTrials p' gen
if flipped
then return $ nTrials - ix
else return ix

where
binomialTPE n p g =
Expand Down Expand Up @@ -424,16 +422,14 @@ binomial nTrials prob gen =

-- Acceptance / rejection comparison
step5 :: Int -> Double -> m Int
step5 ix v = if var <= accept
then if p > 0
then return ix
else return $ n - ix
else hh
where
var = log v
accept = logFactorial bigM + logFactorial (n - bigM) -
logFactorial ix - logFactorial (n - ix) +
fromIntegral (ix - bigM) * log (p / q)
step5 ix v
| var <= accept = return $ if p > 0 then ix else n - ix
| otherwise = hh
where
var = log v
accept = logFactorial bigM + logFactorial (n - bigM) -
logFactorial ix - logFactorial (n - ix) +
fromIntegral (ix - bigM) * log (p / q)

h :: Double -> Double -> m Int
h u v | -- Triangular region
Expand Down
2 changes: 1 addition & 1 deletion mwc-random.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 3.0
build-type: Simple
name: mwc-random
version: 0.15.0.3
version: 0.15.1.0
license: BSD-2-Clause
license-file: LICENSE
copyright: 2009, 2010, 2011 Bryan O'Sullivan
Expand Down
15 changes: 7 additions & 8 deletions tests/props.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Control.Monad
import Control.Monad.Reader
import Data.Word
import qualified Data.Vector.Unboxed as U

Expand Down Expand Up @@ -133,17 +132,17 @@ betaBinomial a b n g = do
nSamples :: Int
nSamples = 10000

a, b :: Double
a = 600.0
b = 400.0
alpha, delta :: Double
alpha = 600.0
delta = 400.0

n :: Int
n = 10
nTrials :: Int
nTrials = 10

prop_betaBinomialMean :: IO ()
prop_betaBinomialMean = do
g <- create
ss <- replicateM nSamples $ betaBinomial 600 400 10 g
ss <- replicateM nSamples $ betaBinomial alpha delta nTrials g
let m = fromIntegral (sum ss) / fromIntegral nSamples
let x1 = fromIntegral n * a / (a + b)
let x1 = fromIntegral nTrials * alpha / (alpha + delta)
assertBool ("Mean is " ++ show x1 ++ " but estimated as " ++ show m) (abs (m - x1) < 0.001)

0 comments on commit bb03f54

Please sign in to comment.