Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

Likelihood.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft.featextractors;
00024 
00025 import java.util.Arrays;
00026 
00027 import com.meapsoft.FeatChunk;
00028 import com.meapsoft.FeatFile;
00029 
00037 public class Likelihood extends MetaFeatureExtractor 
00038 {       
00039         public void features(FeatFile featFile, boolean clearOriginalFeatures)
00040         {               
00041                 int totalChunks = featFile.chunks.size();
00042                                 
00043                 FeatChunk testChunk = (FeatChunk)featFile.chunks.elementAt(0);
00044                 double[] testChunkFeatures = testChunk.getFeatures();
00045                 int numFeatures = testChunkFeatures.length;
00046                 
00047                 // Pass 1 - gather feature statistics
00048                 double sumx[] = new double[numFeatures];
00049                 double sumx2[] = new double[numFeatures];
00050                 double meanx[] = new double[numFeatures];
00051                 double stdx[] = new double[numFeatures];
00052                 double mahaldist[] = new double[totalChunks];
00053                 
00054                 Arrays.fill(sumx, 0);
00055                 Arrays.fill(sumx2, 0);
00056                 Arrays.fill(meanx, 0);
00057                 Arrays.fill(stdx, 0);
00058                 Arrays.fill(mahaldist, 0);
00059                 
00060                 double N = 0.0;
00061                 
00062                 for(int chunk=0; chunk<totalChunks; chunk++) 
00063                 {
00064                         FeatChunk curChunk = (FeatChunk)featFile.chunks.elementAt(chunk);
00065                         double[] features = curChunk.getFeatures();
00066                         
00067                         for(int feature = 0; feature < numFeatures; feature++)
00068                         {
00069                                 sumx[feature] += features[feature];
00070                                 sumx2[feature] += features[feature] * features[feature];
00071                         }
00072                         N = N + 1.0;
00073                 }
00074                 
00075                 // figure mean and variance
00076                 for(int feature = 0; feature < numFeatures; feature++)
00077                 {
00078                         meanx[feature] = sumx[feature] / N;
00079                         stdx[feature]  = Math.sqrt(sumx2[feature]/N - meanx[feature]*meanx[feature]);
00080                 }
00081                 
00082                 // Pass 2 - figure Mahalanobis distance (= unlikeliness score)
00083                 // under diagonal covariance Gaussian for each frame
00084                 // (i.e. Euclidean distance to mean in a variance-normalized space)
00085                 
00086                 for(int chunk=0; chunk<totalChunks; chunk++) 
00087                 {
00088                         FeatChunk curChunk = (FeatChunk)featFile.chunks.elementAt(chunk);
00089                         double[] features = curChunk.getFeatures();
00090                 
00091                         mahaldist[chunk] = 0;
00092                         
00093                         for(int feature = 0; feature < numFeatures; feature++)
00094                         {
00095                                 double dist = (features[feature] - meanx[feature])/stdx[feature];
00096                                 mahaldist[chunk] += dist*dist;
00097                         }
00098                         mahaldist[chunk] = Math.sqrt(mahaldist[chunk]/numFeatures);
00099                         
00100                         double value = mahaldist[chunk];
00101                         double[] feats = new double[1];
00102                         feats[0] = value;
00103                         
00104             if(clearOriginalFeatures)
00105                 curChunk.clearFeatures();
00106 
00107                         curChunk.addFeature(feats);
00108                 }
00109         } 
00110         
00111         public String description()
00112         {
00113                 return "Returns the likelihood of each chunk. Lower numbers mean a segment is more common, " 
00114             + "higher numbers mean it is more distinct.";
00115         }
00116         
00117 }

Generated on Tue Feb 6 19:02:26 2007 for MEAPsoft by doxygen1.2.18