Search in sources :

Example 1 with AbstractPolygon2D

use of dr.geo.AbstractPolygon2D in project beast-mcmc by beast-dev.

the class UniformGeoSpatialOperator method doOperation.

/**
 * change the parameter and return the hastings ratio.
 */
public final double doOperation() {
    double[] mass = null;
    int whichRegion = 0;
    if (polygonList.size() > 1) {
        mass = new double[polygonList.size()];
        for (int i = 0; i < polygonList.size(); ++i) {
            mass[i] = polygonList.get(i).calculateArea();
        }
        whichRegion = MathUtils.randomChoicePDF(mass);
    }
    totalOps++;
    if (whichRegion == 0) {
        // For debugging prior probability of first region
        currentSum++;
    }
    AbstractPolygon2D polygon = polygonList.get(whichRegion);
    double[][] minMax = polygon.getXYMinMax();
    int attempts = 0;
    Point2D pt;
    do {
        pt = new Point2D.Double((MathUtils.nextDouble() * (minMax[0][1] - minMax[0][0])) + minMax[0][0], (MathUtils.nextDouble() * (minMax[1][1] - minMax[1][0])) + minMax[1][0]);
        attempts++;
    } while (!polygon.containsPoint2D(pt));
    if (DEBUG) {
        System.err.println("region: " + whichRegion + " attempts: " + attempts + " " + mass[0] + " " + mass[1] + "     " + ((double) currentSum / (double) totalOps));
    }
    parameter.setParameterValue(0, pt.getX());
    parameter.setParameterValue(1, pt.getY());
    return 0.0;
}
Also used : AbstractPolygon2D(dr.geo.AbstractPolygon2D) Point2D(java.awt.geom.Point2D)

Example 2 with AbstractPolygon2D

use of dr.geo.AbstractPolygon2D in project beast-mcmc by beast-dev.

the class UniformGeoSpatialOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    if (parameter.getDimension() == 0) {
        throw new XMLParseException("parameter with 0 dimension.");
    }
    MultivariateDistributionLikelihood likelihood = (MultivariateDistributionLikelihood) xo.getChild(MultivariateDistributionLikelihood.class);
    if (likelihood == null) {
        CachedDistributionLikelihood cached = (CachedDistributionLikelihood) xo.getChild(CachedDistributionLikelihood.class);
        AbstractDistributionLikelihood ab = cached.getDistributionLikelihood();
        if (!(ab instanceof MultivariateDistributionLikelihood)) {
            throw new XMLParseException("invalid likelihood type in " + xo.getId());
        }
        likelihood = (MultivariateDistributionLikelihood) ab;
    }
    List<AbstractPolygon2D> polygonList = new ArrayList<AbstractPolygon2D>();
    if (likelihood.getDistribution() instanceof MultiRegionGeoSpatialDistribution) {
        for (GeoSpatialDistribution spatial : ((MultiRegionGeoSpatialDistribution) likelihood.getDistribution()).getRegions()) {
            polygonList.add(spatial.getRegion());
        }
    } else if (likelihood.getDistribution() instanceof GeoSpatialDistribution) {
        polygonList.add(((GeoSpatialDistribution) likelihood.getDistribution()).getRegion());
    } else {
        throw new XMLParseException("Multivariate distribution must be either a GeoSpatialDistribution " + "or a MultiRegionGeoSpatialDistribution");
    }
    return new UniformGeoSpatialOperator(parameter, weight, polygonList);
}
Also used : AbstractPolygon2D(dr.geo.AbstractPolygon2D) GeoSpatialDistribution(dr.geo.GeoSpatialDistribution) MultiRegionGeoSpatialDistribution(dr.geo.MultiRegionGeoSpatialDistribution) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) AbstractDistributionLikelihood(dr.inference.distribution.AbstractDistributionLikelihood) ArrayList(java.util.ArrayList) Parameter(dr.inference.model.Parameter) CachedDistributionLikelihood(dr.inference.distribution.CachedDistributionLikelihood) MultiRegionGeoSpatialDistribution(dr.geo.MultiRegionGeoSpatialDistribution)

Aggregations

AbstractPolygon2D (dr.geo.AbstractPolygon2D)2 GeoSpatialDistribution (dr.geo.GeoSpatialDistribution)1 MultiRegionGeoSpatialDistribution (dr.geo.MultiRegionGeoSpatialDistribution)1 AbstractDistributionLikelihood (dr.inference.distribution.AbstractDistributionLikelihood)1 CachedDistributionLikelihood (dr.inference.distribution.CachedDistributionLikelihood)1 MultivariateDistributionLikelihood (dr.inference.distribution.MultivariateDistributionLikelihood)1 Parameter (dr.inference.model.Parameter)1 Point2D (java.awt.geom.Point2D)1 ArrayList (java.util.ArrayList)1