Re: generating non-IID random sequences
- To: mathgroup at smc.vnet.net
- Subject: [mg79123] Re: generating non-IID random sequences
- From: Peter Pein <petsie at dordos.net>
- Date: Thu, 19 Jul 2007 03:29:58 -0400 (EDT)
- References: <f7kegd$58u$1@smc.vnet.net>
Yaroslav Bulatov schrieb: > I'm looking for a fast way to sample from a Markov-1 sequence of > random bits. The method below is 3600 times slower than built-in > RandomInteger function, can it be made much faster? > > p = 0.9; (* the probability of encountering 00 or 11 *) > f = RandomChoice[{p^# (1 - p)^(1 - #), p^(1 - #) (1 - p)^#} -> {1, 0}] > &; > NestList[f, RandomChoice[{0, 1}], 100000] > > Hi Yaroslav, isn't this formula a bit too complicated to be used in a fast generator? Just return the same bit as the last with probability p: SeedRandom[1]; Timing[With[{p=0.9}, rlist=NestList[If[Random[]<=p,#,1-#]&,Random[Integer],10^6];]][[1]] Count[Partition[rlist,2,1],{x_,x_},{1}]/Length[rlist]//N Out[32]= 0.300019 Second Out[33]= 0.899887 , which is pretty near 0.9 :-) Peter