Debian Codegraph @

src/dr/math/matrixAlgebra/missingData/MissingOps.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package dr.math.matrixAlgebra.missingData;
2
3 import dr.math.matrixAlgebra.Vector;
4 import dr.math.matrixAlgebra.WrappedVector;
5 import dr.util.Transform;
6 import org.ejml.alg.dense.decomposition.lu.LUDecompositionAlt_D64;
7 import org.ejml.alg.dense.decomposition.qr.QRColPivDecompositionHouseholderColumn_D64;
8 import org.ejml.alg.dense.linsol.lu.LinearSolverLu_D64;
9 import org.ejml.alg.dense.misc.UnrolledDeterminantFromMinor;
10 import org.ejml.alg.dense.misc.UnrolledInverseFromMinor;
11 import org.ejml.data.DenseMatrix64F;
12 import org.ejml.factory.LinearSolverFactory;
13 import org.ejml.interfaces.decomposition.QRPDecomposition;
14 import org.ejml.interfaces.decomposition.SingularValueDecomposition;
15 import org.ejml.interfaces.linsol.LinearSolver;
16 import org.ejml.ops.CommonOps;
17
18 import java.util.Arrays;
19
20 import static dr.math.matrixAlgebra.missingData.InversionResult.Code.FULLY_OBSERVED;
21 import static dr.math.matrixAlgebra.missingData.InversionResult.Code.NOT_OBSERVED;
22 import static dr.math.matrixAlgebra.missingData.InversionResult.Code.PARTIALLY_OBSERVED;
23
24 /**
25 * @author Marc A. Suchard
26 */
27 public class MissingOps {
28
29 public static DenseMatrix64F wrap(final double[] source, final int offset,
30 final int numRows, final int numCols) {
31 double[] buffer = new double[numRows * numCols];
32 return wrap(source, offset, numRows, numCols, buffer);
33 }
34
35 public static DenseMatrix64F wrap(final double[] source, final int offset,
36 final int numRows, final int numCols,
37 final double[] buffer) {
38 System.arraycopy(source, offset, buffer, 0, numRows * numCols);
39 return DenseMatrix64F.wrap(numRows, numCols, buffer);
40 }
41
42
43 public static void gatherRowsAndColumns(final DenseMatrix64F source, final DenseMatrix64F destination,
44 final int[] rowIndices, final int[] colIndices) {
45
46 final int rowLength = rowIndices.length;
47 final int colLength = colIndices.length;
48 final double[] out = destination.getData();
49
50 int index = 0;
51 for (int i = 0; i < rowLength; ++i) {
52 final int rowIndex = rowIndices[i];
53 for (int j = 0; j < colLength; ++j) {
54 out[index] = source.unsafe_get(rowIndex, colIndices[j]);
55 ++index;
56 }
57 }
58 }
59
60 public static void scatterRowsAndColumns(final DenseMatrix64F source, final DenseMatrix64F destination,
61 final int[] rowIdices, final int[] colIndices, final boolean clear) {
62 if (clear) {
63 Arrays.fill(destination.getData(), 0.0);
64 }
65
66 final int rowLength = rowIdices.length;
67 final int colLength = colIndices.length;
68 final double[] in = source.getData();
69
70 int index = 0;
71 for (int i = 0; i < rowLength; ++i) {
72 final int rowIndex = rowIdices[i];
73 for (int j = 0; j < colLength; ++j) {
74 destination.unsafe_set(rowIndex, colIndices[j], in[index]);
75 ++index;
76 }
77 }
78 }
79
80 public static void unwrap(final DenseMatrix64F source, final double[] destination, final int offset) {
81 System.arraycopy(source.getData(), 0, destination, offset, source.getNumElements());
82 }
83
84 public static boolean anyDiagonalInfinities(DenseMatrix64F source) {
85 boolean anyInfinities = false;
86 for (int i = 0; i < source.getNumCols() && !anyInfinities; ++i) {
87 if (Double.isInfinite(source.unsafe_get(i, i))) {
88 anyInfinities = true;
89 }
90 }
91 return anyInfinities;
92 }
93
94 public static boolean allFiniteDiagonals(DenseMatrix64F source) {
95 boolean allFinite = true;
96
97 final int length = source.getNumCols();
98 for (int i = 0; i < length; ++i) {
99 allFinite &= !Double.isInfinite(source.unsafe_get(i, i));
100 }
101 return allFinite;
102 }
103
104 public static int countFiniteDiagonals(DenseMatrix64F source) {
105 final int length = source.getNumCols();
106
107 int count = 0;
108 for (int i = 0; i < length; ++i) {
109 final double d = source.unsafe_get(i, i);
110 if (!Double.isInfinite(d)) {
111 ++count;
112 }
113 }
114 return count;
115 }
116
117 public static int countZeroDiagonals(DenseMatrix64F source) {
118 final int length = source.getNumCols();
119
120 int count = 0;
121 for (int i = 0; i < length; ++i) {
122 final double d = source.unsafe_get(i, i);
123 if (d == 0.0) {
124 ++count;
125 }
126 }
127 return count;
128 }
129
130 public static void getFiniteDiagonalIndices(final DenseMatrix64F source, final int[] indices) {
131 final int length = source.getNumCols();
132
133 int index = 0;
134 for (int i = 0; i < length; ++i) {
135 final double d = source.unsafe_get(i, i);
136 if (!Double.isInfinite(d)) {
137 indices[index] = i;
138 ++index;
139 }
140 }
141 }
142
143 public static int countFiniteNonZeroDiagonals(DenseMatrix64F source) {
144 final int length = source.getNumCols();
145
146 int count = 0;
147 for (int i = 0; i < length; ++i) {
148 final double d = source.unsafe_get(i, i);
149 if (!Double.isInfinite(d) && d != 0.0) {
150 ++count;
151 }
152 }
153 return count;
154 }
155
156 public static void getFiniteNonZeroDiagonalIndices(final DenseMatrix64F source, final int[] indices) {
157 final int length = source.getNumCols();
158
159 int index = 0;
160 for (int i = 0; i < length; ++i) {
161 final double d = source.unsafe_get(i, i);
162 if (!Double.isInfinite(d) && d != 0.0) {
163 indices[index] = i;
164 ++index;
165 }
166 }
167 }
168
169 public static void addToDiagonal(DenseMatrix64F source, double increment) {
170 final int width = source.getNumRows();
171 for (int i = 0; i < width; ++i) {
172 source.unsafe_set(i,i, source.unsafe_get(i, i) + increment);
173 }
174 }
175
176 public static double det(DenseMatrix64F mat) {
177 int numCol = mat.getNumCols();
178 int numRow = mat.getNumRows();
179 if(numCol != numRow) {
180 throw new IllegalArgumentException("Must be a square matrix.");
181 } else if(numCol <= 6) {
182 return numCol >= 2? UnrolledDeterminantFromMinor.det(mat):mat.get(0);
183 } else {
184 LUDecompositionAlt_D64 alg = new LUDecompositionAlt_D64();
185 if(alg.inputModified()) {
186 mat = mat.copy();
187 }
188
189 return !alg.decompose(mat)?0.0D:alg.computeDeterminant().real;
190 }
191 }
192
193 public static double invertAndGetDeterminant(DenseMatrix64F mat, DenseMatrix64F result) {
194
195 final int numCol = mat.getNumCols();
196 final int numRow = mat.getNumRows();
197 if (numCol != numRow) {
198 throw new IllegalArgumentException("Must be a square matrix.");
199 }
200
201 if (numCol <= 5) {
202
203 if (numCol >= 2) {
204 UnrolledInverseFromMinor.inv(mat, result);
205 } else {
206 result.set(0, 1.0D / mat.get(0));
207 }
208
209 return numCol >= 2 ?
210 UnrolledDeterminantFromMinor.det(mat) :
211 mat.get(0);
212 } else {
213
214 LUDecompositionAlt_D64 alg = new LUDecompositionAlt_D64();
215 LinearSolverLu_D64 solver = new LinearSolverLu_D64(alg);
216 if (solver.modifiesA()) {
217 mat = mat.copy();
218 }
219
220 if (!solver.setA(mat)) {
221 return Double.NaN;
222 }
223
224 solver.invert(result);
225
226 return alg.computeDeterminant().real;
227
228 }
229 }
230
231 public static InversionResult safeDeterminant(DenseMatrix64F source, boolean invert) {
232 final int finiteCount = countFiniteNonZeroDiagonals(source);
233
234 InversionResult result;
235
236 if (finiteCount == 0) {
237 result = new InversionResult(NOT_OBSERVED, 0, 0);
238 } else {
239 LinearSolver<DenseMatrix64F> solver = LinearSolverFactory.pseudoInverse(true);
240 solver.setA(source);
241
242 SingularValueDecomposition<DenseMatrix64F> svd = solver.getDecomposition();
243 double[] values = svd.getSingularValues();
244
245
246 if (values == null) {
247 throw new RuntimeException("Unable to perform SVD");
248 }
249
250 int dim = 0;
251 double det = 1;
252 for (int i = 0; i < values.length; ++i) {
253 final double lambda = values[i];
254 if (lambda != 0.0) {
255 det *= lambda;
256 ++dim;
257 }
258 }
259
260 if (invert) {
261 det = 1.0 / det;
262 }
263
264 result = new InversionResult(dim == source.getNumCols() ? FULLY_OBSERVED : PARTIALLY_OBSERVED, dim, det);
265 }
266
267 return result;
268 }
269
270 public static InversionResult safeSolve(DenseMatrix64F A,
271 WrappedVector b,
272 WrappedVector x,
273 boolean getDeterminat) {
274 final int dim = b.getDim();
275
276 assert(A.getNumRows() == dim && A.getNumCols() == dim);
277
278 final DenseMatrix64F B = wrap(b.getBuffer(), b.getOffset(), dim, 1);
279 final DenseMatrix64F X = new DenseMatrix64F(dim, 1);
280
281 InversionResult ir = safeSolve(A, B, X, getDeterminat);
282
283
284 for (int row = 0; row < dim; ++row) {
285 x.set(row, X.unsafe_get(row, 0));
286 }
287
288 return ir;
289 }
290
291 public static InversionResult safeSolve(DenseMatrix64F A, DenseMatrix64F B, DenseMatrix64F X, boolean getDeterminant) {
292
293 final int finiteCount = countFiniteNonZeroDiagonals(A);
294
295 InversionResult result;
296 if (finiteCount == 0) {
297 Arrays.fill(X.getData(), 0);
298 result = new InversionResult(NOT_OBSERVED, 0, 0);
299 } else {
300
301 LinearSolver<DenseMatrix64F> solver = LinearSolverFactory.pseudoInverse(true);
302 solver.setA(A);
303 solver.solve(B, X);
304
305 int dim = 0;
306 double det = 1;
307
308 if (getDeterminant) {
309 SingularValueDecomposition<DenseMatrix64F> svd = solver.getDecomposition();
310 double[] values = svd.getSingularValues();
311
312 for (int i = 0; i < values.length; ++i) {
313 final double lambda = values[i];
314 if (lambda != 0.0) {
315 det *= lambda;
316 ++dim;
317 }
318 }
319 }
320
321 result = new InversionResult(dim == A.getNumCols() ? FULLY_OBSERVED : PARTIALLY_OBSERVED, dim, 1 / det);
322 }
323
324 return result;
325 }
326
327
328 public static InversionResult safeInvert(DenseMatrix64F source, DenseMatrix64F destination, boolean getDeterminant) {
329
330 final int dim = source.getNumCols();
331 final int finiteCount = countFiniteNonZeroDiagonals(source);
332 double det = 0;
333
334 if (finiteCount == dim) {
335 if (getDeterminant) {
336 det = invertAndGetDeterminant(source, destination);
337 } else {
338 CommonOps.invert(source, destination);
339 }
340 return new InversionResult(FULLY_OBSERVED, dim, det);
341 } else {
342 if (finiteCount == 0) {
343 Arrays.fill(destination.getData(), 0);
344 return new InversionResult(NOT_OBSERVED, 0, 0);
345 } else {
346 final int[] finiteIndices = new int[finiteCount];
347 getFiniteNonZeroDiagonalIndices(source, finiteIndices);
348
349 final DenseMatrix64F subSource = new DenseMatrix64F(finiteCount, finiteCount);
350 gatherRowsAndColumns(source, subSource, finiteIndices, finiteIndices);
351
352 final DenseMatrix64F inverseSubSource = new DenseMatrix64F(finiteCount, finiteCount);
353 if (getDeterminant) {
354 det = invertAndGetDeterminant(subSource, inverseSubSource);
355 } else {
356 CommonOps.invert(subSource, inverseSubSource);
357 }
358
359 scatterRowsAndColumns(inverseSubSource, destination, finiteIndices, finiteIndices, true);
360
361 return new InversionResult(PARTIALLY_OBSERVED, finiteCount, det);
362 }
363 }
364 }
365
366 public static void weightedAverage(final WrappedVector mi,
367 final DenseMatrix64F Pi,
368 final WrappedVector mj,
369 final DenseMatrix64F Pj,
370 final WrappedVector mk,
371 final DenseMatrix64F Vk,
372 final int dimTrait) {
373 final double[] tmp = new double[dimTrait];
374 for (int g = 0; g < dimTrait; ++g) {
375 double sum = 0.0;
376 for (int h = 0; h < dimTrait; ++h) {
377 sum += Pi.unsafe_get(g, h) * mi.get(h);
378 sum += Pj.unsafe_get(g, h) * mj.get(h);
379 }
380 tmp[g] = sum;
381 }
382 for (int g = 0; g < dimTrait; ++g) {
383 double sum = 0.0;
384 for (int h = 0; h < dimTrait; ++h) {
385 sum += Vk.unsafe_get(g, h) * tmp[h];
386 }
387 mk.set(g, sum);
388 }
389 }
390
391 public static void weightedAverage(final double[] ipartial,
392 final int ibo,
393 final DenseMatrix64F Pi,
394 final double[] jpartial,
395 final int jbo,
396 final DenseMatrix64F Pj,
397 final double[] kpartial,
398 final int kbo,
399 final DenseMatrix64F Vk,
400 final int dimTrait) {
401 final double[] tmp = new double[dimTrait];
402 for (int g = 0; g < dimTrait; ++g) {
403 double sum = 0.0;
404 for (int h = 0; h < dimTrait; ++h) {
405 sum += Pi.unsafe_get(g, h) * ipartial[ibo + h];
406 sum += Pj.unsafe_get(g, h) * jpartial[jbo + h];
407 }
408 tmp[g] = sum;
409 }
410 for (int g = 0; g < dimTrait; ++g) {
411 double sum = 0.0;
412 for (int h = 0; h < dimTrait; ++h) {
413 sum += Vk.unsafe_get(g, h) * tmp[h];
414 }
415 kpartial[kbo + g] = sum;
416 }
417 }
418 }