blib/lib/Math/Round/Fair.pm

filter: Statementstmt 91.8% 113 / 123 Branchbran 73.3% 22 / 30 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 94.4% 17 / 18 Podpod 66.6% 2 / 3 totaltotal 85.0% 159 / 187 CC 26 SCAR 17.6
CC
26
Cov
86%
_check_invariants
20.0
round_fair
7.5
_adjust_input
7.2
CRAP 5.8
↑ Summary
1 package Math::Round::Fair;
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use 5.005000;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Devel::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use List::Util qw/shuffle sum min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use POSIX qw/floor ceil DBL_EPSILON/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION = '0.03';
12  
13 6 our @EXPORT_OK = qw/round_fair round_adjacent/;
14  
15 6 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
16 6 my $debug;
17 13 ▶ sub DEBUG { $debug }
Subroutine DEBUG: called CC=1 SCAR=0.0
Pod undocumented
18  
19 6 ▶ $debug = $ENV{MATH_ROUND_FAIR_DEBUG} || 0;
Condition100%2/2
$ENV{'MATH_ROUND_FAIR_DEBUG'} || 0
l!l
51
MC/DC100%1/1
$ENV{'MATH_ROUND_FAIR_DEBUG'} || 0
$ENV{'MATH_ROUND_FAIR_DEBUG'}
Truth table100%2/2
A
A= $ENV{'MATH_ROUND_FAIR_DEBUG'}
Aresult
11
00
20 6 ▶ use Devel::Assert DEBUG() ? ('-all -verbose') : ();
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() ? : 0 6 6
21  
22 # used in assertions
23 6 ▶ eval q{use Perl6::Junction 'none'} if DEBUG();
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 5 1 6
24 }
25  
26  
27 =head1 NAME
28  
29 Math::Round::Fair - distribute rounding errors fairly
30  
31 =head1 SYNOPSIS
32  
33 use Math::Round::Fair 'round_fair', 'round_adjacent';
34  
35 my $cents = 7;
36 my @weights = (1, 2, 3, 2, 1);
37 my @allocation = round_fair($cents, @weights);
38  
39 print "@allocation\n";
40  
41 # output will be one of the following:
42 # 0 1 3 2 1
43 # 0 2 2 2 1
44 # 0 2 3 1 1
45 # 0 2 3 2 0
46 # 1 1 2 2 1
47 # 1 1 3 1 1
48 # 1 1 3 2 0
49 # 1 2 2 1 1
50 # 1 2 2 2 0
51  
52 my @total;
53 for ( 1..900 ) {
54 @allocation = round_fair($cents, @weights);
55 @total[$_] += @allocation[$_] for 0..$#allocation;
56 }
57 print "@total\n";
58  
59 # output will be *near* 700 1400 2100 1400 700, e.g.:
60 # 698 1411 2096 1418 677
61  
62  
63 my @rounded = round_adjacent(0.95, 0.65, 0.41, 0.99);
64 # @rounded will be one of the following:
65 # 59% of the time: 1, 1, 0, 1
66 # 35% of the time: 1, 0, 1, 1
67 # 5% of the time: 0, 1, 1, 1
68 # 1% of the time: 1, 1, 1, 0
69  
70  
71 =head1 DESCRIPTION
72  
73 This module provides two exportable functions, C<round_fair>, which
74 allocates an integer value, fairly distributing rounding errors, and
75 C<round_adjacent>, which takes a list of real numbers and rounds them up, or
76 down, to an adjacent integer, fairly. Both functions return a list of fairly
77 rounded integer values.
78  
79 C<round_fair> and C<round_adjacent> round up, or down, randomly, where the
80 probability of rounding up is equal to the fraction to round. For example, 0.5
81 will round to 1.0 with a probability of 0.5. 0.3 will round to 1.0 3 out of 10
82 times and to zero 7 out of 10 times, on average.
83  
84 Consider the problem of distributing one indivisible item, for example a penny,
85 across three evenly weighted accounts, A, B, and C.
86  
87 Using a naive approach, none of the accounts will receive an allocation since
88 the allocated portion to each is 1/3 and 1/3 rounds to zero. We are left with
89 1 unallocated item.
90  
91 Another approach is to adjust the basis at each step. We start with 1 item to
92 allocate to 3 accounts. 1/3 rounds to 0, so account A receives no allocation,
93 and we drop it from consideration. Now, we have 2 accounts and one item to
94 allocate. 1/2 rounds to 1, so we allocate 1 item to account B. Account C
95 gets no allocation since there is nothing left to allocate.
96  
97 But what happens if we allocate one item to the same three accounts 10,000
98 times? Ideally, two accounts should end up with 3,333 items and one should end
99 up with 3,334 items.
100  
101 Using the naive approach, all three accounts receive no allocation since at
102 each round the allocation is 1/3 which rounds to zero. Using the second method,
103 account A and account C will receive no allocation, and account B will receive
104 a total allocation of 10,000 items. Account B always receives the benefit of
105 the rounding errors using the second method.
106  
107 The algorithm employed by this module uses randomness to ensure a fair distribution of
108 rounding errors. In our example problem, we start with 1 item to allocate. We
109 calculate account A's share, 1/3. Since it is less than one item, we give it a
110 1/3 chance of rounding up (and, therefore, a 2/3 chance of rounding down). It
111 wins the allocation 1/3 of the time. 2/3 of the time we continue to B. We
112 calculate B's allocation as 1/2 (since there are only 2 accounts remaining and
113 one item to allocate). B rounds up 1/2 of 2/3 (or 1/3) of the time and down
114 1/2 of 2/3 (or 1/3) of the time. If neither A nor B rounds up (which occurs
115 2/3 * 1/2, or 1/3 of the time), C's allocation is calculated as 1/1 since we
116 have one item to allocate and only one account to allocate it to. So, 1/3 of
117 the time C receives the benefit of the rounding error. We never end up with
118 any unallocated items.
119  
120 This algorithm works for any number of weighted allocations.
121  
122 =over 4
123  
124 =item round_fair($value, @weights)
125  
126 Returns a list of integer values that sum to C<$value> where each return value
127 is a portion of C<$value> allocated by the respective weights in C<@weights>.
128 The number of return values is equal to the number of elements in C<@weights>
129  
130 C<$value> must be an integer.
131  
132 =cut
133  
134 52 ▶sub round_fair {
Subroutine round_fair: called CC=7 SCAR=20.1
Pod documented
135 52 my $value = shift;
136  
137 52 ▶ croak "Value to be allocated must be an integer >= 0" unless int($value) == $value && $value >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless int $value == $value and $value >= 0 0 52 52
Condition33%1/3
int $value == $value and $value >= 0
!ll&&!rl&&r
0052
MC/DC0%0/2
int $value == $value and $value >= 0
int $value == $value $value >= 0
Truth table33%1/3
A and B
A= int $value == $valueB= $value >= 0
ABresult
0X0
100
111
138  
139 52 ▶ return ($value) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 52 52
140 52 ▶ return (0) x @_ if $value == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value == 0 0 52 52
141  
142 52 my $basis = 0;
143 52 for my $w ( @_ ) {
144 214 ▶ croak "Weights must be > 0" unless $w > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $w > 0 0 214 214
145 214 $basis += $w;
146 }
147  
148 52 my $sum = 0;
149 52 my @in = map { my $r = $value * $_ / $basis; $sum += $r; $r } @_;
150  
151 # First, create the extra entry for the total, so that the sum of
152 # the new array is zero.
153 52 push @in, -$sum;
154  
155 52 my $out = _round_adjacent_arrayref(\@in);
156 52 pop @$out; # Discard the entry for the total
157  
158 52 return @$out;
159 }
160  
161 =item round_adjacent(@input_values)
162  
163 Returns a list of integer values, each of which is numerically
164 adjacent to the corresponding element of @input_values, and whose total is
165 numerically adjacent to the total of @input_values.
166  
167 The expected value of each output value is equal to the corresponding element
168 of @input_values (within a small error margin due to the limited machine
169 precision).
170  
171 =cut
172  
173 11002 ▶sub round_adjacent {
Subroutine round_adjacent: called CC=2 SCAR=6.9
Pod documented
174 11002 ▶ return () unless @_; # identity
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 11001 11002
175  
176 # First, create the extra entry for the total, so that the sum of
177 # the new array is zero.
178 11001 push @_, -sum(@_);
179  
180 # use a reference to eliminate an unnecessary copy
181 11001 my $out = _round_adjacent_arrayref(\@_);
182  
183 11001 pop @$out; # Discard the entry for the total
184 11001 return @$out;
185 }
186  
187 11053 ▶sub _round_adjacent_arrayref {
Subroutine _round_adjacent_arrayref: called CC=2 SCAR=6.9
188 11053 my $in = shift;
189  
190 # Next, shuffle the order, so that the input order has no effect
191 # on the randomness characteristics.
192 11053 my @order = shuffle($[ .. $#{$in});
193 11053 @$in = map $in->[$_], @order;
194  
195 11053 my $out = _round_adjacent_core($in);
196  
197 11053 assert(sum(@$out) == 0);
198  
199 # put the output back into original order
200 11053 my @r;
201 11053 $r[$order[$_]] = $out->[$_] for $[ .. $#order;
202  
203 11053 return \@r;
204 }
205  
206 # Like _round_adjacent_arrayref, except that the inputs must sum to zero, and the
207 # input order may affect the variance and correlations, etc.
208 11053 ▶sub _round_adjacent_core {
Subroutine _round_adjacent_core: called CC=6 SCAR=17.9
209 11053 my $in = shift;
210  
211 11053 assert(scalar @$in); # @$in must not be empty
212  
213 11053 my $eps1 = 4.0 * DBL_EPSILON() * (1 + @$in);
214 11053 my $eps = $eps1;
215 11053 my @fp = map { my $ip = floor($_); $_ - $ip } @$in;
216  
217 11053 assert(none(@fp) < 0.0);
218  
219 # TBD: Maybe accuracy or fairness can be improved by
220 # re-adjusting after every iteration. This would slow it
221 # down significantly, though.
222 11053 _adjust_input(\@fp);
223  
224 11053 my @out;
225 11053 INPUT: while() {
226 70270 $eps += $eps1;
227  
228 70270 assert(_check_invariants($eps, $in, \@fp));
229  
230 # Calculate the next output. Discard the next input in the
231 # process.
232 70270 my $p0 = shift @fp; # Probability of having to overpay
233 70270 ▶ my $r0 = rand()<$p0 ? 1 : 0; # 1 if selected to overpay; else 0
Branch: 1 branch
BranchTrueFalseTotal
rand() < $p0 ? : 24163 46107 70270
234 70270 push @out, floor(shift @$in) + $r0;
235  
236 70270 ▶ last unless @fp;
Branch: 1 branch
BranchTrueFalseTotal
unless @fp 11053 59217 70270
237  
238 # Now adjust the remaining fractional parts.
239  
240 # $slack[i] = min( $p0 * $fp[i], (1-$p0) * (1-$fp[i]) ).
241 59217 my @slack;
242 59217 my $tslack = 0.0;
243 59217 do {
244 @slack = map {
245 59217 if ( 1 ) {
246 502371 my $slack = min $p0 * $_, (1 - $_) * (1.0 - $p0);
247 502371 $tslack += $slack;
248 502371 $slack;
249 }
250 else {
251 # This is fewer FLOPS, but the perf benefit
252 # is only 1% on a modern system, and it leads
253 # to greater numerical errors for some reason.
254 my $add = $p0 + $_;
255 my $mult = $p0 * $_;
256 $add > 1.0 ? 1.0 - $add + $mult : $mult
257 }
258 } @fp;
259 };
260  
261 # See bottom of file for proof of this property:
262 59217 assert($tslack + $eps >= $p0 * (1.0 - $p0));
263  
264 # wrapped in assert to make it a noop when DEBUG() == 0
265 59217 assert(do { warn "TSLACK = $tslack\n" if DEBUG() > 1; 1 });
266  
267 59217 ▶ if ( $tslack > $eps1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tslack > $eps1) 37161 22056 59217
268 37161 $eps += 128.0 * $eps1 * $eps / $tslack;
269 # NOTE: The expected value of gain is
270 # $p0 * ($p0 - 1.0) /$tslack +
271 # (1.0 - $p0) * $p0 / $tslack = 0
272 37161 my $gain = do {
273 37161 ▶ if ( $r0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r0) { } 17547 19614 37161
274 # Last guy overpaid, so the probabilities for
275 # subsequent payers drop.
276 17547 ($p0 - 1.0) / $tslack;
277 }
278 else {
279 # Last guy underpaid, so the probabilities for
280 # subsequent payers rise.
281 19614 $p0 / $tslack;
282 }
283 };
284  
285 # NOTE: The change in the sum of @fp due to this step
286 # is $tslack * $gain, which is either $p0 or ($p0 - 1).
287 # Either way, the sum remains an integer, because it
288 # was reduced by $p0 when we shifted off the first
289 # element early in the INPUT loop iteration.
290 # Also note that each element of @fp stays in the range
291 # [0,1] because if $r0, then slack($_, $p0) * -$gain <=
292 # $p0 * $_ * (1.0 - $p0) / ($p0 * (1.0 - $p0)) ==
293 # $_, and otherwise slack($_, $p0) * $gain <=
294 # (1 - $p0) * (1 - $_) * $p0 / ($p0 * (1.0 - $p0)) ==
295 # 1 - $_.
296 # We modify in place here, for performance.
297 37161 $_ += shift(@slack) * $gain for @fp;
298 }
299 }
300 11053 assert(@$in == 0);
301 11053 return \@out;
302 }
303  
304 11053 ▶sub _adjust_input {
Subroutine _adjust_input: called CC=7 SCAR=19.7
305 11053 my $p = shift;
306  
307 # Adjust @$p to account for numerical errors due to small
308 # difference of large numbers when the integer parts are big.
309 11053 my $sum = sum @$p;
310 11053 ▶ if ( $sum != floor($sum) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum != floor($sum)) 4019 7034 11053
311 4019 my $target = floor($sum + 0.5);
312  
313 4019 ▶ die "Total loss of precision"
Branch: 1 branch
BranchTrueFalseTotal
unless abs $sum - $target < "0.1" and $sum + "0.05" != $sum 0 4019 4019
Condition33%1/3
abs $sum - $target < "0.1" and $sum + "0.05" != $sum
!ll&&!rl&&r
004019
MC/DC0%0/2
abs $sum - $target < "0.1" and $sum + "0.05" != $sum
abs $sum - $target < "0.1" $sum + "0.05" != $sum
Truth table33%1/3
A and B
A= abs $sum - $target < "0.1"B= $sum + "0.05" != $sum
ABresult
0X0
100
111
314 unless abs($sum - $target) < 0.1 && $sum + 0.05 != $sum;
315  
316 4019 my $adj = $target / $sum;
317 4019 ▶ if ( $adj <= 1.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adj <= 1.0) { } 2618 1401 4019
318 2618 $_ *= $adj for @$p;
319 } else {
320 1401 $adj = (@$p - $target) / (@$p - $sum);
321 1401 $_ = 1.0 - (1.0-$_) * $adj for @$p;
322 }
323 }
324 }
325  
326 0 ▶sub _check_invariants {
Subroutine _check_invariants: not called CC=4 SCAR=30.0
327 0 my ( $eps, $v, $fp ) = @_;
328  
329 0 ▶ if ( DEBUG() > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (DEBUG() > 1) 0 0 0
330 0 warn sprintf "%d %f\n", floor($_), $_ for @$fp;
331 }
332  
333 0 assert(@$v && @$v == @$fp);
334  
335 0 for ( @$fp ) {
336 0 assert($_ >= -$eps);
337 0 assert($_ <= 1.0 + $eps);
338 }
339  
340 0 my $sum = sum @$fp;
341 0 assert(abs($sum - floor($sum + 0.5)) < $eps * (1 + $sum));
342  
343 0 1;
344 }
345  
346 6 1;
347  
348 __END__
↑ Summary