// Path: ensemble-clustering/src/main/java/com/oculusinfo/ml/feature/bagofwords/distance/CosineDistance.java
// public class CosineDistance extends DistanceFunction<BagOfWordsFeature> {
// 	private static final long serialVersionUID = -635994591459075095L;
// 
// 	public CosineDistance() {
// 		this(1);
// 	}
// 	
// 	public CosineDistance(double weight) {
// 		super(weight);
// 	}
// 	
// 	@Override
// 	public double distance(BagOfWordsFeature x, BagOfWordsFeature y) {
// 		double dotprod = 0, xlength = 0, ylength = 0;
// 		
// 		for (FeatureFrequency xf : x.getValues()) {
// 			xlength += xf.frequency * xf.frequency;
// 			FeatureFrequency yf = y.getCount(xf.feature.getName());
// 			if (yf != null) dotprod += xf.frequency * yf.frequency;
// 		}
// 		for (FeatureFrequency yf : y.getValues()) {
// 			ylength += yf.frequency * yf.frequency;
// 		}
// 		// if both are empty then distance is max
// 		if (xlength == 0 || ylength == 0) return 1.0;
// 		
// 		return 1.0 - (dotprod / ( Math.sqrt(xlength) * Math.sqrt(ylength) ));
// 	}
// 
// }


import com.oculusinfo.ml.feature.bagofwords.BagOfWordsFeature;
import com.oculusinfo.ml.feature.bagofwords.distance.CosineDistance;
import java.util.Collections;
import junit.framework.Assert;
import org.junit.Test;
/**
 * Copyright (c) 2013 Oculus Info Inc.
 * http://www.oculusinfo.com/
 *
 * Released under the MIT License.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.oculusinfo.ml.distance;





public class TestCosineDistance {
double epsilon = 0.00001;
	
	private boolean isEqual(double d1, double d2) {
		return (Math.abs( d1 - d2 ) < epsilon );
	}
	
//	@Test
//	public void testTiming() {
//		NominalFeature t1 = new NominalFeature();
//		t1.incrementValue("dog");
//		t1.incrementValue("food");
//		t1.incrementValue("house");
//		t1.incrementValue("walk");
//		t1.incrementValue("yard");
//		
//		NominalFeature t2 = new NominalFeature();
//		t2.incrementValue("cat");
//		t2.incrementValue("food");
//		t2.incrementValue("house");
//		t2.incrementValue("sand");
//		t2.incrementValue("box");
//		
//		CosineDistance d = new CosineDistance();
////		EuclideanDistance d = new EuclideanDistance(1);
//		
//		long start = System.currentTimeMillis();
//		double distance = 0;
//
//		for (int i=0; i < 300000*50 ; i++) {
////			distance = d.distance(t1, t2);
//			distance = d.aveMinDistance(Collections.singletonList(t1), Collections.singletonList(t2));
////			Concept cx = taxonomy.findConcept(t1.getConcept());
////			Concept cy = taxonomy.findConcept(t2.getConcept());
////			Concept lca = cx.findCommonAncestor(cy);
//		}
//		double distanceTime = System.currentTimeMillis() - start;
//		System.out.println("Time: " + distanceTime/1000);
//	}
	
	@Test
	public void testIdentical() {
		BagOfWordsFeature t1 = new BagOfWordsFeature();
		t1.incrementValue("dog");
		t1.incrementValue("food");
		t1.incrementValue("house");
		t1.incrementValue("walk");
		t1.incrementValue("yard");
		
		BagOfWordsFeature t2 = new BagOfWordsFeature();
		t2.incrementValue("dog");
		t2.incrementValue("food");
		t2.incrementValue("house");
		t2.incrementValue("walk");
		t2.incrementValue("yard");
		
// Path: ensemble-clustering/src/main/java/com/oculusinfo/ml/feature/bagofwords/distance/CosineDistance.java
// public class CosineDistance extends DistanceFunction<BagOfWordsFeature> {
// 	private static final long serialVersionUID = -635994591459075095L;
// 
// 	public CosineDistance() {
// 		this(1);
// 	}
// 	
// 	public CosineDistance(double weight) {
// 		super(weight);
// 	}
// 	
// 	@Override
// 	public double distance(BagOfWordsFeature x, BagOfWordsFeature y) {
// 		double dotprod = 0, xlength = 0, ylength = 0;
// 		
// 		for (FeatureFrequency xf : x.getValues()) {
// 			xlength += xf.frequency * xf.frequency;
// 			FeatureFrequency yf = y.getCount(xf.feature.getName());
// 			if (yf != null) dotprod += xf.frequency * yf.frequency;
// 		}
// 		for (FeatureFrequency yf : y.getValues()) {
// 			ylength += yf.frequency * yf.frequency;
// 		}
// 		// if both are empty then distance is max
// 		if (xlength == 0 || ylength == 0) return 1.0;
// 		
// 		return 1.0 - (dotprod / ( Math.sqrt(xlength) * Math.sqrt(ylength) ));
// 	}
// 
// }


// Path: ensemble-clustering/src/test/java/com/oculusinfo/ml/distance/TestCosineDistance.java
import com.oculusinfo.ml.feature.bagofwords.BagOfWordsFeature;
import com.oculusinfo.ml.feature.bagofwords.distance.CosineDistance;
import java.util.Collections;
import junit.framework.Assert;
import org.junit.Test;

/**
 * Copyright (c) 2013 Oculus Info Inc.
 * http://www.oculusinfo.com/
 *
 * Released under the MIT License.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.oculusinfo.ml.distance;





public class TestCosineDistance {
double epsilon = 0.00001;
	
	private boolean isEqual(double d1, double d2) {
		return (Math.abs( d1 - d2 ) < epsilon );
	}
	
//	@Test
//	public void testTiming() {
//		NominalFeature t1 = new NominalFeature();
//		t1.incrementValue("dog");
//		t1.incrementValue("food");
//		t1.incrementValue("house");
//		t1.incrementValue("walk");
//		t1.incrementValue("yard");
//		
//		NominalFeature t2 = new NominalFeature();
//		t2.incrementValue("cat");
//		t2.incrementValue("food");
//		t2.incrementValue("house");
//		t2.incrementValue("sand");
//		t2.incrementValue("box");
//		
//		CosineDistance d = new CosineDistance();
////		EuclideanDistance d = new EuclideanDistance(1);
//		
//		long start = System.currentTimeMillis();
//		double distance = 0;
//
//		for (int i=0; i < 300000*50 ; i++) {
////			distance = d.distance(t1, t2);
//			distance = d.aveMinDistance(Collections.singletonList(t1), Collections.singletonList(t2));
////			Concept cx = taxonomy.findConcept(t1.getConcept());
////			Concept cy = taxonomy.findConcept(t2.getConcept());
////			Concept lca = cx.findCommonAncestor(cy);
//		}
//		double distanceTime = System.currentTimeMillis() - start;
//		System.out.println("Time: " + distanceTime/1000);
//	}
	
	@Test
	public void testIdentical() {
		BagOfWordsFeature t1 = new BagOfWordsFeature();
		t1.incrementValue("dog");
		t1.incrementValue("food");
		t1.incrementValue("house");
		t1.incrementValue("walk");
		t1.incrementValue("yard");
		
		BagOfWordsFeature t2 = new BagOfWordsFeature();
		t2.incrementValue("dog");
		t2.incrementValue("food");
		t2.incrementValue("house");
		t2.incrementValue("walk");
		t2.incrementValue("yard");
		
		CosineDistance d = new CosineDistance();