| 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.inferencexml.distribution; |
| 27 |
|
| 28 |
import dr.inference.distribution.AbstractDistributionLikelihood; |
| 29 |
import dr.inference.distribution.CachedDistributionLikelihood; |
| 30 |
import dr.inference.distribution.DistributionLikelihood; |
| 31 |
import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 32 |
import dr.inference.model.Likelihood; |
| 33 |
import dr.inference.model.Variable; |
| 34 |
import dr.math.distributions.CompoundGaussianProcess; |
| 35 |
import dr.math.distributions.GaussianProcessRandomGenerator; |
| 36 |
import dr.util.Attribute; |
| 37 |
import dr.xml.*; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.List; |
| 41 |
import java.util.logging.Logger; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class CompoundGaussianProcessParser extends AbstractXMLObjectParser { |
| 47 |
|
| 48 |
public static final String NAME = "compoundGaussianProcess"; |
| 49 |
|
| 50 |
public String getParserName() { |
| 51 |
return NAME; |
| 52 |
} |
| 53 |
|
| 54 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 55 |
|
| 56 |
List<GaussianProcessRandomGenerator> gpList = new ArrayList<GaussianProcessRandomGenerator>(); |
| 57 |
List<Likelihood> likelihoodList = new ArrayList<Likelihood>(); |
| 58 |
List<Integer> copyList = new ArrayList<Integer>(); |
| 59 |
|
| 60 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 61 |
Object obj = xo.getChild(i); |
| 62 |
GaussianProcessRandomGenerator gp = null; |
| 63 |
Likelihood likelihood = null; |
| 64 |
int copies = -1; |
| 65 |
if (obj instanceof DistributionLikelihood) { |
| 66 |
DistributionLikelihood dl = (DistributionLikelihood) obj; |
| 67 |
if (!(dl.getDistribution() instanceof GaussianProcessRandomGenerator)) { |
| 68 |
throw new XMLParseException("Not a Gaussian process"); |
| 69 |
} |
| 70 |
likelihood = dl; |
| 71 |
gp = (GaussianProcessRandomGenerator) dl.getDistribution(); |
| 72 |
copies = 0; |
| 73 |
for (Attribute<double[]> datum : dl.getDataList()) { |
| 74 |
|
| 75 |
|
| 76 |
copies += datum.getAttributeValue().length; |
| 77 |
} |
| 78 |
} else if (obj instanceof MultivariateDistributionLikelihood) { |
| 79 |
MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) obj; |
| 80 |
if (!(mdl.getDistribution() instanceof GaussianProcessRandomGenerator)) { |
| 81 |
throw new XMLParseException("Not a Gaussian process"); |
| 82 |
} |
| 83 |
likelihood = mdl; |
| 84 |
gp = (GaussianProcessRandomGenerator) mdl.getDistribution(); |
| 85 |
copies = 0; |
| 86 |
double[] draw = (double[]) gp.nextRandom(); |
| 87 |
for (Attribute<double[]> datum : mdl.getDataList()) { |
| 88 |
|
| 89 |
copies += datum.getAttributeValue().length / draw.length; |
| 90 |
} |
| 91 |
} else if (obj instanceof GaussianProcessRandomGenerator) { |
| 92 |
gp = (GaussianProcessRandomGenerator) obj; |
| 93 |
likelihood = gp.getLikelihood(); |
| 94 |
copies = 1; |
| 95 |
} else { |
| 96 |
throw new XMLParseException("Not a Gaussian process"); |
| 97 |
} |
| 98 |
gpList.add(gp); |
| 99 |
likelihoodList.add(likelihood); |
| 100 |
copyList.add(copies); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
return new CompoundGaussianProcess(gpList, likelihoodList, copyList); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 112 |
return rules; |
| 113 |
} |
| 114 |
|
| 115 |
private final XMLSyntaxRule[] rules = { |
| 116 |
new ElementRule(GaussianProcessRandomGenerator.class, 1, Integer.MAX_VALUE), |
| 117 |
}; |
| 118 |
|
| 119 |
public String getParserDescription() { |
| 120 |
return "Returned a Gaussian process formed from an ordered list of independent Gaussian processes"; |
| 121 |
} |
| 122 |
|
| 123 |
public Class getReturnType() { |
| 124 |
return GaussianProcessRandomGenerator.class; |
| 125 |
} |
| 126 |
} |