Statistics-Contingency-0.09/000755 000765 000765 00000000000 12154774572 015476 5ustar00kenken000000 000000 Statistics-Contingency-0.09/Build.PL000644 000765 000765 00000002165 12154774572 016776 0ustar00kenken000000 000000 use strict; use warnings; use Module::Build 0.3601; my %module_build_args = ( "build_requires" => { "Module::Build" => "0.3601" }, "configure_requires" => { "ExtUtils::MakeMaker" => "6.30", "Module::Build" => "0.3601" }, "dist_abstract" => "Calculate precision, recall, F1, accuracy, etc.", "dist_author" => [ "Ken Williams " ], "dist_name" => "Statistics-Contingency", "dist_version" => "0.09", "license" => "perl", "module_name" => "Statistics::Contingency", "recommends" => {}, "recursive_test_files" => 1, "requires" => { "Params::Validate" => 0, "strict" => 0 }, "script_files" => [], "test_requires" => { "Test" => 0 } ); unless ( eval { Module::Build->VERSION(0.4004) } ) { my $tr = delete $module_build_args{test_requires}; my $br = $module_build_args{build_requires}; for my $mod ( keys %$tr ) { if ( exists $br->{$mod} ) { $br->{$mod} = $tr->{$mod} if $tr->{$mod} > $br->{$mod}; } else { $br->{$mod} = $tr->{$mod}; } } } my $build = Module::Build->new(%module_build_args); $build->create_build_script; Statistics-Contingency-0.09/Changes000644 000765 000765 00000004072 12154774572 016774 0ustar00kenken000000 000000 Revision history for Perl extension Statistics::Contingency. 0.09 - Sat Jun 8 22:39:02 CDT 2013 - Converted to Git and Dist::Zilla. Moved from Google Code to GitHub. 0.08 - Sat Aug 23 22:20:30 2008 - Fix a "can't take log of 0" error that sometimes happens when printing the stats_table(). [Erik Aronesty & Jochen Leidner] 0.07 - Tue Jun 10 20:44:58 2008 - Added a set_entries() method to directly set the four entries of the contingency table. - Added a Build.PL as an alternative installer to the Makefile.PL. 0.06 Sat Feb 8 12:18:54 CST 2003 - The output of stats_table() will now auto-expand to show 3 significant digits in all data elements, or however many is specified as an argument to the method. - Added a note in the documentation pointing out the fact that micro_accuracy is always equal to macro_accuracy, and micro_error is always equal to macro_error. 0.05 Wed Oct 30 18:47:17 AEST 2002 - Since miE and maE are the same, don't print both in the stats_table() method. Just call it 'Err'. 0.04 Wed Sep 18 19:06:01 AEST 2002 - A precision edge case has been changed. Now, if no categories were assigned, and some categories should have been assigned, the precision is returned as 0. Previous versions returned 1. This edge case isn't discussed in the literature I've seen. Note that with this change, the macro-precision will typically be reported as a lower number than before (but the micro-precision usually won't change). Recall and F1 aren't affected by this change (they're both still 0), nor are error and accuracy. - Tests have been added to ensure the above changes stick. 0.03 Wed Aug 7 23:53 AEST 2002 - Corrected some documentation errors - Added Params::Validate to the dependency list in Makefile.PL 0.02 Fri Jul 19 13:53:00 2002 - Calling add_result() will now delete any cached calculations, so you can check the various metrics while you add results iteratively. 0.01 Mon Jun 24 13:34:40 2002 - original version; created by h2xs 1.21 with options -XA -n Statistics::Contingency Statistics-Contingency-0.09/dist.ini000644 000765 000765 00000000532 12154774572 017142 0ustar00kenken000000 000000 name = Statistics-Contingency version = 0.09 author = Ken Williams license = Perl_5 copyright_holder = Ken Williams [@Basic] [PkgVersion] [PodVersion] [PruneFiles] match = ~$ match = ^Statistics- [Signature] [Bugtracker] [Repository] [ModuleBuild] [Test::Perl::Critic] ;[PodCoverageTests] [AutoPrereqs] [Git::Tag] Statistics-Contingency-0.09/INSTALL000644 000765 000765 00000000455 12154774572 016533 0ustar00kenken000000 000000 Installation instructions for Statistics::Contingency To install this module, follow the standard steps for installing most Perl modules: perl Makefile.PL make make test make install Or you may use the CPAN.pm module, which will automatically execute these steps for you. -Ken Statistics-Contingency-0.09/lib/000755 000765 000765 00000000000 12154774572 016244 5ustar00kenken000000 000000 Statistics-Contingency-0.09/LICENSE000644 000765 000765 00000043700 12154774572 016507 0ustar00kenken000000 000000 This software is copyright (c) 2013 by Ken Williams. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2013 by Ken Williams. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2013 by Ken Williams. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Statistics-Contingency-0.09/Makefile.PL000644 000765 000765 00000002742 12154774572 017455 0ustar00kenken000000 000000 use strict; use warnings; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Calculate precision, recall, F1, accuracy, etc.", "AUTHOR" => "Ken Williams ", "BUILD_REQUIRES" => { "Module::Build" => "0.3601" }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30", "Module::Build" => "0.3601" }, "DISTNAME" => "Statistics-Contingency", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "Statistics::Contingency", "PREREQ_PM" => { "Params::Validate" => 0, "strict" => 0 }, "TEST_REQUIRES" => { "Test" => 0 }, "VERSION" => "0.09", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { my $tr = delete $WriteMakefileArgs{TEST_REQUIRES}; my $br = $WriteMakefileArgs{BUILD_REQUIRES}; for my $mod ( keys %$tr ) { if ( exists $br->{$mod} ) { $br->{$mod} = $tr->{$mod} if $tr->{$mod} > $br->{$mod}; } else { $br->{$mod} = $tr->{$mod}; } } } unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); Statistics-Contingency-0.09/MANIFEST000644 000765 000765 00000000226 12154774572 016627 0ustar00kenken000000 000000 Build.PL Changes INSTALL LICENSE MANIFEST META.yml Makefile.PL README SIGNATURE dist.ini lib/Statistics/Contingency.pm t/01-basic.t t/author-critic.t Statistics-Contingency-0.09/META.yml000644 000765 000765 00000001262 12154774572 016750 0ustar00kenken000000 000000 --- abstract: 'Calculate precision, recall, F1, accuracy, etc.' author: - 'Ken Williams ' build_requires: Module::Build: 0.3601 Test: 0 configure_requires: ExtUtils::MakeMaker: 6.30 Module::Build: 0.3601 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.300034, CPAN::Meta::Converter version 2.120921' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Statistics-Contingency requires: Params::Validate: 0 strict: 0 resources: bugtracker: http://rt.cpan.org/Public/Dist/Display.html?Name=Statistics-Contingency repository: git://github.com/kenahoo/Statistics-Contingency.git version: 0.09 Statistics-Contingency-0.09/README000644 000765 000765 00000000502 12154774572 016353 0ustar00kenken000000 000000 This archive contains the distribution Statistics-Contingency, version 0.09: Calculate precision, recall, F1, accuracy, etc. This software is copyright (c) 2013 by Ken Williams. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Statistics-Contingency-0.09/SIGNATURE000644 000765 000765 00000002675 12154774572 016774 0ustar00kenken000000 000000 This file contains message digests of all files listed in MANIFEST, signed via the Module::Signature module, version 0.68. To verify the content in this distribution, first make sure you have Module::Signature installed, then type: % cpansign -v It will check each file's integrity, as well as the signature's validity. If "==> Signature verified OK! <==" is not displayed, the distribution may already have been compromised, and you should not run its Makefile.PL or Build.PL. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 SHA1 71215d83269583adf9c752c6a4df8fff47790c4f Build.PL SHA1 b6e7a78f94abf00c7d84b35f39a5703268f1d301 Changes SHA1 950227ef6d7a1b3572bac3cd9b6f03c58a5f4588 INSTALL SHA1 8cb63de1ae4c54a120beb97cacc1fe2efe4d266b LICENSE SHA1 9b2032d5d6308be9fa789514a3dde854e907960f MANIFEST SHA1 cf1e3b089be010832e8a7e3aecfd5cc503ce78ae META.yml SHA1 8acf957face3f15a7b9ad80d622f6f0529da8573 Makefile.PL SHA1 8fb5daabe5614da8963b90b841ed70e2d72489ea README SHA1 a637881bb1fe775fb67e536246488bafc0859ec3 dist.ini SHA1 0584901c33a80373b474989b730d03b0003860fa lib/Statistics/Contingency.pm SHA1 7003b69adf3459b676b70cb781d47bb9f3169aa0 t/01-basic.t SHA1 fa45d6e6ab1cd421349dea4ef527bfd5cdc8a09e t/author-critic.t -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org iEYEARECAAYFAlGz+XkACgkQgrvMBLfvlHaeaQCfeOl8pRiOOORYrI3wLCP+ln3g ZfIAnjLU3eEahUwFQCCTxLSsPc/CNTos =j+74 -----END PGP SIGNATURE----- Statistics-Contingency-0.09/t/000755 000765 000765 00000000000 12154774572 015741 5ustar00kenken000000 000000 Statistics-Contingency-0.09/t/01-basic.t000644 000765 000765 00000004454 12154774572 017434 0ustar00kenken000000 000000 # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl test.pl' ######################### use strict; use Test; BEGIN { plan tests => 24 }; use Statistics::Contingency; ok(1); my $all_categories = [qw(sports politics finance world)]; { my $e = new Statistics::Contingency(categories => $all_categories); ok $e; $e->add_result(['sports','finance'], ['sports']); ok $e->micro_recall, 1, "micro recall"; ok $e->micro_precision, 0.5, "micro precision"; ok $e->micro_F1, 2/3, "micro F1"; } { my $e = new Statistics::Contingency(categories => $all_categories); $e->add_result(['sports','finance'], ['politics']); ok $e->micro_recall, 0, "micro recall"; ok $e->micro_precision, 0, "micro precision"; ok $e->micro_F1, 0, "micro F1"; } { my $e = new Statistics::Contingency(categories => $all_categories); $e->add_result(['sports','finance'], ['sports']); ok $e->micro_recall, 1, "micro recall"; ok $e->macro_recall, 1, "macro recall"; $e->add_result(['sports','finance'], ['politics']); ok $e->micro_recall, 0.5, "micro recall"; ok $e->micro_precision, 0.25, "micro precision"; ok $e->micro_F1, 1/3, "micro F1"; ok $e->macro_recall, 0.75, "macro recall"; ok $e->macro_precision, 0.375, "macro precision"; ok $e->macro_F1, 5/12, "macro F1"; } { my $e = new Statistics::Contingency(categories => $all_categories); $e->add_result([], ['politics']); ok $e->micro_recall, 0, "micro recall"; ok $e->micro_precision, 0, "micro precision"; ok $e->micro_F1, 0, "micro F1"; } { my $e = new Statistics::Contingency(categories => $all_categories); $e->add_result([], []); ok $e->micro_recall, 1, "micro recall"; ok $e->micro_precision, 1, "micro precision"; ok $e->micro_F1, 1, "micro F1"; } { my $e = new Statistics::Contingency(categories => $all_categories); $e->add_result(['sports','finance'], ['sports']); print $e->stats_table; $e = new Statistics::Contingency(categories => $all_categories); $e->add_result(['sports','finance'], ['politics']); print $e->stats_table; } { my $e = new Statistics::Contingency(categories => $all_categories); $e->set_entries(2, 3, 5, 19); ok $e->micro_precision, 2/(2+3), "micro precision\n"; ok $e->micro_recall, 2/(2+5), "micro recall\n"; } Statistics-Contingency-0.09/t/author-critic.t000644 000765 000765 00000000666 12154774572 020713 0ustar00kenken000000 000000 #!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for testing by the author'); } } use strict; use warnings; use Test::More; use English qw(-no_match_vars); eval "use Test::Perl::Critic"; plan skip_all => 'Test::Perl::Critic required to criticise code' if $@; Test::Perl::Critic->import( -profile => "perlcritic.rc" ) if -e "perlcritic.rc"; all_critic_ok(); Statistics-Contingency-0.09/lib/Statistics/000755 000765 000765 00000000000 12154774572 020376 5ustar00kenken000000 000000 Statistics-Contingency-0.09/lib/Statistics/Contingency.pm000644 000765 000765 00000031377 12154774572 023227 0ustar00kenken000000 000000 use strict; package Statistics::Contingency; { $Statistics::Contingency::VERSION = '0.09'; } # Correct=Y Correct=N # +-----------+-----------+ # Assigned=Y | a | b | # +-----------+-----------+ # Assigned=N | c | d | # +-----------+-----------+ # accuracy = (a+d)/(a+b+c+d) # precision = a/(a+b) # recall = a/(a+c) # F1 = 2a/(2a + b + c) # Edge cases: # precision(0,0,+,d) = 0 # precision(a,0,c,d) = 1 # precision(0,+,c,d) = 0 # recall(a,b,0,d) = 1 # recall(0,b,+,d) = 0 # F1(a,0,0,d) = 1 # F1(0,+++,d) = 0 use Params::Validate qw(:all); sub new { my $package = shift; my $self = bless { validate @_, { verbose => { type => SCALAR, default => 0 }, categories => { type => ARRAYREF|HASHREF }, } }, $package; $self->{$_} = 0 foreach qw(a b c d); my $c = delete $self->{categories}; $self->{categories} = { map {($_ => {a=>0, b=>0, c=>0, d=>0})} UNIVERSAL::isa($c, 'HASH') ? keys(%$c) : @$c }; return $self; } sub set_entries { my $self = shift; @{ $self }{'a', 'b', 'c', 'd'} = @_; } sub add_result { my ($self, $assigned, $correct, $name) = @_; my $cats_table = $self->{categories}; # Hashify foreach ($assigned, $correct) { $_ = {$_ => 1}, next unless ref $_; next if UNIVERSAL::isa($_, 'HASH'); # Leave alone $_ = { map {($_ => 1)} @$_ }, next if UNIVERSAL::isa($_, 'ARRAY'); die "Unknown type '$_' for category list"; } # Add to the micro/macro tables foreach my $cat (keys %$cats_table) { $cats_table->{$cat}{a}++, $self->{a}++ if $assigned->{$cat} and $correct->{$cat}; $cats_table->{$cat}{b}++, $self->{b}++ if $assigned->{$cat} and !$correct->{$cat}; $cats_table->{$cat}{c}++, $self->{c}++ if !$assigned->{$cat} and $correct->{$cat}; $cats_table->{$cat}{d}++, $self->{d}++ if !$assigned->{$cat} and !$correct->{$cat}; } if ($self->{verbose}) { print "$name: assigned=(@{[ keys %$assigned ]}) correct=(@{[ keys %$correct ]})\n"; } # Clear any cached results delete $self->{macro}; $self->{hypotheses}++; } sub _invert { my ($self, $x, $y) = @_; return 1 unless $y; return 0 unless $x; return 1 / (1 + $y/$x); } sub _accuracy { my $h = $_[1]; return 1 unless grep $h->{$_}, qw(a b c d); return +($h->{a} + $h->{d}) / ($h->{a} + $h->{b} + $h->{c} + $h->{d}); } sub _error { my $h = $_[1]; return 0 unless grep $h->{$_}, qw(a b c d); return +($h->{b} + $h->{c}) / ($h->{a} + $h->{b} + $h->{c} + $h->{d}); } sub _precision { my ($self, $h) = @_; return 0 if $h->{c} and !$h->{a} and !$h->{b}; return $self->_invert($h->{a}, $h->{b}); } sub _recall { my ($self, $h) = @_; return $self->_invert($h->{a}, $h->{c}); } sub _F1 { my ($self, $h) = @_; return $self->_invert(2 * $h->{a}, $h->{b} + $h->{c}); } # Fills in precision, recall, etc. for each category, and computes their averages sub _macro_stats { my $self = shift; return $self->{macro} if $self->{macro}; my @metrics = qw(precision recall F1 accuracy error); my $cats = $self->{categories}; die "No category information has been recorded" unless keys %$cats; my %results; while (my ($cat, $scores) = each %$cats) { foreach my $metric (@metrics) { my $method = "_$metric"; $results{$metric} += ($scores->{$metric} = $self->$method($scores)); } } foreach (@metrics) { $results{$_} /= keys %$cats; } $self->{macro} = \%results; } sub micro_accuracy { $_[0]->_accuracy( $_[0]) } sub micro_error { $_[0]->_error( $_[0]) } sub micro_precision { $_[0]->_precision($_[0]) } sub micro_recall { $_[0]->_recall( $_[0]) } sub micro_F1 { $_[0]->_F1( $_[0]) } sub macro_accuracy { shift()->_macro_stats->{accuracy} } sub macro_error { shift()->_macro_stats->{error} } sub macro_precision { shift()->_macro_stats->{precision} } sub macro_recall { shift()->_macro_stats->{recall} } sub macro_F1 { shift()->_macro_stats->{F1} } sub category_stats { my $self = shift; $self->_macro_stats; return $self->{categories}; } sub stats_table { my $self = shift; my $figs = shift || 3; my @data = map $self->_sig_figs($_, $figs), ( $self->macro_recall, $self->macro_precision, $self->macro_F1, $self->micro_recall, $self->micro_precision, $self->micro_F1, $self->micro_error, ); my $m = 0; # Max length of @data items for (@data) { $m = length() if length() > $m; } my $s = ' ' x ($m - 4); my $out = "+" . ("-" x (10 + 7*$m)) . "+\n"; $out .= "| $s maR $s maP$s maF1 $s miR $s miP$s miF1 $s Err |\n"; $out .= "| %${m}s %${m}s %${m}s %${m}s %${m}s %${m}s %${m}s |\n"; $out .= "+" . ("-" x (10 + 7*$m)) . "+\n"; return sprintf($out, @data); } sub _sig_figs { my ($self, $number, $figs) = @_; my $after_point = $figs - int ($number != 0 ? log($number)/log(10) : 0); return sprintf "%.${after_point}f", $number; } 1; __END__ =head1 NAME Statistics::Contingency - Calculate precision, recall, F1, accuracy, etc. =head1 VERSION version 0.09 =head1 SYNOPSIS use Statistics::Contingency; my $s = new Statistics::Contingency(categories => \@all_categories); while (...something...) { ... $s->add_result($assigned_categories, $correct_categories); } print "Micro F1: ", $s->micro_F1, "\n"; # Access a single statistic print $s->stats_table; # Show several stats in table form =head1 DESCRIPTION The C class helps you calculate several useful statistical measures based on 2x2 "contingency tables". I use these measures to help judge the results of automatic text categorization experiments, but they are useful in other situations as well. The general usage flow is to tally a whole bunch of results in the C object, then query that object to obtain the measures you are interested in. When all results have been collected, you can get a report on accuracy, precision, recall, F1, and so on, with both macro-averaging and micro-averaging over categories. =head2 Macro vs. Micro Statistics All of the statistics offered by this module can be calculated for each category and then averaged, or can be calculated over all decisions and then averaged. The former is called macro-averaging (specifically, macro-averaging with respect to category), and the latter is called micro-averaging. The two procedures bias the results differently - micro-averaging tends to over-emphasize the performance on the largest categories, while macro-averaging over-emphasizes the performance on the smallest. It's often best to look at both of them to get a good idea of how your data distributes across categories. =head2 Statistics available All of the statistics are calculated based on a so-called "contingency table", which looks like this: Correct=Y Correct=N +-----------+-----------+ Assigned=Y | a | b | +-----------+-----------+ Assigned=N | c | d | +-----------+-----------+ a, b, c, and d are counts that reflect how the assigned categories matched the correct categories. Depending on whether a macro-statistic or a micro-statistic is being calculated, these numbers will be tallied per-category or for the entire result set. The following statistics are available: =over 4 =item * accuracy This measures the portion of all decisions that were correct decisions. It is defined as C<(a+d)/(a+b+c+d)>. It falls in the range from 0 to 1, with 1 being the best score. Note that macro-accuracy and micro-accuracy will always give the same number. =item * error This measures the portion of all decisions that were incorrect decisions. It is defined as C<(b+c)/(a+b+c+d)>. It falls in the range from 0 to 1, with 0 being the best score. Note that macro-error and micro-error will always give the same number. =item * precision This measures the portion of the assigned categories that were correct. It is defined as C. It falls in the range from 0 to 1, with 1 being the best score. =item * recall This measures the portion of the correct categories that were assigned. It is defined as C. It falls in the range from 0 to 1, with 1 being the best score. =item * F1 This measures an even combination of precision and recall. It is defined as C<2*p*r/(p+r)>. In terms of a, b, and c, it may be expressed as C<2a/(2a+b+c)>. It falls in the range from 0 to 1, with 1 being the best score. =back The F1 measure is often the only simple measure that is worth trying to maximize on its own - consider the fact that you can get a perfect precision score by always assigning zero categories, or a perfect recall score by always assigning every category. A truly smart system will assign the correct categories and only the correct categories, maximizing precision and recall at the same time, and therefore maximizing the F1 score. Sometimes it's worth trying to maximize the accuracy score, but accuracy (and its counterpart error) are considered fairly crude scores that don't give much information about the performance of a categorizer. =head1 METHODS The general execution flow when using this class is to create a C object, add a bunch of results to it, and then report on the results. =over 4 =item * $e = Statistics::Contingency->new() Returns a new C object. Expects a C parameter specifying the entire set of categories that may be assigned during this experiment. Also accepts a C parameter - if true, some diagnostic status information will be displayed when certain actions are performed. =item * $e->add_result($assigned_categories, $correct_categories, $name) Adds a new result to the experiment. The lists of assigned and correct categories can be given as an array of category names (strings), as a hash whose keys are the category names and whose values are anything logically true, or as a single string if there is only one category. If you've already got the lists in hash form, this will be the fastest way to pass them. Otherwise, the current implementation will convert them to hash form internally in order to make its calculations efficient. The C<$name> parameter is an optional name for this result. It will only be used in error messages or debugging/progress output. In the current implementation, we only store the contingency tables per category, as well as a table for the entire result set. This means that you can't recover information about any particular single result from the C object. =item * $e->set_entries($a, $b, $c, $d) If you don't wish to use the c interface, but still take advantage of the calculation methods and the various edge cases they handle, you can directly set the four elements of the contingency table with this method. =item * $e->micro_accuracy Returns the micro-averaged accuracy for the data set. =item * $e->micro_error Returns the micro-averaged error for the data set. =item * $e->micro_precision Returns the micro-averaged precision for the data set. =item * $e->micro_recall Returns the micro-averaged recall for the data set. =item * $e->micro_F1 Returns the micro-averaged F1 for the data set. =item * $e->macro_accuracy Returns the macro-averaged accuracy for the data set. =item * $e->macro_error Returns the macro-averaged error for the data set. =item * $e->macro_precision Returns the macro-averaged precision for the data set. =item * $e->macro_recall Returns the macro-averaged recall for the data set. =item * $e->macro_F1 Returns the macro-averaged F1 for the data set. =item * $e->stats_table Returns a string combining several statistics in one graphic table. Since accuracy is 1 minus error, we only report error since it takes less space to print. An optional argument specifies the number of significant digits to show in the data - the default is 3 significant digits. =item * $e->category_stats Returns a hash reference whose keys are the names of each category, and whose values contain the various statistical measures (accuracy, error, precision, recall, or F1) about each category as a hash reference. For example, to print a single statistic: print $e->category_stats->{sports}{recall}, "\n"; Or to print certain statistics for all categtories: my $stats = $e->category_stats; while (my ($cat, $value) = each %$stats) { print "Category '$cat': \n"; print " Accuracy: $value->{accuracy}\n"; print " Precision: $value->{precision}\n"; print " F1: $value->{F1}\n"; } =back =head1 AUTHOR Ken Williams =head1 COPYRIGHT Copyright 2002-2008 Ken Williams. All rights reserved. This distribution is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut