Bar Charts, BinCounts and Random Numbers

The frequency of occurrence of the data

Below is some experimental data from measuring the background radiation for 10 second intervals repeated 25 times:

Num[0]=1.;
Num[1]=6.;
Num[2]=7.;
Num[3]=5.;
Num[4]=3.;
Num[5]=2.;
Num[6]=0.;
Num[7]=1.;
Num[8]=0.;
Num[9]=0.;

For example, there were a total of 5 observations each having 3 counts of the background radiation.  The total number of observations is

TotNum=(∑_(n=0)^9)Num[n]

which is 25 as expected.

Conversion to probabilities

The probability Prob[n] of n counts is defined by

Prob[n_]:=((Num[n])/TotNum)

The probability must be one if summed over all the possibilities

(∑_(n=0)^9)Prob[n]

and so this checks.

The average number of counts ⟨ n ⟩

By definition ⟨ n ⟩ is defined nAve

nAve=(∑_(n=0)^9)n*Prob[n]

The standard deviation (σ^2)

σ2=(∑_(n=0)^9)(((n-nAve))^2)*Prob[n]

and the rms deviation σ is

σ=(Sqrt[σ2])

Graph of the counts data

First construct a table of the data

CountsData=Table[{n,Prob[n]},{n,0,9}];

and next graph the results

ListPlot[CountsData,PlotStyle->PointSize[0.02]]

BarChart of Counts Data

BarChartData=Table[Prob[n],{n,0,9}];

BarChart[BarChartData]

Random Number Generator

There is a random number generator built-in to Mathematica.  For example,

Random[]

generates real random numbers in the range 0 to 1.  If the numbers produced are random, then the distribution on the range 0<x<1 should be a constant.  The Do Loop belows generates 10 random number on the range  0<x<1 and stores the numbers as RandomData.

RandomData=Table[Random[],{n,1,10}]

You can check the value of the 2 random number stored

RandomData[[2]]

This number will change if you run the Table function again.

It is easy enough to generate a graph of the RanNum[n] versus the trial number n:

ListPlot[RandomData,PlotRange->{0,1}]

If you repeat the above for 100 datapoints then

RandomData=Table[Random[],{n,1,100}];ListPlot[RandomData,PlotRange->{0,1}]

It is fairly convincing to the eye that the data is uniformly distributed on the range 0<X<1.  However, we want to check this mathematically.  Use the Mathematica function BinCounts to put the data above into bins.

The Size of the Bins

You must use a bit a judgement in picking the size of the Bins.  Obviously 2 bins will not tell us much and we would expect (on average) there to be 50 observations in each bin (but there are statistical fluctuations which would make this not exactly true in each case).

min=0;max=1.;width=0.5;
BinData=BinCounts[RandomData,{min,max,width}]

Notice we did not get 50, 50 and the fluctuation 5 was rather large since the total number of observation 100 was relatively small.

Try bins of width=0.1 and on average we expect 0.1*100 =10 counts per bin (but there are fluctuations).

min=0;max=1.;width=0.1;
BinData=BinCounts[RandomData,{min,max,width}]

Next let us to back and change the total number of observations to 10,000

RandomData=Table[Random[],{n,1,10000}];ListPlot[RandomData,PlotRange->{0,1}]

If the bins are 0.5 wide then we expect roughly 0.5*10000=5000 per bin on average.

min=0;max=1.;width=0.5;
BinData=BinCounts[RandomData,{min,max,width}]

The 5032 is only 32 more observations that the 5000 expected on average and similarly 4968 is only 32 less that 5000.

If the bins are 0.1 wide then we expect roughly 0.1*10000=1000 observation in each bin on average.

min=0;max=1.;width=0.1;
BinData=BinCounts[RandomData,{min,max,width}]

BarChart[BinData,AxesLabel->{"Bin #","Observations"}]

This is fairly convincing that the Random Number Generator does what it is supposed to do.  That is, generate numbers between 0<X<1 with equal probability.

Increase the Number of Observations to 1000000 and keep the bin width 0.1

Now we expect on average 0.1*1000000=100000  observations in each bin.

RandomData=Table[Random[],{n,1,1000000}];
min=0;max=1.;width=0.1;
BinData=BinCounts[RandomData,{min,max,width}]

You can prove that the statistical fluctuations are roughly (Sqrt[n])  where n is the number of observations in each bin.  For the case at hand n=100000  so  (Sqrt[n])=316 which might seem large but compared with n, this small (3%)

(Sqrt[100000.])

(316./100000)

BarChart[BinData,AxesLabel->{"Bin #","Observations"}]

This is really convincing proof that the random number generator is really random.

The effect of decreasing the bin width to 0.001 for example

RandomData=Table[Random[],{n,1,1000000}];
min=0;max=1.;width=0.001;
BinData=BinCounts[RandomData,{min,max,width}];

BarChart[BinData,AxesLabel->{"Bin #","Observations"}]

Now the graph is not so smooth and this is the result of taking the bin width much smaller.  You can estimate the size of the fluctuation as follows.  Each bin on average will have 0.001*1000000=1000 observations and the size of the statistical fluctuation is (Sqrt[n])=(Sqrt[1000])=31.6  and this is about 100 times larger than when we had wider bins.

(Sqrt[1000.])

The relative size of the fluctuation is now (31.6/1000)= 3.2%

Conclusion:  How to pick the bin width?

Pick the bin width so the relative statistical fluctuation is "small".  What is meant by small is determined by the requirements of your experiment and the number of observations.