Debian Codegraph @

src/dr/evomodel/continuous/LatentFactorModelSimulator.java

raw symbols SCIP
1 package dr.evomodel.continuous;
2
3 import dr.inference.model.FastMatrixParameter;
4 import dr.inference.model.MatrixParameter;
5 import dr.inference.model.MatrixParameterInterface;
6 import dr.inference.model.Parameter;
7 import dr.math.distributions.NormalDistribution;
8 import dr.xml.*;
9
10 /**
11 * @author Max Tolkoff
12 */
13 public class LatentFactorModelSimulator {
14
15 public static final String SIMULATE_LATENT_FACTOR_MODEL = "simulateLatentFactorModel";
16 public static final String PRECISION = "precision";
17
18
19 // **************************************************************
20 // XMLObjectParser
21 // **************************************************************
22
23 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
24
25 public String getParserName() { return SIMULATE_LATENT_FACTOR_MODEL; }
26
27 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
28
29 final String name = xo.hasId() ? xo.getId() : null;
30
31 FullyConjugateMultivariateTraitLikelihood treeSample = (FullyConjugateMultivariateTraitLikelihood)xo.getChild(FullyConjugateMultivariateTraitLikelihood.class);
32 MatrixParameterInterface Loadings = (MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class);
33
34 GaussianProcessFromTree randomTips = new GaussianProcessFromTree(treeSample);
35
36 int ntaxa = treeSample.getTreeModel().getExternalNodeCount();
37 int ntrait = Loadings.getRowDimension();
38
39 FastMatrixParameter data = new FastMatrixParameter(name, ntrait, ntaxa, 0);
40
41 if(treeSample.getNumData() * treeSample.getDimTrait() != Loadings.getColumnDimension()){
42 throw new RuntimeException("Number of factors in tree and loadings matrix must be identical:\n Factor Matrix: "
43 + treeSample.getNumData() * treeSample.getDimTrait() +
44 "\n Loadings Matrix: "
45 + Loadings.getColumnDimension()
46 );
47 }
48
49 Parameter precision = (Parameter) xo.getChild(PRECISION).getChild(Parameter.class);
50 int nfac = Loadings.getColumnDimension();
51
52 double[] Factors = randomTips.nextRandomFast();
53
54 for (int i = 0; i < ntrait; i++) {
55 for (int j = 0; j < ntaxa; j++) {
56 double sum = 0;
57 for (int k = 0; k < nfac; k++) {
58 sum += Factors[j * nfac + k] * Loadings.getParameterValue(i, k);
59 }
60 double draw = (Double)
61 (new NormalDistribution(sum, 1 / Math.sqrt(precision.getParameterValue(i)))).nextRandom();
62 data.setParameterValue(i, j, draw);
63 }
64
65 }
66
67 return data;
68 }
69
70 //************************************************************************
71 // AbstractXMLObjectParser implementation
72 //************************************************************************
73
74 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
75
76 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
77 new ElementRule(FullyConjugateMultivariateTraitLikelihood.class),
78 new ElementRule(MatrixParameterInterface.class),
79 new ElementRule(PRECISION,
80 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}
81 )
82 };
83
84 public String getParserDescription() {
85 return "Simulates a data matrix for a Latent Factor Model";
86 }
87
88 public Class getReturnType() { return MatrixParameter.class; }
89 };
90
91 }