| 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.inference.multidimensionalscaling; |
| 27 |
|
| 28 |
import dr.math.distributions.NormalDistribution; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class MultiDimensionalScalingCoreImpl implements MultiDimensionalScalingCore { |
| 45 |
|
| 46 |
@Override |
| 47 |
public void initialize(int embeddingDimension, int locationCount, long flags) { |
| 48 |
this.embeddingDimension = embeddingDimension; |
| 49 |
this.locationCount = locationCount; |
| 50 |
this.observationCount = (locationCount * (locationCount - 1)) / 2; |
| 51 |
|
| 52 |
observations = new double[locationCount][locationCount]; |
| 53 |
increments = new double[locationCount][locationCount]; |
| 54 |
storedIncrements = null; |
| 55 |
incrementsKnown = false; |
| 56 |
sumOfIncrementsKnown = false; |
| 57 |
|
| 58 |
isLeftTruncated = (flags & MultiDimensionalScalingCore.LEFT_TRUNCATION) != 0; |
| 59 |
|
| 60 |
updatedLocation = -1; |
| 61 |
|
| 62 |
locations = new double[locationCount][embeddingDimension]; |
| 63 |
storedLocations = new double[locationCount][embeddingDimension]; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void setPairwiseData(double[] observations) { |
| 68 |
if (observations.length != (locationCount * locationCount)) { |
| 69 |
throw new RuntimeException("Observation data is not the correct dimension"); |
| 70 |
} |
| 71 |
|
| 72 |
int k = 0; |
| 73 |
for (int i = 0; i < locationCount; i++) { |
| 74 |
System.arraycopy(observations, k, this.observations[i], 0, locationCount); |
| 75 |
k += locationCount; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public double[] getPairwiseData() { |
| 81 |
double[] data = new double[locationCount * locationCount]; |
| 82 |
int k = 0; |
| 83 |
for (int i = 0; i < locationCount; ++i) { |
| 84 |
System.arraycopy(observations[i], 0, data, k, locationCount); |
| 85 |
k += locationCount; |
| 86 |
} |
| 87 |
return data; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void setParameters(double[] parameters) { |
| 92 |
precision = parameters[0]; |
| 93 |
|
| 94 |
|
| 95 |
if (isLeftTruncated) { |
| 96 |
incrementsKnown = false; |
| 97 |
sumOfIncrementsKnown = false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void updateLocation(int locationIndex, double[] location) { |
| 103 |
if (updatedLocation != -1 || locationIndex == -1) { |
| 104 |
|
| 105 |
incrementsKnown = false; |
| 106 |
storedIncrements = null; |
| 107 |
} |
| 108 |
|
| 109 |
if (locationIndex != -1) { |
| 110 |
updatedLocation = locationIndex; |
| 111 |
|
| 112 |
if (location.length != embeddingDimension) { |
| 113 |
throw new RuntimeException("Location is not the correct dimension"); |
| 114 |
} |
| 115 |
|
| 116 |
System.arraycopy(location, 0, locations[locationIndex], 0, embeddingDimension); |
| 117 |
|
| 118 |
} else { |
| 119 |
if (location.length != embeddingDimension * locationCount) { |
| 120 |
throw new RuntimeException("Location is the not correct dimension"); |
| 121 |
} |
| 122 |
|
| 123 |
int offset = 0; |
| 124 |
for (int i = 0; i < locationCount; ++i) { |
| 125 |
System.arraycopy(location, offset, locations[i], 0, embeddingDimension); |
| 126 |
offset += embeddingDimension; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
sumOfIncrementsKnown = false; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public double calculateLogLikelihood() { |
| 135 |
if (!sumOfIncrementsKnown) { |
| 136 |
|
| 137 |
if (!incrementsKnown) { |
| 138 |
computeSumOfSquaredResiduals(); |
| 139 |
} else { |
| 140 |
updateSumOfSquaredResiduals(); |
| 141 |
} |
| 142 |
sumOfIncrementsKnown = true; |
| 143 |
} |
| 144 |
|
| 145 |
double logLikelihood = 0.5 * (Math.log(precision) - Math.log(2 * Math.PI)) * observationCount; |
| 146 |
|
| 147 |
if (isLeftTruncated) { |
| 148 |
logLikelihood -= sumOfIncrements; |
| 149 |
} else { |
| 150 |
logLikelihood -= 0.5 * precision * sumOfIncrements; |
| 151 |
} |
| 152 |
|
| 153 |
return logLikelihood; |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void storeState() { |
| 158 |
|
| 159 |
storedSumOfIncrements = sumOfIncrements; |
| 160 |
storedIncrements = null; |
| 161 |
|
| 162 |
|
| 163 |
for (int i = 0; i < locationCount; i++) { |
| 164 |
System.arraycopy(locations[i], 0 , storedLocations[i], 0, embeddingDimension); |
| 165 |
} |
| 166 |
updatedLocation = -1; |
| 167 |
|
| 168 |
|
| 169 |
storedPrecision = precision; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void restoreState() { |
| 174 |
|
| 175 |
sumOfIncrements = storedSumOfIncrements; |
| 176 |
sumOfIncrementsKnown = true; |
| 177 |
|
| 178 |
if (storedIncrements != null) { |
| 179 |
System.arraycopy(storedIncrements, 0 , increments[updatedLocation], 0, locationCount); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
incrementsKnown = true; |
| 184 |
} else { |
| 185 |
incrementsKnown = false; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
double[][] tmp1 = storedLocations; |
| 190 |
storedLocations = locations; |
| 191 |
locations = tmp1; |
| 192 |
|
| 193 |
|
| 194 |
precision = storedPrecision; |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public void acceptState() { |
| 199 |
if (storedIncrements != null) { |
| 200 |
for (int j = 0; j < locationCount; ++j) { |
| 201 |
increments[j][updatedLocation] = increments[updatedLocation][j]; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public void getGradient(double[] location) { |
| 208 |
throw new RuntimeException("Not yet implemented."); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public void makeDirty() { |
| 213 |
sumOfIncrementsKnown = false; |
| 214 |
incrementsKnown = false; |
| 215 |
} |
| 216 |
|
| 217 |
private void computeSumOfSquaredResiduals() { |
| 218 |
|
| 219 |
final double oneOverSd = Math.sqrt(precision); |
| 220 |
final double scale = 0.5 * precision; |
| 221 |
|
| 222 |
|
| 223 |
sumOfIncrements = 0.0; |
| 224 |
for (int i = 0; i < locationCount; i++) { |
| 225 |
|
| 226 |
for (int j = 0; j < locationCount; j++) { |
| 227 |
double distance = calculateDistance(locations[i], locations[j]); |
| 228 |
double residual = distance - observations[i][j]; |
| 229 |
double increment = residual * residual; |
| 230 |
if (isLeftTruncated) { |
| 231 |
increment = scale * increment; |
| 232 |
if (i != j) { |
| 233 |
increment += computeTruncation(distance, oneOverSd); |
| 234 |
|
| 235 |
} |
| 236 |
} |
| 237 |
increments[i][j] = increment; |
| 238 |
|
| 239 |
sumOfIncrements += increment; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
sumOfIncrements /= 2; |
| 244 |
|
| 245 |
incrementsKnown = true; |
| 246 |
sumOfIncrementsKnown = true; |
| 247 |
} |
| 248 |
|
| 249 |
private void updateSumOfSquaredResiduals() { |
| 250 |
|
| 251 |
final double oneOverSd = Math.sqrt(precision); |
| 252 |
final double scale = 0.5 * precision; |
| 253 |
|
| 254 |
double delta = 0.0; |
| 255 |
|
| 256 |
final int i = updatedLocation; |
| 257 |
|
| 258 |
storedIncrements = new double[locationCount]; |
| 259 |
System.arraycopy(increments[i], 0, storedIncrements, 0, locationCount); |
| 260 |
|
| 261 |
for (int j = 0; j < locationCount; j++) { |
| 262 |
double distance = calculateDistance(locations[i], locations[j]); |
| 263 |
double residual = distance - observations[i][j]; |
| 264 |
double increment = residual * residual; |
| 265 |
|
| 266 |
if (isLeftTruncated) { |
| 267 |
increment = scale * increment; |
| 268 |
if (i != j) { |
| 269 |
increment += computeTruncation(distance, oneOverSd); |
| 270 |
|
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
delta += increment - increments[i][j]; |
| 275 |
increments[i][j] = increment; |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
sumOfIncrements += delta; |
| 280 |
} |
| 281 |
|
| 282 |
private double calculateDistance(double[] X, double[] Y) { |
| 283 |
double sum = 0.0; |
| 284 |
for (int i = 0; i < embeddingDimension; i++) { |
| 285 |
double difference = X[i] - Y[i]; |
| 286 |
sum += difference * difference; |
| 287 |
} |
| 288 |
return Math.sqrt(sum); |
| 289 |
} |
| 290 |
|
| 291 |
private double computeTruncation(double mean, double oneOverSd) { |
| 292 |
return NormalDistribution.standardCDF(mean * oneOverSd, true); |
| 293 |
} |
| 294 |
|
| 295 |
private int embeddingDimension; |
| 296 |
private boolean isLeftTruncated = false; |
| 297 |
private int locationCount; |
| 298 |
private int observationCount; |
| 299 |
private double precision; |
| 300 |
private double storedPrecision; |
| 301 |
|
| 302 |
private int updatedLocation = -1; |
| 303 |
|
| 304 |
private double[][] observations; |
| 305 |
private double[][] locations; |
| 306 |
private double[][] storedLocations; |
| 307 |
|
| 308 |
private boolean incrementsKnown = false; |
| 309 |
|
| 310 |
private boolean sumOfIncrementsKnown = false; |
| 311 |
private double[][] increments; |
| 312 |
|
| 313 |
private double[] storedIncrements; |
| 314 |
|
| 315 |
private double sumOfIncrements; |
| 316 |
private double storedSumOfIncrements; |
| 317 |
|
| 318 |
} |