annotate src/samer/models/JointHistogramBase.java @ 3:15b93db27c04

Get StreamSource to compile, update args for demo
author samer
date Fri, 05 Apr 2019 17:00:18 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * Copyright (c) 2001, Samer Abdallah, King's College London.
samer@0 3 * All rights reserved.
samer@0 4 *
samer@0 5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 6 * without even the implied warranty of MERCHANTABILITY or
samer@0 7 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 8 */
samer@0 9
samer@0 10 package samer.models;
samer@0 11
samer@0 12 import samer.core.*;
samer@0 13 import samer.core.types.*;
samer@0 14 import samer.core.util.*;
samer@0 15 import samer.maths.*;
samer@0 16 import samer.tools.*;
samer@0 17 import java.util.*;
samer@0 18
samer@0 19 public class JointHistogramBase extends AnonymousTask {
samer@0 20 Matrix bins;
samer@0 21 IMap xmap,ymap;
samer@0 22 double[][] binArray;
samer@0 23 double[] lbwx, lbwy; // log of bin widths
samer@0 24 int count, M;
samer@0 25 DoubleModel x, y;
samer@0 26 VDouble L;
samer@0 27
samer@0 28 public JointHistogramBase()
samer@0 29 {
samer@0 30 M=Shell.getInt("bins",32);
samer@0 31 setMaps(new LinearMap(-1,1,M),new LinearMap(-1,1,M));
samer@0 32 bins = new Matrix("bins",M,M);
samer@0 33 L=new VDouble("likelihood");
samer@0 34 binArray=bins.getArray();
samer@0 35 lbwx=new double[M];
samer@0 36 lbwy=new double[M];
samer@0 37 }
samer@0 38
samer@0 39 public void setInputs(DoubleModel _x, DoubleModel _y) { x=_x; y=_y; }
samer@0 40 public void setMaps(IMap xm, IMap ym) {
samer@0 41 xmap=xm; xmap.setIntRange(M);
samer@0 42 ymap=ym; ymap.setIntRange(M);
samer@0 43 // this assumes bins matrix is square
samer@0 44 for (int i=0; i<binArray.length; i++) {
samer@0 45 lbwx[i]=Math.log(xmap.inverseFromInt(i+1) - xmap.inverseFromInt(i));
samer@0 46 lbwy[i]=Math.log(ymap.inverseFromInt(i+1) - ymap.inverseFromInt(i));
samer@0 47 }
samer@0 48 }
samer@0 49 public void clear() { count=0; bins.zero(); bins.changed(); }
samer@0 50 public Matrix getBinMatrix() { return bins; }
samer@0 51 public VDouble getLikelihoodSignal() { return L; }
samer@0 52
samer@0 53 public void dispose() { bins.dispose(); L.dispose(); }
samer@0 54 public void run() { L.set(data(x.get(),y.get())); }
samer@0 55
samer@0 56 public void normalise() {
samer@0 57 int n=binArray.length, c=0;
samer@0 58 for (int i=0; i<n; i++) c+=(int)Mathx.sum(binArray[i]);
samer@0 59 count=c;
samer@0 60 Shell.print("JointHistogram: count="+count);
samer@0 61 }
samer@0 62
samer@0 63 public final double data(double x, double y) {
samer@0 64 int k=xmap.clipInt(x);
samer@0 65 int j=ymap.clipInt(y);
samer@0 66 double L=-Math.log((++binArray[j][k])/(double)(++count)) + lbwx[k] + lbwy[j];
samer@0 67 bins.changed();
samer@0 68 return L;
samer@0 69 }
samer@0 70 }
samer@0 71
samer@0 72