haskell-numerics / random-fu

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

MonadRandom (State s) vs RandomGen s => MonadRandom (State s) #44

Closed FintanH closed 4 years ago

FintanH commented 5 years ago

I'm trying to use sampleState but I would like to use a different seed to kick this off, TFGen.

I can't seem to do this because I'm constrained to use StdGen or PureMT. I was wondering if it might be possible to utilise the fact that all these seeds are instances of RandomGen and that way we can define it as:

sampleState :: (RandomGen s, Sampleable d (State s) t, MonadRandom (State s)) => d t -> s -> (t, s)

that'll allow the functionality to be extended. I'll dig into the code to see if it's possible myself.

idontgetoutmuch commented 5 years ago

I seem to be able to use MWC

import Data.Random
import Data.Random.Source.MWC
import Data.Vector (singleton)

main :: IO ()
main = do
    x <- sum <$> mapM (\m -> (initialize (singleton m) >>= \s -> sampleFrom s StdNormal) :: IO Double) [1..10000]
    print x

I did try

$(randomSource [d|
  instance (PrimMonad m) => RandomSource (ReaderT TF.TFGen m) TF.TFGen where
    getRandomWord64From = undefined
    getRandomDoubleFrom = undefined
  |])

which compiles but what should the undefined be? mwc-random provides uniform so we can replace undefined with uniform and fmap wordToDouble . uniform. Maybe you can write uniform for tf-random?

I am very grateful for your contribution but your proposal seems to be quite a big change that I need more time than I have at the moment to properly assess.