Algorithm-RandomMatrixGeneration
view release on metacpan or search on metacpan
NAME
Algorithm::RandomMatrixGeneration - Generate internal cell values for a
matrix given fixed marginal totals.
SYNOPSIS
use Algorithm::RandomMatrixGeneration;
my @result = generateMatrix(\@row_marginals, \@col_marginals);
Example: Negative Integer Valued Marginals:
use Algorithm::RandomMatrixGeneration;
my @rmar = ('-5','5','-3');
my @cmar = ('2','3','-2','-6');
my @result = generateMatrix(\@rmargs, \@cmargs, "-");
Output matrix could be:
0 -1 1 3 2 -5 3 -2
0 5
0 -2 2 3 3 -4
Example: Positive Real Valued Marginals:
use Algorithm::RandomMatrixGeneration;
my @rmargs = (13.01,11,13,13,12,13);
my @cmargs = (23.005,32.005,10,10);
my @result = generateMatrix(\@rmargs, \@cmargs, 3);
Output matrix could be:
0 2.694 1 9.665 2 0.393 3 0.258
0 6.539 1 0.910 2 2.209 3 1.342
0 8.469 1 3.565 2 0.839 3 0.127
0 2.719 1 2.748 2 0.604 3 6.929
0 0.946 1 3.771 2 5.939 3 1.344
0 1.638 1 11.346 2 0.016
INPUTS
The generateMatrix function can take 4 parameters:
1. Single dimensional array containing row marginals (Can be real valued
or integers) =item 2. Single dimensional array containing column
marginals (Can be real valued or integers) =item 3. Precision: For the
integer valued marginal specifying "-". For real valued marginals
specify the required precision for the generated matrix values.
(Recommended Precision = 4) =item 4. Seed: Seed for the random number
generator (Default: None) (Optional parameter) =back
OUTPUT
The generateMatrix function returns a two dimensional array
containing the generated random matrix. The generated matrix is
stored in sparse format in this returned array. That is, only
non-zero values are stored in this matrix. Thus to access the values
in the returned matrix one can use:
for(my $row=0; $i<=$num_rows; $i++)
{
for(my $col=0; $j<=$num_cols; $j++)
{
if(defined $returned_matrix[$row][$col])
{
print "$col $returned_matrix[$row][$col] ";
}
}
print "\n";
}
DESCRIPTION
This module generates a random matrix given the row and column
marginals in such a way that the row and column marginals of the
resultant matrix are same as the given marginals.
If the given marginals are real valued then the generated cell
values are real too. If the given marginals are integer valued then
the generated cell values are integers. If any of the marginals are
negative then few/all of the generated cell values would be negative
too.
FURTHER DETAILS
For example, given the following marginals this module would
generate the appropriate values for "x"s such that the row and the
column marginals are held fixed.
x x x x x | 3
x x x x x | 2
x x x x x | 3
x x x x x | 2
------------------------------
2 2 2 2 2 | 10
The algorithm we have used here: For each cell while traversing the
matrix in in row-major interpretation. 1. Generate random number
using the steps given below. 2. Reduce row and column marginals by
value of generated value. End for. Done.
Random number generation algorithm: for each cell C(i,j) { # Find
the range (min, max) for the random number generation max =
MIN(row_marg[i], col_marg[j])
# If max !=0 then decide the min.
# To decide min value sum together the col_marginals for all
# the columns past the current column - this sum gives the total
# of the column marginals yet to be satisfied beyond the current col.
# Subtract this sum from the current row_marginal to compute
# the lower bound on the random number. We do this because if we
# do not set this lower bound and thus a number smaller than this
# bound is generated then we will have a situation where satisfying
# both row_marginal and column marginals will be impossible.
( run in 0.393 second using v1.01-cache-2.11-cpan-13bb782fe5a )