Mercurial > hg > jslab
view src/samer/maths/random/PosteriorSampler.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +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; } }