Home > Mateda2.0 > sampling > MNGibbsGenerateIndividual.m

# MNGibbsGenerateIndividual

## PURPOSE

[NewIndividual] = MOAGenerateIndividual(OldIndividual,Cliques,Tables,Card,GibbSteps)

## SYNOPSIS

function [NewIndividual] = MOAGenerateIndividual(OldIndividual,Cliques,Tables,Card,GibbSteps)

## DESCRIPTION

     [NewIndividual] = MOAGenerateIndividual(OldIndividual,Cliques,Tables,Card,GibbSteps)
     MOASampleIndividual samples one individual using Gibbs Sampling
     INPUTS
     OldIndividual: Starting configuration for sampling (By default it is
     random)
     Cliques: Structure of the model in a list of cliques that defines the
              neighborhood of the variable.  Each row of Cliques is a clique. The first value is the number of overlapping variables (neighbors of
              variable i). The second, is the number of new variables (Variable i, but it could be extended to blocks of variables).
              Then, neighbor  variables are listed and  finally variable i is listed.
     NumbVar: Number of variables
     Card: Vector with the dimension of all the variables.
     OUTPUTS
     NewIndividual: Generated individual

     Last version 8/26/2008. Roberto Santana (roberto.santana@ehu.es)

## CROSS-REFERENCE INFORMATION

This function calls:

- FindAccCard [AccCard] = FindAccCard(length,Card)
- NumconvertCard [valindex] = NumconvertCard(num,length,AccCard)

This function is called by:

## SOURCE CODE

    0001 function [NewIndividual] = MOAGenerateIndividual(OldIndividual,Cliques,Tables,Card,GibbSteps)
    0002 % [NewIndividual] = MOAGenerateIndividual(OldIndividual,Cliques,Tables,Card,GibbSteps)
    0003 % MOASampleIndividual samples one individual using Gibbs Sampling
    0004 % INPUTS
    0005 % OldIndividual: Starting configuration for sampling (By default it is
    0006 % random)
    0007 % Cliques: Structure of the model in a list of cliques that defines the
    0008 %          neighborhood of the variable.  Each row of Cliques is a clique. The first value is the number of overlapping variables (neighbors of
    0009 %          variable i). The second, is the number of new variables (Variable i, but it could be extended to blocks of variables).
    0010 %          Then, neighbor  variables are listed and  finally variable i is listed.
    0011 % NumbVar: Number of variables
    0012 % Card: Vector with the dimension of all the variables.
    0013 % OUTPUTS
    0014 % NewIndividual: Generated individual
    0015 %
    0016 % Last version 8/26/2008. Roberto Santana (roberto.santana@ehu.es)
    0017
    0018 NumbVar = size(OldIndividual,2);
    0019
    0020 NewIndividual = OldIndividual;
    0021 a = 0;
    0022 while a<GibbSteps
    0023
    0024  for kk=1:NumbVar
    0025
    0026   VarToChange = kk; %fix(NumbVar*rand)+1; % Var to be changed is selected. This allows  the possibility of generalizing
    0027                                    % to blocks of variables by doing
    0028                                    % BlockToChange = fix(size(Cliques,1)*rand)+1;
    0029
    0030   i = VarToChange; % Just for simplicity
    0031   sizeCliqOther = Cliques(i,2);
    0032   sizeCliqSolap = Cliques(i,1);
    0033
    0034
    0035   if(sizeCliqSolap > 0)
    0036     CliqSolap = Cliques(i,3:Cliques(i,1)+2);
    0037     AccCardSolap = FindAccCard(sizeCliqSolap,Card(CliqSolap));
    0038     IndexInTableNeighbors =   NumconvertCard(NewIndividual(CliqSolap),sizeCliqSolap,AccCardSolap)+1;
    0039   else   % The var has no neighbors, use univariate probabilities
    0040     CliqSolap = [];
    0041     AccCardSolap = [];
    0042     dimSolap = 1;
    0043     IndexInTableNeighbors = 1;  %The table has an entry for each value of the variable
    0044   end
    0045
    0046   auxcumvector = cumsum(Tables{i}(IndexInTableNeighbors,:));
    0047   auxrand = rand;
    0048   auxvect = find(auxrand < auxcumvector);  % The new value is selected according to conditional probabilities
    0049   NewIndividual(i) =  auxvect(1)-1;
    0050  end,
    0051   a = a+1;
    0052 end,
    0053
    0054
    0055
    0056