| 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.math.GammaFunction; |
| 29 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 30 |
import dr.math.matrixAlgebra.Matrix; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class InverseWishartDistribution implements MultivariateDistribution, WishartStatistics { |
| 36 |
|
| 37 |
public static final String TYPE = "InverseWishart"; |
| 38 |
|
| 39 |
private double df; |
| 40 |
private int dim; |
| 41 |
private double[][] scaleMatrix; |
| 42 |
private Matrix S; |
| 43 |
private double logNormalizationConstant; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public InverseWishartDistribution(double df, double[][] scaleMatrix) { |
| 54 |
this.df = df; |
| 55 |
this.scaleMatrix = scaleMatrix; |
| 56 |
this.dim = scaleMatrix.length; |
| 57 |
|
| 58 |
S = new Matrix(scaleMatrix); |
| 59 |
computeNormalizationConstant(); |
| 60 |
} |
| 61 |
|
| 62 |
private void computeNormalizationConstant() { |
| 63 |
logNormalizationConstant = 0; |
| 64 |
try { |
| 65 |
logNormalizationConstant = df / 2.0 * Math.log(new Matrix(scaleMatrix).determinant()); |
| 66 |
} catch (IllegalDimension illegalDimension) { |
| 67 |
illegalDimension.printStackTrace(); |
| 68 |
} |
| 69 |
logNormalizationConstant -= df * dim / 2.0 * Math.log(2); |
| 70 |
logNormalizationConstant -= dim * (dim - 1) / 4.0 * Math.log(Math.PI); |
| 71 |
for (int i = 1; i <= dim; i++) { |
| 72 |
logNormalizationConstant -= GammaFunction.lnGamma((df + 1 - i) / 2.0); |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
public String getType() { |
| 79 |
return TYPE; |
| 80 |
} |
| 81 |
|
| 82 |
public double[][] getScaleMatrix() { |
| 83 |
return scaleMatrix; |
| 84 |
} |
| 85 |
|
| 86 |
public double[] getMean() { |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
public double getDF() { |
| 91 |
return df; |
| 92 |
} |
| 93 |
|
| 94 |
public double[][] scaleMatrix() { |
| 95 |
return scaleMatrix; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
public double logPdf(double[] x) { |
| 100 |
Matrix W = new Matrix(x, dim, dim); |
| 101 |
double logDensity = 0; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
try { |
| 117 |
logDensity = Math.log(W.determinant()); |
| 118 |
|
| 119 |
logDensity *= -0.5; |
| 120 |
logDensity *= df + dim + 1; |
| 121 |
|
| 122 |
Matrix product = S.product(W.inverse()); |
| 123 |
|
| 124 |
for (int i = 0; i < dim; i++) |
| 125 |
logDensity -= 0.5 * product.component(i, i); |
| 126 |
|
| 127 |
} catch (IllegalDimension illegalDimension) { |
| 128 |
illegalDimension.printStackTrace(); |
| 129 |
} |
| 130 |
|
| 131 |
logDensity += logNormalizationConstant; |
| 132 |
return logDensity; |
| 133 |
} |
| 134 |
|
| 135 |
} |