view src/samer/models/JointHistogramBase.java @ 5:b67a33c44de7

Remove some crap, etc
author samer
date Fri, 05 Apr 2019 21:34:25 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	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;
	}
}