dimanche 16 septembre 2018

Iterate and getStdRandom

I have the following code (trying to simulate a Gaussian random walk):

import System.Random
import Control.Applicative

getStdNormal :: StdGen -> (Float,StdGen)
getStdNormal g = let l = drop 1 . take 49 $ iterate (\(_,g') -> random g') (0,g) in
                ((/ 2) . (+ (-24)) . sum . map fst $ l, snd . last $ l)

mphi = getStdRandom getStdNormal

l1 = setStdGen (mkStdGen 20180916) >> (sequence . take 10 $
                iterate (\s -> (\a -> \b ->  b) <$> s <*> mphi) (return 0))

l2 = setStdGen (mkStdGen 20180916) >> (sequence . take 10 $
                iterate (\s -> (\a -> \b -> a + b) <$> s <*> mphi) (return 0))

l1 is evaluated to the list of (apparently normal) random values, just as I can expect:

[0.0,1.1155739,-0.24667645,0.7793722,-0.18391132,0.23517609,-0.80208874,-1.5305595,-0.28670216,0.53894806]

However, l2 behaves far from my expectations. The list obviously is not the series of l1 partial sums, and is very unlikely to have standard normal differences:

[0.0,1.1155739,0.55951977,0.22015285,-2.0858078,0.6170025,-5.20298,0.3877325,1.410594,0.8647003]

(Yet note that the first two members are correct.) In fact, I can't explain where do the numbers in l2 come from, although I tend to blame laziness in mphi.

Why doesn't the code work as intended? Many thanks!




Aucun commentaire:

Enregistrer un commentaire