use of dr.inference.model.Variable 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.model.Variable in project beast-mcmc by beast-dev.
the class CachedDistributionLikelihoodParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
final String name = xo.hasId() ? xo.getId() : CACHED_PRIOR;
final AbstractDistributionLikelihood likelihood = (AbstractDistributionLikelihood) xo.getChild(AbstractDistributionLikelihood.class);
final Variable variable = (Variable) xo.getChild(Variable.class);
final Logger logger = Logger.getLogger("dr.inference");
logger.info("Constructing a cache around likelihood '" + likelihood.getId() + "', signal = " + variable.getVariableName());
return new CachedDistributionLikelihood(name, likelihood, variable);
}
use of dr.inference.model.Variable in project beast-mcmc by beast-dev.
the class GammaSiteBMAParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getElementFirstChild(SUBSTITUTION_MODEL);
Parameter muParam = (Parameter) xo.getElementFirstChild(MUTATION_RATE);
Parameter logitInvar = (Parameter) xo.getElementFirstChild(LOGIT_PROPORTION_INVARIANT);
final XMLObject cxo = xo.getChild(LOG_GAMMA_SHAPE);
Parameter logShape = (Parameter) cxo.getChild(Parameter.class);
int catCount = cxo.getIntegerAttribute(GAMMA_CATEGORIES);
Variable<Integer> modelChoose = (Variable<Integer>) xo.getElementFirstChild(MODEL_CHOOSE);
return new GammaSiteBMA(substitutionModel, muParam, logitInvar, logShape, catCount, modelChoose);
}
use of dr.inference.model.Variable in project beast-mcmc by beast-dev.
the class GTRParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
XMLObject cxo = xo.getChild(FREQUENCIES);
FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
Variable rateACValue = null;
if (xo.hasChildNamed(A_TO_C)) {
rateACValue = (Variable) xo.getElementFirstChild(A_TO_C);
}
Variable rateAGValue = null;
if (xo.hasChildNamed(A_TO_G)) {
rateAGValue = (Variable) xo.getElementFirstChild(A_TO_G);
}
Variable rateATValue = null;
if (xo.hasChildNamed(A_TO_T)) {
rateATValue = (Variable) xo.getElementFirstChild(A_TO_T);
}
Variable rateCGValue = null;
if (xo.hasChildNamed(C_TO_G)) {
rateCGValue = (Variable) xo.getElementFirstChild(C_TO_G);
}
Variable rateCTValue = null;
if (xo.hasChildNamed(C_TO_T)) {
rateCTValue = (Variable) xo.getElementFirstChild(C_TO_T);
}
Variable rateGTValue = null;
if (xo.hasChildNamed(G_TO_T)) {
rateGTValue = (Variable) xo.getElementFirstChild(G_TO_T);
}
int countNull = 0;
if (rateACValue == null)
countNull++;
if (rateAGValue == null)
countNull++;
if (rateATValue == null)
countNull++;
if (rateCGValue == null)
countNull++;
if (rateCTValue == null)
countNull++;
if (rateGTValue == null)
countNull++;
if (countNull != 1)
throw new XMLParseException("Only five parameters may be specified in GTR, leave exactly one out, the others will be specifed relative to the one left out.");
return new GTR(rateACValue, rateAGValue, rateATValue, rateCGValue, rateCTValue, rateGTValue, freqModel);
}
use of dr.inference.model.Variable in project beast-mcmc by beast-dev.
the class NtdBMAParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Variable kappa = (Variable) xo.getElementFirstChild(KAPPA);
Variable tn = (Variable) xo.getElementFirstChild(TN);
Variable ac = (Variable) xo.getElementFirstChild(AC);
Variable at = (Variable) xo.getElementFirstChild(AT);
Variable gc = (Variable) xo.getElementFirstChild(GC);
Variable gt = (Variable) xo.getElementFirstChild(GT);
Variable modelChoose = (Variable) xo.getElementFirstChild(MODEL_CHOOSE);
XMLObject cxo = xo.getChild(FREQUENCIES);
FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
return new NtdBMA(kappa, tn, ac, at, gc, gt, modelChoose, freqModel);
}
Aggregations