unbbayes.simulation.montecarlo.sampling
Class AMonteCarloSampling

java.lang.Object
  extended by unbbayes.simulation.montecarlo.sampling.AMonteCarloSampling
All Implemented Interfaces:
IMonteCarloSampling, ILongTaskProgressObservable
Direct Known Subclasses:
MapMonteCarloSampling, MatrixMonteCarloSampling

public abstract class AMonteCarloSampling
extends Object
implements IMonteCarloSampling


Field Summary
protected  int currentProgress
           
protected  String currentProgressStatus
           
protected  int[] factors
           
protected  int maxProgress
           
protected  int nTrials
           
protected  ProbabilisticNetwork pn
           
protected  Map<Integer,Integer> sampledStatesMap
           
protected  byte[][] sampledStatesMatrix
           
protected  List<Node> samplingNodeOrderQueue
           
protected  int[] timesSampled
           
 
Constructor Summary
AMonteCarloSampling()
           
 
Method Summary
protected  void addToSamplingOrderQueue(ArrayList<Node> children, boolean[] nodeAddedList)
          Take the children of a node that have already been added to the queue.
protected  void computeFactors()
          Calculate the factors necessary to transform the linear coordinate into a multidimensional one (which is the the state for each possible node - target and evidence).
protected  void createSamplingOrderQueue()
          Creates the queue of the nodes that are going to be analyzed.
protected  double[][] getCumulativeDistributionFunction(double[] pmf)
          Creates the cumulative distribution function (cdf) based on the node RV's pmf.
 int getCurrentProgress()
          The current number of this long task progress.
 String getCurrentProgressStatus()
          Returns a message with a description of the current status of the long task progress.
protected  Integer getIndexInQueue(Node node)
          Retrieves the node's index in the queue.
 int getLinearCoord(int[] multidimensionalCoord)
          Get the linear coordinate from the multidimensional one.
 int getMaxProgress()
          The maximum number allowed for this long task progress.
 byte[] getMultidimensionalCoord(int linearCoord)
          Get the multidimensional coordinate from the linear one.
protected  List<Integer> getParentsIndexesInQueue(ProbabilisticNode node)
          Return the indexes (sampling order) in the queue for the parents of a given node.
 int getPercentageDone()
          Returns the percentage of the progress done so far.
protected  double[] getProbabilityMassFunction(int[] sampledStates, List<Integer> parentsIndexes, ProbabilisticNode node)
          Creates the probability mass function based on the states sampled for the parents.
abstract  byte[][] getSampledStatesCompactMatrix()
          Returns the generated compact sample matrix.
abstract  Map<Integer,Integer> getSampledStatesMap()
          Returns the generated sample map, with key = linear coord (representing the sates sampled) and value = number of times this key was sampled.
abstract  byte[][] getSampledStatesMatrix()
          Returns the generated sample matrix.
 List<Node> getSamplingNodeOrderQueue()
          Return the order the nodes are in the sampled matrix.
protected  byte getState(double[] pmf)
          Uses the pmf to retrieve the cdf to choose a state from a random generated number (between 0 and 1).
abstract  int[] getStatesSetTimesSampled()
          The number of times the ith set of states was sampled.
protected  void initSamplingOrderQueue(boolean[] nodeAddedList)
          Initializes the queue with the nodes that are root.
 void notityObservers(LongTaskProgressChangedEvent event)
           
 void registerObserver(ILongTaskProgressObserver observer)
           
 void removeObserver(ILongTaskProgressObserver observer)
           
abstract  void start(ProbabilisticNetwork pn, int nTrials)
          Generates the MC sample with the given size for the given probabilistic network.
protected  void updateProgress(int progress)
           
protected  void updateProgress(int progress, String progressStatus)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxProgress

protected int maxProgress

currentProgress

protected int currentProgress

currentProgressStatus

protected String currentProgressStatus

pn

protected ProbabilisticNetwork pn

