| 1 |
package dr.evomodel.treedatalikelihood.preorder; |
| 2 |
|
| 3 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 4 |
import dr.math.matrixAlgebra.Matrix; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
public class ConditionalVarianceAndTransform { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final private double[][] cholesky; |
| 24 |
final private Matrix affineTransform; |
| 25 |
private Matrix sBar; |
| 26 |
private final int[] missingIndices; |
| 27 |
private final int[] notMissingIndices; |
| 28 |
private final double[] tempStorage; |
| 29 |
|
| 30 |
private final int numMissing; |
| 31 |
private final int numNotMissing; |
| 32 |
|
| 33 |
private static final boolean DEBUG = false; |
| 34 |
|
| 35 |
public ConditionalVarianceAndTransform(final Matrix variance, final int[] missingIndices, final int[] notMissingIndices) { |
| 36 |
|
| 37 |
assert (missingIndices.length + notMissingIndices.length == variance.rows()); |
| 38 |
assert (missingIndices.length + notMissingIndices.length == variance.columns()); |
| 39 |
|
| 40 |
this.missingIndices = missingIndices; |
| 41 |
this.notMissingIndices = notMissingIndices; |
| 42 |
|
| 43 |
if (DEBUG) { |
| 44 |
System.err.println("variance:\n" + variance); |
| 45 |
} |
| 46 |
|
| 47 |
Matrix S12S22Inv = null; |
| 48 |
sBar = null; |
| 49 |
|
| 50 |
try { |
| 51 |
|
| 52 |
Matrix S22 = variance.extractRowsAndColumns(notMissingIndices, notMissingIndices); |
| 53 |
if (DEBUG) { |
| 54 |
System.err.println("S22:\n" + S22); |
| 55 |
} |
| 56 |
|
| 57 |
Matrix S22Inv = S22.inverse(); |
| 58 |
if (DEBUG) { |
| 59 |
System.err.println("S22Inv:\n" + S22Inv); |
| 60 |
} |
| 61 |
|
| 62 |
Matrix S12 = variance.extractRowsAndColumns(missingIndices, notMissingIndices); |
| 63 |
if (DEBUG) { |
| 64 |
System.err.println("S12:\n" + S12); |
| 65 |
} |
| 66 |
|
| 67 |
S12S22Inv = S12.product(S22Inv); |
| 68 |
if (DEBUG) { |
| 69 |
System.err.println("S12S22Inv:\n" + S12S22Inv); |
| 70 |
} |
| 71 |
|
| 72 |
Matrix S12S22InvS21 = S12S22Inv.productWithTransposed(S12); |
| 73 |
if (DEBUG) { |
| 74 |
System.err.println("S12S22InvS21:\n" + S12S22InvS21); |
| 75 |
} |
| 76 |
|
| 77 |
sBar = variance.extractRowsAndColumns(missingIndices, missingIndices); |
| 78 |
sBar.decumulate(S12S22InvS21); |
| 79 |
if (DEBUG) { |
| 80 |
System.err.println("sBar:\n" + sBar); |
| 81 |
} |
| 82 |
|
| 83 |
} catch (IllegalDimension illegalDimension) { |
| 84 |
illegalDimension.printStackTrace(); |
| 85 |
} |
| 86 |
|
| 87 |
this.affineTransform = S12S22Inv; |
| 88 |
this.cholesky = ProcessSimulationDelegate.AbstractContinuousTraitDelegate.getCholeskyOfVariance(sBar); |
| 89 |
this.tempStorage = new double[missingIndices.length]; |
| 90 |
|
| 91 |
this.numMissing = missingIndices.length; |
| 92 |
this.numNotMissing = notMissingIndices.length; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
public double[] getConditionalMean(final double[] y, final int offsetY, |
| 97 |
final double[] mu, final int offsetMu) { |
| 98 |
|
| 99 |
double[] muBar = new double[numMissing]; |
| 100 |
|
| 101 |
double[] shift = new double[numNotMissing]; |
| 102 |
for (int i = 0; i < numNotMissing; ++i) { |
| 103 |
final int notI = notMissingIndices[i]; |
| 104 |
shift[i] = y[offsetY + notI] - mu[offsetMu + notI]; |
| 105 |
} |
| 106 |
|
| 107 |
for (int i = 0; i < numMissing; ++i) { |
| 108 |
double delta = 0.0; |
| 109 |
for (int k = 0; k < numNotMissing; ++k) { |
| 110 |
delta += affineTransform.component(i, k) * shift[k]; |
| 111 |
} |
| 112 |
|
| 113 |
muBar[i] = mu[offsetMu + missingIndices[i]] + delta; |
| 114 |
} |
| 115 |
|
| 116 |
return muBar; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
double[][] getConditionalCholesky() { |
| 127 |
return cholesky; |
| 128 |
} |
| 129 |
|
| 130 |
Matrix getVariance() { |
| 131 |
return sBar; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
double[] getTemporaryStorage() { |
| 139 |
return tempStorage; |
| 140 |
} |
| 141 |
} |