blib/lib/Statistics/Krippendorff.pm

filter: Statementstmt 100% 107 / 107 Branchbran 100% 22 / 22 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 24 / 24 Podpod 100% 11 / 11 totaltotal 98.8% 175 / 177 CC 24 SCAR 13.9
CC
24
Cov
99%
_build_coincidence
10.0
delta_masi
5.0
is_valid
4.0
CRAP 4.0
↑ Summary
1 =head1 NAME
2  
3 Statistics::Krippendorff - Calculate Krippendorff's alpha
4  
5 =head1 VERSION
6  
7 Version 0.06
8  
9 =cut
10  
11 package Statistics::Krippendorff;
12  
13 5 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 our $VERSION = '0.06';
20  
21 5 ▶use List::Util qw{ min sum };
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 has units => (is => 'ro',
26 required => 1,
27 coerce => \&_units_array2hash);
28  
29 has delta => (is => 'rw',
30 11 ▶ default => sub { \&delta_nominal },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 11 ▶ trigger => sub ($self, $d) {
Subroutine __ANON__: called CC=2 SCAR=6.9
32 $self->delta($self->_deltas->{$d})
33 11 ▶ if exists $self->_deltas->{$d};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_deltas->{$d} 2 9 11
34 5 });
35  
36 5 has coincidence => (is => 'lazy', init_arg => undef);
37  
38 5 has _vals => (is => 'lazy',
39 init_arg => undef,
40 builder => '_build_vals');
41  
42 5 has _frequency => (is => 'lazy',
43 init_arg => undef,
44 builder => '_build_frequency');
45  
46 5 has _expected => (is => 'lazy',
47 init_arg => undef,
48 builder => '_build_expected');
49  
50 has _deltas => (is => 'ro',
51 init_arg => undef,
52 15 ▶ default => sub { +{
Subroutine __ANON__: called CC=1 SCAR=0.0
53 15 nominal => \&delta_nominal,
54 interval => \&delta_interval,
55 ordinal => \&delta_ordinal,
56 ratio => \&delta_ratio,
57 jaccard => \&delta_jaccard,
58 masi => \&delta_masi
59 5 } });
60  
61 11 ▶sub alpha($self) {
Subroutine alpha: called CC=1 SCAR=0.0
Pod documented
62 my $d_o = sum(map {
63 11 my $v = $_;
64 map {
65 56 $self->coincidence->{$v}{$_} * $self->delta->($self, $v, $_)
66 } $self->vals
67 } $self->vals);
68 my $d_e = sum(map {
69 11 my $v = $_;
70 map {
71 56 $self->_expected->{$v}{$_} * $self->delta->($self, $v, $_)
72 } $self->vals
73 } $self->vals);
74 11 my $alpha = 1 - $d_o / $d_e;
75 11 return $alpha
76 }
77  
78 190 ▶sub vals($self) { @{ $self->_vals } }
Subroutine vals: called CC=1 SCAR=0.0
Pod documented
79  
80 594 ▶sub frequency($self, $value) {
Subroutine frequency: called CC=1 SCAR=0.0
Pod documented
81 594 return $self->_frequency->{$value}
82 }
83  
84 6 ▶sub pairable_values($self) {
Subroutine pairable_values: called CC=1 SCAR=0.0
Pod documented
85 6 return sum(values %{ $self->_frequency })
86 }
87  
88 7 ▶sub is_valid($self) {
Subroutine is_valid: called CC=4 SCAR=13.9
Pod documented
89 7 for my $unit (@{ $self->units }) {
90 14 ▶ return if 1 >= keys %$unit;
Branch: 1 branch
BranchTrueFalseTotal
if 1 >= keys %$unit 5 9 14
91 9 ▶ return if grep ! defined, values %$unit;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), values %$unit) 1 8 9
92 }
93 1 return 1
94 }
95  
96 116 ▶sub delta_nominal($, $s1, $s2) { $s1 eq $s2 ? 0 : 1 }
Subroutine delta_nominal: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$s1 eq $s2 ? : 27 89 116
97  
98 82 ▶sub delta_interval($, $v0, $v1) { ($v0 - $v1) ** 2 }
Subroutine delta_interval: called CC=1 SCAR=0.0
Pod documented
99  
100 50 ▶sub delta_ordinal($self, $v0, $v1) {
Subroutine delta_ordinal: called CC=2 SCAR=7.1
Pod documented
101 50 my ($from, $to) = sort { $a <=> $b } $v0, $v1;
102 50 ▶ (sum(map $self->frequency($_) // 0, $from .. $to)
Condition50%1/2
$self->frequency($_) // 0
l!l
1300
MC/DC0%0/1
$self->frequency($_) // 0
$self->frequency($_)
Truth table50%1/2
A
A= $self->frequency($_)
Aresult
11
00
103 - ($self->frequency($from) + $self->frequency($to))/ 2) ** 2
104 }
105  
106 50 ▶sub delta_ratio($, $v0, $v1) { (($v0 - $v1) / ($v0 + $v1)) ** 2}
Subroutine delta_ratio: called CC=1 SCAR=0.0
Pod documented
107  
108 270 ▶sub delta_jaccard($, $s1, $s2) {
Subroutine delta_jaccard: called CC=1 SCAR=0.0
Pod documented
109 270 my @s1 = split /,/, $s1;
110 270 my @s2 = split /,/, $s2;
111  
112 270 my %union;
113 270 @union{ @s1, @s2 } = ();
114  
115 270 my %intersection;
116 270 @intersection{@s1} = ();
117  
118 270 return 1 - (grep exists $intersection{$_}, @s2) / keys %union
119 }
120  
121 57 ▶sub delta_masi($, $v0, $v1) {
Subroutine delta_masi: called CC=5 SCAR=16.1
Pod documented
122 57 my @v0 = split /,/, $v0;
123 57 my @v1 = split /,/, $v1;
124 57 my %union;
125 57 @union{ @v0, @v1 } = ();
126 57 my $union = keys %union;
127  
128 57 my %intersection;
129 57 @intersection{ @v0 } = ();
130 57 my $intersection = grep exists $intersection{$_}, @v1;
131  
132 57 ▶ my $m = (@v0 == @v1 && @v0 == $intersection) ? 1
Branch: 3 branches
BranchTrueFalseTotal
@v0 == @v1 && @v0 == $intersection ? : 10 47 57
$intersection == &min(scalar @v0, scalar @v1) ? : 26 21 47
$intersection > 0 ? : 8 13 21
Condition100%3/3
@v0 == @v1 and @v0 == $intersection
!ll&&!rl&&r
38910
MC/DC100%2/2
@v0 == @v1 and @v0 == $intersection
@v0 == @v1 @v0 == $intersection
Truth table100%3/3
A and B
A= @v0 == @v1B= @v0 == $intersection
ABresult
0X0
100
111
133 : $intersection == min(scalar @v0, scalar @v1) ? 2 / 3
134 : $intersection > 0 ? 1 / 3
135 : 0;
136 57 return 1 - $intersection / $union * $m
137 }
138  
139 15 ▶sub _units_array2hash($units) {
Subroutine _units_array2hash: called CC=2 SCAR=6.9
140 15 ▶ if (ref [] eq ref $units->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref [] eq ref $units->[0]) 9 6 15
141 return [map {
142 9 my $unit = $_;
143 42 +{map +($_ => $unit->[$_]),
144 grep defined $unit->[$_],
145 0 .. $#$unit}
146 } @$units]
147 }
148 6 return $units
149 }
150  
151 6 ▶sub _build_vals($self) {
Subroutine _build_vals: called CC=1 SCAR=0.0
152 6 my %subf;
153 6 @subf{ map values %$_, @{ $self->units } } = ();
154 6 return [sort keys %subf]
155 }
156  
157 6 ▶sub _build_coincidence($self) {
Subroutine _build_coincidence: called CC=10 SCAR=23.0
158 6 my @vals = $self->vals;
159 6 my %coinc;
160 6 @{ $coinc{$_} }{@vals} = (0) x @vals for @vals;
161  
162 6 for my $unit (@{ $self->units }) {
163 47 my %is_value;
164 47 @is_value{ values %$unit } = ();
165 47 my @values = keys %is_value;
166 47 my @keys = keys %$unit;
167  
168 47 for my $v (@values) {
169 68 for my $v_ (@values) {
170 118 my $coinc_count = 0;
171 118 for my $key1 (@keys) {
172 319 for my $key2 (@keys) {
173 945 ▶ next if $key1 eq $key2;
Branch: 1 branch
BranchTrueFalseTotal
if $key1 eq $key2 319 626 945
174  
175 ++$coinc_count
176 if $unit->{$key1} eq $v
177 626 ▶ && $unit->{$key2} eq $v_;
Branch: 1 branch
BranchTrueFalseTotal
if $unit->{$key1} eq $v and $unit->{$key2} eq $v_ 206 420 626
Condition100%3/3
$unit->{$key1} eq $v and $unit->{$key2} eq $v_
!ll&&!rl&&r
308112206
MC/DC100%2/2
$unit->{$key1} eq $v and $unit->{$key2} eq $v_
$unit->{$key1} eq $v $unit->{$key2} eq $v_
Truth table100%3/3
A and B
A= $unit->{$key1} eq $vB= $unit->{$key2} eq $v_
ABresult
0X0
100
111
178 }
179 }
180 118 $coinc{$v}{$v_} += $coinc_count / (@keys - 1);
181 }
182 }
183 }
184 6 return \%coinc
185 }
186  
187 6 ▶sub _build_frequency($self) {
Subroutine _build_frequency: called CC=1 SCAR=0.0
188 6 my %f;
189 6 @f{ $self->vals } = map sum(values %{ $self->coincidence->{$_} }),
190 $self->vals;
191 6 return \%f
192 }
193  
194 6 ▶sub _build_expected($self) {
Subroutine _build_expected: called CC=4 SCAR=13.9
195 6 my %exp;
196 6 my $n = $self->pairable_values - 1;
197 6 for my $v ($self->vals) {
198 32 for my $v_ ($self->vals) {
199 182 ▶ $exp{$v}{$v_} = ($v eq $v_
Branch: 1 branch
BranchTrueFalseTotal
$v eq $v_ ? : 32 150 182
200 ? $self->frequency($v) * ($self->frequency($v) - 1)
201 : $self->frequency($v) * $self->frequency($v_)
202 ) / $n;
203 }
204 }
205 6 return \%exp
206 }
207  
208 =head1 SYNOPSIS
209  
210 use experimental qw( signatures );
211 use Statistics::Krippendorff ();
212  
213 my @units = ({coder1 => 1, coder2 => 1},
214 {coder1 => 2, coder2 => 2, coder3 => 1},
215 {coder2 => 3, coder3 => 2});
216 my $sk = 'Statistics::Krippendorff'->new(units => \@units);
217 my $alpha1 = $sk->alpha;
218 $sk->delta('nominal'); # Same as default.
219 my $alpha2 = $sk->alpha;
220  
221 my $ski = 'Statistics::Krippendorff'->new(
222 units => [[1, 1], [2,2,1], [undef,3,2]],
223 delta => sub ($, $v0, $v1) { ($v0 - $v1) ** 2 });
224 my $alpha_interval = $ski->alpha;
225  
226 =head1 METHODS
227  
228 =head2 new
229  
230 my $sk = 'Statistics::Krippendorff'->new(
231 units => \@units,
232 delta => 'nominal');
233  
234 The constructor. It accepts the following named arguments:
235  
236 =head3 units
237  
238 An array reference of units. All units of analysis must be of the same type,
239 but there are two possible types they all can have:
240  
241 =over
242  
243 =item 1.
244  
245 Each unit is a hash reference of the form
246  
247 { coder1 => 'value1', coder3 => 'value2', ... }
248  
249 =item 2.
250  
251 Each unit is an array reference of the form
252  
253 ['value1', undef, 'value2']
254  
255 where the coder is encoded by the position in the array, missing data are
256 indicated by an C<undef>.
257  
258  
259 =back
260  
261 In both the cases, there must be at least two values in each unit. If you want
262 to validate this precondition, call C<is_valid>.
263  
264 =head3 delta
265  
266 An optional argument defaulting to delta_nominal. You can specify any function
267 C<f($self, $v1, $v2)> that compares the two values C<$v1> and C<$v2> and
268 returns their distance (a number between 0 and 1). Several common methods are
269 predefined, you can use a code reference like C<&Statistics::Krippendorff::delta_nominal> or just a string C<nominal>:
270  
271 =head4 delta_nominal
272  
273 Used for nominal data, i.e. labels with no ordering.
274  
275 =head4 delta_ordinal
276  
277 Used for numeric values that are ordered, but can't be used in mathematical
278 operations, for example number of stars in a movie rating system (we don't say
279 that the distance from one star to two stars is the same as the distance from
280 three starts to four stars). See the implementation on why C<$self> is needed
281 as a parameter to delta.
282  
283 =head4 delta_interval
284  
285 Used for numeric values that can be used in mathematical operations.
286  
287 =head4 delta_ratio
288  
289 Used for non-negative numeric values (think degrees Kelvin).
290  
291 =head4 delta_jaccard
292  
293 This can be used when coders can specify more than one value. Join the values
294 with commas; Jaccard index then uses the formula C<intersection_size /
295 union_size>. If you sort the values before joining them, the expected
296 coincidence matrix is smaller and the algorithm runs faster, but the resulting
297 coefficient should be the same.
298  
299 =head4 delta_masi
300  
301 The weighted metric for measuring agreement on set-valued items introduced by
302 R. Passonneau (2006). Use comma separated values as above in C<delta_jaccard>.
303  
304 =head2 alpha
305  
306 my $alpha = $sk->alpha;
307  
308 Returns Krippendorff's alpha.
309  
310 =head2 delta
311  
312 $sk->delta(sub($self, $v1, $v2) {});
313 $sk->delta('jaccard');
314  
315 The difference function used to calculate the alpha. You can specify it in the
316 constructor (see above), but you can later change it so something else, too.
317  
318 =head2 is_valid
319  
320 print "OK" if $sk->is_valid;
321  
322 Check that each unit has at least two responses. If you use a hash
323 representation of a unit, the values must be always defined.
324  
325 =head2 frequency
326  
327 my $freq = $sk->frequency('val1');
328  
329 Returns the frequency of the given value.
330  
331 =head2 pairable_values
332  
333 Returns the total number of all pairable values (i.e. the sum of all
334 frequencies).
335  
336 =head2 vals
337  
338 Returns a sorted list of all the possible values.
339  
340 =head1 AUTHOR
341  
342 E. Choroba, C<< <choroba at cpan.org> >>
343  
344 =head1 BUGS
345  
346 Please report any bugs or feature requests to
347 L<https://github.com/choroba/statistics-krippendorff/issues>, via
348 e-mail to C<bug-statistics-krippendorff at rt.cpan.org>, or through
349 the web interface at
350 L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Statistics-Krippendorff>.
351 I will be notified, and then you'll automatically be notified of
352 progress on your bug as I make changes.
353  
354 =head1 SUPPORT
355  
356 You can find documentation for this module with the perldoc command.
357  
358 perldoc Statistics::Krippendorff
359  
360  
361 You can also look for information at:
362  
363 =over 4
364  
365 =item * GitHub (report bugs here)
366  
367 L<https://github.com/choroba/statistics-krippendorff>
368  
369 =item * Search CPAN
370  
371 L<https://metacpan.org/release/Statistics-Krippendorff>
372  
373 =item * RT: CPAN's request tracker (you can report bugs here, too)
374  
375 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Statistics-Krippendorff>
376  
377 =back
378  
379  
380 =head1 ACKNOWLEDGEMENTS
381  
382 Implementation inspired by
383 L<Wikipedia|https://en.wikipedia.org/wiki/Krippendorff%27s_alpha>,
384 additional tests taken from
385 L<https://www.infoamerica.org/documentos_pdf/kripen.pdf>.
386  
387 =head1 LICENSE AND COPYRIGHT
388  
389 This software is Copyright (c) 2025 by E. Choroba.
390  
391 This is free software, licensed under:
392  
393 The Artistic License 2.0 (GPL Compatible)
394  
395  
396 =cut
397  
398 5 __PACKAGE__
↑ Summary