| 1 | /* |
| 2 | * MultivariateNormalGibbsOperatorParser.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.inferencexml.operators; |
| 27 | |
| 28 | import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 29 | import dr.inference.operators.MultivariateNormalGibbsOperator; |
| 30 | import dr.math.matrixAlgebra.IllegalDimension; |
| 31 | import dr.xml.*; |
| 32 | |
| 33 | /** |
| 34 | * Created with IntelliJ IDEA. |
| 35 | * User: max |
| 36 | * Date: 2/21/14 |
| 37 | * Time: 2:20 PM |
| 38 | * To change this template use File | Settings | File Templates. |
| 39 | */ |
| 40 | public class MultivariateNormalGibbsOperatorParser extends AbstractXMLObjectParser { |
| 41 | public static final String MVN_GIBBS_SAMPLER="MultivariateNormalGibbsOperator"; |
| 42 | public static final String PRIOR="prior"; |
| 43 | public static final String LIKELIHOOD="likelihood"; |
| 44 | public static final String WEIGHT="weight"; |
| 45 | |
| 46 | @Override |
| 47 | public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 48 | |
| 49 | MultivariateDistributionLikelihood prior= (MultivariateDistributionLikelihood) xo.getChild(PRIOR).getChild(MultivariateDistributionLikelihood.class); |
| 50 | MultivariateDistributionLikelihood likelihood= (MultivariateDistributionLikelihood) xo.getChild(LIKELIHOOD).getChild(MultivariateDistributionLikelihood.class); |
| 51 | // CompoundParameter data = (CompoundParameter) xo.getChild(CompoundParameter.class); |
| 52 | String weightTemp= (String) xo.getAttribute(WEIGHT); |
| 53 | Double weight=Double.parseDouble(weightTemp); |
| 54 | |
| 55 | //TODO check that it gives the right likelihood and the MVN distributions are conformable |
| 56 | // if (!(prior.getDistribution() instanceof MultivariateNormalDistributionModel)) { |
| 57 | // throw new XMLParseException("Only a Wishart distribution is conjugate for Gibbs sampling"); |
| 58 | // } |
| 59 | // |
| 60 | // // Make sure precMatrix is square and dim(precMatrix) = dim(parameter) |
| 61 | // if (precMatrix.getColumnDimension() != precMatrix.getRowDimension()) { |
| 62 | // throw new XMLParseException("The variance matrix is not square or of wrong dimension"); |
| 63 | // } |
| 64 | |
| 65 | |
| 66 | try { |
| 67 | return new MultivariateNormalGibbsOperator(likelihood, prior, weight); //To change body of implemented methods use File | Settings | File Templates. |
| 68 | } catch (IllegalDimension illegalDimension) { |
| 69 | illegalDimension.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| 70 | } |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public XMLSyntaxRule[] getSyntaxRules() { |
| 76 | return rules; |
| 77 | } |
| 78 | |
| 79 | private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 80 | new ElementRule(PRIOR, new XMLSyntaxRule[]{new ElementRule(MultivariateDistributionLikelihood.class)}), |
| 81 | new ElementRule(LIKELIHOOD, new XMLSyntaxRule[]{new ElementRule(MultivariateDistributionLikelihood.class)}), |
| 82 | // new ElementRule(CompoundParameter.class), |
| 83 | AttributeRule.newDoubleRule(WEIGHT), |
| 84 | }; |
| 85 | |
| 86 | @Override |
| 87 | public String getParserDescription() { |
| 88 | return "Multivariate Normal Gibbs Sampler"; //To change body of implemented methods use File | Settings | File Templates. |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Class getReturnType() { |
| 93 | return MultivariateNormalGibbsOperator.class; //To change body of implemented methods use File | Settings | File Templates. |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String getParserName() { |
| 98 | return MVN_GIBBS_SAMPLER; //To change body of implemented methods use File | Settings | File Templates. |
| 99 | } |
| 100 | } |