nTrials

protected int nTrials

samplingNodeOrderQueue

protected List<Node> samplingNodeOrderQueue

sampledStatesMap

protected Map<Integer,Integer> sampledStatesMap

sampledStatesMatrix

protected byte[][] sampledStatesMatrix

timesSampled

protected int[] timesSampled

factors

protected int[] factors
Constructor Detail

AMonteCarloSampling

public AMonteCarloSampling()
Method Detail

registerObserver

public void registerObserver(ILongTaskProgressObserver observer)
Specified by:
registerObserver in interface ILongTaskProgressObservable

removeObserver

public void removeObserver(ILongTaskProgressObserver observer)
Specified by:
removeObserver in interface ILongTaskProgressObservable

notityObservers

public void notityObservers(LongTaskProgressChangedEvent event)
Specified by:
notityObservers in interface ILongTaskProgressObservable

getMaxProgress

public int getMaxProgress()
Description copied from interface: ILongTaskProgressObservable
The maximum number allowed for this long task progress. It represents 100%.

Specified by:
getMaxProgress in interface ILongTaskProgressObservable
Returns:
the maximum number allowed for this long task progress. It represents 100%.

getCurrentProgress

public int getCurrentProgress()
Description copied from interface: ILongTaskProgressObservable
The current number of this long task progress. It represents a percentage.

Specified by:
getCurrentProgress in interface ILongTaskProgressObservable
Returns:
the current number of this long task progress. It represents a percentage.

getPercentageDone

public int getPercentageDone()
Description copied from interface: ILongTaskProgressObservable
Returns the percentage of the progress done so far. It should be Math.round((float)currentProgress / maxProgress * 10000), a number between 0 and 10000.

Specified by:
getPercentageDone in interface ILongTaskProgressObservable
Returns:
the percentage of the progress done so far.

getCurrentProgressStatus

public String getCurrentProgressStatus()
Description copied from interface: ILongTaskProgressObservable
Returns a message with a description of the current status of the long task progress.

Specified by:
getCurrentProgressStatus in interface ILongTaskProgressObservable
Returns:
a message with a description of the current status of the long task progress.

updateProgress

protected void updateProgress(int progress,
                              String progressStatus)

updateProgress

protected void updateProgress(int progress)

getSampledStatesMatrix

