Search in sources :

Example 1 with TwoStates

use of dr.evolution.datatype.TwoStates in project beast-mcmc by beast-dev.

the class OrderedLatentLiabilityTransformParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    OrderedLatentLiabilityLikelihood likelihood = (OrderedLatentLiabilityLikelihood) xo.getChild(OrderedLatentLiabilityLikelihood.class);
    CompoundParameter parameter = likelihood.getTipTraitParameter();
    DataType dataType = likelihood.getPatternList().getDataType();
    if (!(dataType instanceof TwoStates)) {
        throw new XMLParseException("Liability transformation is currently only implemented for binary traits");
    }
    Parameter mask = null;
    if (xo.hasChildNamed(MaskedParameterParser.MASKING)) {
        mask = (Parameter) xo.getElementFirstChild(MaskedParameterParser.MASKING);
    }
    List<Transform> transforms = new ArrayList<Transform>();
    int index = 0;
    for (int tip = 0; tip < parameter.getParameterCount(); ++tip) {
        final int[] tipData = likelihood.getData(tip);
        for (int trait = 0; trait < tipData.length; ++trait) {
            int discreteState = tipData[trait];
            boolean valid = true;
            Transform transform;
            if (discreteState == 0) {
                transform = Transform.LOG_NEGATE;
                if (parameter.getParameterValue(index) >= 0.0) {
                    valid = false;
                }
            } else if (discreteState == 1) {
                transform = Transform.LOG;
                if (parameter.getParameterValue(index) <= 0.0) {
                    valid = false;
                }
            } else {
                transform = Transform.NONE;
            // transforms.add(Transform.NONE);
            }
            if (!valid) {
                throw new XMLParseException("Incompatible binary trait and latent value in tip '" + parameter.getParameter(tip).getId() + "'");
            }
            if (mask == null || mask.getParameterValue(index) == 1.0) {
                transforms.add(transform);
            }
            ++index;
        }
    }
    return new Transform.Array(transforms, parameter);
}
Also used : OrderedLatentLiabilityLikelihood(dr.evomodel.continuous.OrderedLatentLiabilityLikelihood) TwoStates(dr.evolution.datatype.TwoStates) ArrayList(java.util.ArrayList) DataType(dr.evolution.datatype.DataType) Transform(dr.util.Transform)

Aggregations

DataType (dr.evolution.datatype.DataType)1 TwoStates (dr.evolution.datatype.TwoStates)1 OrderedLatentLiabilityLikelihood (dr.evomodel.continuous.OrderedLatentLiabilityLikelihood)1 Transform (dr.util.Transform)1 ArrayList (java.util.ArrayList)1