5
$\begingroup$

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.

what it's supposed to be

Update: With the suggestion of ilian, I got the following graph.

what I got from ilian's solution

Quite different, I am thinking why. Any suggestion would be appreciated!

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

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]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Nice reproduction. +1 $\endgroup$ – LLlAMnYP May 19 '15 at 14:21
4
$\begingroup$

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}]

Proper sum

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.