# Simple Monte Carlo simulation

I want to find the Monte Carlo simulation of the integral $$\int_0^1 \sqrt{1-x^2}\mathrm dx$$

The Cesàro mean is $$I_n=\frac1n\sum_{k=1}^{n} f(U_k)$$ I want to plot the scatter plot ((1,$I_1$)(2,$I_2$)....(500,$I_{500}$)). First I generate random variables:

u = RandomReal[1, 500]


Then I find the function value at these points:

f = (1 - u^2)^(0.5)


Then I do the partial sum,

s = Accumulate[f]


But I want the sample mean for all n, not only sample mean for n=500. In s I have all partial sums, but I do not know how to divide them by corresponding n. I should have the following graph. Update: With the suggestion of ilian, I got the following graph. Quite different, I am thinking why. Any suggestion would be appreciated!

• Perhaps s = Accumulate[f] / Range[Length[f]] ? – ilian May 19 '15 at 13:16
• @ilian,thanks for help, but the generated graph does not similar to the above one, I am thinking why. – Bob May 19 '15 at 13:27

I misunderstood and commented only about computing the Cesaro means as per the question

In s I have all partial sums, but I do not know how to divide them by corresponding n.

The desired scatter plot of 500 Monte Carlo attempts with samples of increasing length could be obtained with something like

ticks = Range[-0.06, 0.06, 0.02];
s = Table[u = RandomReal[1, n];
f = (1 - u^2)^(0.5); Mean[f], {n, 500}];
ListPlot[s, AxesOrigin -> {0, Pi/4}, PlotRange -> {{0, 500}, {Pi/4 - 0.06,  Pi/4 + 0.06}},
Ticks -> {Automatic, Transpose[{ticks + Pi/4, ticks /. {0. -> Pi/4}}]}, AspectRatio -> 1] • Nice reproduction. +1 – LLlAMnYP May 19 '15 at 14:21

In your example plot, the Monte Carlo integral is computed afresh for each new amount of sampling points:

s[n_] := Total[Sqrt[1 - #^2] & /@ RandomReal[1, n]]/n


Then take one random point, find the mean, take two new random points, find their mean, and so on until 500. The result is:

ListPlot[Table[s[n], {n, 500}], PlotRange -> {0.7, 0.9}] The solution suggested by ilian is slightly different. There each subsequent value is correlated with the previous one, as they are the Cesàro means of the same sequence.

• +1 for the better explanation; I could have read the question more carefully. – ilian May 19 '15 at 14:34