use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class Tutorial1 method main.
public static void main(String[] arg) throws IOException, TraceException {
// constructing random variable representing mean of normal distribution
Variable.D mean = new Variable.D("mean", 1.0);
// give mean a uniform prior [-1000, 1000]
mean.addBounds(new Parameter.DefaultBounds(1000, -1000, 1));
// constructing random variable representing stdev of normal distribution
Variable.D stdev = new Variable.D("stdev", 1.0);
// give stdev a uniform prior [0, 1000]
stdev.addBounds(new Parameter.DefaultBounds(1000, 0, 1));
// construct normal distribution model
NormalDistributionModel normal = new NormalDistributionModel(mean, stdev);
// construct a likelihood for normal distribution
DistributionLikelihood likelihood = new DistributionLikelihood(normal);
// construct data
Attribute.Default<double[]> d = new Attribute.Default<double[]>("x", new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
// add data (representing 9 independent observations) to likelihood
likelihood.addData(d);
// construct two "operators" to be used as the proposal distribution
MCMCOperator meanMove = new ScaleOperator(mean, 0.75);
MCMCOperator stdevMove = new ScaleOperator(stdev, 0.75);
// construct a logger to log progress of MCMC run to stdout (screen)
MCLogger logger1 = new MCLogger(100);
logger1.add(mean);
logger1.add(stdev);
// construct a logger to log to a log file for later analysis
MCLogger logger2 = new MCLogger("tutorial1.log", 100, false, 0);
logger2.add(mean);
logger2.add(stdev);
// construct MCMC object
MCMC mcmc = new MCMC("tutorial1:normal");
// initialize MCMC with chain length, likelihood, operators and loggers
mcmc.init(100000, likelihood, new MCMCOperator[] { meanMove, stdevMove }, new Logger[] { logger1, logger2 });
// run the mcmc
mcmc.chain();
// perform post-analysis
TraceAnalysis.report("tutorial1.log");
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class TruncatedDistributionLikelihoodParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DistributionLikelihood likelihood = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
Parameter low;
Parameter high;
if (xo.getChild(LOW) != null) {
low = (Parameter) xo.getChild(LOW).getChild(Parameter.class);
} else {
low = new Parameter.Default("low", Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}
if (xo.getChild(HIGH) != null) {
high = (Parameter) xo.getChild(HIGH).getChild(Parameter.class);
} else {
high = new Parameter.Default("high", Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}
return new TruncatedDistributionLikelihood(likelihood, low, high);
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class TwoPartsDistributionLikelihoodParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DistributionLikelihood priorLikelihood = (DistributionLikelihood) xo.getElementFirstChild(PRIOR);
DistributionLikelihood pseudoPriorLikelihood = (DistributionLikelihood) xo.getElementFirstChild(PSEUDO_PRIOR);
Distribution prior = priorLikelihood.getDistribution();
Distribution pseudoPrior = pseudoPriorLikelihood.getDistribution();
Parameter bitVector = (Parameter) xo.getElementFirstChild(PARAMETER_VECTOR);
int paramIndex = xo.getIntegerAttribute(PARAMETER_INDEX);
Parameter selectedVariable = (Parameter) xo.getElementFirstChild(SELECTED_VARIABLE);
TwoPartsDistributionLikelihood likelihood = new TwoPartsDistributionLikelihood(prior, pseudoPrior, bitVector, paramIndex);
likelihood.addData(selectedVariable);
return likelihood;
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class AppendedPotentialDerivativeParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
List<GradientWrtParameterProvider> gradList = new ArrayList<GradientWrtParameterProvider>();
// TODO Remove?
List<Likelihood> likelihoodList = new ArrayList<Likelihood>();
for (int i = 0; i < xo.getChildCount(); ++i) {
Object obj = xo.getChild(i);
GradientWrtParameterProvider grad;
Likelihood likelihood;
if (obj instanceof DistributionLikelihood) {
DistributionLikelihood dl = (DistributionLikelihood) obj;
if (!(dl.getDistribution() instanceof GradientProvider)) {
throw new XMLParseException("Not a gradient provider");
}
throw new RuntimeException("Not yet implemented");
} else if (obj instanceof MultivariateDistributionLikelihood) {
final MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) obj;
if (!(mdl.getDistribution() instanceof GradientProvider)) {
throw new XMLParseException("Not a gradient provider");
}
final GradientProvider provider = (GradientProvider) mdl.getDistribution();
final Parameter parameter = mdl.getDataParameter();
likelihood = mdl;
grad = new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
} else if (obj instanceof GradientWrtParameterProvider) {
grad = (GradientWrtParameterProvider) obj;
likelihood = grad.getLikelihood();
} else {
throw new XMLParseException("Not a Gaussian process");
}
gradList.add(grad);
likelihoodList.add(likelihood);
}
return new CompoundGradient(gradList);
}
use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.
the class GradientWrapperParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Object obj = xo.getChild(0);
if (obj instanceof MultivariateDistributionLikelihood) {
final MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) xo.getChild(MultivariateDistributionLikelihood.class);
if (!(mdl.getDistribution() instanceof GradientProvider)) {
throw new XMLParseException("Not a gradient provider");
}
final GradientProvider provider = (GradientProvider) mdl.getDistribution();
final Parameter parameter = mdl.getDataParameter();
return new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
} else {
DistributionLikelihood dl = (DistributionLikelihood) obj;
if (!(dl.getDistribution() instanceof GradientProvider)) {
throw new XMLParseException("Not a gradient provider");
}
throw new RuntimeException("Not yet implemented");
}
}
Aggregations