0 )
s = s + Dist[i];
}
return s;
}
//Predicts what must be observed
private static void prediction()
{
for(int i= 0; i < nClasses; i++)
{
double probSubI = Dist[i] / sizeSample;
Predict[i] = probSubI * sizeSimulation;
}
}
//Runs a chi^2-test to check the accuracy
//of our source of random numbers
//that must fit the distribution given by Dist
private static double chiTest()
{
double d= 0;
for(int i= 0; i < nClasses; i++)
{
if (Predict[i]> 0 )
Discrep[i] =
Math.pow((count[i] - Predict[i]),2)
/Predict[i];
d = d + Discrep[i];
}
return d;
}
//The range of discrepancies is evaluated
//and is used to calculate the length of each
//interval to group Discrepancies.
public static double intervalLength()
{
min=100000;
max = 0;
int lim = sizeSimulation;
for(int i = 0; i < lim; i++)
{
if (Events[i] < min ) min = Events[i];
if (Events[i] > max ) max = Events[i];
}
System.out.println( "min = " + min );
System.out.println( "max = " + max );
double range = max - min;
double length = range /nClasses;
return length;
}
//Prints the frequency table
private static void printFreqTable(double FreqTable[][],
int nClasses)
{
System.out.println( "x and its absolute frequency" );
for ( int i = 0; i <= nClasses; i++ )
System.out.println( FreqTable[ i ][0]+
" "+ (int) (FreqTable[ i ][1]));
}
//Calculates the sum of absolute frequencies
//of a frequency table
private static void
sum(double FreqTable[][])
{
double sum = 0;
for ( int i = 0; i <= nClasses; i++ )
sum = sum + FreqTable[i][1];
System.out.println( "Number of events = " + sum);
}
//The border of each interval is calculated
private static void getBorders(double Vect[])
{
Barriers[0] = infLimit;
classMarkers[0] = infLimit + intervalLength/2;
//Calculate maximal value of data
double max = Vect[0];
for(int j=1; j < Vect.length; j++)
if (Vect[j] > max) max = Vect[j];
System.out.println( "Max value = " + max);
//Calculates number of classes
nClasses = (int) ((max- infLimit)/intervalLength) + 1;
System.out.println( "Number of classes = " + nClasses);
//Calculates interval borders and class markers
for(int j=1; j <= nClasses; j++)
{
Barriers[j] = Barriers[j-1] + intervalLength;
classMarkers[j] = classMarkers[j-1] + intervalLength;
}
/*System.out.println( "Borders are" );
printVector(Barriers);
System.out.println( "Class markers are" );
printVector(classMarkers);*/
}
//Data are grouped into interval classes.
private static void group(double Vect[])
{
intervalLength = intervalLength();
infLimit = min - intervalLength/2;
getBorders(Vect);
int nData = sizeSimulation;
//Class markers a re defined
for(int j=0; j < nClasses; j++)
{
FreqTable[j][0] = classMarkers[j];
FreqTable[j][1] = 0;
}
//Frequencies are calculated.
for(int i=0; i< nData; i++)
{
for(int j=0; j < nClasses; j++)
{
if ((Vect[i] >= Barriers[j]) &
(Vect[i] < Barriers[j+1]))
FreqTable[j][1] =FreqTable[j][1] +1;
}
}
}
//The claim that there is a uniform distribution
//in within each inner barrier is tested
private static void test()
{
group(Events);
System.out.println("Frequency table of events");
printFreqTable(FreqTable, nClasses);
//Minor test for correct grouping
sum(FreqTable);
System.out.println("Frequencies of events (for Excel)");
for(int i = 0; i <= nClasses ; i++)
System.out.println(i + "\t" + FreqTable[i][1]);
}
public static void main(String[] args)
{
for (int i=0; i< Dist.length;i++) count[i]=0;
for (int i = 0; i < sizeSimulation;i++)
{
//A random decimal number in (0,10) is generated
//r.nextDouble() generates a random decimal number
//with a uniform distribution in within 0 and 1.
double e = 10*r.nextDouble();
//Outputs are renamed
if (e<3)
{e = e/3; count[0] = count[0]+1;}
if ((3<= e )& (e<5))
{e = 1+ (e-3)/2; count[1]=count[1]+1;}
if ((5<= e )& (e<10))
{ e = 3 + (e-5)/5; count[3]=count[3]+1;}
Events[i] = e;
}
printAll = false;
if (printAll)
{
System.out.println(" Simulated events");
printVector(Events);
}
System.out.println("Observed in simulation values");
printVector(count);
sizeSample = sizeSample();
prediction();
System.out.println("Predicted values");
printVector(Predict);
double dExp = chiTest();
System.out.println("Ho: observed values fit " +
"predicted ones");
System.out.println("Discrepancies");
printVector(Discrep);
System.out.println("Experimental discrepancy = " + dExp);
System.out.println("Critical chi2 = " + criticalChi2);
if (dExp > criticalChi2)
System.out.println("The event is extreme "
+ " according to Ho: invent another theory.");
else
System.out.println("Normal event according to Ho:"
+ "there is no reason to reject Ho");
//We open 10 classes for each interval
nClasses = 10* nClasses;
//test for uniformity in within integer borders
System.out.println(" \nTEST zone");
test();
}//End of main()
}//End of class A215 RandGeneratorr10
\end{verbatim}
\section{Conclusions}
It is amazing that randomness (the incapability to predict the next datum) could be simulated by
very simple deterministic recursive functions. We learned to device random generators with a
uniform, binomial or normal distributions. At last, we simulated a source of random numbers with
anyone distribution. We also paid attention to the study of the intuitive belief that with more
events a simulated distribution becomes more like the expected one. This resulted to be false, but
the concept of relative frequency was found to converge to the $p$ parameter of a binomial
distribution although the convergence is a fuzzy one. Convergence for the normal distribution was
better, a fact that forces one to think that one must try huge numbers to witness a better fitting
to the expected distribution.
\chapter{The Pandora box }
\label{chap13}
\texttt{Randomness as part of science.}
Here we learn the basics on how science deals with randomness.
\section{General view}
Modern science is built upon \index{materialism} \textbf{materialism}, which is a philosophical
school of thought that amounts to a \index{cosmovision} \textbf{cosmovision} or \index{world view}
\textbf{world view}, an ensemble of principles to interpret the world. According to it, mind is a
byproduct of the dynamics of matter, which is compounded of elementary particles, time and space.
Materialism is not imposed by anybody. Instead, the natural and spectacular development of
science has been framed by materialism and, moreover, those that consider that matter is enough to
explain everything, life included, have largely influenced the development of science.
Nevertheless,
it has not been shown that matter is enough to explain life. Concretely and in spite of all
efforts and promises, artificial life exists on
computers, not in reality. Since we lack artificial life, we cannot claim that we have understood
life. Because of this vacuum, materialism must be included in science as a regulative, dogmatic
principle that
demands that all explanations must be given in terms of matter such as it is known in labs.
Now, given materialism, the purpose of science is to describe matter, its fundamental parts,
interactions and dynamics. Descriptions are given in terms of hypothesis, theories. The \index{null
hypothesis} \textbf{null hypothesis} of
science is randomness. This means that everything in this world can be explained by randomness. Say,
we can deny the existence of gravitational fields that cause the falling of bodies towards the
center of the Earth and claim, instead, that bodies fall by mere randomness. Nevertheless, such an
explanation has a an insignificantly low
probability. By contrast, modern gravitational theories produce quantitative predictions that
allow the exact location of an ensemble of satellites that enable GPS. Given this, no one on
Earth beliefs that gravitation can be replaced by randomness. With this we are declaring that
randomness is everything that cannot be explained in a simple way by deterministic models.
Deterministic theories, similar to gravitation, are grandiose but at last explain very few things.
In fact, with rare exceptions all events that happen around you have exceedingly low
probabilities.
So, insignificant probabilities happen all
the time and science must accept them. In particular, insignificant probabilities that appear in a
distribution of central tendency, such as in a normal
distribution, are important in science when they represent an event that is an \index{outlier}
\textbf{outlier}, i.e., one that is far away from the mean. In a
case like this, an outlier becomes a matter of further investigation to see whether or not it is
explained by the same mechanism that generates the events in the center of the distribution.
Thus, outliers represent a place where new discoveries could be preferentially searched for.
Life is an outlier in comparison with most material systems. Say, a mammal can keep its corporeal
temperature approximately constant during long periods of time but, by contrast, most systems tend
to level their temperature to that of the milieu. This suffices to understand why life is a
phenomenon of tremendous scientific importance. In other words, science concedes a
great status to theories that pretend to explain life. Among them, the Evolutionary Theory of
the origin of species has been singled out as the official explanation that is, furthermore,
extremely elegant: it is the champion in the art of combining determinism and randomness. On one
hand we have that mutation is declared to be at random while in the other we see that selection is
an emerging deterministic phenomenon that is expected to produce better and better fitting of
organisms to their environment. We have found this belief very interesting and worth being
studied at depth. To begin with, let us try to understand randomness a bit better.
\bigskip
\section{Competence among models}
Randomness has many forms of being that are distinguished by their distributions, i.e., by the
relative frequencies of events in long experiments. So, randomness has many channels and ensuing
hypotheses or models to explain facts. Nevertheless, some models do it better than others. One
family of channels refer to uniform distributions in which all events happen with equal
probability. Allowed events form a set that is called the
support of the distribution. We will consider that the source of events corresponds
with the landed faces of a die. Our dies can have 6 or whatever numbers of faces.
\begin{teo}
\textbf{How many faces has the die? } Given a sequence of numbers that was generated with a die, we
must decide how many faces the die has.
\end{teo}
Let us consider the sequence 2, 3, 3,4,5,4,2,1,2,3,2,4,3,5,1,2,3, 5, 4. One can imagine various
theories to explain these data. To fix ideas, let us consider just two theories. First: the sequence
is produced by a source that functions like a die with 5 faces that fall with a uniform
distribution. Second: the sequence is produced by a
source that functions as a die with 6 faces with a uniform distribution but 6 does not appear just
because of randomness. It is clear that
the first theory is sound. But we cannot reject the second one: it also explains data although with
less probability.
So, let us device a program that a) simulates a die with 6 faces and b) asks how many trials are
necessary to produce a sequence 15 numbers long that mimics a die with 5 faces.
\begin{verbatim}
//Program A216 Die
//A source of random numbers simulates
//a face with 6 faces.
//Can it simulate a die with 5 faces?
//Yes, sometimes it does.
//We count how many trials are
//necessary to produce a sequence 15 numbers long
//that mimicks a die with 5 faces.
package ejvol1;
import java.util.Random;
public class Die
{
//Turn on of the random generator
private static Random r = new Random();
//size of the random sequence
private static int length= 15;
//Count counters the trials
private static int count;
//Absolute frequencies of faces 1 to 6
//that appeared in the sequence.
static int Observed[ ] = new int[7];
//Criticak chi
static private double criticalChi2 = 9.48;
public static double Obschisquare(int length)
{
double chi = 0;
//The die with five faces is associated to a distribution
//that assigns a probability 1/5 to numbers 1 to 5.
//Hence, a sequence of landed faces
//must have 1/5*length instances of each face.
double Expected = length/5;
//Expected distribution is compared to observed one.
//Discrepancy is approximately distributed
//as a chi square:
for(int j = 1; j<= 5; j++)
chi = chi + (Observed[j]-Expected)
*(Observed[j]-Expected)/Expected;
System.out.println("Observed chi-square = " + chi);
return chi;
}
//Sequences are generated with a uiform distribution
//in within 1 and 6 included
public static void tryAndTry(int length)
{
count = count +1;
System.out.print(" Trial " + count + " ");
// Initialization
for(int j = 1; j<= 6; j++)
Observed[j] = 0;
//We generate a random sequence
for(int j = 0; j< length; j++)
{
// We choose a uniform distribution
// from 1 to 6, as in a die.
int c = r.nextInt(6)+1;
System.out.print(c ) ;
// bookkeeping
Observed[c] = Observed[c]+1;
}
// Report
// for(int j = 1; j<= 6; j++)
// System.out.println(j + "\t" + Observed[j]);
if (Observed[6] > 0)
{
//System.out.println("It landed 6");
System.out.println(" Sequence with a 6");
tryAndTry(length);
}
else
{
System.out.println(" Sequence WITHOUT a 6");
//A uniform distribution
//with support 1...5 is needed.
if (Obschisquare(length) >= criticalChi2)
{
System.out.println(" NON UNIFORM distribution ");
tryAndTry(length);
}
else
System.out.println(" Uniform distribution ");
}
}
public static void main(String[] args)
{
tryAndTry(length);
System.out.println("We succeed at the trial " + count);
}
}//End of Program A216 Die
\end{verbatim}
\begin{teo}%\label{ m1}
\framebox[1\width]{\textbf{ Exercise }} Run the program and play with the code. Play enough to
agree else disagree with the Author: for the chosen length of sequences, a die with 5 faces can
easily be mimicked by a die with 6 faces.
\end{teo}
\begin{teo}
\colorbox{palegray}{\textbf{ Example }}
\end{teo} Let us modify the previous code to learn in which form the number of trials to achieve the
first success depends on the length of the sequence.
\begin{verbatim}
//Program A218 Die2
//Mimicking a die of 5 faces
//with a die of 6 faces.
package ejvol1;
import java.util.Random;
public class Die2
{
//Turn on of the random generator
static Random r = new Random();
//long is a very long integer
static long nTrials = 0; // number of trials.
static int Observed[ ] = new int[100];
//size of the random sequence
private static int length;
private static double Obschisquare(int length)
{
double chi = 0;
//The die with five faces is associated to a distribution
//that assigns a probability 1/5 to numbers 1 to 5.
//Hence, a sequence of landed faces
//must have 1/5*length instances of each face.
double Expected = length/5;
//Expected distribution is compared to observed one.
//Discrepancy is approximately distributed
//as a chi square:
for(int j = 1; j<= 5; j++)
chi = chi + (Observed[j]-Expected)
*(Observed[j]-Expected)/Expected;
//System.out.println("Observed chi-square = " + chi);
return chi;
}
// While we have not succeed
// try and try
private static void tryAndTry(int length)
{
// done is a flag that says
//when we have succeeded
int done = 0;
while (done==0 )
{
nTrials = nTrials +1;
//System.out.println(" Trial " + nTrials);
//Initialization
for(int j = 1; j<= 6; j++)
Observed[j] = 0;
//We generate a random sequence
for(int j = 0; j