Search in sources :

Example 1 with MultivariateDistributionLikelihood

use of dr.inference.distribution.MultivariateDistributionLikelihood 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);
}
Also used : CompoundGradient(dr.inference.hmc.CompoundGradient) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) ArrayList(java.util.ArrayList) GradientWrtParameterProvider(dr.inference.hmc.GradientWrtParameterProvider) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

Example 2 with MultivariateDistributionLikelihood

use of dr.inference.distribution.MultivariateDistributionLikelihood in project beast-mcmc by beast-dev.

the class GeoSpatialDistribution method main.

public static void main(String[] args) {
    // test reading in KML file with multiple polygons and fillValue attribute
    List<AbstractPolygon2D> polygons = Polygon2D.readKMLFile("Multiple_polygons/9.285_105.7244444.kml");
    System.out.println(polygons.size() + " polygons found.");
    // check if all the polygons have either a fillValue or do not have this fillValue
    AbstractPolygon2D first = polygons.get(0);
    boolean equalFills = first.hasFillValue();
    if (polygons.size() > 1) {
        for (int i = 1; i < polygons.size(); i++) {
            if (!(equalFills == polygons.get(i).hasFillValue())) {
                System.out.println("Inconsistent fillValue attributes provided.");
            }
        }
    }
    if (equalFills) {
        double sum = 0.0;
        for (AbstractPolygon2D region : polygons) {
            sum += region.getFillValue();
        }
        if (Math.abs(sum - 1.0) > 1E-12) {
            System.out.println("\nFill values do not sum to 1 : " + sum);
        }
    }
    boolean overAllFill = equalFills;
    List<GeoSpatialDistribution> geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();
    for (AbstractPolygon2D region : polygons) {
        geoSpatialDistributions.add(new GeoSpatialDistribution("test", region, false));
    }
    MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new MultiRegionGeoSpatialDistribution("multi", geoSpatialDistributions, false, overAllFill));
    Parameter.Default parameter = new Parameter.Default("coordinate", 2);
    likelihood.addData(parameter);
    double logL = likelihood.calculateLogLikelihood();
    System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
    System.out.println("logL = " + logL);
    System.out.println("L = " + Math.exp(logL));
    parameter.setParameterValue(0, 11.40);
    parameter.setParameterValue(1, 106.90);
    likelihood.makeDirty();
    logL = likelihood.calculateLogLikelihood();
    System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
    System.out.println("logL = " + logL);
    System.out.println("L = " + Math.exp(logL));
    // test reading in KML file with a single polygon without fillValue attribute
    polygons = Polygon2D.readKMLFile("Districts_polygons/21.035_106.063.kml");
    System.out.println("\n" + polygons.size() + " polygons found.");
    first = polygons.get(0);
    equalFills = first.hasFillValue();
    if (polygons.size() > 1) {
        for (int i = 1; i < polygons.size(); i++) {
            if (!(equalFills == polygons.get(i).hasFillValue())) {
                System.out.println("Inconsistent fillValue attributes provided.");
            }
        }
    }
    if (equalFills) {
        double sum = 0.0;
        for (AbstractPolygon2D region : polygons) {
            sum += region.getFillValue();
        }
        if (Math.abs(sum - 1.0) > 1E-12) {
            System.out.println("\nFill values do not sum to 1 : " + sum);
        }
    }
    overAllFill = equalFills;
    geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();
    for (AbstractPolygon2D region : polygons) {
        geoSpatialDistributions.add(new GeoSpatialDistribution("test", region, false));
    }
    likelihood = new MultivariateDistributionLikelihood(new MultiRegionGeoSpatialDistribution("multi", geoSpatialDistributions, false, overAllFill));
    likelihood.addData(parameter);
    logL = likelihood.calculateLogLikelihood();
    System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
    System.out.println("logL = " + logL);
    System.out.println("L = " + Math.exp(logL));
    parameter.setParameterValue(0, 21.0);
    parameter.setParameterValue(1, 106.03);
    likelihood.makeDirty();
    logL = likelihood.calculateLogLikelihood();
    System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
    System.out.println("logL = " + logL);
    System.out.println("L = " + Math.exp(logL));
    parameter.setParameterValue(0, 21.0);
    parameter.setParameterValue(1, 106.10);
    likelihood.makeDirty();
    logL = likelihood.calculateLogLikelihood();
    System.out.println("\n(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ")");
    System.out.println("logL = " + logL);
    System.out.println("L = " + Math.exp(logL));
}
Also used : MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) ArrayList(java.util.ArrayList) Parameter(dr.inference.model.Parameter)

