Debian Codegraph @

src/dr/evomodel/branchmodel/lineagespecific/DirichletProcessPriorLogger.java

raw symbols SCIP
1 /*
2 * DirichletProcessPriorLogger.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.branchmodel.lineagespecific;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import dr.app.bss.Utils;
32 import dr.inference.distribution.ParametricMultivariateDistributionModel;
33 import dr.inference.loggers.LogColumn;
34 import dr.inference.loggers.Loggable;
35 import dr.inference.loggers.NumberColumn;
36 import dr.inference.model.CompoundParameter;
37 import dr.inference.model.Parameter;
38 import dr.math.distributions.NormalDistribution;
39
40 public class DirichletProcessPriorLogger implements Loggable {
41
42 // private ParametricMultivariateDistributionModel baseModel;
43 private Parameter precisionParameter;
44 private Parameter categoriesParameter;
45 private CompoundParameter uniquelyRealizedParameters;
46
47 private int uniqueRealizationCount;
48 private int realizationCount;
49
50 private double[] categoryProbabilities;
51 private int newCategoryIndex;
52 private double meanForCategory;
53 private double newX;
54
55 public DirichletProcessPriorLogger(
56 Parameter precisionParameter, //
57 Parameter categoriesParameter, //
58 CompoundParameter uniquelyRealizedParameters //
59 ) {
60
61 this.precisionParameter = precisionParameter;
62 this.uniquelyRealizedParameters = uniquelyRealizedParameters;
63 this.categoriesParameter = categoriesParameter;
64
65 this.uniqueRealizationCount = uniquelyRealizedParameters.getDimension();
66 this.realizationCount = categoriesParameter.getDimension();
67
68 }// END: Constructor
69
70 private double[] getCategoryProbs() {
71
72 double[] probs = new double[uniqueRealizationCount];
73
74 for (int i = 0; i < realizationCount; i++) {
75 probs[(int) categoriesParameter.getParameterValue(i)]++;
76 }// END: N loop
77
78 // Utils.printArray(probs);
79
80 for (int i = 0; i < uniqueRealizationCount; i++) {
81 probs[i] = probs[i] / realizationCount;
82 }// END: categoryCount loop
83
84 // probs = new double[]{0.10, 0.10, 0.10, 0.10, 0.10,
85 // 0.10, 0.10, 0.10, 0.10, 0.10};
86
87 return probs;
88 }// END: getCategoryProbs
89
90 @Override
91 public LogColumn[] getColumns() {
92
93 // this.categoryProbabilities = getCategoryProbs();
94
95 List<LogColumn> columns = new ArrayList<LogColumn>();
96
97 columns.add(new NewLogger("x.new"));
98 columns.add(new NewCategoryLogger("category.new"));
99 columns.add(new NewMeanLogger("mean.new"));
100 for (int i = 0; i < uniquelyRealizedParameters.getDimension(); i++) {
101 columns.add(new ProbabilitiesLogger("pi.", i));
102 }
103
104 LogColumn[] rtnColumns = new LogColumn[columns.size()];
105
106 return columns.toArray(rtnColumns);
107 }// END: getColumns
108
109 private void getNew() {
110
111 this.categoryProbabilities = getCategoryProbs();
112
113 this.newCategoryIndex = Utils.sample(categoryProbabilities);
114 this.meanForCategory = uniquelyRealizedParameters
115 .getParameterValue(newCategoryIndex);
116
117 //TODO: generalize
118 double sd = precisionParameter.getParameterValue(0);
119
120 // System.out.println("FUBAR:" + sd);
121
122 NormalDistribution nd = new NormalDistribution(meanForCategory, sd);
123 this.newX = (Double) nd.nextRandom();
124
125 }
126
127 private class NewLogger extends NumberColumn {
128
129 public NewLogger(String label) {
130 super(label);
131 }
132
133 @Override
134 public double getDoubleValue() {
135
136 getNew();
137
138 return newX;
139 }// END: getDoubleValue
140
141 }// END: NewLogger class
142
143 private class NewMeanLogger extends NumberColumn {
144
145 public NewMeanLogger(String label) {
146 super(label);
147 }
148
149 @Override
150 public double getDoubleValue() {
151
152 return meanForCategory;
153 }// END: getDoubleValue
154
155 }// END: NewCategoryLogger class
156
157 private class ProbabilitiesLogger extends NumberColumn {
158
159 private int i;
160
161 public ProbabilitiesLogger(String label, int i) {
162 super(label + i);
163 this.i = i;
164 }
165
166 @Override
167 public double getDoubleValue() {
168
169 return categoryProbabilities[i];
170 }// END: getDoubleValue
171
172 }// END: NewCategoryLogger class
173
174 private class NewCategoryLogger extends NumberColumn {
175
176 public NewCategoryLogger(String label) {
177 super(label);
178 }
179
180 @Override
181 public double getDoubleValue() {
182
183 return newCategoryIndex;
184 }// END: getDoubleValue
185
186 }// END: NewCategoryLogger class
187
188 }// END: class