Clair::RandomDistribution

RandomDistributionBase


SummaryPackage variablesSynopsisDescriptionGeneral documentationMethods

SummaryTop
Clair::RandomDistribution::RandomDistributionBase - base class for all distributions

Package variablesTop
No package variables defined.

Included modulesTop
Carp
Math::Random

SynopsisTop
Do not try to instantiate this class - it is an abstract base
providing methods and structures for creating probability
distributions, drawing random values from distributions,
and so on.

DescriptionTop
This class implements the following methods, which in general should
not be overridden:
 - new_distribution
    Base class constructor; only to be called from child classes
- draw_rand_from_dist
    Draws a random integer from the distribution
- dump_distribution_array
    Returns a string containing the distribution array,
    useful for debugging, suitable for printing
This class expects the following methods to be implemented by child
classes, which should be overridden:
	- dist_function
	    Returns a weight value given a random variable
	    NOTE: These weights need not sum to 1

MethodsTop
dist_functionDescriptionCode
draw_rand_from_distNo descriptionCode
dump_distribution_arrayDescriptionCode
new_distributionDescriptionCode

Methods description


dist_functioncode    nextTop
This is a skeleton method, which should be overridden by all children
classes.

dump_distribution_arraycodeprevnextTop
This method returns a string containing the underlying distribution
array representation. This is useful primarily for debugging.

new_distributioncodeprevnextTop
This is the base class constructor. It should be called only by the
constructor of a child class. It depends on the method
"dist_function" having been implemented, because it uses this
function to build the internal distribution representation.
Parameters:
dist_size => number of values in distribution (positive integer)
dist_name => name of distribution (string)

Methods code


dist_functiondescriptionprevnextTop
sub dist_function {
  croak "dist_function has not been implemented\n";
  return;
}

draw_rand_from_distdescriptionprevnextTop
sub draw_rand_from_dist {
  my $self = shift;
  my $rand_val = random_uniform ();	# Rand val on interval [0,1]
my $dist_array = $self->{dist_array}; # lower, upper end of search interval
my ($l, $u) = (0, $self->{dist_size}); my $i; # index of probe
while ($l <= $u) { $i = ($l + $u) >> 1; if ($dist_array->[$i] < $rand_val) { $l = $i+1; } elsif ($dist_array->[$i-1] >= $rand_val) { $u = $i-1; } else { return $i; } }
}

dump_distribution_arraydescriptionprevnextTop
sub dump_distribution_array {
  my $self = shift;
  my $out_str = "";

  for (my $i = 0; $i <= $self->{dist_size}; $i++) {
    $out_str .= "$i -> " . $self->{dist_array}->[$i] . "\n"; 
  }

  return $out_str;
}

new_distributiondescriptionprevnextTop
sub new_distribution {
  my $class = shift;
  my %params = @_;
  my $dist_size = $params{dist_size};	# use local var for repeated access
# We require the following args: "dist_size" and "dist_name"
unless ($dist_size) { croak "Must specify number of values in distribution (positive integer)\n"; } # Create generic instance hash
my $self = bless { %params, dist_array => [ -1 ] }, $class; # Populate distribution array representation
# NOTE: All actual values start at index 1
my $running_sum = 0; for (my $i = 1; $i <= $dist_size; $i++) { $running_sum += $self->dist_function ($i); push (@{$self->{dist_array}}, $running_sum); } # Running sum now contains the total weight of the dist
for (my $i = 1; $i <= $dist_size; $i++) { $self->{dist_array}->[$i] /= $running_sum;
} # Array is done. Return our instance.
return $self;
}

General documentation


No general documentation available.