| 1 |
package dr.inference.distribution; |
| 2 |
|
| 3 |
import dr.inference.model.*; |
| 4 |
import dr.math.distributions.PoissonDistribution; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
public class RowDimensionPoissonPrior extends AbstractModelLikelihood implements MatrixSizePrior { |
| 10 |
public RowDimensionPoissonPrior(String id, double untruncatedMean, MatrixParameterInterface parameter, DeterminentalPointProcessPrior DPP, boolean transpose){ |
| 11 |
super(id); |
| 12 |
this.poisson = new PoissonDistribution(untruncatedMean); |
| 13 |
this.parameter = parameter; |
| 14 |
if(parameter != null) |
| 15 |
addVariable(parameter); |
| 16 |
this.transpose = transpose; |
| 17 |
this.DPP = DPP; |
| 18 |
if(parameter != null && !(parameter instanceof AdaptableSizeFastMatrixParameter)){ |
| 19 |
if(!transpose){ |
| 20 |
count = new int[parameter.getColumnDimension()]; |
| 21 |
storedCount = new int[parameter.getColumnDimension()]; |
| 22 |
for (int i = 0; i < count.length; i++) { |
| 23 |
for (int j = 0; j < parameter.getRowDimension(); j++) { |
| 24 |
count[i] += parameter.getParameterValue(j, i); |
| 25 |
} |
| 26 |
|
| 27 |
} |
| 28 |
} |
| 29 |
else { |
| 30 |
count = new int[parameter.getRowDimension()]; |
| 31 |
storedCount = new int[parameter.getRowDimension()]; |
| 32 |
for (int i = 0; i < count.length; i++) { |
| 33 |
for (int j = 0; j < parameter.getColumnDimension(); j++) { |
| 34 |
count[i] += parameter.getParameterValue(i, j); |
| 35 |
} |
| 36 |
|
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected void storeState(){ |
| 49 |
if(count != null) |
| 50 |
System.arraycopy(count, 0, storedCount, 0, count.length); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
protected void restoreState() { |
| 55 |
int[] temp = storedCount; |
| 56 |
storedCount = count; |
| 57 |
count = temp; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected void acceptState() { |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public Model getModel() { |
| 102 |
return this; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public double getLogLikelihood() { |
| 107 |
|
| 108 |
if (DPP != null) |
| 109 |
return poisson.logPdf(DPP.getSum()) - Math.log(1 - Math.exp(-poisson.mean())); |
| 110 |
if(count != null){ |
| 111 |
if(parameter != null && !(parameter instanceof AdaptableSizeFastMatrixParameter)){ |
| 112 |
if(!transpose){ |
| 113 |
count = new int[parameter.getColumnDimension()]; |
| 114 |
storedCount = new int[parameter.getColumnDimension()]; |
| 115 |
for (int i = 0; i < count.length; i++) { |
| 116 |
for (int j = 0; j < parameter.getRowDimension(); j++) { |
| 117 |
count[i] += parameter.getParameterValue(j, i); |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
} |
| 122 |
else { |
| 123 |
count = new int[parameter.getRowDimension()]; |
| 124 |
storedCount = new int[parameter.getRowDimension()]; |
| 125 |
for (int i = 0; i < count.length; i++) { |
| 126 |
for (int j = 0; j < parameter.getColumnDimension(); j++) { |
| 127 |
count[i] += parameter.getParameterValue(i, j); |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
double nonZero = 0; |
| 135 |
for (int i = 0; i < count.length; i++) { |
| 136 |
if(count[i] != 0){ |
| 137 |
nonZero ++; |
| 138 |
} |
| 139 |
if(count[i] < 0) |
| 140 |
throw new RuntimeException("Less than 0 elements are 0. Check for an error"); |
| 141 |
} |
| 142 |
return poisson.logPdf(nonZero) - Math.log(1 - Math.exp(-poisson.mean())); |
| 143 |
} |
| 144 |
if(!transpose){ |
| 145 |
return poisson.logPdf(parameter.getRowDimension()) - Math.log(1 - Math.exp(-poisson.mean())); |
| 146 |
} |
| 147 |
else |
| 148 |
return poisson.logPdf(parameter.getColumnDimension()) - Math.log(1 - Math.exp(-poisson.mean())); |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public void makeDirty() { |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public double getSizeLogLikelihood() { |
| 158 |
return getLogLikelihood(); |
| 159 |
} |
| 160 |
|
| 161 |
PoissonDistribution poisson; |
| 162 |
MatrixParameterInterface parameter; |
| 163 |
boolean transpose; |
| 164 |
DeterminentalPointProcessPrior DPP; |
| 165 |
int[] count; |
| 166 |
int[] storedCount; |
| 167 |
} |