use of dr.inference.distribution.ParametricDistributionModel in project beast-mcmc by beast-dev.
the class EllipticalSliceOperator method main.
public static void main(String[] arg) {
// Define normal model
// Starting values
Parameter thetaParameter = new Parameter.Default(new double[] { 1.0, 0.0 });
MaskedParameter meanParameter = new MaskedParameter(thetaParameter, new Parameter.Default(new double[] { 1.0, 0.0 }), true);
TransformedParameter precParameter = new TransformedParameter(new MaskedParameter(thetaParameter, new Parameter.Default(new double[] { 0.0, 1.0 }), true), new Transform.LogTransform(), true);
// System.err.println(thetaParameter);
// System.err.println(meanParameter);
// System.err.println(precParameter);
ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, precParameter, true);
DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);
// Define prior
MultivariateNormalDistribution priorDistribution = new MultivariateNormalDistribution(new double[] { 0.0, 0.0 }, new double[][] { { 0.001, 0.0 }, { 0.0, 0.001 } });
MultivariateDistributionLikelihood prior = new MultivariateDistributionLikelihood(priorDistribution);
prior.addData(thetaParameter);
// Define data
// likelihood.addData(new Attribute.Default<double[]>("Data", new double[] {0.0, 2.0, 4.0}));
likelihood.addData(new Attribute.Default<double[]>("Data", new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
List<Likelihood> list = new ArrayList<Likelihood>();
list.add(likelihood);
list.add(prior);
CompoundLikelihood posterior = new CompoundLikelihood(0, list);
EllipticalSliceOperator sliceSampler = new EllipticalSliceOperator(thetaParameter, priorDistribution, false, true);
final int dim = thetaParameter.getDimension();
final int length = 100000;
double[] mean = new double[dim];
double[] variance = new double[dim];
Parameter[] log = new Parameter[dim];
log[0] = meanParameter;
log[1] = precParameter;
for (int i = 0; i < length; i++) {
sliceSampler.doOperation(posterior);
for (int j = 0; j < dim; ++j) {
double x = log[j].getValue(0);
mean[j] += x;
variance[j] += x * x;
}
}
for (int j = 0; j < dim; ++j) {
mean[j] /= length;
variance[j] /= length;
variance[j] -= mean[j] * mean[j];
}
System.out.println("E(x)\tStErr(x)");
for (int j = 0; j < dim; ++j) {
System.out.println(mean[j] + " " + Math.sqrt(variance[j]));
}
}
use of dr.inference.distribution.ParametricDistributionModel in project beast-mcmc by beast-dev.
the class SliceOperator method main.
public static void main(String[] arg) {
// Define normal model
// Starting value
Parameter meanParameter = new Parameter.Default(1.0);
// Fixed value
Variable<Double> stdev = new Variable.D(1.0, 1);
ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, stdev);
DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);
// Define prior
// Hyper-priors
DistributionLikelihood prior = new DistributionLikelihood(new NormalDistribution(0.0, 1.0));
prior.addData(meanParameter);
// Define data
likelihood.addData(new Attribute.Default<double[]>("Data", new double[] { 0.0, 1.0, 2.0 }));
List<Likelihood> list = new ArrayList<Likelihood>();
list.add(likelihood);
list.add(prior);
CompoundLikelihood posterior = new CompoundLikelihood(0, list);
SliceOperator sliceSampler = new SliceOperator(meanParameter);
final int length = 10000;
double mean = 0;
double variance = 0;
for (int i = 0; i < length; i++) {
sliceSampler.doOperation(posterior);
double x = meanParameter.getValue(0);
mean += x;
variance += x * x;
}
mean /= length;
variance /= length;
variance -= mean * mean;
System.out.println("E(x) = " + mean);
System.out.println("V(x) = " + variance);
}
use of dr.inference.distribution.ParametricDistributionModel in project beast-mcmc by beast-dev.
the class SampleNonActiveGibbsOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
XMLObject cxo = xo.getChild(DISTRIBUTION);
ParametricDistributionModel distribution = (ParametricDistributionModel) cxo.getChild(ParametricDistributionModel.class);
cxo = xo.getChild(DATA_PARAMETER);
Parameter data = (Parameter) cxo.getChild(Parameter.class);
cxo = xo.getChild(INDICATOR_PARAMETER);
Parameter indicators = (Parameter) cxo.getChild(Parameter.class);
return new SampleNonActiveGibbsOperator(distribution, data, indicators, weight);
}
use of dr.inference.distribution.ParametricDistributionModel in project beast-mcmc by beast-dev.
the class RandomWalkModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter data = (Parameter) xo.getChild(Parameter.class);
ParametricDistributionModel distribution = (ParametricDistributionModel) xo.getChild(ParametricDistributionModel.class);
boolean logScale = false;
if (xo.hasAttribute(LOG_SCALE))
logScale = xo.getBooleanAttribute(LOG_SCALE);
return new RandomWalkModel(distribution, data, false, logScale);
}
use of dr.inference.distribution.ParametricDistributionModel in project beast-mcmc by beast-dev.
the class OrnsteinUhlenbeckPriorLikelihoodParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter mean = getParam(xo, MEAN);
Parameter sigma = getParam(xo, SIGMA);
Parameter lambda = getParam(xo, LAMBDA);
final boolean logSpace = xo.getAttribute(LOG_SPACE, false);
final boolean normalize = xo.getAttribute(NORMALIZE, false);
VariableDemographicModel m = (VariableDemographicModel) xo.getChild(VariableDemographicModel.class);
if (m != null) {
ParametricDistributionModel popMeanPrior = (ParametricDistributionModel) xo.getChild(ParametricDistributionModel.class);
return new OrnsteinUhlenbeckPriorLikelihood(mean, sigma, lambda, m, logSpace, normalize, popMeanPrior);
}
final XMLObject cxo1 = xo.getChild(DATA);
Parameter dataParameter = (Parameter) cxo1.getChild(Parameter.class);
final XMLObject cxo2 = xo.getChild(TIMES);
final Parameter timesParameter = (Parameter) cxo2.getChild(Parameter.class);
return new OrnsteinUhlenbeckPriorLikelihood(mean, sigma, lambda, dataParameter, timesParameter, logSpace, normalize);
}
Aggregations