| 1 |
package dr.inferencexml.operators; |
| 2 |
|
| 3 |
import dr.inference.distribution.DeterminentalPointProcessPrior; |
| 4 |
import dr.inference.distribution.RowDimensionPoissonPrior; |
| 5 |
import dr.inference.model.AbstractModelLikelihood; |
| 6 |
import dr.inference.model.AdaptableSizeFastMatrixParameter; |
| 7 |
import dr.inference.model.LatentFactorModel; |
| 8 |
import dr.inference.model.Likelihood; |
| 9 |
import dr.inference.operators.*; |
| 10 |
import dr.xml.*; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class FactorRJMCMCOperatorParser extends AbstractXMLObjectParser{ |
| 16 |
public static final String FACTOR_RJMCMC_OPERATOR = "factorRJMCMCOperator"; |
| 17 |
public static final String WEIGHT = "weight"; |
| 18 |
public static final String LOADINGS_SPARSITY = "loadingsSparsity"; |
| 19 |
public static final String CHAIN_LENGTH = "chainLength"; |
| 20 |
public static final String FACTORS = "factors"; |
| 21 |
public static final String LOADINGS = "loadings"; |
| 22 |
public static final String CUTOFFS = "cutoffs"; |
| 23 |
public static final String SIZE_PARAMETER = "sizeParameter"; |
| 24 |
public static final String LOADINGS_PRIOR = "loadingsPrior"; |
| 25 |
public static final String FACTOR_OPERATOR = "factorOperator"; |
| 26 |
public static final String LOADINGS_OPERATOR = "loadingsOperator"; |
| 27 |
public static final String ROW_PRIOR = "rowPrior"; |
| 28 |
public static final String SPARSITY_PRIOR = "sparsityPrior"; |
| 29 |
public static final String NEGATION_OPERATOR = "negationOperator"; |
| 30 |
|
| 31 |
@Override |
| 32 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 33 |
|
| 34 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 35 |
int chainLength = xo.getIntegerAttribute(CHAIN_LENGTH); |
| 36 |
double sizeParameter = xo.getDoubleAttribute(SIZE_PARAMETER); |
| 37 |
|
| 38 |
|
| 39 |
AdaptableSizeFastMatrixParameter factors, loadings, cutoffs, loadingsSparcity; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
if(xo.hasChildNamed(FACTORS)) |
| 44 |
factors = (AdaptableSizeFastMatrixParameter) xo.getChild(FACTORS).getChild(AdaptableSizeFastMatrixParameter.class); |
| 45 |
else |
| 46 |
factors = null; |
| 47 |
|
| 48 |
|
| 49 |
loadings = (AdaptableSizeFastMatrixParameter) xo.getChild(LOADINGS).getChild(AdaptableSizeFastMatrixParameter.class); |
| 50 |
|
| 51 |
if(xo.hasChildNamed(CUTOFFS)){ |
| 52 |
cutoffs = (AdaptableSizeFastMatrixParameter) xo.getChild(CUTOFFS).getChild(AdaptableSizeFastMatrixParameter.class);} |
| 53 |
else |
| 54 |
cutoffs = null; |
| 55 |
if(xo.hasChildNamed(LOADINGS_SPARSITY)) |
| 56 |
loadingsSparcity = (AdaptableSizeFastMatrixParameter) xo.getChild(LOADINGS_SPARSITY).getChild(AdaptableSizeFastMatrixParameter.class); |
| 57 |
else |
| 58 |
loadingsSparcity = null; |
| 59 |
|
| 60 |
|
| 61 |
DeterminentalPointProcessPrior DPP = null; |
| 62 |
if(xo.getChild(SPARSITY_PRIOR) != null) |
| 63 |
DPP = (DeterminentalPointProcessPrior) xo.getChild(SPARSITY_PRIOR).getChild(DeterminentalPointProcessPrior.class); |
| 64 |
SimpleMCMCOperator NOp= null; |
| 65 |
if(xo.getChild(NEGATION_OPERATOR) != null){ |
| 66 |
NOp = (SimpleMCMCOperator) xo.getChild(NEGATION_OPERATOR).getChild(SimpleMCMCOperator.class); |
| 67 |
} |
| 68 |
AbstractModelLikelihood LFM = (AbstractModelLikelihood) xo.getChild(AbstractModelLikelihood.class); |
| 69 |
RowDimensionPoissonPrior rowPrior = (RowDimensionPoissonPrior) xo.getChild(ROW_PRIOR).getChild(RowDimensionPoissonPrior.class); |
| 70 |
Likelihood loadingsPrior = null; |
| 71 |
if(xo.hasChildNamed(LOADINGS_PRIOR)){ |
| 72 |
loadingsPrior = (Likelihood) xo.getChild(LOADINGS_PRIOR).getChild(Likelihood.class); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
SimpleMCMCOperator sparsityOperator = null; |
| 77 |
if (xo.getChild(BitFlipOperator.class) != null) |
| 78 |
sparsityOperator = (BitFlipOperator) xo.getChild(BitFlipOperator.class); |
| 79 |
if(xo.getChild(LoadingsSparsityOperator.class) != null){ |
| 80 |
sparsityOperator = (LoadingsSparsityOperator) xo.getChild(LoadingsSparsityOperator.class); |
| 81 |
} |
| 82 |
SimpleMCMCOperator loadingsOperator = (SimpleMCMCOperator) xo.getChild(LOADINGS_OPERATOR).getChild(SimpleMCMCOperator.class); |
| 83 |
SimpleMCMCOperator factorOperator = null; |
| 84 |
if(xo.getChild(FACTOR_OPERATOR) != null) |
| 85 |
factorOperator = (SimpleMCMCOperator) xo.getChild(FACTOR_OPERATOR).getChild(FactorTreeGibbsOperator.class); |
| 86 |
|
| 87 |
LatentFactorModelPrecisionGibbsOperator precisionGibbsOperator = null; |
| 88 |
if(xo.getChild(LatentFactorModelPrecisionGibbsOperator.class) != null){ |
| 89 |
System.out.println("here"); |
| 90 |
precisionGibbsOperator = (LatentFactorModelPrecisionGibbsOperator) xo.getChild(LatentFactorModelPrecisionGibbsOperator.class); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
return new FactorRJMCMCOperator(weight, sizeParameter, chainLength, factors, |
| 97 |
loadings, cutoffs, loadingsSparcity, LFM, DPP, |
| 98 |
loadingsPrior, loadingsOperator, factorOperator, |
| 99 |
sparsityOperator, NOp, rowPrior, precisionGibbsOperator); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 104 |
return rules; |
| 105 |
} |
| 106 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 107 |
new ElementRule(LOADINGS_OPERATOR, new XMLSyntaxRule[]{ |
| 108 |
new ElementRule(SimpleMCMCOperator.class)}), |
| 109 |
new ElementRule(FACTOR_OPERATOR, new XMLSyntaxRule[]{ |
| 110 |
new ElementRule(SimpleMCMCOperator.class)}, true), |
| 111 |
new OrRule(new ElementRule(BitFlipOperator.class, true), new ElementRule(LoadingsSparsityOperator.class, true)), |
| 112 |
new ElementRule(AbstractModelLikelihood.class), |
| 113 |
new ElementRule(LatentFactorModelPrecisionGibbsOperator.class, true), |
| 114 |
new ElementRule(SPARSITY_PRIOR, new XMLSyntaxRule[]{ |
| 115 |
new ElementRule(DeterminentalPointProcessPrior.class)}, true), |
| 116 |
new ElementRule(FACTORS, new XMLSyntaxRule[]{ |
| 117 |
new ElementRule(AdaptableSizeFastMatrixParameter.class)}, true), |
| 118 |
new ElementRule(LOADINGS, new XMLSyntaxRule[]{ |
| 119 |
new ElementRule(AdaptableSizeFastMatrixParameter.class)}), |
| 120 |
new ElementRule(CUTOFFS, new XMLSyntaxRule[]{ |
| 121 |
new ElementRule(AdaptableSizeFastMatrixParameter.class)}, true), |
| 122 |
new ElementRule(LOADINGS_SPARSITY, new XMLSyntaxRule[]{ |
| 123 |
new ElementRule(AdaptableSizeFastMatrixParameter.class)}, true), |
| 124 |
new ElementRule(NEGATION_OPERATOR, new XMLSyntaxRule[]{ |
| 125 |
new ElementRule(SimpleMCMCOperator.class)}, true), |
| 126 |
new ElementRule(ROW_PRIOR, new XMLSyntaxRule[]{ |
| 127 |
new ElementRule(RowDimensionPoissonPrior.class)}), |
| 128 |
AttributeRule.newDoubleRule(WEIGHT), |
| 129 |
AttributeRule.newIntegerRule(CHAIN_LENGTH), |
| 130 |
AttributeRule.newDoubleRule(SIZE_PARAMETER), |
| 131 |
}; |
| 132 |
|
| 133 |
@Override |
| 134 |
public String getParserDescription() { |
| 135 |
return "RJMCMC to determine the number of factors in a factor analysis model"; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public Class getReturnType() { |
| 140 |
return FactorRJMCMCOperator.class; |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public String getParserName() { |
| 145 |
return FACTOR_RJMCMC_OPERATOR; |
| 146 |
} |
| 147 |
} |