| 1 |
package dr.evomodel.treedatalikelihood.preorder; |
| 2 |
|
| 3 |
import dr.math.matrixAlgebra.WrappedVector; |
| 4 |
import org.ejml.data.DenseMatrix64F; |
| 5 |
import org.ejml.ops.CommonOps; |
| 6 |
|
| 7 |
import static dr.math.matrixAlgebra.missingData.MissingOps.gatherRowsAndColumns; |
| 8 |
|
| 9 |
/** |
| 10 |
* @author Marc A. Suchard |
| 11 |
*/ |
| 12 |
public class ConditionalVarianceAndTransform2 { |
| 13 |
|
| 14 |
/** |
| 15 |
* For partially observed tips: (y_1, y_2)^t \sim N(\mu, \Sigma) where |
| 16 |
* <p> |
| 17 |
* \mu = (\mu_1, \mu_2)^t |
| 18 |
* \Sigma = ((\Sigma_{11}, \Sigma_{12}), (\Sigma_{21}, \Sigma_{22})^t |
| 19 |
* <p> |
| 20 |
* then y_1 | y_2 \sim N (\bar{\mu}, \bar{\Sigma}), where |
| 21 |
* <p> |
| 22 |
* \bar{\mu} = \mu_1 + \Sigma_{12}\Sigma_{22}^{-1}(y_2 - \mu_2), and |
| 23 |
* \bar{\Sigma} = \Sigma_{11} - \Sigma_{12}\Sigma_{22}^1\Sigma{21} |
| 24 |
*/ |
| 25 |
|
| 26 |
final private DenseMatrix64F sBar; |
| 27 |
final private DenseMatrix64F 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 DenseMatrix64F sBarInv = null; |
| 40 |
|
| 41 |
ConditionalVarianceAndTransform2(final DenseMatrix64F 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 |
DenseMatrix64F S22 = new DenseMatrix64F(notMissingIndices.length, notMissingIndices.length); |
| 55 |
gatherRowsAndColumns(variance, S22, notMissingIndices, notMissingIndices); |
| 56 |
|
| 57 |
if (DEBUG) { |
| 58 |
System.err.println("S22:\n" + S22); |
| 59 |
} |
| 60 |
|
| 61 |
DenseMatrix64F S22Inv = new DenseMatrix64F(notMissingIndices.length, notMissingIndices.length); |
| 62 |
CommonOps.invert(S22, S22Inv); |
| 63 |
|
| 64 |
if (DEBUG) { |
| 65 |
System.err.println("S22Inv:\n" + S22Inv); |
| 66 |
} |
| 67 |
|
| 68 |
DenseMatrix64F S12 = new DenseMatrix64F(missingIndices.length, notMissingIndices.length); |
| 69 |
gatherRowsAndColumns(variance, S12, missingIndices, notMissingIndices); |
| 70 |
|
| 71 |
if (DEBUG) { |
| 72 |
System.err.println("S12:\n" + S12); |
| 73 |
} |
| 74 |
|
| 75 |
DenseMatrix64F S12S22Inv = new DenseMatrix64F(missingIndices.length, notMissingIndices.length); |
| 76 |
CommonOps.mult(S12, S22Inv, S12S22Inv); |
| 77 |
|
| 78 |
if (DEBUG) { |
| 79 |
System.err.println("S12S22Inv:\n" + S12S22Inv); |
| 80 |
} |
| 81 |
|
| 82 |
DenseMatrix64F S12S22InvS21 = new DenseMatrix64F(missingIndices.length, missingIndices.length); |
| 83 |
CommonOps.multTransB(S12S22Inv, S12, S12S22InvS21); |
| 84 |
|
| 85 |
if (DEBUG) { |
| 86 |
System.err.println("S12S22InvS21:\n" + S12S22InvS21); |
| 87 |
} |
| 88 |
|
| 89 |
sBar = new DenseMatrix64F(missingIndices.length, missingIndices.length); |
| 90 |
gatherRowsAndColumns(variance, sBar, missingIndices, missingIndices); |
| 91 |
CommonOps.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 |
// void scatterResult(final double[] source, final int offsetSource, |
| 131 |
// final double[] destination, final int offsetDestination) { |
| 132 |
// for (int i = 0; i < numMissing; ++i) { |
| 133 |
// destination[offsetDestination + missingIndices[i]] = source[offsetSource + i]; |
| 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 |
// public final DenseMatrix64F getAffineTransform() { |
| 145 |
// return affineTransform; |
| 146 |
// } |
| 147 |
|
| 148 |
final DenseMatrix64F getConditionalVariance() { |
| 149 |
return sBar; |
| 150 |
} |
| 151 |
|
| 152 |
final DenseMatrix64F getConditionalPrecision() { |
| 153 |
if (sBarInv == null) { |
| 154 |
sBarInv = new DenseMatrix64F(numMissing, numMissing); |
| 155 |
CommonOps.invert(sBar, sBarInv); |
| 156 |
} |
| 157 |
return sBarInv; |
| 158 |
} |
| 159 |
|
| 160 |
final double[] getTemporaryStorage() { |
| 161 |
return tempStorage; |
| 162 |
} |
| 163 |
} |