/* * MultivariatePolyaDistributionLikelihood.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.math.distributions; import dr.inference.loggers.LogColumn; import dr.inference.loggers.NumberColumn; import dr.inference.model.*; import dr.math.GammaFunction; import dr.xml.*; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Package: MultivariatePolyaDistributionLikelihood * Description: * this class provides a model for over-dispersed multinomial counts. The model follows Dirichlet-Multinomial distribution with * multinomial parameters integrated out analytically. This model is also known as Multivariate Polya distribution. * Standard parametrization involves k intensities a_i's. This implementation uses the standard parametrization internally, but allows * for re-parametrization as frequencies (k-1 df) and dispersion parameters, where a = sum_i=1^k a_i is dispersion and f_i = a_i/a *

* Created by * * @author Alexander V. Alekseyenko (alexander.alekseyenko@gmail.com) * Date: Sep 23, 2010 * Time: 12:25:14 PM */ public class MultivariatePolyaDistributionLikelihood extends AbstractModel implements Likelihood { protected Parameter frequencies; protected Parameter dispersion; protected Parameter alphas; protected boolean usingAlphas; protected boolean isAlphasKnown; protected MatrixParameter data; protected double fixedNorm; protected double variableNorm; protected double storedFixedNorm; protected double storedVariableNorm; protected double logLikelihood; protected double storedLogLikelihood; protected boolean isLogLikelihoodKnown; protected boolean isFixedNormKnown; protected boolean isVariableNormKnown; protected double rowSums[]; public MultivariatePolyaDistributionLikelihood(String modelID, MatrixParameter data, Parameter frequencies, Parameter dispersion) { super(modelID); this.frequencies = frequencies; this.dispersion = dispersion; this.alphas = new Parameter.Default(frequencies.getDimension()); computeAlphas(); this.data = data; isFixedNormKnown = false; isVariableNormKnown = false; addVariable(this.frequencies); addVariable(this.dispersion); addVariable(this.data); if (this.alphas.getDimension() != data.getColumnDimension()) { System.err.println("Dimensions of the frequency vector and number of columns do not match!"); } } public MultivariatePolyaDistributionLikelihood(String modelID, MatrixParameter data, Parameter alphas) { super(modelID); this.alphas = alphas; isAlphasKnown = true; usingAlphas = true; this.frequencies = new Parameter.Default(alphas.getDimension()); this.dispersion = new Parameter.Default(1); this.data = data; isFixedNormKnown = false; isVariableNormKnown = false; addVariable(this.alphas); addVariable(this.data); if (this.alphas.getDimension() != data.getColumnDimension()) { System.err.println("Dimensions of the frequency vector and number of columns do not match!"); } } /* Compute alphas from frequencies and dispersion */ protected void computeAlphas(){ double disp=dispersion.getParameterValue(0); double[] freqs = frequencies.getParameterValues(); for(int i=0; i getLikelihoodSet() { return new HashSet(Arrays.asList(this)); } public void setUsed() { } public LogColumn[] getColumns() { return new LogColumn[]{ new NumberColumn(this.getId()) { public double getDoubleValue() { return getLogLikelihood(); } } }; } public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { public String getParserName() { return MVPLIKE; } public Object parseXMLObject(XMLObject xo) throws XMLParseException { MatrixParameter data; Parameter dispersion; Parameter frequencies; Parameter rates; if (xo.hasChildNamed(DATA)) { data = (MatrixParameter) xo.getChild(DATA).getChild(MatrixParameter.class); } else { throw new XMLParseException("Missing data element!"); } if (xo.hasChildNamed(RATES)) { rates = (Parameter) xo.getChild(RATES).getChild(Parameter.class); if (rates.getDimension() != data.getColumnDimension()) { throw new XMLParseException("The number of data columns must match the dimension of " + RATES + " parameter (" + data.getColumnDimension() + " != " + rates.getDimension() + "!"); } } else if (xo.hasChildNamed(FREQ)) { frequencies = (Parameter) xo.getChild(FREQ).getChild(Parameter.class); if (xo.hasChildNamed(DISPERSION)) { dispersion = (Parameter) xo.getChild(DISPERSION).getChild(Parameter.class); } else { throw new XMLParseException(DISPERSION + " element has to be specified when using " + FREQ +" parametrization"); } if (dispersion.getDimension() != 1) { throw new XMLParseException("Dispersion parameter must be of dimmension exactly 1!"); } if (frequencies.getDimension() != data.getColumnDimension()) { throw new XMLParseException("The number of data columns must match the dimension of "+ FREQ + " parameter (" + data.getColumnDimension() + " != " + frequencies.getDimension() + "!"); } return new MultivariatePolyaDistributionLikelihood(MVPLIKE, data, frequencies, dispersion); } else { throw new XMLParseException("Either " + FREQ + " or " + RATES + "element has to be specified!"); } return new MultivariatePolyaDistributionLikelihood(MVPLIKE, data, rates); } //************************************************************************ // AbstractXMLObjectParser implementation //************************************************************************ public String getParserDescription() { return "A matrix parameter constructed from its component parameters."; } public XMLSyntaxRule[] getSyntaxRules() { return rules; } private final XMLSyntaxRule[] rules = { new ElementRule(DATA, new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}, false), new XORRule(new ElementRule(RATES, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, false), new ElementRule(FREQ, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, false)), new ElementRule(DISPERSION, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true), }; public Class getReturnType() { return MatrixParameter.class; } }; public static final String MVPLIKE = "mvPolyaLikelihood"; public static final String DATA = "data"; public static final String DISPERSION = "dispersion"; public static final String FREQ = "frequencies"; public static final String RATES = "alpha"; }