Debian Codegraph @

src/dr/inference/model/MonotonicStatistic.java

raw symbols SCIP
1 /*
2 * MonotonicStatistic.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.model;
27
28 import dr.xml.*;
29
30 import java.util.ArrayList;
31
32 /**
33 * A class that returns a log likelihood of a series of statistics.
34 * If all the statistics are in increasing (decreasing) order then it returns 0.0 otherwise -INF.
35 *
36 * @author Marc Suchard
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 * @return boolean result of test.
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 { // not strict
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 * Reads a distribution likelihood from a DOM Document element.
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 // AbstractXMLObjectParser implementation
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