Debian Codegraph @

src/dr/oldevomodel/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.oldevomodel.substmodel;
27
28 import dr.evolution.datatype.DataType;
29 import dr.oldevomodelxml.substmodel.FrequencyModelParser;
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 * A constructor which allows a more programmatic approach with
48 * fixed frequencies.
49 * @param dataType DataType
50 * @param frequencyParameter double[]
51 */
52 public FrequencyModel(DataType dataType, double[] frequencyParameter) {
53 this(dataType, new Parameter.Default(frequencyParameter));
54 }
55
56 public FrequencyModel(DataType dataType, Parameter frequencyParameter) {
57
58 super(FrequencyModelParser.FREQUENCY_MODEL);
59
60 double sum = getSumOfFrequencies(frequencyParameter);
61
62 if (Math.abs(sum - 1.0) > 1e-8) {
63 throw new IllegalArgumentException("Frequencies do not sum to 1, they sum to " + sum);
64 }
65
66 this.frequencyParameter = frequencyParameter;
67 addVariable(frequencyParameter);
68 frequencyParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, frequencyParameter.getDimension()));
69 this.dataType = dataType;
70 }
71
72 /**
73 * @param frequencies the frequencies
74 * @return return the sum of frequencies
75 */
76 private double getSumOfFrequencies(Parameter frequencies) {
77 double total = 0.0;
78 for (int i = 0; i < frequencies.getDimension(); i++) {
79 total += frequencies.getParameterValue(i);
80 }
81 return total;
82 }
83
84 public void setFrequency(int i, double value) {
85 frequencyParameter.setParameterValue(i, value);
86 }
87
88 public double getFrequency(int i) {
89 return frequencyParameter.getParameterValue(i);
90 }
91
92 public int getFrequencyCount() {
93 return frequencyParameter.getDimension();
94 }
95
96 public Parameter getFrequencyParameter() {
97 return frequencyParameter;
98 }
99
100 public double[] getFrequencies() {
101 double[] frequencies = new double[getFrequencyCount()];
102 for (int i = 0; i < frequencies.length; i++) {
103 frequencies[i] = getFrequency(i);
104 }
105 return frequencies;
106 }
107
108 public double[] getCumulativeFrequencies() {
109 double[] frequencies = getFrequencies();
110 for (int i = 1; i < frequencies.length; i++) {
111 frequencies[i] += frequencies[i - 1];
112 }
113 return frequencies;
114 }
115
116 public DataType getDataType() {
117 return dataType;
118 }
119
120 // *****************************************************************
121 // Interface Model
122 // *****************************************************************
123
124 protected void handleModelChangedEvent(Model model, Object object, int index) {
125 // no intermediates need recalculating....
126 }
127
128 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
129 // no intermediates need recalculating....
130 }
131
132 protected void storeState() {
133 } // no state apart from parameters to store
134
135 protected void restoreState() {
136 } // no state apart from parameters to restore
137
138 protected void acceptState() {
139 } // no state apart from parameters to accept
140
141 public Element createElement(Document doc) {
142 throw new RuntimeException("Not implemented!");
143 }
144
145 private DataType dataType = null;
146 Parameter frequencyParameter = null;
147
148 }