| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 84 |
return Double.NEGATIVE_INFINITY; |
| 85 |
} |
| 86 |
} else { |
| 87 |
|
| 88 |
return Double.NEGATIVE_INFINITY; |
| 89 |
} |
| 90 |
return 0.0; |
| 91 |
} |
| 92 |
|
| 93 |
public int getStepCount() { |
| 94 |
return 0; |
| 95 |
} |
| 96 |
} |