Multinomial/AliasMethod.pm

filter: Statementstmt 100% 96 / 96 Branchbran 82.1% 23 / 28 Conditioncond 75.0% 6 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 92.8% 143 / 154 CC 20 SCAR 17.6
CC
20
Cov
95%
_initialise_alias_tables
12.0
_validate_prng_object
4.1
draw_n_samples
4.0
CRAP 5.8
↑ Summary Multinomial.pm »
1 package Statistics::Sampler::Multinomial::AliasMethod;
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 qw /min sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::MoreUtils qw /first_index/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw /blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 1 ▶use parent qw /Statistics::Sampler::Multinomial/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 9 ▶sub _validate_prng_object {
Subroutine _validate_prng_object: called CC=4 SCAR=14.1
18 9 my ($self, $prng) = @_;
19
20 # Math::Random::MT::Auto has boolean op overloading
21 # so make sure we don't trigger it or our tests fail
22 # i.e. don't use "if $prng" directly
23 # (and we waste a random number, but that's less of an issue)
24 9 ▶ return 1 if !defined $prng;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prng 1 8 9
25
26 8 ▶ croak 'prng arg is not an object'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($prng) 0 8 8
27 if not blessed $prng;
28 8 ▶ croak 'prng arg does not have rand() method'
Branch: 1 branch
BranchTrueFalseTotal
unless $prng->can("rand") 0 8 8
29 if not $prng->can('rand');
30
31 8 return 1;
32 }
33
34 8 ▶sub _initialise_alias_tables {
Subroutine _initialise_alias_tables: called CC=12 SCAR=24.9
35 8 my ($self, %args) = @_;
36
37 8 my $probs = $self->{data};
38
39 # caller has not promised they sum to 1
40 8 ▶ if (!$self->{data_sum_to_one}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"data_sum_to_one"}) 8 0 8
41 8 my $sum = sum (@$probs);
42 8 ▶ if ($sum != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum != 1) 8 0 8
43 8 my @scaled_probs = map {$_ / $sum} @$probs;
44 8 $probs = \@scaled_probs;
45 }
46 }
47
48 # algorithm and comments stolen/adapted from
49 # https://hips.seas.harvard.edu/blog/2013/03/03/the-alias-method-efficient-sampling-with-many-discrete-outcomes/
50
51 8 my (@smaller, @larger);
52 8 my @J = (0) x scalar @$probs;
53 8 my @q = (0) x scalar @$probs;
54 8 my $kk = -1;
55 8 my $K = scalar @$probs;
56
57 8 foreach my $prob (@$probs){
58 111 $kk++;
59 111 $q[$kk] = $K * $prob;
60 111 ▶ if ($q[$kk] < 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q[$kk] < 1) { } 65 46 111
61 65 push @smaller, $kk
62 }
63 else {
64 46 push @larger, $kk;
65 }
66 }
67
68 # Loop though and create little binary mixtures that
69 # appropriately allocate the larger outcomes over the
70 # overall uniform mixture.
71 8 ▶ while (scalar @smaller && scalar @larger) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @smaller and scalar @larger) 101 8 109
Condition100%2/2
scalar @smaller and scalar @larger
!ll
7102
MC/DC100%1/1
scalar @smaller and scalar @larger
scalar @smaller
Truth table100%2/2
A
A= scalar @smaller
Aresult
00
11
72 101 my $small = pop @smaller;
73 101 my $large = pop @larger;
74
75 101 $J[$small] = $large;
76 101 $q[$large] = ($q[$large] + $q[$small]) - 1;
77
78 101 ▶ if ($q[$large] < 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q[$large] < 1) { } 37 64 101
79 37 push @smaller, $large;
80 }
81 else {
82 64 push @larger, $large;
83 }
84 }
85 # handle numeric stability issues
86 # courtesy http://www.keithschwarz.com/darts-dice-coins/
87 8 ▶ while (scalar @larger) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @larger) 9 8 17
88 9 my $g = shift @larger;
89 9 $q[$g] = 1;
90 }
91 8 ▶ while (scalar @smaller) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @smaller) 1 8 9
92 1 my $l = shift @smaller;
93 1 $q[$l] = 1;
94 }
95
96 # need better names for these,
97 8 $self->{J} = \@J;
98 8 $self->{q} = \@q;
99
100 8 ▶ return if !defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 5 3 8
101
102 # should not expose these to the caller
103 3 my %results = (J => \@J, q => \@q);
104 3 ▶ return wantarray ? %results : \%results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
105 }
106
107 10 ▶sub draw {
Subroutine draw: called CC=3 SCAR=11.0
Pod documented
108 10 my ($self, $args) = @_;
109
110 10 my $prng = $self->{prng};
111
112 my $q = $self->{q}
113 10 ▶ // do {$self->_initialise_alias_tables; $self->{q}};
Condition66%2/3
$self->{"q"} // do { $self->_initialise_alias_tables; $self->{"q"} }
l!l&&r!l&&!r
640
MC/DC0%0/2
$self->{"q"} // do { $self->_initialise_alias_tables; $self->{"q"} }
$self->{"q"} do { $self->_initialise_alias_tables; $self->{"q"} }
Truth table66%2/3
A // B
A= $self->{"q"}B= do { $self->_initialise_alias_tables; $self->{"q"} }
ABresult
1X1
011
000
114
115 10 my $J = $self->{J};
116 10 my $K = scalar @$J;
117 10 my $kk = int ($prng->rand * $K);
118
119 # Draw from the binary mixture, either keeping the
120 # small one, or choosing the associated larger one.
121 10 ▶ return ($prng->rand < $q->[$kk]) ? $kk : $J->[$kk];
Branch: 1 branch
BranchTrueFalseTotal
$prng->rand < $q->[$kk] ? : 9 1 10
122 }
123
124 4 ▶sub draw_n_samples {
Subroutine draw_n_samples: called CC=4 SCAR=13.9
Pod documented
125 4 my ($self, $n) = @_;
126
127 4 my $prng = $self->{prng};
128
129 my $q = $self->{q}
130 4 ▶ // do {$self->_initialise_alias_tables; $self->{q}};
Condition66%2/3
$self->{"q"} // do { $self->_initialise_alias_tables; $self->{"q"} }
l!l&&r!l&&!r
310
MC/DC0%0/2
$self->{"q"} // do { $self->_initialise_alias_tables; $self->{"q"} }
$self->{"q"} do { $self->_initialise_alias_tables; $self->{"q"} }
Truth table66%2/3
A // B
A= $self->{"q"}B= do { $self->_initialise_alias_tables; $self->{"q"} }
ABresult
1X1
011
000
131 4 my $J = $self->{J};
132 4 my $K = scalar @$J;
133
134 4 my @draws = (0) x $K;
135 4 for (1..$n) {
136 41 my $kk = int ($prng->rand * $K);
137 # Draw from the binary mixture, either keeping the
138 # small one, or choosing the associated larger one.
139 # {SWL: could try to use Data::Alias or refaliasing here
140 # as the derefs cause overhead, albeit the big overhead
141 # is the method calls}
142 #push @draws, ($prng->rand < $q->[$kk]) ? $kk : $J->[$kk];
143 41 ▶ $draws[($prng->rand < $q->[$kk]) ? $kk : $J->[$kk]]++;
Branch: 1 branch
BranchTrueFalseTotal
$prng->rand < $q->[$kk] ? : 26 15 41
144 }
145
146 4 return \@draws;
147 }
148
149 # Cuckoo package to act as a method wrapper
150 # to use the perl PRNG stream by default.
151 1 package Statistics::Sampler::Multinomial::AliasMethod::DefaultPRNG {
152 1 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
153 1 return bless {}, __PACKAGE__;
154 }
155 12 ▶ sub rand {
Subroutine rand: called CC=1 SCAR=0.0
156 12 rand();
157 }
158 1 1;
159 }
160
161 1 1;
162 __END__
↑ Summary Multinomial.pm »