| 1 | package Statistics::Sampler::Multinomial::Indexed; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.014; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 1 | our $VERSION = '1.02'; | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 1 | ▶ | use Ref::Util qw /is_arrayref/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 1 | ▶ | use List::Util 1.29 qw /min max sum pairmap/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | #use List::MoreUtils qw /first_index/; | ||||||||||||||||||||||
| 13 | 1 | ▶ | use Scalar::Util qw /blessed looks_like_number/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | #use POSIX qw /ceil floor/; | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 1 | ▶ | use parent qw /Statistics::Sampler::Multinomial/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 7 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 20 | 7 | my $pkg = shift; | |||||||||||||||||||||
| 21 | 7 | my $self = Statistics::Sampler::Multinomial->new(@_); | |||||||||||||||||||||
| 22 | 7 | bless $self, __PACKAGE__; | |||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | 7 | $self->build_index; | |||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 7 | return $self; | |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | 1 | ▶ | sub _clone_inner { | ||||||||||||||||||||
|
Subroutine
_clone_inner: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 30 | 1 | my $self = shift; | |||||||||||||||||||||
| 31 | # parent class handles most details | ||||||||||||||||||||||
| 32 | 1 | my $clone = $self->SUPER::_clone_inner; | |||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | # generate a dup of the index array-of-arrays | ||||||||||||||||||||||
| 35 | 1 | $clone->{index} = [map {[@$_]} @{$self->{index}}]; | |||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 1 | return $clone; | |||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | # Build a tree based index of cumulative values. | ||||||||||||||||||||||
| 42 | # This will help the single draw methods. | ||||||||||||||||||||||
| 43 | # Idea from | ||||||||||||||||||||||
| 44 | # https://www.chiark.greenend.org.uk/~sgtatham/algorithms/cumulative.html | ||||||||||||||||||||||
| 45 | 8 | ▶ | sub build_index { | ||||||||||||||||||||
|
Subroutine
build_index: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||
| 46 | 8 | my $self = shift; | |||||||||||||||||||||
| 47 | 8 | my $data = $self->{data}; | |||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | #my $max_depth = 1 + logb (scalar @$data); | ||||||||||||||||||||||
| 50 | 8 | my $max_depth = 1 + int (log (scalar @$data) / log (2)); | |||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | # each index entry contains the cumulative sum of its terminals | ||||||||||||||||||||||
| 53 | # and each level is half the length of the one below | ||||||||||||||||||||||
| 54 | 8 | my @indexed; | |||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | # bottom is just the data | ||||||||||||||||||||||
| 57 | 8 | $indexed[$max_depth] = $data; | |||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 8 | for my $level (reverse (0..$max_depth-1)) { | |||||||||||||||||||||
| 60 | 27 | my $pop; | |||||||||||||||||||||
| 61 | 27 | ▶ | if (@{$indexed[$level+1]} % 2) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 62 | 10 | push @{$indexed[$level+1]}, 0; | |||||||||||||||||||||
| 63 | 10 | $pop = 1; | |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||
| 65 | 27 | ▶ | @{$indexed[$level]} = pairmap {($a//0)+($b//0)} @{$indexed[$level+1]}; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Condition100%4/4
$a // 0
$b // 0
MC/DC100%1/1
$a // 0
$a
MC/DC100%1/1
$b // 0
$b
Truth table100%2/2
A
A= $a
Truth table100%2/2
A
A= $b
| |||||||||||||||||||||||
| 66 | 27 | ▶ | if ($pop) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 67 | 10 | pop @{$indexed[$level+1]}; | |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 8 | $self->{index} = \@indexed; | |||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | 8 | return; | |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 45 | ▶ | sub draw { | ||||||||||||||||||||
|
Subroutine
draw: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 77 | 45 | my ($self) = @_; | |||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | 45 | my $prng = $self->{prng}; | |||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | my $data = $self->{data} | ||||||||||||||||||||||
| 82 | 45 | ▶ | // croak 'it appears setup has not been run yet'; | ||||||||||||||||||||
|
Condition50%1/2
$self->{"data"} // croak("it appears setup has not been run yet")
MC/DC0%0/1
$self->{"data"} // croak("it appears setup has not been run yet")
$self->{"data"}
Truth table50%1/2
A
A= $self->{"data"}
| |||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | 45 | ▶ | return 0 if @$data == 1; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 45 | my $indexed = $self->{index}; | |||||||||||||||||||||
| 87 | 45 | my $norm = $indexed->[0][0]; | |||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 45 | my $rand = $prng->rand * $norm; | |||||||||||||||||||||
| 90 | 45 | my $rand_orig = $rand; | |||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | # climb down the index tree | ||||||||||||||||||||||
| 93 | # start from 1 as 0 has single value | ||||||||||||||||||||||
| 94 | 45 | my $level = 1; | |||||||||||||||||||||
| 95 | # current array items | ||||||||||||||||||||||
| 96 | 45 | my $left = 0; | |||||||||||||||||||||
| 97 | 45 | my $right = 1; | |||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 45 | ▶ | while ($level < $#$indexed) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 100 | 95 | ▶ | if ($rand <= $indexed->[$level][$left]) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 101 | # descending left | ||||||||||||||||||||||
| 102 | 65 | $left *= 2; | |||||||||||||||||||||
| 103 | 65 | $right = $left + 1; | |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||
| 106 | # descending right, | ||||||||||||||||||||||
| 107 | # so update target since left part | ||||||||||||||||||||||
| 108 | # of tree not in these sums | ||||||||||||||||||||||
| 109 | 30 | $rand -= $indexed->[$level][$left]; | |||||||||||||||||||||
| 110 | 30 | $left = $right * 2; | |||||||||||||||||||||
| 111 | 30 | $right = $left + 1; | |||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||
| 113 | 95 | $level++; | |||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||
| 115 | |||||||||||||||||||||||
| 116 | 45 | ▶ | return $rand > $data->[$left] ? $right : $left; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | # should rebuild the index if a new index exceeds next power of two | ||||||||||||||||||||||
| 120 | 2 | ▶ | sub update_values { | ||||||||||||||||||||
|
Subroutine
update_values: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||
| 121 | 2 | my ($self, %args) = @_; | |||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | 2 | ▶ | if (!defined $self->{sum}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 124 | 2 | $self->_initialise; | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | 2 | my $data = $self->{data}; | |||||||||||||||||||||
| 128 | 2 | my $indexed = $self->{index}; | |||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | 2 | my $max_update_iter = max (keys %args); | |||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | 2 | ▶ | if ($max_update_iter > $#$data) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 133 | # if someone passes in an iter that needs extra index levels | ||||||||||||||||||||||
| 134 | # then we rebuild the whole index | ||||||||||||||||||||||
| 135 | 1 | my $max_depth = int (log (scalar @$data) / log (2)); | |||||||||||||||||||||
| 136 | 1 | my $new_max_depth = int (log ($max_update_iter+1) / log (2)); | |||||||||||||||||||||
| 137 | 1 | ▶ | if ($new_max_depth > $max_depth) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 138 | 1 | $self->SUPER::update_values (%args); | |||||||||||||||||||||
| 139 | 1 | $self->{index} = undef; | |||||||||||||||||||||
| 140 | 1 | $self->build_index; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 2 | my $count = 0; | |||||||||||||||||||||
| 146 | 2 | foreach my $iter (keys %args) { | |||||||||||||||||||||
| 147 | 3 | ▶ | croak "iter $iter is not numeric" | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 148 | if !looks_like_number $iter; | ||||||||||||||||||||||
| 149 | |||||||||||||||||||||||
| 150 | 3 | ▶ | my $diff = $args{$iter} - ($data->[$iter] // 0); | ||||||||||||||||||||
|
Condition50%1/2
$data->[$iter] // 0
MC/DC0%0/1
$data->[$iter] // 0
$data->[$iter]
Truth table50%1/2
A
A= $data->[$iter]
| |||||||||||||||||||||||
| 151 | 3 | $self->{sum} += $diff; | |||||||||||||||||||||
| 152 | 3 | $data->[$iter] = $args{$iter}; | |||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | # update the index - bitshift is faster | ||||||||||||||||||||||
| 155 | #my $idx = int ($iter / 2); | ||||||||||||||||||||||
| 156 | 3 | my $idx = $iter >> 1; | |||||||||||||||||||||
| 157 | 3 | foreach my $level (reverse (0 .. $#$indexed-1)) { | |||||||||||||||||||||
| 158 | 9 | $indexed->[$level][$idx] += $diff; | |||||||||||||||||||||
| 159 | #$idx = int ($idx / 2); | ||||||||||||||||||||||
| 160 | 9 | $idx >>= 1; | |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | 3 | $count ++; | |||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | 2 | return $count; | |||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||
| 168 | |||||||||||||||||||||||
| 169 | |||||||||||||||||||||||
| 170 | 1 | 1; | |||||||||||||||||||||
| 171 | |||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||