blib/lib/Quantum/Superpositions/Lazy/Statistics.pm

filter: Statementstmt 97.3% 109 / 112 Branchbran 90.0% 18 / 20 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 96.8% 31 / 32 Podpod 100% 4 / 4 totaltotal 96.5% 170 / 176 CC 15 SCAR 11.1
CC
15
Cov
96%
weighted_median
9.0
find_border_elements
4.0
weighted_mean
3.0
CRAP 3.0
« blib/lib/Quantum/Superpositions/Lazy/Operation/Logical.pm ↑ Summary blib/lib/Quantum/Superpositions/Lazy/Computation.pm »
1 package Quantum::Superpositions::Lazy::Statistics;
2 15 $Quantum::Superpositions::Lazy::Statistics::VERSION = '1.12';
3 15 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Quantum::Superpositions::Lazy::Role::Collapsible;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Quantum::Superpositions::Lazy::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Types::Standard qw(ArrayRef ConsumerOf InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Sort::Key qw(keysort nkeysort);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use List::Util qw(sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # This approximation should be well within the range of 32 bit
13 # floating point values - 6 digits (IEEE 754)
14 15 ▶use constant HALF_APPROX => "0.500000";
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub transform_states
17 2 ▶{
Subroutine transform_states: called CC=1 SCAR=0.0
18 2 my ($items, $transformer) = @_;
19 my @transformed = map {
20 2 $_->clone_with(value => $transformer)
21 } @$items;
22  
23 2 return \@transformed;
24 }
25  
26 sub weight_to_probability
27 376 ▶{
Subroutine weight_to_probability: called CC=2 SCAR=8.1
28 376 my ($item, $weight_sum) = @_;
29 return $item->clone_with(
30 weight => sub { shift() / $weight_sum }
31 376 ▶ ) if defined $item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item 376 0 376
32  
33 0 return $item;
34 }
35  
36 sub weighted_mean
37 6 ▶{
Subroutine weighted_mean: called CC=3 SCAR=11.0
38 6 my ($list_ref, $weight_sum) = @_;
39 6 ▶ $weight_sum = sum0 map { $_->weight }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $weight_sum 1 5 6
40 $list_ref->@*
41 unless defined $weight_sum;
42  
43 6 ▶ if ($weight_sum > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight_sum > 0) 5 1 6
44 5 my @values = map { $_->value * $_->weight / $weight_sum } $list_ref->@*;
45 5 return sum0 @values;
46 }
47 1 return undef;
48 }
49  
50 # The sorting order is irrelevant here
51 sub weighted_median
52 5 ▶{
Subroutine weighted_median: called CC=9 SCAR=22.0
53 5 my ($sorted_list_ref, $average) = @_;
54 5 ▶ $average //= 0;
Condition100%2/2
$average //= 0
l!l
14
MC/DC100%1/1
$average //= 0
$average
Truth table100%2/2
A
A= $average
Aresult
11
00
55  
56 my $approx_half = sub {
57 my ($value) = @_;
58  
59 return HALF_APPROX eq substr(($value . (0 x length HALF_APPROX)), 0, length HALF_APPROX);
60 5 };
61  
62 5 my $running_sum = 0;
63 5 my $last_el;
64 my @found;
65  
66 5 for my $el (@{$sorted_list_ref}) {
67 206 $running_sum += $el->weight;
68  
69 206 ▶ if ($running_sum > 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running_sum > "0.5") 4 202 206
70 4 ▶ push @found, $last_el if $approx_half->($running_sum - $el->weight);
Branch: 1 branch
BranchTrueFalseTotal
if &$approx_half($running_sum - $el->weight) 2 2 4
71 4 push @found, $el;
72 4 last;
73 }
74  
75 202 $last_el = $el;
76 }
77  
78 # if we're allowed to average the result, do that
79 5 ▶ return weighted_mean(\@found)
Branch: 1 branch
BranchTrueFalseTotal
if $average 1 4 5
80 if $average;
81  
82 # get the lowest weight value if we can't average the two
83 # be biased towards the first value
84 4 ▶ return $found[1]->weight < $found[0]->weight ? $found[1]->value : $found[0]->value
Branch: 2 branches
BranchTrueFalseTotal
if @found == 2 1 3 4
$found[1]->weight < $found[0]->weight ? : 0 1 1
85 if @found == 2;
86  
87 3 ▶ return @found > 0 ? $found[0]->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
@found > 0 ? : 2 1 3
88 }
89  
90 # CAUTION: float == comparison inside. Will only work for elements
91 # that were obtained in a similar fasion
92 sub find_border_elements
93 6 ▶{
Subroutine find_border_elements: called CC=4 SCAR=13.9
94 6 my ($sorted) = @_;
95 6 my @found;
96 6 for my $state (@$sorted) {
97 742 ▶ push @found, $state
Branch: 1 branch
BranchTrueFalseTotal
if @found == 0 or $found[-1]->weight == $state->weight 8 734 742
Condition100%3/3
@found == 0 or $found[-1]->weight == $state->weight
l!l&&r!l&&!r
62734
MC/DC100%2/2
@found == 0 or $found[-1]->weight == $state->weight
@found == 0 $found[-1]->weight == $state->weight
Truth table100%3/3
A or B
A= @found == 0B= $found[-1]->weight == $state->weight
ABresult
1X1
011
000
98 if @found == 0 || $found[-1]->weight == $state->weight;
99 }
100  
101 6 return \@found;
102 }
103  
104 15 my %options = (
105 is => "ro",
106 lazy => 1,
107 init_arg => undef,
108 );
109  
110 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 15 our $implementation = __PACKAGE__;
113  
114 15 has "parent" => (
115 is => "ro",
116 isa => ConsumerOf ["Quantum::Superpositions::Lazy::Role::Collapsible"],
117 weak_ref => 1,
118 );
119  
120 # Sorted in ascending order
121 has "sorted_by_probability" => (
122 %options,
123 isa => ArrayRef [InstanceOf ["Quantum::Superpositions::Lazy::State"]],
124 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 6 my ($self) = @_;
126  
127 [
128 map {
129 376 weight_to_probability($_, $self->parent->weight_sum)
130 }
131 376 ▶ nkeysort {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 376 $_->weight
133 }
134 6 $self->parent->states->@*
135 ];
136 },
137 15 );
138  
139 # Sorted in ascending order
140 # (we use sorted_by_probability to avoid copying states twice in weight_to_probability)
141 has "sorted_by_value_str" => (
142 %options,
143 isa => ArrayRef [InstanceOf ["Quantum::Superpositions::Lazy::State"]],
144 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 4 my ($self) = @_;
146  
147 [
148 372 ▶ keysort { $_->value }
Subroutine __ANON__: called CC=1 SCAR=0.0
149 4 $self->sorted_by_probability->@*
150 ];
151 },
152 15 );
153  
154 has "sorted_by_value_num" => (
155 %options,
156 isa => ArrayRef [InstanceOf ["Quantum::Superpositions::Lazy::State"]],
157 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 1 my ($self) = @_;
159  
160 [
161 3 ▶ nkeysort { $_->value }
Subroutine __ANON__: called CC=1 SCAR=0.0
162 1 $self->sorted_by_probability->@*
163 ];
164 },
165 15 );
166  
167 # Other consumer indicator
168 has "most_probable" => (
169 %options,
170 isa => InstanceOf ["Quantum::Superpositions::Lazy::Superposition"],
171 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 3 my ($self) = @_;
173  
174 3 my @sorted = reverse $self->sorted_by_probability->@*;
175 3 return Quantum::Superpositions::Lazy::Superposition->new(
176 states => find_border_elements(\@sorted)
177 );
178 },
179 15 );
180  
181 has "least_probable" => (
182 %options,
183 isa => InstanceOf ["Quantum::Superpositions::Lazy::Superposition"],
184 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 3 my ($self) = @_;
186  
187 3 my $sorted = $self->sorted_by_probability;
188 3 return Quantum::Superpositions::Lazy::Superposition->new(
189 states => find_border_elements($sorted)
190 );
191 },
192 15 );
193  
194 has "median_str" => (
195 %options,
196 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
197 4 my ($self) = @_;
198  
199 4 weighted_median($self->sorted_by_value_str);
200 },
201 15 );
202  
203 has "median_num" => (
204 %options,
205 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 1 my ($self) = @_;
207  
208 1 weighted_median($self->sorted_by_value_num, 1);
209 },
210 15 );
211  
212 has "mean" => (
213 %options,
214 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 3 my ($self) = @_;
216  
217 # since the mean won't return a state, we're free not
218 # to make copies of the states.
219 3 weighted_mean($self->parent->states, $self->parent->weight_sum);
220 },
221 15 );
222  
223 has "variance" => (
224 %options,
225 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
226 2 my ($self) = @_;
227  
228 # transform_states is required here so that we don't modify existing states
229 weighted_mean(
230 2 ▶ transform_states($self->parent->states, sub { $_[0]**2 }),
Subroutine __ANON__: called CC=1 SCAR=0.0
231 $self->parent->weight_sum
232 )
233 -
234 $self->mean**2;
235 },
236 15 );
237  
238 sub sorted_by_value
239 0 ▶{
Subroutine sorted_by_value: not called CC=1 SCAR=6.9
Pod documented
240 0 my ($self) = @_;
241 0 return $self->sorted_by_value_str;
242 }
243  
244 sub median
245 5 ▶{
Subroutine median: called CC=1 SCAR=0.0
Pod documented
246 5 my ($self) = @_;
247 5 return $self->median_str;
248 }
249  
250 sub expected_value
251 2 ▶{
Subroutine expected_value: called CC=1 SCAR=0.0
Pod documented
252 2 my ($self) = @_;
253 2 return $self->mean;
254 }
255  
256 sub standard_deviation
257 2 ▶{
Subroutine standard_deviation: called CC=1 SCAR=0.0
Pod documented
258 2 my ($self) = @_;
259 2 return sqrt $self->variance;
260 }
261  
262 15 1;
263  
264 __END__
« blib/lib/Quantum/Superpositions/Lazy/Operation/Logical.pm ↑ Summary blib/lib/Quantum/Superpositions/Lazy/Computation.pm »