Example 3 with MultivariateDistributionLikelihood

use of dr.inference.distribution.MultivariateDistributionLikelihood 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]));
    }
}
Also used : MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) Attribute(dr.util.Attribute) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) ArrayList(java.util.ArrayList) MultivariateNormalDistribution(dr.math.distributions.MultivariateNormalDistribution) ParametricDistributionModel(dr.inference.distribution.ParametricDistributionModel) NormalDistributionModel(dr.inference.distribution.NormalDistributionModel) Transform(dr.util.Transform) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

Example 4 with MultivariateDistributionLikelihood

use of dr.inference.distribution.MultivariateDistributionLikelihood in project beast-mcmc by beast-dev.

the class CompoundGradientParser 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 CompoundDerivative(gradList);
}
Also used : MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) ArrayList(java.util.ArrayList) CompoundDerivative(dr.inference.hmc.CompoundDerivative) GradientWrtParameterProvider(dr.inference.hmc.GradientWrtParameterProvider) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

Example 5 with MultivariateDistributionLikelihood

use of dr.inference.distribution.MultivariateDistributionLikelihood in project beast-mcmc by beast-dev.

the class HessianWrapperParser 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 HessianProvider)) {
            throw new XMLParseException("Not a hessian provider");
        }
        final HessianProvider provider = (HessianProvider) mdl.getDistribution();
        final Parameter parameter = mdl.getDataParameter();
        return new HessianWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
    } else {
        DistributionLikelihood dl = (DistributionLikelihood) obj;
        if (!(dl.getDistribution() instanceof HessianProvider)) {
            throw new XMLParseException("Not a hessian provider");
        }
        final HessianProvider provider = (HessianProvider) dl.getDistribution();
        final Parameter parameter = (Parameter) xo.getChild(Parameter.class);
        return new HessianWrtParameterProvider.ParameterWrapper(provider, parameter, dl);
    }
}
Also used : MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) Parameter(dr.inference.model.Parameter) HessianProvider(dr.inference.model.HessianProvider) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

Aggregations

MultivariateDistributionLikelihood (dr.inference.distribution.MultivariateDistributionLikelihood)13 Parameter (dr.inference.model.Parameter)8 ArrayList (java.util.ArrayList)8 DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)6 AbstractDistributionLikelihood (dr.inference.distribution.AbstractDistributionLikelihood)3 CachedDistributionLikelihood (dr.inference.distribution.CachedDistributionLikelihood)3 GradientWrtParameterProvider (dr.inference.hmc.GradientWrtParameterProvider)3 Likelihood (dr.inference.model.Likelihood)3 GaussianProcessRandomGenerator (dr.math.distributions.GaussianProcessRandomGenerator)2 MultivariateDistribution (dr.math.distributions.MultivariateDistribution)2 MultivariateNormalDistribution (dr.math.distributions.MultivariateNormalDistribution)2 Attribute (dr.util.Attribute)2 GMRFSkyrideLikelihood (dr.evomodel.coalescent.GMRFSkyrideLikelihood)1 GMRFSkyrideFixedEffectsGibbsOperator (dr.evomodel.coalescent.operators.GMRFSkyrideFixedEffectsGibbsOperator)1 AbstractPolygon2D (dr.geo.AbstractPolygon2D)1 GeoSpatialDistribution (dr.geo.GeoSpatialDistribution)1 MultiRegionGeoSpatialDistribution (dr.geo.MultiRegionGeoSpatialDistribution)1 EmpiricalDistributionLikelihood (dr.inference.distribution.EmpiricalDistributionLikelihood)1 MultivariateNormalDistributionModel (dr.inference.distribution.MultivariateNormalDistributionModel)1 NormalDistributionModel (dr.inference.distribution.NormalDistributionModel)1