diff src/samer/models/IIDPrior.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/IIDPrior.java	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,85 @@
+package samer.models;
+
+import samer.core.*;
+import samer.core.types.*;
+import samer.maths.*;
+import samer.functions.*;
+import samer.maths.opt.*;
+import samer.tools.*;
+
+/**
+	Non adaptive model of vector in which each component is
+	independent of the others and all are identically distributed
+	according to a given prior
+	*/
+
+public class IIDPrior extends AnonymousTask implements Model
+{
+	Vec 		x;
+	VVector	e; // , g;
+	Function	logprior, phi;
+	VDouble	E;
+
+	double [] _x, _e, _g;
+
+	// default model is Gaussian
+	public IIDPrior(int n) { this(n,new HalfSquare()); }
+	public IIDPrior(Vec in) { this(in.size(),new HalfSquare()); setInput(in); }
+	public IIDPrior(Vec in, Function E) { this(in.size(),E); setInput(in); }
+	public IIDPrior(int N, Function f)
+	{
+		int n=N;
+		e=new VVector("e",n);
+		// g=new VVector("phi",n);
+		E=new VDouble("E");
+		logprior = f; phi = f.derivative();
+		_e = e.array();
+		_g = new double[n]; // g.array();
+	}
+
+	public int getSize() { return e.size(); }
+
+	public String toString() { return "IIDPrior:"+logprior.format("x"); }
+
+	public void dispose() {
+		e.dispose();
+		// g.dispose();
+		E.dispose();
+		logprior.dispose();
+		phi.dispose();
+	}
+
+	public void setInput(Vec input) {	x=input; _x=x.array(); }
+	public void setLogPrior(Function f) {
+		phi.dispose(); logprior.dispose();
+		logprior=f; phi=f.derivative();
+	}
+
+	public VVector	getEnergyVector() { return e; }
+	public VDouble	getEnergySignal() { return E; }
+	public double	getEnergy() { return E.value; }
+	public double [] getGradient() { return _g; 	}
+	public void infer() {}
+	public void compute()
+	{
+	  	phi.apply(_x,_g); // g.changed();
+		logprior.apply(_x,_e);	e.changed();
+		E.set(Mathx.sum(_e));
+	}
+
+	public Functionx functionx() {
+		return new Functionx() {
+			public void dispose() {}
+			public void evaluate(Datum P) { P.f=evaluate(P.x,P.g); }
+			public double evaluate(double [] x, double [] g) {
+				logprior.apply(x,g);
+				double E=Mathx.sum(g);
+				phi.apply(x,g);
+				return E;
+			}
+		};
+	}
+
+	public void run() { compute(); }
+}
+