Debian Codegraph @

src/dr/inference/model/IndianBuffetProcessPrior.java

raw symbols SCIP
1 /*
2 * IndianBuffetProcessPrior.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.inference.model;
27
28
29 import dr.math.Poisson;
30 import dr.math.distributions.PoissonDistribution;
31 import org.apache.commons.math.special.Beta;
32
33 /**
34 * @author Max Tolkoff
35 */
36 public class IndianBuffetProcessPrior extends AbstractModelLikelihood implements MatrixSizePrior {
37
38 public IndianBuffetProcessPrior(Parameter alpha, Parameter beta, AdaptableSizeFastMatrixParameter data) {
39 super(null);
40 this.alpha=alpha;
41 alpha.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0, 1));
42 addVariable(alpha);
43 this.beta=beta;
44 beta.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0, 1));
45 addVariable(beta);
46 this.data=data;
47 addVariable(data);
48 for (int i = 0; i < data.getRowDimension(); i++) {
49 if (data.getParameterValue(i, 0) != 0)
50 containsNonZeroElements[0] = true;
51 }
52 for (int i = 0; i <data.getColumnDimension() ; i++) {
53 for (int j = 0; j < data.getRowDimension(); j++) {
54 rowCount[i] += Math.abs(data.getParameterValue(j, i));
55 }
56 }
57 ncols = data.getColumnDimension();
58 }
59
60 private int factorial(int num){
61 if(num<0){
62 throw new RuntimeException("Cannot take a negative factorial");
63 }
64 else if(num==0){
65 return 1;
66 }
67 else
68 {
69 int fac=1;
70 for (int i = 0; i <num ; i++) {
71 fac*=(i+1);
72 }
73 return fac;
74 }
75 }
76
77 private double H(){
78 if(!betaKnown) {
79 H = 0;
80 for (int i = 0; i < data.getRowDimension(); i++) {
81 H += beta.getParameterValue(0) / (beta.getParameterValue(0) + i);
82 }
83 }
84 return H;
85 }
86
87 @Override
88 protected void handleModelChangedEvent(Model model, Object object, int index) {
89
90 }
91
92 @Override
93 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
94 if(ncols != data.getColumnDimension()){
95 int sum = 0;
96 for (int i = 0; i < data.getRowDimension(); i++) {
97 sum += data.getParameterValue(i, data.getColumnDimension()-1);
98 }
99 rowCount[data.getColumnDimension() - 1] = sum;
100 ncols = data.getColumnDimension();
101 }
102 else{
103 double value = data.getParameterValue(index);
104 int col = index / data.getRowDimension();
105 if(value == 0.0){
106 rowCount[col] -= 1;
107 if(rowCount[col] == 0){
108 containsNonZeroElements[col] = false;
109 }
110 }
111 else{
112 rowCount[col] += 1;
113 containsNonZeroElements[col] = true;
114 }
115 }
116 likelihoodKnown=false;
117 if(variable==beta)
118 betaKnown=false;
119 if(variable==data)
120 dataKnown=false;
121
122 }
123
124 @Override
125 protected void storeState() {
126 storedBetaKnown=betaKnown;
127 storedContainsNonZeroElements=containsNonZeroElements;
128 storedDataKnown=dataKnown;
129 storedLikelihoodKnown=likelihoodKnown;
130 storedLogLikelihood=logLikelihood;
131 storedRowCount=rowCount;
132 storedKPlus=KPlus;
133 storedH=H;
134 storedBottom=bottom;
135 storedSum2=sum2;
136 storedncols=ncols;
137
138 }
139
140 @Override
141 protected void restoreState() {
142 betaKnown=storedBetaKnown;
143 containsNonZeroElements=storedContainsNonZeroElements;
144 dataKnown=storedDataKnown;
145 likelihoodKnown=storedLikelihoodKnown;
146 logLikelihood=storedLogLikelihood;
147 rowCount=storedRowCount;
148 KPlus=storedKPlus;
149 H=storedH;
150 bottom=storedBottom;
151 sum2=storedSum2;
152 ncols=storedncols;
153 }
154
155 @Override
156 protected void acceptState() {
157
158 }
159
160 @Override
161 public Model getModel() {
162 return this;
163 }
164
165 @Override
166 public double getLogLikelihood() {
167 if(!likelihoodKnown){
168 logLikelihood=calculateLogLikelihood();
169 likelihoodKnown=true;
170 }
171 return logLikelihood;
172 }
173
174 private double calculateLogLikelihood(){
175
176 int sum;
177
178 if(!dataKnown) {
179 bottom=1;
180 boolean[] isExplored= new boolean[data.getColumnDimension()];
181 containsNonZeroElements = new boolean[data.getColumnDimension()];
182 rowCount = new int[data.getColumnDimension()];
183 boolean same;
184 for (int i = 0; i < data.getColumnDimension(); i++) {
185 sum = 1;
186 if (!isExplored[i]) {
187 for (int j = i + 1; j < data.getColumnDimension(); j++) {
188 same = true;
189 if (!isExplored[j]) {
190 for (int k = 0; k < data.getRowDimension(); k++) {
191 if (Math.abs(data.getParameterValue(k, i)) != Math.abs(data.getParameterValue(k, j)))
192 same = false;
193 // if (data.getParameterValue(k, j) != 0) {
194 // containsNonZeroElements[j] = true;
195 // }
196 // rowCount[j]+=data.getParameterValue(k,j);
197 }
198 }
199 if (same && containsNonZeroElements[j]) {
200 isExplored[j] = true;
201 sum += 1;
202 } else if (!containsNonZeroElements[j]) {
203 isExplored[j] = true;
204 }
205 }
206 }
207 bottom *= factorial(sum);
208
209 }
210 }
211
212
213 if(!dataKnown || !betaKnown){
214 sum2=0;
215 KPlus=0;
216 for (int i = 0; i <data.getColumnDimension() ; i++) {
217 if(containsNonZeroElements[i]) {
218 KPlus++;
219 sum2+=Beta.logBeta(rowCount[i], data.getRowDimension() + beta.getParameterValue(0) - rowCount[i]);
220 }
221 }
222 }
223 double p1=KPlus*Math.log(alpha.getParameterValue(0)*beta.getParameterValue(0)/bottom);
224 double p2=-alpha.getParameterValue(0)*H();
225 double p3=sum2;
226 betaKnown=true;
227 dataKnown=true;
228 return p1+p2+p3;
229 }
230
231 @Override
232 public double getSizeLogLikelihood() {
233 PoissonDistribution poisson = new PoissonDistribution(alpha.getParameterValue(0) * H());
234 calculateLogLikelihood();
235 return poisson.logPdf(KPlus) - Math.log(1 - Math.exp(-poisson.mean()));
236 }
237
238 public int[] getRowCount() {
239 return rowCount;
240 }
241
242 public AdaptableSizeFastMatrixParameter getData() {
243 return data;
244 }
245
246 @Override
247 public void makeDirty() {
248 betaKnown=false;
249 dataKnown=false;
250
251 }
252
253 boolean likelihoodKnown;
254 boolean storedLikelihoodKnown;
255 double logLikelihood;
256 double storedLogLikelihood;
257 boolean betaKnown=false;
258 boolean dataKnown=false;
259 boolean storedDataKnown;
260 boolean storedBetaKnown;
261 int[] rowCount;
262 int[] storedRowCount;
263 int KPlus;
264 int storedKPlus;
265 boolean[] containsNonZeroElements;
266 boolean[] storedContainsNonZeroElements;
267 double H;
268 double storedH;
269 int bottom;
270 int storedBottom;
271 double sum2;
272 double storedSum2;
273 int ncols;
274 int storedncols;
275
276 AdaptableSizeFastMatrixParameter data;
277 Parameter alpha;
278 Parameter beta;
279 }