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

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