| 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.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 |
|
| 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 |
} |
| 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 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
for (int i = 0; i < uniqueRealizationCount; i++) { |
| 81 |
probs[i] = probs[i] / realizationCount; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
return probs; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public LogColumn[] getColumns() { |
| 92 |
|
| 93 |
|
| 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 |
} |
| 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 |
|
| 118 |
double sd = precisionParameter.getParameterValue(0); |
| 119 |
|
| 120 |
|
| 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 |
} |
| 140 |
|
| 141 |
} |
| 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 |
} |
| 154 |
|
| 155 |
} |
| 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 |
} |
| 171 |
|
| 172 |
} |
| 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 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
} |