1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4#########################
5
6use strict;
7use Test;
8BEGIN { plan tests => 24 };
9
10use Statistics::Contingency;
11
12ok(1);
13
14my $all_categories = [qw(sports politics finance world)];
15
16{
17  my $e = new Statistics::Contingency(categories => $all_categories);
18  ok $e;
19
20  $e->add_result(['sports','finance'], ['sports']);
21  ok $e->micro_recall, 1, "micro recall";
22  ok $e->micro_precision, 0.5, "micro precision";
23  ok $e->micro_F1, 2/3, "micro F1";
24}
25
26{
27  my $e = new Statistics::Contingency(categories => $all_categories);
28  $e->add_result(['sports','finance'], ['politics']);
29  ok $e->micro_recall, 0, "micro recall";
30  ok $e->micro_precision, 0, "micro precision";
31  ok $e->micro_F1, 0, "micro F1";
32}
33
34{
35  my $e = new Statistics::Contingency(categories => $all_categories);
36
37  $e->add_result(['sports','finance'], ['sports']);
38  ok $e->micro_recall, 1, "micro recall";
39  ok $e->macro_recall, 1, "macro recall";
40
41  $e->add_result(['sports','finance'], ['politics']);
42
43  ok $e->micro_recall, 0.5, "micro recall";
44  ok $e->micro_precision, 0.25, "micro precision";
45  ok $e->micro_F1, 1/3, "micro F1";
46
47  ok $e->macro_recall, 0.75, "macro recall";
48  ok $e->macro_precision, 0.375, "macro precision";
49  ok $e->macro_F1, 5/12, "macro F1";
50}
51
52{
53  my $e = new Statistics::Contingency(categories => $all_categories);
54  $e->add_result([], ['politics']);
55  ok $e->micro_recall, 0, "micro recall";
56  ok $e->micro_precision, 0, "micro precision";
57  ok $e->micro_F1, 0, "micro F1";
58}
59
60{
61  my $e = new Statistics::Contingency(categories => $all_categories);
62  $e->add_result([], []);
63  ok $e->micro_recall, 1, "micro recall";
64  ok $e->micro_precision, 1, "micro precision";
65  ok $e->micro_F1, 1, "micro F1";
66}
67
68{
69  my $e = new Statistics::Contingency(categories => $all_categories);
70  $e->add_result(['sports','finance'], ['sports']);
71  print $e->stats_table;
72
73  $e = new Statistics::Contingency(categories => $all_categories);
74  $e->add_result(['sports','finance'], ['politics']);
75  print $e->stats_table;
76}
77
78{
79  my $e = new Statistics::Contingency(categories => $all_categories);
80  $e->set_entries(2, 3, 5, 19);
81  ok $e->micro_precision, 2/(2+3), "micro precision\n";
82  ok $e->micro_recall,    2/(2+5), "micro recall\n";
83}
84