Re: average of the consecutive coin tosses
- To: mathgroup at smc.vnet.net
- Subject: [mg121952] Re: average of the consecutive coin tosses
- From: michael partensky <partensky at gmail.com>
- Date: Fri, 7 Oct 2011 04:47:03 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201110060818.EAA22381@smc.vnet.net>
On Thu, Oct 6, 2011 at 10:28 AM, Sseziwa Mukasa <mukasa at gmail.com> wrote: > > On Oct 6, 2011, at 9:32 AM, michael partensky wrote: > > Thanks, Sseziwa. > This example is designed for students, to compare the Mathematica style > with the "For" loop. > I just wanted to make it more elegant. > > For instance, I did not find a nice way of dividing the total value of the > random table corresponding to each step, by the corresponding number of > trials. That forced me to multiply # by the flipCount > before dividing it by ++flipCount. It does not look neat. > > Any ideas? > > > The only options I could thing of were carrying the trial number in the > NestList argument: > > coinFlipNestNoGlobal[n_]:=Rest[NestList[Block[{iter=#[[2]],mean=#[[1]]},{(iter > mean + RandomInteger[])/(iter+1),iter+1}]&,{0.,0},n]][[All,1]] > > If the number of trials is small, you can use FoldList: > > coinFlipFold[n_]:=Rest[FoldList[((#2-1) #1 + > RandomInteger[])/#2&,0.,Range[n]] > > Another approach is work backward and use Reap and Sow > > > coinFlipReap[n_]:=Reverse[Last[Reap[Nest[Block[{},Sow[Mean[#]];Rest[#]]&,RandomInteger[{0,1},{n}],n]]][[1]]] > > None of these are completely satisfying to me, what you need, and I've > often wanted it in Mathematica, is a form of nest where you can tell how > deep in the nest you are in the callback function. > *This is exactly what I wanted. Why isn't it there? Would be very natural to have. Well, how would I know ?! :)* > > Regards, > Sseziwa > > PS - For unrelated reasons I can't test these expressions in Mathematica > now so there may be typos in the expressions above but I think you get the > gist. > > Best wishes, > and thanks again for your help. > MP > > On Thu, Oct 6, 2011 at 9:17 AM, Sseziwa Mukasa <mukasa at gmail.com> wrote: > >> Michael, >> >> I misunderstood the intent of your previous post, the code example you >> sent is almost correct you should probably wrap the NestList in a Rest to >> remove the initial 0. If you use 0. (note the period) as your initial value >> you don't need the //N either. >> >> Regards, >> Sseziwa >> >> On Oct 6, 2011, at 8:22 AM, michael partensky wrote: >> >> Thanks, Sseziwa. >> My example showed evolution of mean value with the increasing number of >> trials. >> This would be another take, but I am looking for something more elegant >> and robust. >> >> coinFlipNest[n_]:=Module[{flipCount=0},NestList[((flipCount # + >> RandomInteger[])/(++flipCount)//N)&,0,n]] >> >> Michael >> >> On Thu, Oct 6, 2011 at 6:37 AM, Sseziwa Mukasa <mukasa at gmail.com> wrote: >> >>> Mean[RandomInteger[{0,1},{1000}] >>> >>> Regards, >>> Ssezi >>> >>> On Oct 6, 2011, at 4:18 AM, michael partensky wrote: >>> >>> > Hi. >>> > >>> > What are the better ways of doing what this guy does very inefficiently >>> for >>> > the average of the consecutive coin tosses: >>> > >>> > tr = (flips = 1; (#/flips++) & /@ Accumulate[Table[toss, {1000}]]) // >>> N); >>> > >>> > where >>> > >>> > toss= RandomInteger[]; >>> > >>> > Thanks >>> > Michael >>> >>> >> >> > >
- References:
- average of the consecutive coin tosses
- From: michael partensky <partensky@gmail.com>
- average of the consecutive coin tosses