# Re: [R] Random seed problem in MCMC coupling of chains

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 09 Jun 2005 - 04:42:23 EST

Here is a small variation. We define a list to hold the last seed for each chain. Each time we enter the simulation for a chain we use that seed and each time we exit we update it. The loop becomes simpler since the setup is all done prior to looping and everything else is done in the inner loop.

Note that a double loop with nothing between the first and second for is really like a single loop over the i,j pairs so its presumably easier to understand.

library(setRNG)

set.seed(123)
niter <- 3; nchain <- 2
chain <- lapply(1:nchain, function(x) setRNG())

for(i in 1:niter)
for(j in 1:nchain) {

setRNG(chain[[j]]) # get seed

a <- runif(1)
cat("iter:", i, "chain:", j, "runif:", a, "\n")

chain[[j]] <- setRNG() # save seed

}

On 6/8/05, Gorjanc Gregor <Gregor.Gorjanc@bfro.uni-lj.si> wrote:
> Thanks to Paul and Gabor for additional tips/examples. Actually, I find
> Pauls suggestion with setRNG also nice and is exactly what I wanted.
> Paul, if I understand this correctly, your suggestion with setRNG does not
> alter "RNG flow", it just takes care that chains really have equal seeds.
> I remember that I have read somewhere that destroying "RNG flow over and
> over to get real randomness" is not a good idea. Can someone confirm this?
>
> niter <- 3
> nchain <- 2
> for (i in 1:niter) { # iterations
> tmpSeed <- setRNG()
> for (j in 1:nchain) { # chains
> setRNG(tmpSeed)
> a <- runif(1)
> cat("iter:", i, "chain:", j, "runif:", a, "\n")
> }
> }
>
> iter: 1 chain: 1 runif: 0.8160078
> iter: 1 chain: 2 runif: 0.8160078
> iter: 2 chain: 1 runif: 0.4909793
> iter: 2 chain: 2 runif: 0.4909793
> iter: 3 chain: 1 runif: 0.4425924
> iter: 3 chain: 2 runif: 0.4425924
>
> [... removed other stuff ...]
>
> Lep pozdrav / With regards,
> Gregor Gorjanc
>
> ----------------------------------------------------------------------
> University of Ljubljana
> Biotechnical Faculty URI: http://www.bfro.uni-lj.si/MR/ggorjan
> Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si
> Groblje 3 tel: +386 (0)1 72 17 861
> SI-1230 Domzale fax: +386 (0)1 72 17 888
> Slovenia, Europe
> ----------------------------------------------------------------------
> "One must learn by doing the thing; for though you think you know it,
> you have no certainty until you try." Sophocles ~ 450 B.C.
> ----------------------------------------------------------------------
>
>
>
>
>
>
>
>

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jun 09 04:46:09 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:27 EST