1

I simulate a simple experiment, a coin flip. What I do is accumulate the mean of the results up to the i-th experiment. What I can't figure out is why the computed means do not asymptotically approach 1/2. What did I do wrong? The code:

n = 10^4;
c = Table[{i, RandomInteger[]}, {i, 1, n}];
s = 0;
m = {};
For[i = 1, i <= n, i++,
  s += c[[i, 2]];
  AppendTo[m, {i, N[s/i]}];
]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Misery
  • 2,640
  • 1
  • 30
  • 39
  • 2
    This seems to be converging on 0.5 for me but your method is very inefficient. What are you seeing and why do you expect something else? – Mr.Wizard Jan 19 '13 at 10:32

1 Answers1

13

It should converge to 1/2, I think you just need to try higher values for n. Which is probably slow with your current non-functional method. Here's a simpler (and faster, and more functional) way to do the same calculation:

n = 1000000;
means = N[Accumulate[RandomInteger[1, n]]]/Range[n];

Now you can see it converges to 1/2 as expected:

ListLinePlot[means[[;; ;; 100]], PlotRange -> {0.4, 0.6}, 
 Epilog -> {Red, Dashed, Line[{{0, 0.5}, {Length[means], 0.5}}]}]

Mathematica graphics

Niki Estner
  • 36,101
  • 3
  • 92
  • 152
  • Your method is quick enough for Manipulate[ListLinePlot[N[Accumulate[RandomInteger[1, t]]]/Range[t], PlotRange -> {0, 1}, AxesOrigin -> {0, 0.5}], {t, 10, 2000, 5}] – cormullion Jan 19 '13 at 11:10
  • @cormullion try this: Manipulate[SeedRandom[seed]; ListLinePlot[N[Accumulate[RandomInteger[1, t]]]/Range[t], PlotRange -> {0, 1}, AxesOrigin -> {0, 0.5}], {t, 10, 2000, 5}, {seed, 1, 20, 1}] – Mr.Wizard Jan 19 '13 at 11:46
  • @Mr.Wizard That's good. There are also plenty of demonstrations for avid coin-tossers. – cormullion Jan 19 '13 at 12:22
  • @cormullion And for others too http://www.youtube.com/watch?v=7n8LNxGbZbs – Dr. belisarius Jan 19 '13 at 13:10
  • Well... that's the problem... Mathematica is extremely slow. It does support only functional programming well, which I hate... I wish I knew this before I bought it :( Thanks for answers gyus. – Misery Jan 19 '13 at 14:11
  • 4
    @Misery: That's like saying a race car is slow because I don't like switching gears. I use MMA for image processing/number crunching, and in my experience, it is a bit slower but far more productive than e.g. C. If you use it right. – Niki Estner Jan 19 '13 at 14:29
  • 6
    @Misery: No, Mathematica does not "support only functional programming." It supports procedural programming, functional programming, pattern-matching/rule-replacement programming, array-based programming, etc. But as with any interpreted language, the more that can be left to the system to do the better; which is why functional programming can so often be more efficient than procedural programming. Once you "speak" functional programming, you'll like it: it's often much easier to read than procedural programming. Cure the mind-rot of earlier exposure to procedural programming. – murray Jan 19 '13 at 15:25
  • Actually I am used to OOP :| And this one is absolutely not supported :( – Misery Jan 19 '13 at 16:10
  • 2
    @Misery have you seen this?: http://mathematica.stackexchange.com/q/16869/121 – Mr.Wizard Jan 20 '13 at 00:46
  • @Misery nikie's answer is not really an example of FP--rather it's just array programming. Think about it: do you really enjoy typing out the For loop in full, iterating over the elements one-by-one, and tediously building up the result by hand? I certainly don't, and I agree with murray: the need to include all this boilerplate is mainly just psychological and you're better off without it. Also, I can count on one hand (one finger, actually) the number of good Mathematica programmers I know who habitually use procedural idioms, so such code will be considered unusual by most others. – Oleksandr R. Jan 20 '13 at 05:31
  • @OleksandrR. I agree; Personally, I haven't seen anyone other than DL who can write non-compiled procedural code and have it work just as efficiently (or sometimes better) as other idiomatic approaches... belisarius comes second in my mind – rm -rf Mar 20 '13 at 18:02