| 1 | package Statistics::Sampler::Multinomial; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use 5.014; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 3 | our $VERSION = '1.02'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use Ref::Util qw /is_arrayref/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use List::Util qw /min sum/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 3 | ▶ | use List::MoreUtils qw /first_index/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 3 | ▶ | use Scalar::Util qw /blessed looks_like_number/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | #use parent qw/Clone/; | ||||||||||||||||||||
| 15 | 3 | ▶ | use Clone (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 36 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 18 | 36 | my ($class, %args) = @_; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 36 | my $data = $args{data}; | |||||||||||||||||||
| 21 | 36 | ▶ | croak 'data argument not passed' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | if !defined $data; | ||||||||||||||||||||
| 23 | 34 | ▶ | croak 'data argument is not an array ref' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | if !is_arrayref ($data); | ||||||||||||||||||||
| 25 | 28 | ▶ | croak 'data argument is an empty array' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | if !scalar @$data; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 28 | ▶ | my $first_neg_idx = first_index {($_ // 0) < 0} @$data; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_ // 0
MC/DC100%1/1
$_ // 0
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||
| 29 | 28 | ▶ | croak "negative values passed in data array ($data->[$first_neg_idx] at index $first_neg_idx)\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | if $first_neg_idx >= 0; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | my $self = { | ||||||||||||||||||||
| 34 | data => $data, | ||||||||||||||||||||
| 35 | data_sum_to_one => $args{data_sum_to_one}, | ||||||||||||||||||||
| 36 | 26 | }; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 26 | bless $self, $class; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 26 | my $prng = $args{prng}; | |||||||||||||||||||
| 41 | 26 | $self->_validate_prng_object ($prng); | |||||||||||||||||||
| 42 | $self->{prng} | ||||||||||||||||||||
| 43 | 26 | ▶ | = $prng | ||||||||||||||||||
|
Condition66%2/3
$prng // ("${class}::DefaultPRNG")->new
MC/DC0%0/2
$prng // ("${class}::DefaultPRNG")->new
$prng
("${class}::DefaultPRNG")->new
Truth table66%2/3
A // B
A= $prngB= ("${class}::DefaultPRNG")->new
| |||||||||||||||||||||
| 44 | // "${class}::DefaultPRNG"->new; | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 26 | return $self; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 3 | ▶ | sub _clone_inner { | ||||||||||||||||||
|
Subroutine
_clone_inner: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 50 | 3 | my $self = shift; | |||||||||||||||||||
| 51 | 3 | my $clone = bless {}, blessed $self; | |||||||||||||||||||
| 52 | 3 | $clone->{data} = [@{$self->{data}}]; | |||||||||||||||||||
| 53 | 3 | ▶ | $clone->{sum} = $self->{sum} if exists $self->{sum}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | $clone->{data_sum_to_one} = $self->{data_sum_to_one} | ||||||||||||||||||||
| 55 | 3 | ▶ | if exists $self->{data_sum_to_one}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 3 | return $clone; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 3 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 60 | 3 | my $self = shift; | |||||||||||||||||||
| 61 | 3 | my $clone; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 3 | ▶ | if ($self->{prng}->can('clone')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 3 | $clone = do { | |||||||||||||||||||
| 65 | 3 | delete local $self->{prng}; | |||||||||||||||||||
| 66 | 3 | $self->_clone_inner; | |||||||||||||||||||
| 67 | }; | ||||||||||||||||||||
| 68 | 3 | $clone->{prng} = $self->{prng}->clone; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||
| 71 | 0 | $clone = $self->_clone_inner; | |||||||||||||||||||
| 72 | 0 | $clone->{prng} = Clone::clone ($self->{prng}); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | 3 | return $clone; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 0 | ▶ | sub set_prng { | ||||||||||||||||||
|
Subroutine
set_prng: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 78 | 0 | my ($self, $prng) = @_; | |||||||||||||||||||
| 79 | 0 | $self->_validate_prng_object($prng); | |||||||||||||||||||
| 80 | 0 | $self->{prng} = $prng; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 17 | ▶ | sub _validate_prng_object { | ||||||||||||||||||
|
Subroutine
_validate_prng_object: called CC=4 SCAR=14.6
| |||||||||||||||||||||
| 84 | 17 | my ($self, $prng) = @_; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # Math::Random::MT::Auto has boolean op overloading | ||||||||||||||||||||
| 87 | # so make sure we don't trigger it or our tests fail | ||||||||||||||||||||
| 88 | # i.e. don't use "if $prng" directly | ||||||||||||||||||||
| 89 | # (and we waste a random number, but that's less of an issue) | ||||||||||||||||||||
| 90 | #return 1 if !defined $prng; | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | # no default yet, so croak if not passed | ||||||||||||||||||||
| 93 | 17 | ▶ | croak "prng arg is not defined. " | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | . "Math::Random::MT::Auto is a good default to use" | ||||||||||||||||||||
| 95 | if not defined $prng; | ||||||||||||||||||||
| 96 | 17 | ▶ | croak 'prng arg is not an object' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | if not blessed $prng; | ||||||||||||||||||||
| 98 | 17 | ▶ | croak 'prng arg does not have binomial() method' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | if not $prng->can('binomial'); | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 17 | return 1; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 12 | ▶ | sub _initialise { | ||||||||||||||||||
|
Subroutine
_initialise: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 106 | 12 | my ($self, %args) = @_; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 12 | my $probs = $self->{data}; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # caller has not promised they sum to 1 | ||||||||||||||||||||
| 111 | 12 | ▶ | if ( $self->{data_sum_to_one}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | $self->{sum} = 1; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||
| 115 | 12 | my $sum = sum (@$probs); | |||||||||||||||||||
| 116 | 12 | ▶ | if ($sum != 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 12 | my @scaled_probs = map {$_ / $sum} @$probs; | |||||||||||||||||||
| 118 | 12 | $probs = \@scaled_probs; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | 12 | $self->{sum} = $sum; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 12 | return; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | ▶ | sub get_class_count { | ||||||||||||||||||
|
Subroutine
get_class_count: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 127 | 0 | my $self = shift; | |||||||||||||||||||
| 128 | 0 | my $aref = $self->{data}; | |||||||||||||||||||
| 129 | 0 | return scalar @$aref; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | # simplified version of draw_n_samples | ||||||||||||||||||||
| 133 | # as we need a single index result | ||||||||||||||||||||
| 134 | # superseded by faster method | ||||||||||||||||||||
| 135 | 6 | ▶ | sub draw_slow { | ||||||||||||||||||
|
Subroutine
draw_slow: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||
| 136 | 6 | my ($self) = @_; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 6 | my $prng = $self->{prng}; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | my $data = $self->{data} | ||||||||||||||||||||
| 141 | 6 | ▶ | // 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"}
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | my $norm = $self->{sum} | ||||||||||||||||||||
| 144 | 6 | ▶ | // do {$self->_initialise; $self->{sum}}; | ||||||||||||||||||
|
Condition66%2/3
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
MC/DC0%0/2
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
$self->{"sum"}
do {
$self->_initialise;
$self->{"sum"}
}
Truth table66%2/3
A // B
A= $self->{"sum"}B= do {
$self->_initialise;
$self->{"sum"}
}
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 6 | my $kk = -1; | |||||||||||||||||||
| 147 | 6 | foreach my $data_kk (@$data) { | |||||||||||||||||||
| 148 | 15 | $kk++; | |||||||||||||||||||
| 149 | 15 | ▶ | next if !$data_kk; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 15 | ▶ | return $kk | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | if $prng->rand < ($data_kk / $norm); | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 9 | $norm -= $data_kk; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | # we should not get here | ||||||||||||||||||||
| 158 | 0 | return; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | # Simplified version of draw | ||||||||||||||||||||
| 162 | # that minimises calls to the PRNG. | ||||||||||||||||||||
| 163 | # Profiling indicated this was a major bottleneck, | ||||||||||||||||||||
| 164 | # at least with MRMA. | ||||||||||||||||||||
| 165 | # The use of a linear scan is not optimal, | ||||||||||||||||||||
| 166 | # but maintaining a sorted index for binary search | ||||||||||||||||||||
| 167 | # leads to overheads when the list is changed. | ||||||||||||||||||||
| 168 | # Could try a red-black tree or similar... | ||||||||||||||||||||
| 169 | 10120 | ▶ | sub draw { | ||||||||||||||||||
|
Subroutine
draw: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||
| 170 | 10120 | my ($self) = @_; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 10120 | my $prng = $self->{prng}; | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | my $data = $self->{data} | ||||||||||||||||||||
| 175 | 10120 | ▶ | // 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"}
| |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | my $norm = $self->{sum} | ||||||||||||||||||||
| 178 | 10120 | ▶ | // do {$self->_initialise; $self->{sum}}; | ||||||||||||||||||
|
Condition66%2/3
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
MC/DC0%0/2
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
$self->{"sum"}
do {
$self->_initialise;
$self->{"sum"}
}
Truth table66%2/3
A // B
A= $self->{"sum"}B= do {
$self->_initialise;
$self->{"sum"}
}
| |||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 10120 | my $rand = $prng->rand; | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 10120 | ▶ | if ($rand < 0.5) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | 5203 | $rand = $rand * $norm; | |||||||||||||||||||
| 184 | 5203 | my $cumsum; # = $data->[0]; | |||||||||||||||||||
| 185 | 5203 | my $kk = -1; # start one before the start | |||||||||||||||||||
| 186 | 5203 | foreach my $data_kk (@$data) { | |||||||||||||||||||
| 187 | 22947 | $kk++; | |||||||||||||||||||
| 188 | 22947 | ▶ | next if !$data_kk; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | 13431 | $cumsum += $data_kk; | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 13431 | ▶ | return $kk | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | if $rand <= $cumsum; | ||||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | else { | ||||||||||||||||||||
| 196 | # work from the end | ||||||||||||||||||||
| 197 | 4917 | $rand = (1 - $rand) * $norm; | |||||||||||||||||||
| 198 | 4917 | my $cumsum; | |||||||||||||||||||
| 199 | 4917 | my $kk = scalar @$data; # start one beyond the end | |||||||||||||||||||
| 200 | 4917 | foreach my $data_kk (reverse @$data) { | |||||||||||||||||||
| 201 | 5899 | $kk--; | |||||||||||||||||||
| 202 | 5899 | ▶ | next if !$data_kk; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | 5896 | $cumsum += $data_kk; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 5896 | ▶ | return $kk | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | if $rand <= $cumsum; | ||||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | # we should not get here | ||||||||||||||||||||
| 211 | 0 | return; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 3 | ▶ | sub draw_n_samples { | ||||||||||||||||||
|
Subroutine
draw_n_samples: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 215 | 3 | my ($self, $n) = @_; | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 3 | my $prng = $self->{prng}; | |||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | my $data = $self->{data} | ||||||||||||||||||||
| 220 | 3 | ▶ | // 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"}
| |||||||||||||||||||||
| 221 | 3 | my $K = scalar @$data - 1; | |||||||||||||||||||
| 222 | 3 | ▶ | my $norm = $self->{sum} // do {$self->_initialise; $self->{sum}}; | ||||||||||||||||||
|
Condition66%2/3
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
MC/DC0%0/2
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
$self->{"sum"}
do {
$self->_initialise;
$self->{"sum"}
}
Truth table66%2/3
A // B
A= $self->{"sum"}B= do {
$self->_initialise;
$self->{"sum"}
}
| |||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | 3 | my @draws; | |||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | 3 | foreach my $kk (0..$K) { | |||||||||||||||||||
| 227 | # avoid repeated derefs below - unbenchmarked micro-optimisation | ||||||||||||||||||||
| 228 | 46 | my $data_kk = $data->[$kk]; | |||||||||||||||||||
| 229 | 46 | ▶ | if (!$data_kk) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 230 | 4 | $draws[$kk] = 0; | |||||||||||||||||||
| 231 | 4 | next; | |||||||||||||||||||
| 232 | } | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | # MRMA does not like p>1 | ||||||||||||||||||||
| 235 | 42 | ▶ | my $prob = $data_kk > $norm ? 1 : $data_kk / $norm; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 236 | 42 | my $res = $prng->binomial ( | |||||||||||||||||||
| 237 | $prob, | ||||||||||||||||||||
| 238 | $n, | ||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||
| 240 | 42 | $draws[$kk] = $res; | |||||||||||||||||||
| 241 | 42 | $norm -= $data_kk; | |||||||||||||||||||
| 242 | 42 | $n -= $res; | |||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | 3 | return \@draws; | |||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | 10005 | ▶ | sub draw_with_mask { | ||||||||||||||||||
|
Subroutine
draw_with_mask: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 250 | 10005 | my ($self, $mask) = @_; | |||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 10005 | ▶ | croak 'mask argument is not an array ref' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 253 | if !is_arrayref $mask; | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 10005 | ▶ | if (!defined $self->{sum}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 256 | 1 | $self->_initialise; | |||||||||||||||||||
| 257 | } | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | # now we mask | ||||||||||||||||||||
| 260 | 10005 | my @deleted = delete local @{$self->{data}}[@$mask]; | |||||||||||||||||||
| 261 | 10005 | local $self->{sum} = $self->{sum} - sum @deleted; | |||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | 10005 | $self->draw (); | |||||||||||||||||||
| 264 | } | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | # lots of code duplication going on here | ||||||||||||||||||||
| 267 | 2 | ▶ | sub draw_n_samples_with_mask { | ||||||||||||||||||
|
Subroutine
draw_n_samples_with_mask: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 268 | 2 | my ($self, $n, $mask) = @_; | |||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | 2 | ▶ | croak 'mask argument is not an array ref' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 271 | if !is_arrayref $mask; | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | 2 | ▶ | if (!defined $self->{sum}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 274 | 2 | $self->_initialise; | |||||||||||||||||||
| 275 | } | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | # now we mask | ||||||||||||||||||||
| 278 | 2 | my @deleted = delete local @{$self->{data}}[@$mask]; | |||||||||||||||||||
| 279 | 2 | local $self->{sum} = $self->{sum} - sum @deleted; | |||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | 2 | $self->draw_n_samples ($n); | |||||||||||||||||||
| 282 | } | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | 3 | ▶ | sub update_values { | ||||||||||||||||||
|
Subroutine
update_values: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 285 | 3 | my ($self, %args) = @_; | |||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | 3 | ▶ | if (!defined $self->{sum}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 288 | 2 | $self->_initialise; | |||||||||||||||||||
| 289 | } | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | 3 | my $data = $self->{data}; | |||||||||||||||||||
| 292 | 3 | my $count = 0; | |||||||||||||||||||
| 293 | 3 | foreach my $iter (keys %args) { | |||||||||||||||||||
| 294 | 5 | ▶ | croak "iter $iter is not numeric" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 295 | if !looks_like_number $iter; | ||||||||||||||||||||
| 296 | 5 | ▶ | $self->{sum} += $args{$iter} - ($data->[$iter] // 0); | ||||||||||||||||||
|
Condition100%2/2
$data->[$iter] // 0
MC/DC100%1/1
$data->[$iter] // 0
$data->[$iter]
Truth table100%2/2
A
A= $data->[$iter]
| |||||||||||||||||||||
| 297 | 5 | $data->[$iter] = $args{$iter}; | |||||||||||||||||||
| 298 | 5 | $count ++; | |||||||||||||||||||
| 299 | } | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | 3 | return $count; | |||||||||||||||||||
| 302 | } | ||||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | 2 | ▶ | sub get_data { | ||||||||||||||||||
|
Subroutine
get_data: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 305 | 2 | my $self = shift; | |||||||||||||||||||
| 306 | 2 | my $data = $self->{data}; | |||||||||||||||||||
| 307 | 2 | ▶ | return wantarray ? @$data : [@$data]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 308 | } | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | 2 | ▶ | sub get_sum { | ||||||||||||||||||
|
Subroutine
get_sum: called CC=2 SCAR=10.1
Pod
documented
| |||||||||||||||||||||
| 311 | 2 | my $self = shift; | |||||||||||||||||||
| 312 | 0 | ▶ | return $self->{sum} // do {$self->_initialise; $self->{sum}}; | ||||||||||||||||||
|
Condition33%1/3
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
MC/DC0%0/2
$self->{"sum"} // do {
$self->_initialise;
$self->{"sum"}
}
$self->{"sum"}
do {
$self->_initialise;
$self->{"sum"}
}
Truth table33%1/3
A // B
A= $self->{"sum"}B= do {
$self->_initialise;
$self->{"sum"}
}
| |||||||||||||||||||||
| 313 | } | ||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | # Cuckoo package to act as a method wrapper | ||||||||||||||||||||
| 316 | # to use the perl PRNG stream by default. | ||||||||||||||||||||
| 317 | # currently croaks because we have no binomial method | ||||||||||||||||||||
| 318 | 3 | package Statistics::Sampler::Multinomial::DefaultPRNG { | |||||||||||||||||||
| 319 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 320 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 321 | 0 | croak "No default PRNG yet implemented for Statistics::Sampler::Multinomial.\n" | |||||||||||||||||||
| 322 | . "Try Math::Random::MT::Auto."; | ||||||||||||||||||||
| 323 | 0 | return bless {}, __PACKAGE__; | |||||||||||||||||||
| 324 | } | ||||||||||||||||||||
| 325 | #sub rand { | ||||||||||||||||||||
| 326 | # rand(); | ||||||||||||||||||||
| 327 | #} | ||||||||||||||||||||
| 328 | 0 | ▶ | sub binomial { | ||||||||||||||||||
|
Subroutine
binomial: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 329 | ... | ||||||||||||||||||||
| 330 | 0 | } | |||||||||||||||||||
| 331 | 3 | 1; | |||||||||||||||||||
| 332 | } | ||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | 3 | 1; | |||||||||||||||||||
| 336 | __END__ | ||||||||||||||||||||