diff src/samer/models/JointHistogramBase.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/samer/models/JointHistogramBase.java	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,72 @@
+/*
+ *	Copyright (c) 2001, Samer Abdallah, King's College London.
+ *	All rights reserved.
+ *
+ *	This software is provided AS iS and WITHOUT ANY WARRANTY;
+ *	without even the implied warranty of MERCHANTABILITY or
+ *	FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+package samer.models;
+
+import samer.core.*;
+import samer.core.types.*;
+import samer.core.util.*;
+import samer.maths.*;
+import samer.tools.*;
+import java.util.*;
+
+public class JointHistogramBase extends AnonymousTask {
+	Matrix			bins;
+	IMap			xmap,ymap;
+	double[][]		binArray;
+	double[]		lbwx, lbwy; // log of bin widths
+	int				count, M;
+	DoubleModel	x, y;
+	VDouble			L;
+
+	public JointHistogramBase()
+	{
+		M=Shell.getInt("bins",32);
+		setMaps(new LinearMap(-1,1,M),new LinearMap(-1,1,M));
+		bins = new Matrix("bins",M,M);
+		L=new VDouble("likelihood");
+		binArray=bins.getArray();
+		lbwx=new double[M];
+		lbwy=new double[M];
+	}
+
+	public void setInputs(DoubleModel _x, DoubleModel _y) { x=_x; y=_y; }
+	public void setMaps(IMap xm, IMap ym) {
+		xmap=xm; xmap.setIntRange(M);
+		ymap=ym; ymap.setIntRange(M);
+		// this assumes bins matrix is square
+		for (int i=0; i<binArray.length; i++) {
+			lbwx[i]=Math.log(xmap.inverseFromInt(i+1) - xmap.inverseFromInt(i));
+			lbwy[i]=Math.log(ymap.inverseFromInt(i+1) - ymap.inverseFromInt(i));
+		}
+	}
+	public void clear() { count=0; bins.zero(); bins.changed(); }
+	public Matrix getBinMatrix() { return bins; }
+	public VDouble getLikelihoodSignal() { return L; }
+
+	public void dispose() { bins.dispose(); L.dispose(); }
+	public void run() { L.set(data(x.get(),y.get())); }
+
+	public void normalise() {
+		int n=binArray.length, c=0;
+		for (int i=0; i<n; i++) c+=(int)Mathx.sum(binArray[i]);
+		count=c;
+		Shell.print("JointHistogram: count="+count);
+ 	}
+
+	public final double data(double x, double y)	{
+		int k=xmap.clipInt(x);
+		int j=ymap.clipInt(y);
+		double L=-Math.log((++binArray[j][k])/(double)(++count)) + lbwx[k] + lbwy[j];
+		bins.changed();
+		return L;
+	}
+}
+
+