Debian Codegraph @

src/dr/evomodel/substmodel/ProductChainFrequencyModel.java

raw symbols SCIP
1 /*
2 * ProductChainFrequencyModel.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.substmodel;
27
28 import dr.inference.model.Model;
29 import dr.inference.model.Parameter;
30
31 import java.util.List;
32
33 /**
34 * @author Marc A. Suchard
35 * @author Vladimir Minin
36 * <p/>
37 * A class for implementing a kronecker sum of CTMC models in BEAST using BEAGLE
38 * This work is supported by NSF grant 0856099
39 * <p/>
40 * O'Brien JD, Minin VN and Suchard MA (2009) Learning to count: robust estimates for labeled distances between
41 * molecular sequences. Molecular Biology and Evolution, 26, 801-814
42 */
43
44 public class ProductChainFrequencyModel extends FrequencyModel {
45
46 public ProductChainFrequencyModel(String name, List<FrequencyModel> freqModels) {
47 super(name);
48 this.freqModels = freqModels;
49 int freqCount = 1;
50 numBaseModel = freqModels.size();
51 stateSizes = new int[numBaseModel];
52 for (int i = 0; i < numBaseModel; i++) {
53 int size = freqModels.get(i).getFrequencyCount();
54 stateSizes[i] = size;
55 freqCount *= size;
56 addModel(freqModels.get(i));
57 }
58 tmp = new int[numBaseModel];
59 totalFreqCount = freqCount;
60 }
61
62 protected void handleModelChangedEvent(Model model, Object object, int index) {
63 fireModelChanged(model);
64 }
65
66 public void setFrequency(int i, double value) {
67 throw new RuntimeException("Not implemented");
68 }
69
70 public double getFrequency(int index) {
71 double freq = 1.0;
72 decomposeEntry(index, tmp);
73 for (int i = 0; i < numBaseModel; i++) {
74 freq *= freqModels.get(i).getFrequency(tmp[i]);
75 }
76 return freq;
77 }
78
79 public int[] decomposeEntry(int index) {
80 int[] tmp = new int[numBaseModel];
81 decomposeEntry(index, tmp);
82 return tmp;
83 }
84
85 private void decomposeEntry(int index, int[] decomposition) {
86 int current = index;
87 for (int i = numBaseModel - 1; i >= 0; --i) {
88 decomposition[i] = current % stateSizes[i];
89 current /= stateSizes[i];
90 }
91 }
92
93 public int getFrequencyCount() {
94 return totalFreqCount;
95 }
96
97 public Parameter getFrequencyParameter() {
98 throw new RuntimeException("Not implemented");
99 }
100
101 private List<FrequencyModel> freqModels;
102
103 private final int numBaseModel;
104 private final int totalFreqCount;
105 private final int[] stateSizes;
106 private final int[] tmp;
107 }