Skip to content

Commit

Permalink
start to clean up GMRF
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchard committed Nov 1, 2023
1 parent fd12a46 commit 86c3a47
Show file tree
Hide file tree
Showing 12 changed files with 546 additions and 409 deletions.
3 changes: 2 additions & 1 deletion src/dr/app/beast/development_parsers.properties
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ dr.evomodelxml.branchratemodel.TimeVaryingBranchRateModelParser

# GMRF
dr.evomodel.coalescent.GMRFDensityComponent
dr.inferencexml.distribution.RandomFieldParser
dr.inferencexml.distribution.RandomFieldGradientParser
dr.inferencexml.distribution.GaussianMarkovRandomFieldParser
dr.inferencexml.distribution.GaussianMarkovRandomFieldParser2

# GAUSSIAN PROCESS
dr.evomodelxml.coalescent.GPSkytrackAnalysisParser
Expand Down
6 changes: 3 additions & 3 deletions src/dr/evomodel/antigenic/NewAntigenicLikelihood.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,17 @@ public int compare(Measurement lhs, Measurement rhs) {
public void updateParametersOnDevice() {
if (!precisionKnown) {
transferPrecision();
// precisionKnown = true;
precisionKnown = true;
}

if (!locationsKnown) {
transferLocations();
// locationsKnown = true;
locationsKnown = true;
}

if (!observationsKnown) {
transferObservations();
// observationsKnown = true;
observationsKnown = true;
}
}

Expand Down
167 changes: 0 additions & 167 deletions src/dr/inference/distribution/GaussianMarkovRandomFieldModel2.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import dr.inference.model.*;
import dr.inferencexml.distribution.MultivariateNormalDistributionModelParser;
import dr.math.distributions.GaussianMarkovRandomField;
import dr.math.distributions.OldGaussianMarkovRandomField;
import dr.math.distributions.GaussianProcessRandomGenerator;


Expand All @@ -38,12 +38,12 @@
* Pratyusa Datta
*/

public class GaussianMarkovRandomFieldModel extends AbstractModel implements
public class OldGaussianMarkovRandomFieldModel extends AbstractModel implements
ParametricMultivariateDistributionModel, GaussianProcessRandomGenerator, GradientProvider, HessianProvider {

public GaussianMarkovRandomFieldModel(int dim,
Parameter incrementPrecisionParameter,
Parameter startParameter) {
public OldGaussianMarkovRandomFieldModel(int dim,
Parameter incrementPrecisionParameter,
Parameter startParameter) {
super(MultivariateNormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL);

this.dim = dim;
Expand Down Expand Up @@ -146,8 +146,8 @@ public Variable<Double> getLocationVariable() {
// Private instance variables and functions
// **************************************************************

private GaussianMarkovRandomField createNewDistribution() {
return new GaussianMarkovRandomField(getDimension(),
private OldGaussianMarkovRandomField createNewDistribution() {
return new OldGaussianMarkovRandomField(getDimension(),
incrementPrecision.getParameterValue(0), start.getParameterValue(0));
}

Expand All @@ -156,8 +156,8 @@ private GaussianMarkovRandomField createNewDistribution() {
private final Parameter incrementPrecision;
private final Parameter start;

private GaussianMarkovRandomField distribution;
private GaussianMarkovRandomField storedDistribution;
private OldGaussianMarkovRandomField distribution;
private OldGaussianMarkovRandomField storedDistribution;

private boolean distributionKnown;
private boolean storedDistributionKnown;
Expand Down
118 changes: 118 additions & 0 deletions src/dr/inference/distribution/RandomField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* RandomField.java
*
* Copyright (c) 2002-2023 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.math.distributions.RandomFieldDistribution;

/**
* @author Marc Suchard
* @author Pratyusa Datta
* @author Filippo Monti
* @author Xiang Ji
*/

public class RandomField extends AbstractModelLikelihood {

private final Parameter field;
private final RandomFieldDistribution distribution;

private boolean likelihoodKnown;
private double logLikelihood;

private boolean savedLikelihoodKnown;
private double savedLogLikelihood;

public RandomField(String name,
Parameter field,
RandomFieldDistribution distribution) {
super(name);

this.field = field;
this.distribution = distribution;

addVariable(field);
addModel(distribution);

likelihoodKnown = false;
}

public Parameter getField() { return field; }

public RandomFieldDistribution getDistribution() { return distribution; }

@Override
protected void handleModelChangedEvent(Model model, Object object, int index) {
if (model == distribution) {
throw new RuntimeException("Not yet implemented");
} else {
throw new IllegalArgumentException("Unknown model");
}
}

@Override
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
if (variable == field) {
likelihoodKnown = false;
} else {
throw new IllegalArgumentException("Unknown variable");
}
}

@Override
protected void storeState() {
savedLikelihoodKnown = likelihoodKnown;
savedLogLikelihood = logLikelihood;
}

@Override
protected void restoreState() {
likelihoodKnown = savedLikelihoodKnown;
logLikelihood = savedLogLikelihood;
}

@Override
protected void acceptState() { }

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

@Override
public double getLogLikelihood() {

if (!likelihoodKnown) {
logLikelihood = 0;
likelihoodKnown = true;
}

return logLikelihood;
}

@Override
public void makeDirty() {
likelihoodKnown = false;
}
}
Loading

0 comments on commit 86c3a47

Please sign in to comment.