/* * MomentDistributionModel.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.inference.distribution; import dr.inference.model.*; import dr.inference.model.Parameter; import dr.inferencexml.distribution.MomentDistributionModelParser; import dr.math.distributions.RandomGenerator; //@author Max Tolkoff public class MomentDistributionModel extends AbstractModelLikelihood implements ParametricMultivariateDistributionModel, RandomGenerator { public MomentDistributionModel(String id, Parameter mean, Parameter precision, Parameter cutoff, Parameter data) { super(id); this.mean=mean; this.precision=precision; // this.mean = new DuplicatedParameter(mean); // this.mean.addDuplicationParameter(new Parameter.Default(cutoff.getDimension())); // DuplicatedParameter precTemp= new DuplicatedParameter(precision); // precTemp.addDuplicationParameter(new Parameter.Default(cutoff.getDimension())); // this.precision=new DiagonalMatrix(precTemp); addVariable(mean); mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); addVariable(precision); // precision.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); this.cutoff=cutoff; if(cutoff!=null){ addVariable(cutoff); int boundsDim; if(cutoff.getBounds()!=null) boundsDim = cutoff.getBounds().getBoundsDimension(); else boundsDim = cutoff.getDimension(); cutoff.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, boundsDim));} addVariable(data); this.data=data; untruncated=new NormalDistributionModel(mean, precision, true); sumKnown=false; untruncatedKnown=false; } private final Parameter mean; private final Parameter precision; // private final DuplicatedParameter mean; // private final DiagonalMatrix precision; private final Parameter cutoff; private NormalDistributionModel untruncated; private double sum; private boolean sumKnown; private boolean storedSumKnown; private double storedSum; private boolean untruncatedKnown; private boolean storedUntruncatedKnown; private NormalDistributionModel storedUntruncated; private Parameter data; public double logPdf(Parameter data) { // untruncatedKnown=false; // sumKnown=false; checkDistribution(); if(sumKnown) return sum; else { sum=0; } if(cutoff!=null){ if(data.getDimension()!=cutoff.getDimension()){ throw new RuntimeException("Incorrect number of cutoffs"); } for (int i = 0; i Math.abs(data.getParameterValue(i)) && data.getParameterValue(i)!=0){ return Double.NEGATIVE_INFINITY; } else if(data.getParameterValue(i)==0){ sum += 0; } // sum+=-1000-Math.log(precision.getParameterValue(0)); else { sum+=untruncated.logPdf(data.getParameterValue(i)) + getNormalizingConstant(i) ; } } } else{ for (int i = 0; i getLocationVariable() { throw new UnsupportedOperationException("Not implemented"); } }