haskell-numerics / random-fu

A suite of Haskell libraries for representing, manipulating, and sampling random variables
42 stars 21 forks source link

Determistic evaluation of Categorical with shuffled weights #46

Closed FintanH closed 5 years ago

FintanH commented 5 years ago

So I wonder if this is correct behaviour, granted I don't know a whole lot about probability sampling. The below code uses the same seed and weighted categories, but depending on the ordering of the inputs it will choose the category non-deterministically. Is this correct?

import Data.Random
import Data.Random.Distribution.Categorical
import Data.Random.Source.StdGen

main :: IO ()
main = do
  let ab = fromWeightedList [(75, "a"), (25 :: Double, "b")]
      ba = fromWeightedList [(25, "b"), (75 :: Double, "a")]
      seed = mkStdGen 0
  print $ sampleState ab seed
  print $ sampleState ba seed
*Main> main
("b",1601120196 1655838864)
("a",1601120196 1655838864)
idontgetoutmuch commented 5 years ago

I think it is non-deterministic full stop - if you change the seed I suspect you will get a different answer. If you run your sampler multiple times it should give you the values of "a" and "b" in the proportions you specify. Other than that it can randomly give either value (obviously a fixed seed will give the same random result). I am going to close this but please re-open if you feel this isn't a full answer.