# phi.pm Version 0.1

# Statistical library package to calculate the phi coefficient. This package
# should be used with statistic.pl and rank.pl

# The phi coefficient is a measure of the degree of association between
# two binary variables. This measure is similar to the correlation
# coefficient in its interpretation.  Two binary variables are considered 
# positively associated if most of the data falls along the diagonal  
# cells (i.e., n11 and n12 are larger than n12 and n21). In  contrast,  
# two binary variables are considered negatively associated if most of  
# the data falls off the diagonal.

# Copyright (C) 2002,
# Nitin Varma, University of Minnesota, Duluth
# varm0003@d.umn.edu
# Ted Pedersen, University of Minnesota, Duluth
# tpederse@d.umn.edu
#
# 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 2
# of the License, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


package phi;
require Exporter;
@ISA = qw ( Exporter );
@EXPORT = qw ( getStatisticName calculateStatistic );

sub getStatisticName
{
    return "Phi";
}

sub calculateStatistic
{
    ($freq, $freq1, $freq2, $totalBigrams) = @_;
    $a = $freq;
    $b = $freq1 - $freq;
    $c = $freq2 - $freq;
    $d = $totalBigrams - $a - $b -$c;
    if($a == 0)
    {
	$a =1;
    }
    
    if($b == 0)
    {
	$b =1;
    }
    
    if($c == 0)
    {
	$c =1;
    }
    if($d == 0)
    {
	$d =1;
    }


   $numerator = ($a * $d) - ($b * $c);
    $denominator = ($a + $b) * ($a + $c) * ($b + $d) * ($c + $d);
    $numerator *= $numerator; 
    return ($numerator / $denominator);
}

1;




