| 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.math.distributions; |
| 27 |
|
| 28 |
import dr.inference.distribution.DistributionLikelihood; |
| 29 |
import dr.inference.model.CompoundLikelihood; |
| 30 |
import dr.inference.model.Likelihood; |
| 31 |
import dr.xml.Reportable; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
import java.util.concurrent.*; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class CompoundGaussianProcess implements GaussianProcessRandomGenerator, Reportable { |
| 42 |
|
| 43 |
private final List<GaussianProcessRandomGenerator> gpList; |
| 44 |
private final List<Integer> copyList; |
| 45 |
private final List<Likelihood> likelihoodList; |
| 46 |
private final CompoundLikelihood compoundLikelihood; |
| 47 |
|
| 48 |
private final ExecutorService pool; |
| 49 |
private final int threadCount; |
| 50 |
private final List<Callable<DrawResult>> callers; |
| 51 |
|
| 52 |
private static final boolean USE_POOL = false; |
| 53 |
|
| 54 |
private int dimension = -1; |
| 55 |
|
| 56 |
public CompoundGaussianProcess(List<GaussianProcessRandomGenerator> gpList, List<Likelihood> likelihoodList, |
| 57 |
List<Integer> copyList) { |
| 58 |
this.gpList = gpList; |
| 59 |
this.copyList = copyList; |
| 60 |
this.likelihoodList = likelihoodList; |
| 61 |
compoundLikelihood = new CompoundLikelihood(likelihoodList); |
| 62 |
|
| 63 |
if (USE_POOL) { |
| 64 |
callers = createTasks(); |
| 65 |
threadCount = callers.size(); |
| 66 |
pool = Executors.newFixedThreadPool(threadCount); |
| 67 |
} else { |
| 68 |
callers = null; |
| 69 |
threadCount = -1; |
| 70 |
pool = null; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public boolean contains(Likelihood likelihood) { |
| 75 |
return likelihoodList.contains(likelihood); |
| 76 |
} |
| 77 |
|
| 78 |
public int getDimension() { |
| 79 |
if (dimension == -1) { |
| 80 |
dimension = 0; |
| 81 |
for (GaussianProcessRandomGenerator gp : gpList) { |
| 82 |
dimension += gp.getDimension(); |
| 83 |
} |
| 84 |
} |
| 85 |
return dimension; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public double[][] getPrecisionMatrix() { |
| 90 |
if (gpList.size() == 1) { |
| 91 |
return gpList.get(0).getPrecisionMatrix(); |
| 92 |
} else { |
| 93 |
final int dim = getDimension(); |
| 94 |
double[][] precision = new double[dim][dim]; |
| 95 |
|
| 96 |
int offset = 0; |
| 97 |
for (GaussianProcessRandomGenerator gp : gpList) { |
| 98 |
final int d = gp.getDimension(); |
| 99 |
double[][] p = gp.getPrecisionMatrix(); |
| 100 |
|
| 101 |
for (int i = 0; i < d; ++i) { |
| 102 |
System.arraycopy(p[i], 0, precision[offset + i], offset, d); |
| 103 |
} |
| 104 |
|
| 105 |
offset += d; |
| 106 |
} |
| 107 |
|
| 108 |
return precision; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public Likelihood getLikelihood() { return compoundLikelihood; } |
| 114 |
|
| 115 |
@Override |
| 116 |
public String getReport() { |
| 117 |
StringBuilder sb = new StringBuilder(); |
| 118 |
sb.append("compoundGP: " + getLikelihood().getLogLikelihood()); |
| 119 |
return sb.toString(); |
| 120 |
} |
| 121 |
|
| 122 |
private class DrawResult { |
| 123 |
final double[] result; |
| 124 |
final int offset; |
| 125 |
|
| 126 |
DrawResult(double[] result, int offset) { |
| 127 |
this.result = result; |
| 128 |
this.offset = offset; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
private class DrawCaller implements Callable<DrawResult> { |
| 133 |
|
| 134 |
public DrawCaller(GaussianProcessRandomGenerator gp, int copies, int offset, boolean isUnivariate) { |
| 135 |
this.gp = gp; |
| 136 |
this.copies = copies; |
| 137 |
this.offset = offset; |
| 138 |
this.isUnivariate = isUnivariate; |
| 139 |
} |
| 140 |
|
| 141 |
public DrawResult call() throws Exception { |
| 142 |
|
| 143 |
final double[] vector; |
| 144 |
if (isUnivariate) { |
| 145 |
vector = new double[copies]; |
| 146 |
for (int i = 0; i < copies; ++i) { |
| 147 |
vector[i] = (Double) gp.nextRandom(); |
| 148 |
} |
| 149 |
} else { |
| 150 |
vector = (double[]) gp.nextRandom(); |
| 151 |
} |
| 152 |
|
| 153 |
return new DrawResult(vector, offset); |
| 154 |
} |
| 155 |
|
| 156 |
private final GaussianProcessRandomGenerator gp; |
| 157 |
private final int copies; |
| 158 |
private final int offset; |
| 159 |
private final boolean isUnivariate; |
| 160 |
} |
| 161 |
|
| 162 |
private List<Callable<DrawResult>> createTasks() { |
| 163 |
|
| 164 |
List<Callable<DrawResult>> callers = new ArrayList<Callable<DrawResult>>(); |
| 165 |
|
| 166 |
int offset = 0; |
| 167 |
int index = 0; |
| 168 |
for (GaussianProcessRandomGenerator gp : gpList) { |
| 169 |
final int copies = copyList.get(index); |
| 170 |
if (likelihoodList.get(index) instanceof DistributionLikelihood) { |
| 171 |
callers.add(new DrawCaller(gp, copies, offset, true)); |
| 172 |
offset += copies; |
| 173 |
} else { |
| 174 |
for (int i = 0; i < copies; ++i) { |
| 175 |
callers.add(new DrawCaller(gp, 1, offset, false)); |
| 176 |
offset += gp.getDimension(); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return callers; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public Object nextRandom() { |
| 186 |
if (USE_POOL) { |
| 187 |
return nextRandomParallel(); |
| 188 |
} else { |
| 189 |
return nextRandomSerial(); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
private Object nextRandomParallel() { |
| 194 |
|
| 195 |
double[] vector = new double[getDimension()]; |
| 196 |
|
| 197 |
try { |
| 198 |
List<Future<DrawResult>> results = pool.invokeAll(callers); |
| 199 |
|
| 200 |
for (Future<DrawResult> result : results) { |
| 201 |
DrawResult dr = result.get(); |
| 202 |
System.arraycopy(dr.result, 0, vector, dr.offset, dr.result.length); |
| 203 |
} |
| 204 |
|
| 205 |
} catch (InterruptedException e) { |
| 206 |
e.printStackTrace(); |
| 207 |
} catch (ExecutionException e) { |
| 208 |
e.printStackTrace(); |
| 209 |
} |
| 210 |
|
| 211 |
return vector; |
| 212 |
} |
| 213 |
|
| 214 |
private Object nextRandomSerial() { |
| 215 |
|
| 216 |
int size = 0; |
| 217 |
List<double[]> randomList = new ArrayList<double[]>(); |
| 218 |
int index = 0; |
| 219 |
for (GaussianProcessRandomGenerator gp : gpList) { |
| 220 |
final int copies = copyList.get(index); |
| 221 |
if (likelihoodList.get(index) instanceof DistributionLikelihood) { |
| 222 |
double[] vector = new double[copies]; |
| 223 |
for (int i = 0; i < copies; ++i) { |
| 224 |
vector[i] = (Double) gp.nextRandom(); |
| 225 |
} |
| 226 |
randomList.add(vector); |
| 227 |
size += vector.length; |
| 228 |
} else { |
| 229 |
for (int i = 0; i < copyList.get(index); ++i) { |
| 230 |
double[] vector = (double[]) gp.nextRandom(); |
| 231 |
randomList.add(vector); |
| 232 |
size += vector.length; |
| 233 |
} |
| 234 |
} |
| 235 |
++index; |
| 236 |
} |
| 237 |
|
| 238 |
double[] result = new double[size]; |
| 239 |
int offset = 0; |
| 240 |
for (double[] vector : randomList) { |
| 241 |
System.arraycopy(vector, 0, result, offset, vector.length); |
| 242 |
offset += vector.length; |
| 243 |
} |
| 244 |
return result; |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public double logPdf(Object x) { |
| 249 |
throw new RuntimeException("Not yet implemented"); |
| 250 |
} |
| 251 |
} |