Debian Codegraph @

src/dr/evomodel/coalescent/operators/SampleNonActiveGibbsOperator.java

raw symbols SCIP
1 /*
2 * SampleNonActiveGibbsOperator.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.coalescent.operators;
27
28 import dr.inference.distribution.ParametricDistributionModel;
29 import dr.inference.model.Parameter;
30 import dr.inference.operators.GibbsOperator;
31 import dr.inference.operators.SimpleMCMCOperator;
32 import dr.math.MathUtils;
33
34 /**
35
36 */
37 public class SampleNonActiveGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
38 private final ParametricDistributionModel distribution;
39 private final Parameter data;
40 private final Parameter indicators;
41
42 public SampleNonActiveGibbsOperator(ParametricDistributionModel distribution,
43 Parameter data, Parameter indicators, double weight) {
44 this.distribution = distribution;
45 this.data = data;
46 this.indicators = indicators;
47 setWeight(weight);
48 }
49
50
51 public String getPerformanceSuggestion() {
52 return null;
53 }
54
55 public String getOperatorName() {
56 return "SampleNonActive(" + indicators.getId() + ")";
57 }
58
59 public double doOperation() {
60 final int idim = indicators.getDimension();
61
62 final int offset = (data.getDimension() - 1) == idim ? 1 : 0;
63 assert offset == 1 || data.getDimension() == idim : "" + idim + " (?+1) != " + data.getDimension();
64
65 // available locations for direct sampling
66 int[] loc = new int[idim];
67 int nLoc = 0;
68
69 for (int i = 0; i < idim; ++i) {
70 final double value = indicators.getStatisticValue(i);
71 if (value == 0) {
72 loc[nLoc] = i + offset;
73 ++nLoc;
74 }
75 }
76
77 if (nLoc > 0) {
78 final int index = loc[MathUtils.nextInt(nLoc)];
79 try {
80 final double val = distribution.quantile(MathUtils.nextDouble());
81 data.setParameterValue(index, val);
82 } catch (Exception e) {
83 // some distributions fail on extreme values - currently gamma
84 return Double.NEGATIVE_INFINITY;
85 }
86 } else {
87 // throw new OperatorFailedException("no non-active indicators");
88 return Double.NEGATIVE_INFINITY;
89 }
90 return 0.0;
91 }
92
93 public int getStepCount() {
94 return 0;
95 }
96 }