| 1 |
package dr.inference.distribution; |
| 2 |
|
| 3 |
import dr.inference.model.*; |
| 4 |
import dr.math.MathUtils; |
| 5 |
import dr.math.matrixAlgebra.CholeskyDecomposition; |
| 6 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 7 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 8 |
|
| 9 |
import java.util.List; |
| 10 |
import java.util.Vector; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class DeterminentalPointProcessPrior extends AbstractModelLikelihood{ |
| 16 |
|
| 17 |
double theta; |
| 18 |
MatrixParameterInterface data; |
| 19 |
boolean likelihoodKnown = false; |
| 20 |
boolean storedLikelihoodKnown; |
| 21 |
double logLikelihood; |
| 22 |
double storedLogLikelihood; |
| 23 |
double[][] relationshipList; |
| 24 |
double[][] storedRelationshipList; |
| 25 |
boolean[] notZero; |
| 26 |
boolean[] storedNotZero; |
| 27 |
int size; |
| 28 |
int storedSize; |
| 29 |
Vector<Integer> changedList; |
| 30 |
Vector<Integer> storedChangedList; |
| 31 |
Parameter normalizingConstants; |
| 32 |
boolean pathSampling; |
| 33 |
int sum; |
| 34 |
int storedSum; |
| 35 |
|
| 36 |
public DeterminentalPointProcessPrior(String name, double theta, MatrixParameterInterface data, Parameter normalizingConstants, boolean noZeros, boolean pathSampling, boolean resetData) { |
| 37 |
super(name); |
| 38 |
this.normalizingConstants = normalizingConstants; |
| 39 |
this.theta = theta; |
| 40 |
this.data = data; |
| 41 |
addVariable(data); |
| 42 |
relationshipList = new double[data.getColumnDimension()][data.getColumnDimension()]; |
| 43 |
storedRelationshipList = new double[data.getColumnDimension()][data.getColumnDimension()]; |
| 44 |
size = data.getColumnDimension(); |
| 45 |
if(resetData){ |
| 46 |
for (int i = 0; i < data.getRowDimension(); i++) { |
| 47 |
for (int j = 0; j < data.getColumnDimension(); j++) { |
| 48 |
if(i % (j+1) == 0) { |
| 49 |
data.setParameterValueQuietly(i, j, 0); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
if(noZeros){ |
| 55 |
notZero = new boolean[data.getColumnDimension()]; |
| 56 |
storedNotZero = new boolean[data.getColumnDimension()]; |
| 57 |
} |
| 58 |
reset(); |
| 59 |
this.pathSampling = pathSampling; |
| 60 |
changedList = new Vector<Integer>(); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
protected void storeState() { |
| 70 |
storedLikelihoodKnown = likelihoodKnown; |
| 71 |
storedLogLikelihood = logLikelihood; |
| 72 |
storedSize = size; |
| 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 |
|
| 101 |
storedChangedList = (Vector<Integer>) changedList.clone(); |
| 102 |
storedSum = sum; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
protected void restoreState() { |
| 108 |
likelihoodKnown = storedLikelihoodKnown; |
| 109 |
logLikelihood = storedLogLikelihood; |
| 110 |
double[][] relationshipListTemp = relationshipList; |
| 111 |
relationshipList = storedRelationshipList; |
| 112 |
storedRelationshipList = relationshipListTemp; |
| 113 |
size = storedSize; |
| 114 |
Vector<Integer> changedListTemp = changedList; |
| 115 |
changedList = storedChangedList; |
| 116 |
storedChangedList = changedListTemp; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
sum = storedSum; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
protected void acceptState() { |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 130 |
likelihoodKnown = false; |
| 131 |
if(index == -1) |
| 132 |
reset(); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public Model getModel() { |
| 140 |
return this; |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public double getLogLikelihood() { |
| 145 |
makeDirty(); |
| 146 |
if(!likelihoodKnown) { |
| 147 |
logLikelihood = computeLogLikelihood(); |
| 148 |
if(normalizingConstants != null) { |
| 149 |
if(sum != 0) |
| 150 |
logLikelihood += normalizingConstants.getParameterValue(sum - 1); |
| 151 |
else |
| 152 |
logLikelihood += normalizingConstants.getParameterValue(0); |
| 153 |
} |
| 154 |
likelihoodKnown = true; |
| 155 |
} |
| 156 |
return logLikelihood; |
| 157 |
} |
| 158 |
|
| 159 |
public double computeLogLikelihood(){ |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if(pathSampling && notZero != null && sum != data.getColumnDimension()){ |
| 188 |
return Double.NEGATIVE_INFINITY; |
| 189 |
} |
| 190 |
|
| 191 |
CholeskyDecomposition chol = null; |
| 192 |
try { |
| 193 |
chol = new CholeskyDecomposition(relationshipList); |
| 194 |
} catch (IllegalDimension illegalDimension) { |
| 195 |
illegalDimension.printStackTrace(); |
| 196 |
} |
| 197 |
double product = 0; |
| 198 |
for (int i = 0; i <relationshipList.length ; i++) { |
| 199 |
product += Math.log(chol.getL()[i][i]); |
| 200 |
} |
| 201 |
product *= 2; |
| 202 |
return product; |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void makeDirty() { |
| 207 |
likelihoodKnown = false; |
| 208 |
changedList = new Vector<Integer>(); |
| 209 |
|
| 210 |
reset(); |
| 211 |
} |
| 212 |
|
| 213 |
private void findZeros(){ |
| 214 |
sum = 0; |
| 215 |
notZero = new boolean[data.getColumnDimension()]; |
| 216 |
for (int i = 0; i < data.getColumnDimension(); i++) { |
| 217 |
for (int j = 0; j < data.getRowDimension(); j++) { |
| 218 |
if(data.getParameterValue(j, i) == 1) |
| 219 |
{notZero[i] = true; |
| 220 |
sum++; |
| 221 |
break;} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
public int getSum(){ |
| 228 |
findZeros(); |
| 229 |
return sum; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
public void reset(){ |
| 234 |
if(notZero != null){ |
| 235 |
findZeros(); |
| 236 |
if(relationshipList.length != sum && sum != 0){ |
| 237 |
relationshipList = new double[sum][sum]; |
| 238 |
} |
| 239 |
else if(sum == 0) |
| 240 |
relationshipList = new double[1][1]; |
| 241 |
} |
| 242 |
else if(relationshipList.length != data.getColumnDimension() || relationshipList == null){ |
| 243 |
relationshipList = new double[data.getColumnDimension()][data.getColumnDimension()]; |
| 244 |
} |
| 245 |
int offset1 = 0; |
| 246 |
for (int i = 0; i < relationshipList.length; i++) { |
| 247 |
int offset2 = 0; |
| 248 |
if((notZero != null && notZero[i] == true) || notZero == null){ |
| 249 |
for (int j = 0; j < i; j++) { |
| 250 |
if((notZero !=null && notZero[j] == true) || notZero == null){ |
| 251 |
int count = 0; |
| 252 |
for (int k = 0; k < data.getRowDimension(); k++) { |
| 253 |
count += Math.abs(data.getParameterValue(k, i) - data.getParameterValue(k, j)); |
| 254 |
} |
| 255 |
relationshipList[i + offset1][j + offset2] = Math.exp(- count / (theta * theta)); |
| 256 |
relationshipList[j + offset2][i + offset1] = relationshipList[i + offset1][j + offset2]; |
| 257 |
} |
| 258 |
else |
| 259 |
offset2 --; |
| 260 |
} |
| 261 |
} |
| 262 |
else |
| 263 |
offset1 --; |
| 264 |
} |
| 265 |
for (int i = 0; i <relationshipList.length ; i++) { |
| 266 |
relationshipList[i][i] = 1; |
| 267 |
} |
| 268 |
} |
| 269 |
} |