samer@0: /*
samer@0: * Histogram.java
samer@0: *
samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0: * All rights reserved.
samer@0: *
samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0: * without even the implied warranty of MERCHANTABILITY or
samer@0: * FITNESS FOR A PARTICULAR PURPOSE.
samer@0: */
samer@0:
samer@0: package samer.units;
samer@0:
samer@0: import samer.core.*;
samer@0: import samer.core.types.*;
samer@0: import samer.core.util.*;
samer@0: import samer.core.Agent.*;
samer@0: import samer.tools.*;
samer@0: import samer.maths.*;
samer@0: import java.util.*;
samer@0:
samer@0: /**
samer@0: This actually builds several histograms in parallel: one
samer@0: for each element of a Vec or double array. A IMap provides
samer@0: the mapping from element values to histogram bins.
samer@0: The result is a Matrix, each row of which
samer@0: is the histogram for the corresponding element of the
samer@0: input vector.
samer@0: */
samer@0:
samer@0: public class Histogram extends Viewable implements Task, Agent
samer@0: {
samer@0: private Vec input;
samer@0: private Matrix bins;
samer@0: private VMap map;
samer@0: private double[][] binArray;
samer@0: private double[] lbw; // log of bin widths
samer@0: private VVector L;
samer@0: private VDouble sumL;
samer@0: private int N, count;
samer@0: private double[] x;
samer@0:
samer@0: public Histogram( Vec input, int binCount)
samer@0: {
samer@0: super("histogram");
samer@0:
samer@0: Shell.push(getNode());
samer@0:
samer@0: N = input.size();
samer@0: Shell.setAutoRegister(false);
samer@0: bins = new Matrix("bins",N,binCount);
samer@0: map = new VMap(new LinearMap(binCount));
samer@0: L = new VVector("likelihood",N);
samer@0: sumL = new VDouble("sumL");
samer@0: Shell.setAutoRegister(true);
samer@0: count=0; x=input.array();
samer@0: lbw = new double[binCount];
samer@0: Shell.pop();
samer@0:
samer@0: setAgent(this);
samer@0: binArray = bins.getArray();
samer@0: Shell.registerViewable(this);
samer@0: }
samer@0:
samer@0: public Matrix getBinMatrix() { return bins; }
samer@0: public VVector getLikelihoodVector() { return L; }
samer@0: public VDouble getLikelihoodSignal() { return sumL; }
samer@0:
samer@0: public void starting() {}
samer@0: public void stopping() {}
samer@0: public void run()
samer@0: {
samer@0: IMap binmap=map.getMap();
samer@0: double [] l=L.array();
samer@0:
samer@0: if (x!=null) {
samer@0: for(int k=0; k=M) y[i]=1;
samer@0: else {
samer@0: // linearly interpolate
samer@0: double P1=P[i][j];
samer@0: double P0=(j==0)? 0 : P[i][j-1];
samer@0: double d=z-j;
samer@0: y[i]=(1-d)*P0 + d*P1;
samer@0: }
samer@0: }
samer@0: }
samer@0: }
samer@0: }
samer@0: