Debian Codegraph @

src/dr/inference/trace/TraceCorrelation.java

raw symbols SCIP
1 /*
2 * TraceCorrelation.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.trace;
27
28 import java.util.List;
29 import java.util.Map;
30
31 /**
32 * A class that stores the correlation statistics for a trace.
33 * The difference to TraceDistribution is mainly to add ACT and ESS
34 * which require stepSize.
35 *
36 * @author Andrew Rambaut
37 * @author Alexei Drummond
38 * @version $Id: TraceCorrelation.java,v 1.2 2006/11/29 14:53:53 rambaut Exp $
39 */
40 public class TraceCorrelation extends TraceDistribution {
41 private final long stepSize;
42
43 public TraceCorrelation(List<Double> values, TraceType traceType, long stepSize) {
44 this(values, traceType, stepSize, false);
45 }
46
47 public TraceCorrelation(List<Double> values, TraceType traceType, long stepSize, boolean isConstant) {
48 super(values, traceType, isConstant);
49 this.stepSize = stepSize;
50
51 if (stepSize > 0) {
52 analyseCorrelation(values, stepSize);
53 }
54 }
55
56 public TraceCorrelation(List<Double> values, Map<Integer, String> categoryLabelMap, List<Integer> categoryOrder, long stepSize, boolean isConstant) {
57 super(values, categoryLabelMap, categoryOrder, isConstant);
58
59 this.stepSize = stepSize;
60
61 if (stepSize > 0) {
62 analyseCorrelation(values, stepSize);
63 }
64 }
65
66 public double getStdErrorOfMean() {
67 return stdErrorOfMean;
68 }
69
70 public double getACT() {
71 return ACT;
72 }
73
74 public double getESS() {
75 return ESS;
76 }
77
78 //************************************************************************
79 // private methods
80 //************************************************************************
81
82 private double stdErrorOfMean;
83 private double stdErrorOfVariance;
84 private double ACT;
85 private double stdErrOfACT;
86 private double ESS;
87
88 private static final int MAX_LAG = 2000;
89
90 private void analyseCorrelation(List<Double> values, long stepSize) {
91 // this.values = values; // move to TraceDistribution(T[] values)
92
93 if (stepSize > 0) {
94 if (getTraceType().isNumber()) {
95 if (!isConstant()) {
96 double[] doubleValues = new double[values.size()];
97 for (int i = 0; i < values.size(); i++) {
98 doubleValues[i] = values.get(i);
99 }
100 analyseCorrelationNumeric(doubleValues, stepSize);
101 } else {
102 stdErrorOfMean = 0.0;
103 ACT = Double.NaN;
104 ESS = Double.NaN;
105 stdErrOfACT = Double.NaN;
106 }
107
108 } else if (getTraceType() == TraceType.CATEGORICAL) {
109 //todo Do not know how to calculate
110 stdErrorOfMean = Double.NaN;
111 ACT = Double.NaN;
112 ESS = Double.NaN;
113 stdErrOfACT = Double.NaN;
114 // throw new UnsupportedOperationException("should not be categorical");
115 } else {
116 throw new RuntimeException("Trace type is not recognized");
117 }
118 }
119 }
120
121 /**
122 * Analyze trace for numeric values
123 *
124 * @param values the values
125 * @param stepSize the sampling frequency of the values
126 */
127 private void analyseCorrelationNumeric(double[] values, long stepSize) {
128
129 final int samples = values.length;
130 int maxLag = Math.min(samples - 1, MAX_LAG);
131
132 double[] gammaStat = new double[maxLag];
133 //double[] varGammaStat = new double[maxLag];
134 double varStat = 0.0;
135 //double varVarStat = 0.0;
136 //double assVarCor = 1.0;
137 //double del1, del2;
138
139 for (int lag = 0; lag < maxLag; lag++) {
140 for (int j = 0; j < samples - lag; j++) {
141 final double del1 = values[j] - mean;
142 final double del2 = values[j + lag] - mean;
143 gammaStat[lag] += (del1 * del2);
144 //varGammaStat[lag] += (del1*del1*del2*del2);
145 }
146
147 gammaStat[lag] /= ((double) (samples - lag));
148 //varGammaStat[lag] /= ((double) samples-lag);
149 //varGammaStat[lag] -= (gammaStat[0] * gammaStat[0]);
150
151 if (lag == 0) {
152 varStat = gammaStat[0];
153 //varVarStat = varGammaStat[0];
154 //assVarCor = 1.0;
155 } else if (lag % 2 == 0) {
156 // fancy stopping criterion :)
157 if (gammaStat[lag - 1] + gammaStat[lag] > 0) {
158 varStat += 2.0 * (gammaStat[lag - 1] + gammaStat[lag]);
159 // varVarStat += 2.0*(varGammaStat[lag-1] + varGammaStat[lag]);
160 // assVarCor += 2.0*((gammaStat[lag-1] * gammaStat[lag-1]) + (gammaStat[lag] * gammaStat[lag])) / (gammaStat[0] * gammaStat[0]);
161 }
162 // stop
163 else {
164 maxLag = lag;
165 }
166 }
167 }
168
169 // standard error of mean
170 stdErrorOfMean = Math.sqrt(varStat / samples);
171
172 // auto correlation time
173 if (gammaStat[0] == 0) {
174 ACT = 0;
175 } else {
176 ACT = stepSize * varStat / gammaStat[0];
177 }
178
179 // effective sample size
180 if (ACT == 0) {
181 ESS = 1;
182 } else {
183 ESS = (stepSize * samples) / ACT;
184 }
185
186 // standard deviation of autocorrelation time
187 stdErrOfACT = (2.0 * Math.sqrt(2.0 * (2.0 * (double) (maxLag + 1)) / samples) * (varStat / gammaStat[0]) * stepSize);
188
189 // minEqualToMax = true;
190 }
191
192 }