At times the values of a subset of variables of our model
are known. In this case, an inference about the remaining values
can be stated: it is possible to construct a kernel on the
complement of
.
Program 3 illustrates the use of the method
. The example consists in the following situation:
Suppose you have
species of fish and
fish of each species. A
non null quantity
of fish is uniformly selected for
each of the species (i.e.
). The selected fish
are then liberated in an initially non containing fish stank. Knowing
the initial quantities
of fish, you would like to
construct a kernel that permits to simulate fishing until no
more fish are in the stank. You would like also to know the more
probable fish species that you will catch in your
launch. Ready? good fishing !
1 /*============================================================================= 2 * File : fishing.cpp 3 *============================================================================= 4 * 5 *------------------------- Description --------------------------------------- 6 * This program simulates fishing in an stank with n specie of fish where 7 * for each fish specie there are m1,m2,....mn number of fish of specie 1,2,..n 8 * respectively. The program gives the most probable specie to be obtained and 9 * then it draws a fish specie (simuation of fishing) by using the fish specie 10 * distribution given m1,m2,....mn. 11 *----------------------------------------------------------------------------- 12 */ 13 14 #include <pl.h> 15 16 #define N_FISH_SPECIES 5 17 #define M_EXISTING_FISH 10 18 19 main() 20 { 21 /********************************************************************** 22 Defining the variable type, set and values 23 ***********************************************************************/ 24 25 plIntegerType quantity_type(1,M_EXISTING_FISH); 26 plIntegerType specie_type(0,N_FISH_SPECIES-1); 27 plArray quantities("Totalspecies", 28 quantity_type,1,N_FISH_SPECIES); 29 plSymbol species("Species",specie_type); 30 plValues quantities_values(quantities); 31 plValues species_value(species); 32 33 /********************************************************************** 34 Defining the variables set distributions: 35 - P(quantities) = Uniform 36 - P(species | quantities) = conditional probability table 37 ***********************************************************************/ 38 39 plUniform P_quantity(quantities); 40 plCndProbTable P_species_knowing_quantity(species,quantities); 41 42 P_quantity.draw(quantities_values); // Init quantity_values with an 43 // uniform random selection 44 45 /********************************************************************** 46 Compute total number of fish = 47 quantity(1)+quantity(1)+...+quantity(N_FISH_SPECIES) 48 ***********************************************************************/ 49 50 int i, t_i,total_of_fish; 51 total_of_fish = 0; 52 for(i=0;i<N_FISH_SPECIES;i++){ 53 t_i = quantities_values[quantities(i)]; 54 total_of_fish += t_i; 55 } 56 57 /********************************************************************** 58 While there are fish simulate fishing 59 ***********************************************************************/ 60 61 plKernel P_species; 62 i=0; 63 while(i < total_of_fish ) { 64 65 /*** Print the state of the model ***/ 66 cout<<"=================================================\n"; 67 cout<<i+1<<"th Launch \n"; 68 cout<<"Total number of fish : "<<total_of_fish-i<<endl; 69 cout<<"distributed as follows :\n"<<quantities_values<<endl; 70 71 /*** Get the kernel representing the distribution function : 72 P(species) ****/ 73 74 P_species_knowing_quantity.instantiate(P_species,quantities_values); 75 cout<<"P_species = "<<P_species<<endl; 76 77 78 /*** Estimate the species result then draw species ***/ 79 80 P_species.best(species_value); // Estimate species result 81 cout<<"More probable expected result :"; 82 cout<<species_value<<endl; // Print the estimated result 83 84 85 P_species.draw(species_value); // Simulate catching a fish 86 cout<<"Draw result : "; 87 cout<<species_value<<"\n\n"; // Print the catch fish 88 89 quantities_values[species_value[0]] = // Decrement by one the number 90 quantities_values[species_value[0]]-1; // of fish at the corresponding 91 // species 92 93 i++; 94 } 95 }
Lines 25 to 31 is the construction of the variable types, sets
and values. Note that quantities
is a variable set composed by the array of symbol variables:
quantities(0)
,quantities(1)
,...,
quantities(5)
and that species
is a single symbol. Two variable values
are constructed: one for storing the quantity of each fish
specie (line 30) and one for storing the type of specie (line
31). We have then two variable values representing data such
as:
Remember that when printing variables sets and variables values the print name parameter in the variable set constructor will be used, by consequence (1.3) will be printed as:
and, (1.4) as:
Lines 39 construct an uniform distribution on . After
that, in line 40, the conditional distribution of
knowing
is built by a conditional probability table (i.e. a
one dimension histogram, see
for more
details). Line 42 makes an uniform selection of the initial number of
fish for each of the species. Lines 50 to 55 is just the computation
of the total number of fish on the stank. Note that in order to access
the quantities of fish of species
in
quantities_values
we
use quantities(i)
. In fact a variable values is a vector like
object that permits the access to its members by this mean. An
initially empty kernel is constructed in line 61. This kernel
will be passed as a reference value to the instantiate
method. Knowing the quantities
of each of the fish
species we set
P_species
, by means of the instantiate method (line 74). The instantiated kernel
P_species
has a compute function defined as follows:
Line 75 prints the resulting kernel, a probability
table1.3. Lines 80 to 82 gets and prints the element on
P_species
with the highest probability. Lines 85
simulates catching a fish, a random selection
over species
is executed by using the inferred kernel
P_species
and the draw method, line 87 prints the
result. The output of Program 3 is something like
:
================================================= 1th Launch Total number of fish : 39 distributed as follows : {TotalSpecies0=9 TotalSpecies1=4 TotalSpecies2=8 TotalSpecies3=8 TotalSpecies4=10} P_species = P(Species) = Species Probability 0 0.230769 1 0.102564 2 0.205128 3 0.205128 4 0.25641 More probable expected result :{Species=4} Draw result : {Species=0} ================================================= 2th Launch Total number of fish : 38 distributed as follows : {TotalSpecies0=8 TotalSpecies1=4 TotalSpecies2=8 TotalSpecies3=8 TotalSpecies4=10} P_species = P(Species) = Species Probability 0 0.210526 1 0.105263 2 0.210526 3 0.210526 4 0.263158 More probable expected result :{Species=4} Draw result : {Species=2} ================================================= 3th Launch Total number of fish : 37 distributed as follows : {TotalSpecies0=8 TotalSpecies1=4 TotalSpecies2=7 TotalSpecies3=8 TotalSpecies4=10} P_species = P(Species) = Species Probability 0 0.216216 1 0.108108 2 0.189189 3 0.216216 4 0.27027 More probable expected result :{Species=4} Draw result : {Species=4} . . . . . . ================================================= 39th Launch Total number of fish : 1 distributed as follows : {TotalSpecies0=0 TotalSpecies1=1 TotalSpecies2=0 TotalSpecies3=0 TotalSpecies4=0} P_species = P(Species) = Species Probability 0 0 1 1 2 0 3 0 4 0 More probable expected result :{Species=1} Draw result : {Species=1}