| 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.operators; |
| 27 |
|
| 28 |
import dr.inference.distribution.DistributionLikelihood; |
| 29 |
import dr.inference.distribution.IndependentNormalDistributionModel; |
| 30 |
import dr.inference.distribution.LatentFactorModelInterface; |
| 31 |
import dr.inference.distribution.MomentDistributionModel; |
| 32 |
import dr.inference.model.LatentFactorModel; |
| 33 |
import dr.inference.model.Likelihood; |
| 34 |
import dr.inference.model.MatrixParameterInterface; |
| 35 |
import dr.inference.operators.LoadingsGibbsOperator; |
| 36 |
import dr.inference.operators.LoadingsGibbsTruncatedOperator; |
| 37 |
import dr.xml.*; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class LoadingsGibbsOperatorParser extends AbstractXMLObjectParser { |
| 47 |
public static final String LOADINGS_GIBBS_OPERATOR = "loadingsGibbsOperator"; |
| 48 |
public static final String WEIGHT = "weight"; |
| 49 |
private final String RANDOM_SCAN = "randomScan"; |
| 50 |
private final String WORKING_PRIOR = "workingPrior"; |
| 51 |
private final String CUTOFF_PRIOR = "cutoffPrior"; |
| 52 |
private final String MULTI_THREADED = "multiThreaded"; |
| 53 |
private final String NUM_THREADS = "numThreads"; |
| 54 |
private final String PATH_PARAMETER = "pathParameter"; |
| 55 |
private final String UPPER_TRIANGLE = "upperTriangle"; |
| 56 |
|
| 57 |
|
| 58 |
@Override |
| 59 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 60 |
String weightTemp = (String) xo.getAttribute(WEIGHT); |
| 61 |
double weight = Double.parseDouble(weightTemp); |
| 62 |
LatentFactorModel LFM = (LatentFactorModel) xo.getChild(LatentFactorModel.class); |
| 63 |
DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class); |
| 64 |
MomentDistributionModel prior2 = (MomentDistributionModel) xo.getChild(MomentDistributionModel.class); |
| 65 |
IndependentNormalDistributionModel prior3 = (IndependentNormalDistributionModel) xo.getChild(IndependentNormalDistributionModel.class); |
| 66 |
DistributionLikelihood cutoffPrior = null; |
| 67 |
if(xo.hasChildNamed(CUTOFF_PRIOR)){ |
| 68 |
cutoffPrior = (DistributionLikelihood) xo.getChild(CUTOFF_PRIOR).getChild(DistributionLikelihood.class); |
| 69 |
} |
| 70 |
boolean randomScan = xo.getAttribute(RANDOM_SCAN, true); |
| 71 |
int numThreads = xo.getAttribute(NUM_THREADS, 4); |
| 72 |
MatrixParameterInterface loadings=null; |
| 73 |
if(xo.getChild(MatrixParameterInterface.class)!=null){ |
| 74 |
loadings=(MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class); |
| 75 |
} |
| 76 |
DistributionLikelihood WorkingPrior = null; |
| 77 |
if(xo.getChild(WORKING_PRIOR) != null){ |
| 78 |
WorkingPrior = (DistributionLikelihood) xo.getChild(WORKING_PRIOR).getChild(DistributionLikelihood.class); |
| 79 |
} |
| 80 |
boolean multiThreaded = xo.getAttribute(MULTI_THREADED, false); |
| 81 |
boolean upperTriangle = xo.getAttribute(UPPER_TRIANGLE, true); |
| 82 |
|
| 83 |
|
| 84 |
if(prior !=null || prior3 != null){ |
| 85 |
LoadingsGibbsOperator lfmOp = new LoadingsGibbsOperator(LFM, prior, prior3, loadings, weight, randomScan, |
| 86 |
WorkingPrior, multiThreaded, numThreads, upperTriangle); |
| 87 |
if(xo.hasAttribute(PATH_PARAMETER)){ |
| 88 |
System.out.println("WARNING: Setting Path Parameter is intended for debugging purposes only!"); |
| 89 |
lfmOp.setPathParameter(xo.getDoubleAttribute(PATH_PARAMETER)); |
| 90 |
} |
| 91 |
return lfmOp; |
| 92 |
} |
| 93 |
else |
| 94 |
return new LoadingsGibbsTruncatedOperator(LFM, prior2, weight, randomScan, loadings, cutoffPrior); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 99 |
return rules; |
| 100 |
} |
| 101 |
|
| 102 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 103 |
new ElementRule(LatentFactorModelInterface.class), |
| 104 |
|
| 105 |
new XORRule( |
| 106 |
new XORRule( |
| 107 |
new ElementRule(IndependentNormalDistributionModel.class), |
| 108 |
new ElementRule(DistributionLikelihood.class)), |
| 109 |
new AndRule( |
| 110 |
new ElementRule(MomentDistributionModel.class), |
| 111 |
|
| 112 |
new ElementRule(CUTOFF_PRIOR, new XMLSyntaxRule[]{new ElementRule(DistributionLikelihood.class)})) |
| 113 |
), |
| 114 |
|
| 115 |
AttributeRule.newDoubleRule(WEIGHT), |
| 116 |
AttributeRule.newBooleanRule(MULTI_THREADED, true), |
| 117 |
AttributeRule.newIntegerRule(NUM_THREADS, true), |
| 118 |
new ElementRule(WORKING_PRIOR, new XMLSyntaxRule[]{ |
| 119 |
new ElementRule(DistributionLikelihood.class) |
| 120 |
}, true), |
| 121 |
AttributeRule.newDoubleRule(PATH_PARAMETER, true), |
| 122 |
AttributeRule.newBooleanRule(UPPER_TRIANGLE, true) |
| 123 |
}; |
| 124 |
|
| 125 |
@Override |
| 126 |
public String getParserDescription() { |
| 127 |
return "Gibbs sampler for the loadings matrix of a latent factor model"; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public Class getReturnType() { |
| 132 |
return LoadingsGibbsOperator.class; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public String getParserName() { |
| 137 |
return LOADINGS_GIBBS_OPERATOR; |
| 138 |
} |
| 139 |
} |