001/**
002 * 
003 */
004package org.dllearner.algorithms.isle.metrics;
005
006import org.dllearner.algorithms.isle.index.Index;
007import org.semanticweb.owlapi.model.OWLEntity;
008
009/**
010 * Log Likelihood Ratio
011 * @author Andre Melo
012 *
013 */
014public class LLRRelevanceMetric extends AbstractRelevanceMetric {
015
016        public LLRRelevanceMetric(Index index) {
017                super(index);
018        }
019        
020        private double llrIteration(double pXY, double pX, double pY) {
021                if (pXY==0 || pX==0 || pY==0)
022                        return 0;
023                return pXY * Math.log(pXY/(pX*pY));
024        }
025
026        @Override
027        public synchronized double getRelevance(OWLEntity entityA, OWLEntity entityB){
028                double fA = index.getNumberOfDocumentsFor(entityA);
029                double fB = index.getNumberOfDocumentsFor(entityB);
030                double N = index.getTotalNumberOfDocuments();
031                double fAB = index.getNumberOfDocumentsFor(entityA, entityB);
032                
033                if (N==0 || fA==0 || fB==0)
034                        return 0;
035                
036                
037                
038                double pA = fA/N;
039                double pB = fB/N;
040                double pAB = fAB/N;
041
042                double llr = 0;
043                
044                // X=A          and     Y=B
045                llr += llrIteration( pAB,               pA,     pB       );
046                // X=A          and     Y=not B
047                llr += llrIteration( pA-pAB,    pA,     1-pB );
048                // X=not A      and     Y=B
049                llr += llrIteration( pB-pAB,    1-pA,   pB       );
050                // X=not A      and     Y=not B
051                llr += llrIteration( 1-pA-pB+pAB,1-pA,  1-pB );
052                
053                return llr;
054        }
055        
056        @Override
057        public synchronized double getNormalizedRelevance(OWLEntity entityA, OWLEntity entityB){
058                return Double.NaN;
059        }
060
061}