#!/usr/bin/perl

use Getopt::Long;
use vars qw($opt_lambda $opt_fn $opt_fp $opt_spam $opt_ham);
GetOptions("lambda=f", "fn=f", "fp=f", "spam=f", "ham=f");

# lambda value for TCR equation, indicating the "cost" of recovering
# from an FP.  The values are: 1 = tagged only, 9 = mailed back to
# sender asking for token (TMDA style), 999 = deleted outright.
# We (SpamAssassin) use a default of 5, representing "moved to
# infrequently-read folder".

my $lambda = 5; if ($opt_lambda) { $lambda = $opt_lambda; }

if (!$opt_spam && !$opt_ham) {
  die "usage: fn-fp-to-tcr [-lambda l] -fn n -fp n -spam n -ham n\n\n";
}

# convert to the TCR metrics used in the published lit
my $nspamspam = $opt_spam;
my $nspamlegit = $opt_fn;
my $nlegitspam = $opt_fp;
my $nlegitlegit = $opt_ham;
my $nlegit = $opt_ham + $opt_fp;
my $nspam = $opt_spam + $opt_fn;

my $werr = ($lambda * $nlegitspam + $nspamlegit)
                / ($lambda * $nlegit + $nspam);

my $werr_base = $nspam
                / ($lambda * $nlegit + $nspam);

$werr ||= 0.000001;     # avoid / by 0
my $tcr = $werr_base / $werr;

my $sr = ($nspamspam / $nspam) * 100.0;
my $sp = ($nspamspam / ($nspamspam + $nlegitspam)) * 100.0;

printf "TCR: %3.6f (lambda=%d)\nSpamRecall: %3.6f%%\nSpamPrecision: %3.6f%%\n",
                $tcr, $lambda, $sr, $sp;

