| 1 |
package dr.evomodel.treedatalikelihood.preorder; |
| 2 |
|
| 3 |
import dr.math.matrixAlgebra.WrappedVector; |
| •4 |
import org.ejml.data.DMatrixRMaj; |
| 5 |
import org.ejml.dense.row.CommonOps_DDRM; |
| 6 |
|
| 7 |
import static dr.math.matrixAlgebra.missingData.MissingOps.gatherRowsAndColumns; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public class ConditionalVarianceAndTransform2 { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| •26 |
final private DMatrixRMaj sBar; |
| 27 |
final private DMatrixRMaj affineTransform; |
| 28 |
|
| 29 |
private final int[] missingIndices; |
| 30 |
private final int[] notMissingIndices; |
| 31 |
private final double[] tempStorage; |
| 32 |
|
| 33 |
private final int numMissing; |
| 34 |
private final int numNotMissing; |
| 35 |
|
| 36 |
private static final boolean DEBUG = false; |
| 37 |
|
| 38 |
private double[][] cholesky = null; |
| •39 |
private DMatrixRMaj sBarInv = null; |
| 40 |
|
| •41 |
ConditionalVarianceAndTransform2(final DMatrixRMaj variance, |
| 42 |
final int[] missingIndices, final int[] notMissingIndices) { |
| 43 |
|
| 44 |
assert (missingIndices.length + notMissingIndices.length == variance.getNumRows()); |
| 45 |
assert (missingIndices.length + notMissingIndices.length == variance.getNumCols()); |
| 46 |
|
| 47 |
this.missingIndices = missingIndices; |
| 48 |
this.notMissingIndices = notMissingIndices; |
| 49 |
|
| 50 |
if (DEBUG) { |
| 51 |
System.err.println("variance:\n" + variance); |
| 52 |
} |
| 53 |
|
| •54 |
DMatrixRMaj S22 = new DMatrixRMaj(notMissingIndices.length, notMissingIndices.length); |
| 55 |
gatherRowsAndColumns(variance, S22, notMissingIndices, notMissingIndices); |
| 56 |
|
| 57 |
if (DEBUG) { |
| 58 |
System.err.println("S22:\n" + S22); |
| 59 |
} |
| 60 |
|
| •61 |
DMatrixRMaj S22Inv = new DMatrixRMaj(notMissingIndices.length, notMissingIndices.length); |
| 62 |
CommonOps_DDRM.invert(S22, S22Inv); |
| 63 |
|
| 64 |
if (DEBUG) { |
| 65 |
System.err.println("S22Inv:\n" + S22Inv); |
| 66 |
} |
| 67 |
|
| •68 |
DMatrixRMaj S12 = new DMatrixRMaj(missingIndices.length, notMissingIndices.length); |
| 69 |
gatherRowsAndColumns(variance, S12, missingIndices, notMissingIndices); |
| 70 |
|
| 71 |
if (DEBUG) { |
| 72 |
System.err.println("S12:\n" + S12); |
| 73 |
} |
| 74 |
|
| •75 |
DMatrixRMaj S12S22Inv = new DMatrixRMaj(missingIndices.length, notMissingIndices.length); |
| 76 |
CommonOps_DDRM.mult(S12, S22Inv, S12S22Inv); |
| 77 |
|
| 78 |
if (DEBUG) { |
| 79 |
System.err.println("S12S22Inv:\n" + S12S22Inv); |
| 80 |
} |
| 81 |
|
| •82 |
DMatrixRMaj S12S22InvS21 = new DMatrixRMaj(missingIndices.length, missingIndices.length); |
| 83 |
CommonOps_DDRM.multTransB(S12S22Inv, S12, S12S22InvS21); |
| 84 |
|
| 85 |
if (DEBUG) { |
| 86 |
System.err.println("S12S22InvS21:\n" + S12S22InvS21); |
| 87 |
} |
| 88 |
|
| •89 |
sBar = new DMatrixRMaj(missingIndices.length, missingIndices.length); |
| 90 |
gatherRowsAndColumns(variance, sBar, missingIndices, missingIndices); |
| •91 |
CommonOps_DDRM.subtract(sBar, S12S22InvS21, sBar); |
| 92 |
|
| 93 |
|
| 94 |
if (DEBUG) { |
| 95 |
System.err.println("sBar:\n" + sBar); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
this.affineTransform = S12S22Inv; |
| 100 |
this.tempStorage = new double[missingIndices.length]; |
| 101 |
|
| 102 |
this.numMissing = missingIndices.length; |
| 103 |
this.numNotMissing = notMissingIndices.length; |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
public WrappedVector getConditionalMean(final double[] y, final int offsetY, |
| 108 |
final double[] mu, final int offsetMu) { |
| 109 |
|
| 110 |
double[] muBar = new double[numMissing]; |
| 111 |
|
| 112 |
double[] shift = new double[numNotMissing]; |
| 113 |
for (int i = 0; i < numNotMissing; ++i) { |
| 114 |
final int notI = notMissingIndices[i]; |
| 115 |
shift[i] = y[offsetY + notI] - mu[offsetMu + notI]; |
| 116 |
} |
| 117 |
|
| 118 |
for (int i = 0; i < numMissing; ++i) { |
| 119 |
double delta = 0.0; |
| 120 |
for (int k = 0; k < numNotMissing; ++k) { |
| 121 |
delta += affineTransform.unsafe_get(i, k) * shift[k]; |
| 122 |
} |
| 123 |
|
| 124 |
muBar[i] = mu[offsetMu + missingIndices[i]] + delta; |
| 125 |
} |
| 126 |
|
| 127 |
return new WrappedVector.Raw(muBar, 0, numMissing); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
final double[][] getConditionalCholesky() { |
| 138 |
if (cholesky == null) { |
| 139 |
this.cholesky = ProcessSimulationDelegate.AbstractContinuousTraitDelegate.getCholeskyOfVariance(sBar.data, missingIndices.length); |
| 140 |
} |
| 141 |
return cholesky; |
| 142 |
} |
| 143 |
|
| •144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| •148 |
final DMatrixRMaj getConditionalVariance() { |
| 149 |
return sBar; |
| 150 |
} |
| 151 |
|
| •152 |
final DMatrixRMaj getConditionalPrecision() { |
| 153 |
if (sBarInv == null) { |
| •154 |
sBarInv = new DMatrixRMaj(numMissing, numMissing); |
| 155 |
CommonOps_DDRM.invert(sBar, sBarInv); |
| 156 |
} |
| 157 |
return sBarInv; |
| 158 |
} |
| 159 |
|
| 160 |
final double[] getTemporaryStorage() { |
| 161 |
return tempStorage; |
| 162 |
} |
| 163 |
} |