Kappa/Weighted.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 75.0% 3 / 4 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 92.3% 12 / 13 Podpod n/a totaltotal 96.5% 84 / 87 CC 10 SCAR 10.5
CC
10
Cov
97%
_build_expected
6.0
_build__weight
3.2
_build_kappa
2.0
CRAP 2.8
↑ Summary Kappa/Cohen.pm »
1 package Statistics::Kappa::Weighted;
2 2 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '0.03';
5  
6 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use experimental qw{ signatures };
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use List::Util qw{ sum };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 has data => (is => 'ro', required => 1);
13 2 has weight => (is => 'ro', required => 1);
14 2 has kappa => (is => 'lazy', init_arg => undef);
15 2 has categories => (is => 'lazy', init_arg => undef);
16 2 has coincidence => (is => 'lazy', init_arg => undef);
17 2 has expected => (is => 'lazy', init_arg => undef);
18 2 has _weight => (is => 'lazy', init_arg => undef);
19  
20  
21 5 ▶sub _build_kappa($self) {
Subroutine _build_kappa: called CC=2 SCAR=6.9
22 15 1 - sum(map { my $c1 = $_;
23 map +($self->coincidence->{$c1}{$_} // 0)
24 * $self->_weight->($c1, $_),
25 15 ▶ @{ $self->categories }
Condition100%2/2
$self->coincidence->{$c1}{$_} // 0
l!l
396
MC/DC100%1/1
$self->coincidence->{$c1}{$_} // 0
$self->coincidence->{$c1}{$_}
Truth table100%2/2
A
A= $self->coincidence->{$c1}{$_}
Aresult
11
00
26 5 } @{ $self->categories })
27 15 / sum(map { my $c1 = $_;
28 map $self->expected->{$c1}{$_} * $self->_weight->($c1, $_),
29 15 @{ $self->categories }
30 5 } @{ $self->categories })
31 }
32  
33  
34 5 ▶sub _build_categories($self) {
Subroutine _build_categories: called CC=1 SCAR=0.0
35 5 my %c;
36 5 @c{ map @$_, @{ $self->{data} } } = ();
37 5 return [sort keys %c]
38 }
39  
40  
41 5 ▶sub _build_coincidence($self) {
Subroutine _build_coincidence: called CC=2 SCAR=6.9
42 5 my %ci;
43 5 ++$ci{ $_->[0] }{ $_->[1] } for @{ $self->data };
44 5 return \%ci
45 }
46  
47  
48 5 ▶sub _build_expected($self) {
Subroutine _build_expected: called CC=6 SCAR=17.9
49 5 my $ci = $self->coincidence;
50 5 my @cat = @{ $self->categories };
51 5 my %r;
52 5 $r{$_} = sum(values %{ $ci->{$_} }) / @{ $self->data } for @cat;
53 5 my %c;
54 5 for my $cat (@cat) {
55 15 ▶ $c{$cat} = sum(map $_->{$cat} // 0, values %$ci) / @{ $self->data };
Condition100%2/2
$_->{$cat} // 0
l!l
396
MC/DC100%1/1
$_->{$cat} // 0
$_->{$cat}
Truth table100%2/2
A
A= $_->{$cat}
Aresult
11
00
56 }
57  
58 5 my %e;
59 5 for my $c1 (@cat) {
60 15 for my $c2 (@cat) {
61 45 $e{$c1}{$c2} = $r{$c1} * $c{$c2} * @{ $self->data };
62 }
63 }
64 5 return \%e
65 }
66  
67  
68 18 ▶{ my %WEIGHT = (linear => sub { abs($_[0] - $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 36 ▶ quadratic => sub { ($_[0] - $_[1]) ** 2 });
Subroutine __ANON__: called CC=1 SCAR=0.0
70  
71 5 ▶ sub _build__weight($self) {
Subroutine _build__weight: called CC=3 SCAR=11.7
72 5 ▶ return $self->weight if ref sub {} eq ref $self->weight;
Subroutine __ANON__: not called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ref sub { } eq ref $self->weight 2 3 5
73 3 ▶ return $WEIGHT{ $self->weight } if exists $WEIGHT{ $self->weight };
Branch: 1 branch
BranchTrueFalseTotal
if exists $WEIGHT{$self->weight} 3 0 3
74 0 die 'Unknown weight ' . $self->weight
75 }
76 }
77  
78 =encoding utf8
79  
80 =head1 NAME
81  
82 Statistics::Kappa::Weighted - Calculate inter-annotator agreement.
83  
84 =head1 VERSION
85  
86 Version 0.03
87  
88 =head1 SYNOPSIS
89  
90 use Statistics::Kappa::Weighted;
91  
92 sub weight($x, $y) { abs($x - $y) }
93  
94 my @data = ([3, 2], [2, 2], [1, 1], [2, 2], [3, 3]);
95 my $ck = 'Statistics::Kappa::Weighted'->new(data => \@data,
96 weight => \&weight);
97 my $kappa = $ck->kappa;
98  
99 =head1 METHODS
100  
101 =head2 'Statistics::Kappa::Weighted'->new(data => \@data, weight => \&weight)
102  
103 The constructor. It takes a named argument C<data> which should contain an
104 array reference. The elements of the array should be anonymous arrays of two
105 elements, representing answers by the two raters we are comparing. Another
106 argument is C<weight> which should be a code reference returning the weight of
107 disagreement (0 if the two values are the same). Two values of C<weight> are
108 predefined: strings C<'linear'> and C<'quadratic'>. You can use them if the
109 values are numbers and the weight is C<abs($x - $y)> or C<($x - $y) ** 2>,
110 respectively. Note that using C<sub { $_[0] != $_[1] }> turns the weighted
111 𝜅 into unweighted 𝜅.
112  
113 =head2 data
114  
115 =head2 weight
116  
117 =head1 AUTHOR
118  
119 E. Choroba <choroba@matfyz.cz>
120  
121 =head1 BUGS
122  
123 Please report any bugs or feature requests to the L<GitHub
124 repository|https://github.com/choroba/statistics-kappa>.
125  
126 =head1 SUPPORT
127  
128 You can find documentation for this module with the perldoc command.
129  
130 perldoc Statistics::Kappa::Weighted
131  
132 You can also look for information at:
133  
134 =over 4
135  
136 =item * GitHub issue tracker (report bugs here)
137  
138 L<https://github.com/choroba/statistics-kappa/issues>
139  
140 =item * Search CPAN
141  
142 L<https://metacpan.org/release/Statistics-Kappa>
143  
144 =back
145  
146 =head1 SEE ALSO
147  
148 Other modules from L<Statistics::Kappa>: L<Statistics::Kappa::Cohen> and
149 L<Statistics::Kappa::Fleiss>. Another inter-rater agreement statistics:
150 L<Statistics::Krippendorff>.
151  
152 =head1 LICENSE AND COPYRIGHT
153  
154 This software is Copyright (c) 2026 by E. Choroba <choroba@matfyz.cz>.
155  
156 This is free software, licensed under:
157  
158 The Artistic License 2.0 (GPL Compatible)
159  
160 =cut
161  
162 2 __PACKAGE__
↑ Summary Kappa/Cohen.pm »