| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.operators.EllipticalSliceOperator; |
| 32 |
import dr.inferencexml.operators.EllipticalSliceOperatorParser; |
| 33 |
import dr.math.KroneckerOperation; |
| 34 |
import dr.math.distributions.GaussianProcessRandomGenerator; |
| 35 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 36 |
import dr.math.matrixAlgebra.CholeskyDecomposition; |
| 37 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 38 |
import dr.math.matrixAlgebra.Matrix; |
| 39 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 40 |
import dr.xml.*; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class ConstrainedGaussianProcessRandomGenerator implements GaussianProcessRandomGenerator { |
| 47 |
|
| 48 |
private final GaussianProcessRandomGenerator generator; |
| 49 |
private final boolean translationInvariant; |
| 50 |
private final boolean rotationInvariant; |
| 51 |
|
| 52 |
public ConstrainedGaussianProcessRandomGenerator(GaussianProcessRandomGenerator generator, |
| 53 |
boolean translationInvariant, boolean rotationInvariant) { |
| 54 |
this.generator = generator; |
| 55 |
this.translationInvariant = translationInvariant; |
| 56 |
this.rotationInvariant = rotationInvariant; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Likelihood getLikelihood() { |
| 61 |
throw new RuntimeException("Not yet implemented"); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public int getDimension() { |
| 67 |
return generator.getDimension(); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public double[][] getPrecisionMatrix() { |
| 72 |
throw new RuntimeException("Not yet implemented"); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public Object nextRandom() { |
| 78 |
|
| 79 |
double[] draw = (double[]) generator.nextRandom(); |
| 80 |
EllipticalSliceOperator.transformPoint(draw, translationInvariant, rotationInvariant, |
| 81 |
2); |
| 82 |
return draw; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public double logPdf(Object x) { |
| 87 |
throw new RuntimeException("Not yet implemented"); |
| 88 |
|
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
public boolean isTranslationInvariant() { |
| 93 |
return translationInvariant; |
| 94 |
} |
| 95 |
|
| 96 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 97 |
|
| 98 |
public static final String CONSTAINED_GAUSSIAN_PROCESS = "constrainedGaussianProcess"; |
| 99 |
public static final String TRANSLATION_INVARIANT = EllipticalSliceOperatorParser.TRANSLATION_INVARIANT; |
| 100 |
public static final String ROTATION_INVARIANT = EllipticalSliceOperatorParser.ROTATION_INVARIANT; |
| 101 |
|
| 102 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 103 |
new ElementRule(GaussianProcessRandomGenerator.class), |
| 104 |
AttributeRule.newBooleanRule(TRANSLATION_INVARIANT, true), |
| 105 |
AttributeRule.newBooleanRule(ROTATION_INVARIANT, true), |
| 106 |
}; |
| 107 |
|
| 108 |
@Override |
| 109 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 110 |
|
| 111 |
boolean translationInvariant = xo.getAttribute(TRANSLATION_INVARIANT, false); |
| 112 |
boolean rotationInvariant = xo.getAttribute(ROTATION_INVARIANT, false); |
| 113 |
|
| 114 |
GaussianProcessRandomGenerator generator = |
| 115 |
(GaussianProcessRandomGenerator) xo.getChild(GaussianProcessRandomGenerator.class); |
| 116 |
|
| 117 |
return new ConstrainedGaussianProcessRandomGenerator(generator, translationInvariant, rotationInvariant); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 122 |
return rules; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public String getParserDescription() { |
| 127 |
return "Returns a random draw of traits given a trait model and a prior"; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public Class getReturnType() { |
| 132 |
return ConstrainedGaussianProcessRandomGenerator.class; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public String getParserName() { |
| 137 |
return CONSTAINED_GAUSSIAN_PROCESS; |
| 138 |
} |
| 139 |
}; |
| 140 |
} |