Compute the Chi-Square distance between samples. - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Compute the Chi-Square distance between samples.

Demo Code


//package com.java2s;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**/*from   w  ww .j  a  va 2s  .c o m*/
     * Compute the Chi-Square distance between samples.
     * @param i1 First sample.
     * @param i2 Second sample.
     * @return Return the Chi-Square sample between samples.
     */
    //chi-square distance between samples
    public static double chiSquareDistance(Collection<Double> i1,
            Collection<Double> i2) {

        double dist = 0.0;
        double diff;
        double v1, v2;
        double sum;
        assert (i1.size() == i2.size()) : "Dimensions must agree.";

        Iterator<Double> it1 = i1.iterator();
        Iterator<Double> it2 = i2.iterator();

        //Log.v("Info", String.format("Features Number - distance function: %d %d", i1.size(), i2.size()));

        int nFeatures = i1.size();
        for (int i = 0; i < nFeatures; i++) {
            v1 = it1.next();
            v2 = it2.next();

            diff = v1 - v2;
            sum = v1 + v2;

            //Log.v("Chi Square", String.format("%f, %f", v1, v2));

            if (sum > 0.0)
                dist += (diff * diff) / sum;
        }//while

        return 0.5 * dist;
    }
}

Related Tutorials