Debian Codegraph @

src/dr/evomodel/substmodel/FrequencyModel.java

raw symbols SCIP
1 /*
2 * FrequencyModel.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.evomodelxml.substmodel.FrequencyModelParser;
29 import dr.evolution.datatype.DataType;
30 import dr.inference.model.AbstractModel;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37 /**
38 * A model of equlibrium frequencies
39 *
40 * @author Alexei Drummond
41 * @author Andrew Rambaut
42 * @author Marc Suchard
43 * @version $Id: FrequencyModel.java,v 1.26 2005/05/24 20:25:58 rambaut Exp $
44 */
45 public class FrequencyModel extends AbstractModel {
46
47 /**
48 * A constructor which allows a more programmatic approach with
49 * fixed frequencies.
50 *
51 * @param dataType
52 * @param frequencyParameter
53 */
54 public FrequencyModel(DataType dataType, double[] frequencyParameter) {
55 this(dataType, new Parameter.Default(frequencyParameter));
56 }
57
58 public FrequencyModel(String name) {
59 super(name);
60 }
61
62 public FrequencyModel(DataType dataType, Parameter frequencyParameter) {
63
64 super(FrequencyModelParser.FREQUENCY_MODEL);
65
66 double sum = getSumOfFrequencies(frequencyParameter);
67
68 if (Math.abs(sum - 1.0) > 1e-8) {
69 throw new IllegalArgumentException("Frequencies do not sum to 1, they sum to " + sum);
70 }
71
72 this.frequencyParameter = frequencyParameter;
73 addVariable(frequencyParameter);
74 frequencyParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, frequencyParameter.getDimension()));
75 this.dataType = dataType;
76 }
77
78 /**
79 * @param frequencies the frequencies
80 * @return return the sum of frequencies
81 */
82 private double getSumOfFrequencies(Parameter frequencies) {
83 double total = 0.0;
84 for (int i = 0; i < frequencies.getDimension(); i++) {
85 total += frequencies.getParameterValue(i);
86 }
87 return total;
88 }
89
90 public void setFrequency(int i, double value) {
91 frequencyParameter.setParameterValue(i, value);
92 }
93
94 public double getFrequency(int i) {
95 return frequencyParameter.getParameterValue(i);
96 }
97
98 public int getFrequencyCount() {
99 return frequencyParameter.getDimension();
100 }
101
102 public Parameter getFrequencyParameter() {
103 return frequencyParameter;
104 }
105
106 public double[] getFrequencies() {
107 double[] frequencies = new double[getFrequencyCount()];
108 for (int i = 0; i < frequencies.length; i++) {
109 frequencies[i] = getFrequency(i);
110 }
111 return frequencies;
112 }
113
114 public double[] getCumulativeFrequencies() {
115 double[] frequencies = getFrequencies();
116 for (int i = 1; i < frequencies.length; i++) {
117 frequencies[i] += frequencies[i - 1];
118 }
119 return frequencies;
120 }
121
122 public DataType getDataType() {
123 return dataType;
124 }
125
126 // *****************************************************************
127 // Interface Model
128 // *****************************************************************
129
130 protected void handleModelChangedEvent(Model model, Object object, int index) {
131 // no intermediates need recalculating....
132 }
133
134 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
135 // no intermediates need recalculating....
136 }
137
138 protected void storeState() {
139 } // no state apart from parameters to store
140
141 protected void restoreState() {
142 } // no state apart from parameters to restore
143
144 protected void acceptState() {
145 } // no state apart from parameters to accept
146
147 public Element createElement(Document doc) {
148 throw new RuntimeException("Not implemented!");
149 }
150
151 private DataType dataType = null;
152 Parameter frequencyParameter = null;
153
154 }