public abstract byte[][] getSampledStatesMatrix()
Returns the generated sample matrix. The row represents the ith trial and the column represents the jth node from the sampled order. The value matrix[i][j] represents the sampled state index (respecting the node's states order) for the jth node in the ith trial.

Specified by:
getSampledStatesMatrix in interface IMonteCarloSampling
Returns:
The generated sample matrix.

getSampledStatesCompactMatrix

public abstract byte[][] getSampledStatesCompactMatrix()
Returns the generated compact sample matrix. The row represents the ith sampled state set and the column represents the jth node from the sampled order. The value matrix[i][j] represents the sampled state index (respecting the node's states order) for the jth node in the ith sampled state set. To get the number of times the ith set of states was sampled use getStatesSetTimesSampled().

Specified by:
getSampledStatesCompactMatrix in interface IMonteCarloSampling
Returns:
The generated compact sample matrix.

getStatesSetTimesSampled

public abstract int[] getStatesSetTimesSampled()
The number of times the ith set of states was sampled.

Specified by:
getStatesSetTimesSampled in interface IMonteCarloSampling
Returns:
The number of times the ith set of states was sampled.

getSampledStatesMap

public abstract Map<Integer,Integer> getSampledStatesMap()
Returns the generated sample map, with key = linear coord (representing the sates sampled) and value = number of times this key was sampled.

Specified by:
getSampledStatesMap in interface IMonteCarloSampling
Returns:
The generated sample map.

start

public abstract void start(ProbabilisticNetwork pn,
                           int nTrials)
Generates the MC sample with the given size for the given probabilistic network.

Specified by:
start in interface IMonteCarloSampling
Parameters:
pn - Probabilistic network that will be used for sampling.
nTrials - Number of trials to generate.

getSamplingNodeOrderQueue

public List<Node> getSamplingNodeOrderQueue()
Return the order the nodes are in the sampled matrix.

Specified by:
getSamplingNodeOrderQueue in interface IMonteCarloSampling
Returns:
The order the nodes are in the sampled matrix.

createSamplingOrderQueue

protected void createSamplingOrderQueue()
Creates the queue of the nodes that are going to be analyzed.


initSamplingOrderQueue

protected void initSamplingOrderQueue(boolean[] nodeAddedList)
Initializes the queue with the nodes that are root. In other words. It will put in the queue the nodes that do not have parents.

Parameters:
nodeAddedList - Keeps track of the nodes that have already been added to the queue (nodeAddedList[nodeIndex]=true).

addToSamplingOrderQueue

protected void addToSamplingOrderQueue(ArrayList<Node> children,
                                       boolean[] nodeAddedList)
Take the children of a node that have already been added to the queue. Analyze them one by one and add the child that is not in the queue yet.

Parameters:
children - Children of a node that is already in the queue.
nodeAddedList - Nodes that have already been added to the queue.

getParentsIndexesInQueue

protected List<Integer> getParentsIndexesInQueue(ProbabilisticNode node)
Return the indexes (sampling order) in the queue for the parents of a given node.

Parameters:
node - The node to retrieve the parents for finding the indexes.
Returns:
List of indexes (sampling order) of a node's parents in the queue.

getIndexInQueue

protected Integer getIndexInQueue(Node node)
Retrieves the node's index in the queue.

Parameters:
node -
Returns:

getState

protected byte getState(double[] pmf)
Uses the pmf to retrieve the cdf to choose a state from a random generated number (between 0 and 1).

Parameters:
pmf - The probability mass function for the node RV that we want to sample the state for.
Returns:
The sampled state for a given RV (based on its pmf).

getCumulativeDistributionFunction

protected double[][] getCumulativeDistributionFunction(double[] pmf)
Creates the cumulative distribution function (cdf) based on the node RV's pmf.

Parameters:
pmf - The probability mass function of the RV to calculate the cdf.
Returns:
The cumulative distribution function (cdf) for the given pmf.

getProbabilityMassFunction

protected double[] getProbabilityMassFunction(int[] sampledStates,
                                              List<Integer> parentsIndexes,
                                              ProbabilisticNode node)
Creates the probability mass function based on the states sampled for the parents.

Parameters:
sampledStates - The states (sampledStates[nodeIndex]) sampled for the nodes (nodeIndex).
parentsIndexes - The nodeIndex for each parent.
node - The node/RV to calculate the pmf.
Returns:
The probability mass function (pmf) of the node RV.

computeFactors

protected void computeFactors()
Calculate the factors necessary to transform the linear coordinate into a multidimensional one (which is the the state for each possible node - target and evidence). FactorForNode[i + 1] = ProductOf(NumberOfStates[i]), for all previous nodes (i).


getLinearCoord

public final int getLinearCoord(int[] multidimensionalCoord)
Get the linear coordinate from the multidimensional one. LinearCoord = SumOf(StateOf[i] * FactorOf[i]), for all possible nodes (i).

Specified by:
getLinearCoord in interface IMonteCarloSampling
Parameters:
multidimensionalCoord - Multidimensional coordinate (represented by the state for each node).
Returns:
The corresponding linear coordinate.

getMultidimensionalCoord

public final byte[] getMultidimensionalCoord(int linearCoord)
Get the multidimensional coordinate from the linear one.

Specified by:
getMultidimensionalCoord in interface IMonteCarloSampling
Parameters:
linearCoord - The linear coordinate.
Returns:
The corresponding multidimensional coordinate.


Copyright © 2001-2010 University of Brasilia - UnB. All Rights Reserved.