    public double distance(EventSet es1, EventSet es2) {
		EventHistogram h1 = new EventHistogram();
		EventHistogram h2 = new EventHistogram();
		Set<Event> s = new HashSet<Event>();
		List<Pair<Event,Double>> l1 = new ArrayList<Pair<Event,Double>>();
		List<Pair<Event,Double>> l2 = new ArrayList<Pair<Event,Double>>();
		HashMap<Event,Integer> hm1 = new HashMap<Event,Integer>();
		HashMap<Event,Integer> hm2 = new HashMap<Event,Integer>();
		double oldfreq = Double.POSITIVE_INFINITY;
		double correlation = 0.0;
		for (int i = 0; i < es1.size(); i++) {
			h1.add(es1.eventAt(i));
			s.add(es1.eventAt(i));
		}
		for (int i = 0; i < es2.size(); i++) {
			h2.add(es2.eventAt(i));
			s.add(es2.eventAt(i));
		}
		for (Event e: h1) {
			l1.add(new Pair<Event, Double>(e,h1.getRelativeFrequency(e),2) );
		}
		for (Event e: h2) {
			l2.add(new Pair<Event, Double>(e,h2.getRelativeFrequency(e),2) );
		}
		Collections.sort(l1);
		Collections.reverse(l1);
		Collections.sort(l2);
		Collections.reverse(l2);
		int rank = 0;
		int count = 0;
		for (Pair<Event,Double> p : l1) {
			Event e = (Event) (p.getFirst());
			double f = (Double) (p.getSecond());
			count++;
			if (f != oldfreq) {
				rank = count;
				oldfreq = f;
			}
			hm1.put(e,rank);
		}
		rank = 0;
		count = 0;
		for (Pair<Event,Double> p : l2) {
			Event e = (Event) (p.getFirst());
			double f = (Double) (p.getSecond());
			count++;
			if (f != oldfreq) {
				rank = count;
				oldfreq = f;
			}
			hm2.put(e,rank);
		}
		Integer x1, x2, y1, y2;
		for (Event e1 : s) {
			for (Event e2: s) {
				if (e1.equals(e2)) continue;
				x1 = hm1.get(e1);
				if (x1 == null) x1 = hm1.size()+1;
				x2 = hm2.get(e1);
				if (x2 == null) x2 = hm2.size()+1;
				y1 = hm1.get(e2);
				if (y1 == null) y1 = hm1.size()+1;
				y2 = hm2.get(e2);
				if (y2 == null) y2 = hm2.size()+1;
				correlation += (sgn(x1.compareTo(y1)) * sgn(x2.compareTo(y2)));
				System.out.println(correlation);
			}
		}
		correlation /= (hm1.size() * (hm2.size()-1));
		return 1.0 - correlation;
    }

    public double distance(EventSet es1, EventSet es2) {
		EventHistogram h1 = new EventHistogram();
		EventHistogram h2 = new EventHistogram();
		Set<Event> s = new HashSet<Event>();
		List<Pair<Event,Double>> l1 = new ArrayList<Pair<Event,Double>>();
		List<Pair<Event,Double>> l2 = new ArrayList<Pair<Event,Double>>();
		HashMap<Event,Integer> hm1 = new HashMap<Event,Integer>();
		HashMap<Event,Integer> hm2 = new HashMap<Event,Integer>();
		double oldfreq = Double.POSITIVE_INFINITY;
		double correlation = 0.0;
		for (int i = 0; i < es1.size(); i++) {
			h1.add(es1.eventAt(i));
			s.add(es1.eventAt(i));
		}
		for (int i = 0; i < es2.size(); i++) {
			h2.add(es2.eventAt(i));
			s.add(es2.eventAt(i));
		}
		for (Event e: h1) {
			l1.add(new Pair<Event, Double>(e,h1.getRelativeFrequency(e),2) );
		}
		for (Event e: h2) {
			l2.add(new Pair<Event, Double>(e,h2.getRelativeFrequency(e),2) );
		}
		Collections.sort(l1);
		Collections.reverse(l1);
		Collections.sort(l2);
		Collections.reverse(l2);
		int rank = 0;
		int count = 0;
		for (Pair<Event,Double> p : l1) {
			Event e = (Event) (p.getFirst());
			double f = (Double) (p.getSecond());
			count++;
			if (f != oldfreq) {
				rank = count;
				oldfreq = f;
			}
			hm1.put(e,rank);
		}
		rank = 0;
		count = 0;
		for (Pair<Event,Double> p : l2) {
			Event e = (Event) (p.getFirst());
			double f = (Double) (p.getSecond());
			count++;
			if (f != oldfreq) {
				rank = count;
				oldfreq = f;
			}
			hm2.put(e,rank);
		}
		Integer x1, x2, y1, y2;
		Set<Event> s2 = new HashSet<Event>(s);
		for (Event e1 : s) {
			s2.remove(e1);
			for (Event e2: s2) {
				if (e1.equals(e2)) continue;
				x1 = hm1.get(e1);
				if (x1 == null) x1 = hm1.size()+1;
				x2 = hm2.get(e1);
				if (x2 == null) x2 = hm2.size()+1;
				y1 = hm1.get(e2);
				if (y1 == null) y1 = hm1.size()+1;
				y2 = hm2.get(e2);
				if (y2 == null) y2 = hm2.size()+1;
				correlation += (sgn(x1.compareTo(y1)) * sgn(x2.compareTo(y2)));
			}
		}
		correlation /= (hm1.size() * (hm2.size()-1));
		return 1.0 - correlation;
    }
