Debian Codegraph @

src/dr/math/distributions/InverseWishartDistribution.java

raw symbols SCIP
1 /*
2 * InverseWishartDistribution.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Marc Suchard
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 * An Inverser Wishart distribution class for \nu degrees of freedom and scale matrix S with dim k
47 * Expectation = (\nu - k - 1)^{-1} * S
48 *
49 * @param df
50 * @param scaleMatrix
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 // System.err.println("here");
104 // double det = 0;
105 // try {
106 // det = W.determinant();
107 // } catch (IllegalDimension illegalDimension) {
108 // illegalDimension.printStackTrace();
109 // }
110 // if( det < 0 ) {
111 // System.err.println("not positive definite");
112 // return Double.NEGATIVE_INFINITY;
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 }