view src/samer/maths/random/PosteriorSampler.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
package samer.maths.random;
import samer.maths.*;
import samer.core.*;

/**
 sample from posterior
 instead of supplying weighting factor,
 reject a fraction of the samples 
 make take a long time if A<<sigma
*/

class PosteriorSampler extends BaseRandom
{
	double A, sigma;
	double std, var, mean;

	public PosteriorSampler() {}

	public void setA(double a) { A=a; std=sigma/A; var=std*std; }
	public void setSigma(double s) { sigma=s; std=sigma/A; var=std*std; }
	public void setx(double x) { mean=x/A; }

	public double next() 
	{
		double v;
		boolean ok=false;

		do {
			v=mean + std*rnd.nextGaussian();

			if (v<=-var) {
				v=v+var;
				if (mean<0) {
					double k=Math.exp(2*mean);
					double u=rnd.nextDouble();
					if (u<k) ok=true;
				} else ok=true;
			} else if (v>=var) {
				v=v-var;
				if (mean>0) {
					double k=Math.exp(-2*mean);
					double u=rnd.nextDouble();
					if (u<k) ok=true;
				} else ok=true;
			}
		} while (!ok);

		return v;
	}
}