use of dr.inference.distribution.MultivariateDistributionLikelihood 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 if (obj instanceof EmpiricalDistributionLikelihood) {
final Parameter parameter = (Parameter) xo.getChild(Parameter.class);
return new GradientWrtParameterProvider.ParameterWrapper((GradientProvider) obj, parameter, (Likelihood) obj);
} else {
DistributionLikelihood dl = (DistributionLikelihood) obj;
if (!(dl.getDistribution() instanceof GradientProvider)) {
throw new XMLParseException("Not a gradient provider");
}
final GradientProvider provider = (GradientProvider) dl.getDistribution();
final Parameter parameter = (Parameter) xo.getChild(Parameter.class);
return new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, dl);
}
}
use of dr.inference.distribution.MultivariateDistributionLikelihood in project beast-mcmc by beast-dev.
the class EllipticalSliceOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
final Parameter variable = (Parameter) xo.getChild(Parameter.class);
boolean drawByRowTemp = false;
if (xo.hasAttribute(DRAW_BY_ROW)) {
drawByRowTemp = xo.getBooleanAttribute(DRAW_BY_ROW);
}
final boolean drawByRow = drawByRowTemp;
boolean signal = xo.getAttribute(SIGNAL_CONSTITUENT_PARAMETERS, true);
if (!signal) {
Parameter possiblyCompound = variable;
if (variable instanceof MaskedParameter) {
possiblyCompound = ((MaskedParameter) variable).getUnmaskedParameter();
}
if (!(possiblyCompound instanceof CompoundParameter)) {
signal = true;
}
}
double bracketAngle = xo.getAttribute(BRACKET_ANGLE, 0.0);
boolean translationInvariant = xo.getAttribute(TRANSLATION_INVARIANT, false);
boolean rotationInvariant = xo.getAttribute(ROTATION_INVARIANT, false);
GaussianProcessRandomGenerator gaussianProcess = (GaussianProcessRandomGenerator) xo.getChild(GaussianProcessRandomGenerator.class);
if (gaussianProcess == null) {
final MultivariateDistributionLikelihood likelihood = (MultivariateDistributionLikelihood) xo.getChild(MultivariateDistributionLikelihood.class);
if (!(likelihood.getDistribution() instanceof GaussianProcessRandomGenerator)) {
throw new XMLParseException("Elliptical slice sampling only works for multivariate normally distributed random variables");
}
if (likelihood.getDistribution() instanceof MultivariateNormalDistribution)
gaussianProcess = (MultivariateNormalDistribution) likelihood.getDistribution();
if (likelihood.getDistribution() instanceof MultivariateNormalDistributionModel)
gaussianProcess = (MultivariateNormalDistributionModel) likelihood.getDistribution();
}
EllipticalSliceOperator operator = new EllipticalSliceOperator(variable, gaussianProcess, drawByRow, signal, bracketAngle, translationInvariant, rotationInvariant);
operator.setWeight(weight);
return operator;
}
use of dr.inference.distribution.MultivariateDistributionLikelihood in project beast-mcmc by beast-dev.
the class MultivariateNormalGibbsOperatorParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
MultivariateDistributionLikelihood prior = (MultivariateDistributionLikelihood) xo.getChild(PRIOR).getChild(MultivariateDistributionLikelihood.class);
MultivariateDistributionLikelihood likelihood = (MultivariateDistributionLikelihood) xo.getChild(LIKELIHOOD).getChild(MultivariateDistributionLikelihood.class);
// CompoundParameter data = (CompoundParameter) xo.getChild(CompoundParameter.class);
String weightTemp = (String) xo.getAttribute(WEIGHT);
Double weight = Double.parseDouble(weightTemp);
try {
// To change body of implemented methods use File | Settings | File Templates.
return new MultivariateNormalGibbsOperator(likelihood, prior, weight);
} catch (IllegalDimension illegalDimension) {
// To change body of catch statement use File | Settings | File Templates.
illegalDimension.printStackTrace();
}
return null;
}
use of dr.inference.distribution.MultivariateDistributionLikelihood in project beast-mcmc by beast-dev.
the class ManyUniformGeoDistributionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
List<GeoSpatialDistribution> distributions = new ArrayList<GeoSpatialDistribution>();
List<Parameter> parameters = new ArrayList<Parameter>();
List<Likelihood> oldLikelihood = new ArrayList<Likelihood>();
for (int i = 0; i < xo.getChildCount(); ++i) {
Object cxo = xo.getChild(i);
MultivariateDistributionLikelihood ab = null;
if (cxo instanceof MultivariateDistributionLikelihood) {
ab = (MultivariateDistributionLikelihood) cxo;
} else if (cxo instanceof CachedDistributionLikelihood) {
ab = (MultivariateDistributionLikelihood) ((CachedDistributionLikelihood) cxo).getDistributionLikelihood();
}
if (ab != null) {
parameters.add(ab.getDataParameter());
MultivariateDistribution md = ab.getDistribution();
oldLikelihood.add((Likelihood) cxo);
if (md instanceof GeoSpatialDistribution) {
distributions.add((GeoSpatialDistribution) md);
} else {
throw new XMLParseException("Unhandled distribution type in '" + xo.getId() + "'");
}
}
}
return new ManyUniformGeoDistributionModel(xo.getId(), parameters, distributions, oldLikelihood);
}
use of dr.inference.distribution.MultivariateDistributionLikelihood in project beast-mcmc by beast-dev.
the class Polygon2DSampling method main.
// private boolean hasFillValue;
public static void main(String[] args) {
List<AbstractPolygon2D> polygons = Polygon2D.readKMLFile(args[0]);
System.out.println("Reading from file: " + args[0] + "\n");
List<GeoSpatialDistribution> geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();
// check if all the polygons have either a fillValue or do not have this fillValue
boolean overAllFill = false;
AbstractPolygon2D first = polygons.get(0);
System.out.println("Polygon 0: " + first.getFillValue());
boolean equalFills = first.hasFillValue();
if (polygons.size() > 1) {
for (int i = 1; i < polygons.size(); i++) {
if (!(equalFills == polygons.get(i).hasFillValue())) {
throw new RuntimeException("Inconsistent fillValue attributes provided.");
} else {
System.out.println("Polygon " + i + ": " + polygons.get(i).getFillValue());
}
}
}
if (equalFills) {
double sum = 0.0;
for (AbstractPolygon2D region : polygons) {
sum += region.getFillValue();
}
if (Math.abs(sum - 1.0) > 1E-12) {
throw new RuntimeException("Fill values in " + args[0] + " do not sum to 1 : " + sum);
} else {
System.out.println("Provided sampling probabilities sum to 1.0");
}
}
overAllFill = equalFills;
for (AbstractPolygon2D region : polygons) {
geoSpatialDistributions.add(new GeoSpatialDistribution("", region, true));
}
System.out.println("\nTesting sampling probabilities:");
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new MultiRegionGeoSpatialDistribution("", geoSpatialDistributions, false, overAllFill));
double[] coordinates = new double[2];
coordinates[0] = 12.68379879;
coordinates[1] = 108.12982178;
Parameter parameter = new Parameter.Default(coordinates);
likelihood.addData(parameter);
System.out.println("logL = " + likelihood.getLogLikelihood());
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
parameter.setParameterValue(0, 13.68379879);
System.out.println("logL = " + likelihood.getLogLikelihood());
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
parameter.setParameterValue(0, 12.68);
System.out.println("logL = " + likelihood.getLogLikelihood());
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
parameter.setParameterValue(0, 13.03387928);
parameter.setParameterValue(1, 108.51604462);
System.out.println("logL = " + likelihood.getLogLikelihood());
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
parameter.setParameterValue(0, 13.01971245);
parameter.setParameterValue(1, 108.56764221);
System.out.println("logL = " + likelihood.getLogLikelihood());
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
parameter.setParameterValue(0, 13.018);
System.out.println("logL = " + likelihood.getLogLikelihood());
System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
// from 8 to 14 and from 104 to 110
/*System.out.println("\n\n\nplot(0,0,xlim=c(9,14),ylim=c(105,110))");
Random rand = new Random();
for (int i = 0; i < 100000; i++) {
parameter.setParameterValue(0, rand.nextDouble()*6+9);
parameter.setParameterValue(1, rand.nextDouble()*6+105);
if (!Double.isInfinite(likelihood.getLogLikelihood())) {
double color = Math.exp(likelihood.getLogLikelihood());
//System.out.println(color);
String col = "black";
if (Math.abs(color - 0.106508875739645) < 1E-5) {
col = "red";
} else if (Math.abs(color - 0.029585798816568) < 1E-5) {
col = "blue";
} else if (Math.abs(color - 0.0769230769230769) < 1E-5) {
col = "green";
} else if (Math.abs(color - 0.0355029585798817) < 1E-5) {
col = "grey";
} else if (Math.abs(color - 0.124260355029586) < 1E-5) {
col = "purple";
} else if (Math.abs(color - 0.112426035502959) < 1E-5) {
col = "orange";
} else if (Math.abs(color - 0.136094674556213) < 1E-5) {
col = "yellow";
} else if (Math.abs(color - 0.0946745562130177) < 1E-5) {
col = "burlywood";
} else if (Math.abs(color - 0.284023668639053) < 1E-5) {
col = "deepskyblue";
}
System.out.println("points(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ",col=\"" + col + "\",pch=20,bg=\""+ col +"\")");
}
}*/
}
Aggregations