Algorithm-RandomMatrixGeneration
view release on metacpan or search on metacpan
lib/Algorithm/RandomMatrixGeneration.pm view on Meta::CPAN
}
my $fmt_str;
my $add_str;
my $regex;
if($format eq "real")
{
if($precision !~ /^[0-9]+$/)
{
print STDERR "Please specify integer value for precision.\n";
exit 1;
}
# for precision
$fmt_str = "%.$precision" . "f";
$add_str = "0.";
for(my $i=1; $i<$precision; $i++)
{
$add_str .= "0";
}
$add_str .= "1";
$regex = "(\\d+\\.?\\d{0,$precision})";
}
# array to hold the generated matrix
my @ref = ();
my $rem_col_marg = 0;
# for each cell C(i,j)
# for each row (0..n)
for(my $i=0; $i<=$n; $i++)
{
# for each col (0..m)
for(my $j=0; $j<=$m; $j++)
{
# compute the min and max (range) for the cell value
# max = MIN(row_marg[i], col_marg[j])
my $max = $tmp_rmar[$i];
if($tmp_cmar[$j] < $max)
{
$max = $tmp_cmar[$j];
}
# if max = 0 then min = 0
# else assign min a value based on the remaining row_marginal and col_marginals to be satisfied
my $min = 0;
if($max != 0)
{
# sum-up the col_marginals for all the columns beyond the current column
$rem_col_marg = 0;
for(my $k=$j+1; $k<=$m; $k++)
{
$rem_col_marg = $rem_col_marg + $tmp_cmar[$k];
}
# based on the row_marg and the sum_of_col_marg decide the value for min
$min = $tmp_rmar[$i] - $rem_col_marg;
if($signValues eq "positive")
{
if($min < 0)
{
$min = 0;
}
}
}
else
{
$min = $max;
}
if($format eq "real")
{
$min = sprintf($fmt_str, $min);
$max = sprintf($fmt_str, $max);
}
if($signValues eq "negative")
{
if($min > $max)
{
my $tmp_min = $min;
$min = $max;
$max = $tmp_min;
}
}
my $rand_num = 0;
if($min != $max)
{
# generate a random number between the min and max (range)
if($format eq "real")
{
my $rand_max = $max-$min+$add_str;
$rand_num = rand($rand_max);
my $bigfloat = Math::BigFloat->new($rand_num);
my $rand_num_str = $bigfloat->bstr();
$rand_num_str =~/$regex/;
$rand_num = $1;
}
else
{
my $rand_max = $max-$min+1;
$rand_num = int(rand($rand_max));
}
$rand_num += $min;
}
else
{
$rand_num = $min;
}
if($signValues eq "negative")
lib/Algorithm/RandomMatrixGeneration.pm view on Meta::CPAN
=over 4
=item 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
=head1 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";
}
=head1 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.
=head1 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.
if(max != 0)
{
2_term = 0
for each col k > j
{
2_term = 2_term + col_marg[k]
}
min = row_marg[i] - 2_term
if(marginals positive)
{
if(min < 0)
{
min = 0
}
}
}
else
{
min = max = 0 # If max = 0 then min = 0
}
# Generate random number between the range
random_num = rand(min, max)
}
Example:
Cell 0:
max = MIN(3,2) = 2
2_term = 2 + 2 + 2 + 2 = 8
min = 3 - 8 = -5
therefore: min = 0
(min, max) = (0,2) = 0
0 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 |
Cell 1:
max = MIN(3,2) = 2
2_term = 2 + 2 + 2 = 6
min = 3 - 6 = -3
therefore: min = 0
(min, max) = (0,2) = 0
0 0 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 |
( run in 1.187 second using v1.01-cache-2.11-cpan-39bf76dae61 )