> next up previous contents index
: Joint distributions : Programming with ProBT : Kernels   目次   索引

Conditional kernels

\begin{figure}\begin{center}
\psfig{figure=fishing.ps, width=12cm}
\end{center}\end{figure}

At times the values of a subset of variables $\Omega_k$ 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 $\Omega_k$.

Definition 5   Given $\Omega_k$ and its complement $\Omega_s$ over $\Omega$ ( $\Omega_k \cup \Omega_s = \Omega$ and $\Omega_k \cap \Omega_s =
\emptyset $) a conditional kernel on $\Omega_s$ given $\Omega_k$, or on $(\Omega_s,\Omega_k)$ for short is a computable object on $\Omega$ provided of the method $instantiate(\cdot)$. This method returns a kernel on $\Omega_s$ knowing $\omega_k \in \Omega_k$. The method $instantiate(\cdot)$ on a conditional kernel represents then a family of kernels, those obtained for each of the values $\omega_k \in \Omega_k$.

Program 3 illustrates the use of the method $instantiate(\cdot)$. The example consists in the following situation: Suppose you have $n$ species of fish and $m$ fish of each species. A non null quantity $m_{i=1,2\ldots n}$ of fish is uniformly selected for each of the species (i.e. $0<m_{i=1,2,\ldots n} \leq m$). The selected fish are then liberated in an initially non containing fish stank. Knowing the initial quantities $m_1,m_2,\ldots m_n$ 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 $i^{th}$ launch. Ready? good fishing !



Program 3: 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:


$\displaystyle {quantities\_values=}$
  $\textstyle \{quantities(0)=5,quantities(2)=4,\ldots,quantities(5)=8\}$   (1.3)

and,


$\displaystyle species\_value = \{ species = 4\}$     (1.4)

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:


\begin{displaymath}\{TotalSpecies0=5,TotalSpecies1=4,\ldots,TotalSpecies5=8\}\end{displaymath}

and, (1.4) as:

\begin{displaymath}\{Species = 4\}\end{displaymath}

Lines 39 construct an uniform distribution on $quantities$. After that, in line 40, the conditional distribution of $species$ knowing $quantities$ 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 $i$ 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 $quantities(0),quantities(2),\ldots,quantities(5)$ 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:


\begin{displaymath}compute(species= i) =
\frac{quantities(i)}{\sum_{j=1}^{n}quantities(j)}\end{displaymath}

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}


next up previous contents index
: Joint distributions : Programming with ProBT : Kernels   目次   索引
Juan-Manuel Ahuactzin 平成17年3月31日