| 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.inference.model; |
| 27 |
|
| 28 |
import dr.xml.*; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class MonotonicStatistic extends Statistic.Abstract implements BooleanStatistic { |
| 40 |
|
| 41 |
public static final String MONOTONIC_STATISTIC = "monotonicStatistic"; |
| 42 |
public static final String STRICTLY = "strictlyMonotic"; |
| 43 |
public static final String ORDER = "order"; |
| 44 |
|
| 45 |
public MonotonicStatistic(boolean strict, boolean increasing) { |
| 46 |
super(MONOTONIC_STATISTIC); |
| 47 |
this.strict = strict; |
| 48 |
this.increasing = increasing; |
| 49 |
} |
| 50 |
|
| 51 |
public int getDimension() { |
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
public void addStatistic(Statistic stat) { |
| 56 |
dataList.add(stat); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public double getStatisticValue(int dim) { |
| 63 |
return getBoolean(dim) ? 1.0 : 0.0; |
| 64 |
} |
| 65 |
|
| 66 |
public boolean getBoolean(int dim) { |
| 67 |
|
| 68 |
double currentValue; |
| 69 |
if (increasing) |
| 70 |
currentValue = Double.NEGATIVE_INFINITY; |
| 71 |
else |
| 72 |
currentValue = Double.POSITIVE_INFINITY; |
| 73 |
|
| 74 |
for (Statistic statistic : dataList) { |
| 75 |
for (int j = 0; j < statistic.getDimension(); j++) { |
| 76 |
final double newValue = statistic.getStatisticValue(j); |
| 77 |
if (strict) { |
| 78 |
if( (increasing && newValue <= currentValue) || |
| 79 |
(!increasing && newValue >= currentValue) ) |
| 80 |
return false; |
| 81 |
} else { |
| 82 |
if( (increasing && newValue < currentValue) || |
| 83 |
(!increasing && newValue > currentValue) ) |
| 84 |
return false; |
| 85 |
} |
| 86 |
currentValue = newValue; |
| 87 |
} |
| 88 |
} |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 96 |
|
| 97 |
public String getParserName() { |
| 98 |
return MONOTONIC_STATISTIC; |
| 99 |
} |
| 100 |
|
| 101 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 102 |
|
| 103 |
boolean increasing = true; |
| 104 |
String order = xo.getAttribute(ORDER, "increasing"); |
| 105 |
if (order.compareToIgnoreCase("decreasing") == 0) |
| 106 |
increasing = false; |
| 107 |
|
| 108 |
boolean strictly = xo.getAttribute(STRICTLY, false); |
| 109 |
|
| 110 |
MonotonicStatistic monotonicStatistic = new MonotonicStatistic(strictly, increasing); |
| 111 |
|
| 112 |
for(int i=0; i<xo.getChildCount(); i++) { |
| 113 |
if (xo.getChild(i) instanceof Statistic) { |
| 114 |
monotonicStatistic.addStatistic((Statistic)xo.getChild(i)); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return monotonicStatistic; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public String getParserDescription() { |
| 126 |
return ""; |
| 127 |
} |
| 128 |
|
| 129 |
public Class getReturnType() { |
| 130 |
return MonotonicStatistic.class; |
| 131 |
} |
| 132 |
|
| 133 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 134 |
return rules; |
| 135 |
} |
| 136 |
|
| 137 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 138 |
AttributeRule.newStringRule(ORDER, true), |
| 139 |
AttributeRule.newBooleanRule(STRICTLY,true), |
| 140 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 141 |
}; |
| 142 |
|
| 143 |
}; |
| 144 |
|
| 145 |
private boolean strict; |
| 146 |
private boolean increasing; |
| 147 |
protected ArrayList<Statistic> dataList = new ArrayList<Statistic>(); |
| 148 |
|
| 149 |
} |
| 150 |
|