Skip to content

Commit

Permalink
add gmrf2 class,parser
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyusaDatta committed Nov 1, 2023
1 parent 37064a5 commit fd12a46
Show file tree
Hide file tree
Showing 4 changed files with 592 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dr/app/beast/development_parsers.properties
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ dr.evomodelxml.branchratemodel.TimeVaryingBranchRateModelParser
# GMRF
dr.evomodel.coalescent.GMRFDensityComponent
dr.inferencexml.distribution.GaussianMarkovRandomFieldParser
dr.inferencexml.distribution.GaussianMarkovRandomFieldParser2

# GAUSSIAN PROCESS
dr.evomodelxml.coalescent.GPSkytrackAnalysisParser
Expand Down
167 changes: 167 additions & 0 deletions src/dr/inference/distribution/GaussianMarkovRandomFieldModel2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* MultivariateNormalDistributionModel.java
*
* Copyright (c) 2002-2020 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.hmc.GradientWrtParameterProvider;
import dr.inference.hmc.HessianWrtParameterProvider;
import dr.inference.model.*;
import dr.inferencexml.distribution.MultivariateNormalDistributionModelParser;
import dr.math.distributions.GaussianMarkovRandomField2;
import dr.math.distributions.GaussianProcessRandomGenerator;


/**
* A class that acts as a model for gaussian random walk
*
* @author Marc Suchard
* Pratyusa Datta
*/

public class GaussianMarkovRandomFieldModel2 extends AbstractModelLikelihood implements
GradientWrtParameterProvider, HessianWrtParameterProvider {

public GaussianMarkovRandomFieldModel2(Parameter coefficients,
GaussianMarkovRandomField2 distribution) {
super(MultivariateNormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL);

this.coefficients = coefficients;
this.distribution = distribution;
this.dim = coefficients.getDimension();

addModel(distribution);
addVariable(coefficients);
}


public Parameter getincrementPrecision() { return distribution.getincrementPrecision(); }

public Parameter getstart() { return distribution.getstart(); }
public double[][] getScaleMatrix() {
return distribution.getScaleMatrix();
}


public double[] getMean() {
return distribution.getMean();
}

public String getType() {
return distribution.getType();
}

// *****************************************************************
// Interface Model
// *****************************************************************
@Override
public Likelihood getLikelihood() {
return this;
}

@Override
public Model getModel() {
return this;
}

@Override
public Parameter getParameter() {
return coefficients;
}

@Override
public final void makeDirty() {
// Do nothing
}

@Override
public final void handleModelChangedEvent(Model model, Object object, int index) {
// no intermediates need to be recalculated...
}

@Override
public final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
// no intermediates need to be recalculated...
}

@Override
public void storeState() {
// Do nothing
}

@Override
public void restoreState() {
// Do nothing
}

@Override
public void acceptState() {
} // no additional state needs accepting


@Override
public int getDimension() {
return dim;
}


// *****************************************************************
// Interface DensityModel
// *****************************************************************

public Parameter getIncrementPrecision() { return distribution.getincrementPrecision(); }

public Parameter getStart() { return distribution.getstart(); }

public double getLogLikelihood() {
return distribution.logPdf(coefficients.getParameterValues());
}

public double[] getGradientLogDensity() {
return distribution.gradLogPdf(coefficients.getParameterValues());
}


// **************************************************************
// Private instance variables and functions
// **************************************************************




private final Parameter coefficients;
private final GaussianMarkovRandomField2 distribution;
private final int dim;


@Override
public double[] getDiagonalHessianLogDensity() {
return new double[0];
}

@Override
public double[][] getHessianLogDensity() {
return new double[0][];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* MultivariateNormalDistributionModelParser.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.inferencexml.distribution;

import dr.inference.distribution.GaussianMarkovRandomFieldModel2;
import dr.inference.model.Model;
import dr.inference.model.Parameter;
import dr.inference.model.Variable;
import dr.math.distributions.GaussianMarkovRandomField2;
import dr.xml.*;

public class GaussianMarkovRandomFieldParser2 extends AbstractXMLObjectParser {

public static final String NORMAL_DISTRIBUTION_MODEL = "gaussianMarkovRandomField2";
private static final String DIMENSION = "dim";
private static final String PRECISION = "precision";
private static final String START = "start";

public String getParserName() {

return NORMAL_DISTRIBUTION_MODEL;
}

public Object parseXMLObject(XMLObject xo) throws XMLParseException {

Parameter coefficients = (Parameter) xo.getChild(Parameter.class);

int dim = coefficients.getDimension();

XMLObject cxo = xo.getChild(PRECISION);
Parameter incrementPrecision = (Parameter) cxo.getChild(Parameter.class);

if (incrementPrecision.getParameterValue(0) <= 0.0) {
throw new XMLParseException("Scale must be > 0.0");
}

cxo = xo.getChild(START);
Parameter start = (Parameter) cxo.getChild(Parameter.class);



return new GaussianMarkovRandomFieldModel2(coefficients, new GaussianMarkovRandomField2(dim, incrementPrecision, start));
}

public XMLSyntaxRule[] getSyntaxRules() {
return rules;
}

private final XMLSyntaxRule[] rules = {
// AttributeRule.newIntegerRule(DIMENSION),
new ElementRule(PRECISION,
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
new ElementRule(START,
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
};

public String getParserDescription() {
return "Describes a normal distribution with a given mean and precision " +
"that can be used in a distributionLikelihood element";
}

public Class getReturnType() {
return GaussianMarkovRandomFieldModel2.class;
}

}
Loading

0 comments on commit fd12a46

Please sign in to comment.