| 1 | /* |
| 2 | * CoalescentEventsStatistic.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.evomodel.coalescent; |
| 27 | |
| 28 | //import dr.evolution.coalescent.IntervalType; |
| 29 | import dr.inference.model.Statistic; |
| 30 | |
| 31 | /** |
| 32 | * @author Guy Baele |
| 33 | */ |
| 34 | public class CoalescentEventsStatistic extends Statistic.Abstract { |
| 35 | |
| 36 | //public static final boolean TEST_NEW_CODE = true; |
| 37 | |
| 38 | public static final boolean DEBUG = false; |
| 39 | |
| 40 | //private static final boolean FULL_FINAL_INTERVAL = true; |
| 41 | //private static final boolean LOG_COMBINATIONS = true; |
| 42 | //private static final boolean RETURN_RECIPROCAL = false; |
| 43 | |
| 44 | private final CoalescentIntervalProvider coalescent; |
| 45 | //treeModel currently only required for debugging purposes |
| 46 | //private TreeModel treeModel; |
| 47 | private int coalescentEvents; |
| 48 | //private double[] coalescentValues; |
| 49 | |
| 50 | //public CoalescentEventsStatistic(CoalescentIntervalProvider coalescent, TreeModel treeModel) { |
| 51 | public CoalescentEventsStatistic(CoalescentIntervalProvider coalescent) { |
| 52 | |
| 53 | this.coalescent = coalescent; |
| 54 | |
| 55 | //if (TEST_NEW_CODE) { |
| 56 | |
| 57 | this.coalescentEvents = coalescent.getNumberOfCoalescentEvents(); |
| 58 | |
| 59 | /*} else { |
| 60 | |
| 61 | this.coalescentEvents = 0; |
| 62 | if (coalescent instanceof GMRFSkyrideLikelihood) { |
| 63 | this.coalescentEvents = coalescent.getCoalescentIntervalDimension(); |
| 64 | if (DEBUG) { |
| 65 | System.err.println("CoalescentIntervalDimension: " + coalescent.getCoalescentIntervalDimension()); |
| 66 | } |
| 67 | } else { |
| 68 | for (int i = 0; i < coalescent.getCoalescentIntervalDimension(); i++) { |
| 69 | //Not yet implemented for the skygrid model |
| 70 | if (coalescent.getCoalescentIntervalType(i) == IntervalType.COALESCENT) { |
| 71 | coalescentEvents++; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | if (DEBUG) { |
| 76 | System.err.println("Number of coalescent events: " + this.coalescentEvents); |
| 77 | } |
| 78 | |
| 79 | }*/ |
| 80 | |
| 81 | //this.coalescentValues = new double[coalescentEvents]; |
| 82 | if (DEBUG) { |
| 83 | System.err.println("CoalescentEventsStatistic dimension: " + this.coalescentEvents); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public int getDimension() { |
| 88 | return this.coalescentEvents; |
| 89 | } |
| 90 | |
| 91 | public double getStatisticValue(int i) { |
| 92 | |
| 93 | //if (TEST_NEW_CODE) { |
| 94 | |
| 95 | return coalescent.getCoalescentEventsStatisticValue(i); |
| 96 | |
| 97 | /*} else { |
| 98 | |
| 99 | //System.err.println(treeModel); |
| 100 | //i will go from 0 to getDimension() |
| 101 | //GMRFSkyrideLikelihood |
| 102 | if (DEBUG) { |
| 103 | System.err.println("getStatisticValue(int i)"); |
| 104 | } |
| 105 | if (i == 0) { |
| 106 | if (DEBUG) { |
| 107 | System.err.println("coalescentValues.length = " + coalescentValues.length); |
| 108 | } |
| 109 | //reset array of coalescent events |
| 110 | for (int j = 0; j < coalescentValues.length; j++) { |
| 111 | coalescentValues[j] = 0.0; |
| 112 | } |
| 113 | //recalculate everything |
| 114 | int counter = 0; |
| 115 | if (DEBUG) { |
| 116 | System.err.println("coalescent.getCoalescentIntervalDimension() = " + coalescent.getCoalescentIntervalDimension()); |
| 117 | } |
| 118 | for (int j = 0; j < coalescent.getCoalescentIntervalDimension(); j++) { |
| 119 | if (coalescent instanceof GMRFMultilocusSkyrideLikelihood) { |
| 120 | if (coalescent.getCoalescentIntervalType(j) == IntervalType.COALESCENT) { |
| 121 | if (LOG_COMBINATIONS) { |
| 122 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j)*(coalescent.getCoalescentIntervalLineageCount(j)*(coalescent.getCoalescentIntervalLineageCount(j)-1.0))/2.0; |
| 123 | } else { |
| 124 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 125 | } |
| 126 | counter++; |
| 127 | } else if (!FULL_FINAL_INTERVAL) { |
| 128 | if (coalescent.getCoalescentIntervalType(j) == IntervalType.SAMPLE && counter != 0) { |
| 129 | if (LOG_COMBINATIONS) { |
| 130 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j)*(coalescent.getCoalescentIntervalLineageCount(j)*(coalescent.getCoalescentIntervalLineageCount(j)-1.0))/2.0; |
| 131 | } else { |
| 132 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 133 | } |
| 134 | } |
| 135 | } else { |
| 136 | if (coalescent.getCoalescentIntervalType(j) == IntervalType.SAMPLE) { |
| 137 | if (LOG_COMBINATIONS) { |
| 138 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j)*(coalescent.getCoalescentIntervalLineageCount(j)*(coalescent.getCoalescentIntervalLineageCount(j)-1.0))/2.0; |
| 139 | } else { |
| 140 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } else if (coalescent instanceof GMRFSkyrideLikelihood) { |
| 145 | if (DEBUG) { |
| 146 | System.err.println("counter = " + counter); |
| 147 | System.err.println("((GMRFSkyrideLikelihood)coalescent).getSufficientStatistics()[" + j + "] = " + ((GMRFSkyrideLikelihood)coalescent).getSufficientStatistics()[j]); |
| 148 | } |
| 149 | this.coalescentValues[counter] = ((GMRFSkyrideLikelihood)coalescent).getSufficientStatistics()[j]; |
| 150 | counter++; |
| 151 | } else { |
| 152 | //System.err.println(coalescent.getCoalescentIntervalType(j) + " " + coalescent.getCoalescentInterval(j)); |
| 153 | if (coalescent.getCoalescentIntervalType(j) == IntervalType.COALESCENT) { |
| 154 | if (LOG_COMBINATIONS) { |
| 155 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j)*(coalescent.getCoalescentIntervalLineageCount(j)*(coalescent.getCoalescentIntervalLineageCount(j)-1.0))/2.0; |
| 156 | //System.err.println("interval length: " + coalescent.getCoalescentInterval(j)); |
| 157 | //System.err.println("lineage count: " + coalescent.getCoalescentIntervalLineageCount(j)); |
| 158 | //System.err.println("factorial: " + (coalescent.getCoalescentIntervalLineageCount(j)*coalescent.getCoalescentIntervalLineageCount(j)-1.0)/2.0); |
| 159 | //System.err.println("counter " + counter + ": " + this.coalescentValues[counter] + "\n"); |
| 160 | //this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 161 | //this.coalescentValues[counter] = (coalescent.getCoalescentIntervalLineageCount(j)*coalescent.getCoalescentIntervalLineageCount(j)-1.0)/(2.0*this.coalescentValues[counter]); |
| 162 | } else { |
| 163 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 164 | } |
| 165 | counter++; |
| 166 | } else if (!FULL_FINAL_INTERVAL) { |
| 167 | if (coalescent.getCoalescentIntervalType(j) == IntervalType.SAMPLE && counter != 0) { |
| 168 | if (LOG_COMBINATIONS) { |
| 169 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j)*(coalescent.getCoalescentIntervalLineageCount(j)*(coalescent.getCoalescentIntervalLineageCount(j)-1.0))/2.0; |
| 170 | //System.err.println("interval length: " + coalescent.getCoalescentInterval(j)); |
| 171 | //System.err.println("lineage count: " + coalescent.getCoalescentIntervalLineageCount(j)); |
| 172 | //System.err.println("factorial: " + (coalescent.getCoalescentIntervalLineageCount(j)*coalescent.getCoalescentIntervalLineageCount(j)-1.0)/2.0); |
| 173 | //System.err.println("counter " + counter + ": " + this.coalescentValues[counter] + "\n"); |
| 174 | //this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 175 | } else { |
| 176 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 177 | } |
| 178 | } |
| 179 | } else { |
| 180 | if (coalescent.getCoalescentIntervalType(j) == IntervalType.SAMPLE) { |
| 181 | if (LOG_COMBINATIONS) { |
| 182 | //System.err.println("interval length: " + coalescent.getCoalescentInterval(j)); |
| 183 | //System.err.println("lineage count: " + coalescent.getCoalescentIntervalLineageCount(j)); |
| 184 | //System.err.println("factorial: " + (coalescent.getCoalescentIntervalLineageCount(j)*coalescent.getCoalescentIntervalLineageCount(j)-1.0)/2.0); |
| 185 | //System.err.println("counter " + counter + ": " + this.coalescentValues[counter] + "\n"); |
| 186 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j)*(coalescent.getCoalescentIntervalLineageCount(j)*(coalescent.getCoalescentIntervalLineageCount(j)-1.0))/2.0; |
| 187 | //this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 188 | } else { |
| 189 | this.coalescentValues[counter] += coalescent.getCoalescentInterval(j); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | //for (int j = 0; j < this.coalescentEvents; j++) { |
| 197 | // System.err.println(this.coalescentValues[j]); |
| 198 | //} |
| 199 | //System.exit(0); |
| 200 | if (RETURN_RECIPROCAL) { |
| 201 | return 1.0/this.coalescentValues[i]; |
| 202 | } else { |
| 203 | return this.coalescentValues[i]; |
| 204 | } |
| 205 | |
| 206 | }*/ |
| 207 | |
| 208 | } |
| 209 | |
| 210 | public String getStatisticName() { |
| 211 | return "coalescentEventsStatistic"; |
| 212 | } |
| 213 | |
| 214 | } |