Debian Codegraph @

src/dr/inference/distribution/WishartGammalDistributionModel.java

raw symbols SCIP
1 /*
2 * WishartGammalDistributionModel.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.inference.distribution;
27
28 import dr.inference.model.AbstractModel;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.math.distributions.GammaDistribution;
33 import dr.math.distributions.WishartDistribution;
34 import dr.math.distributions.WishartStatistics;
35
36 /**
37 * A class that acts as a model for multivariate Wishart-scaled-by-gamma distribution
38 * See:
39 * Huang A and Wand MP (in press) Simple marginally noninformative prior distributions for covariance matrices.
40 * Bayesian Analysis.
41 *
42 * @author Marc Suchard
43 * @author Max Tolkoff
44 * @author Philipe Lemey
45 * @author Gabriela Cybis
46 */
47
48 public class WishartGammalDistributionModel extends AbstractModel implements ParametricMultivariateDistributionModel,
49 WishartStatistics {
50
51 public WishartGammalDistributionModel(Parameter df, Parameter mixing, Parameter scale, boolean randomMixing) {
52 super(WishartGammalDistributionModel.TYPE);
53
54 this.df = df;
55 this.mixing = mixing;
56 this.scale = scale;
57
58 addVariable(df);
59 addVariable(mixing);
60 addVariable(scale);
61
62 this.randomMixing = randomMixing;
63
64 df.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
65 df.getDimension()));
66
67 mixing.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
68 mixing.getDimension()));
69
70 scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
71 scale.getDimension()));
72
73 dim = mixing.getDimension();
74 if (dim != scale.getDimension()) {
75 throw new IllegalArgumentException();
76 }
77
78 wishartDistributionKnown = false;
79 }
80
81 // *****************************************************************
82 // Interface MultivariateDistribution
83 // *****************************************************************
84
85
86 public double logPdf(double[] x) {
87 if (x.length != dim * dim) {
88 throw new IllegalArgumentException("Data of wrong dimension in " + getId());
89 }
90
91 if (!wishartDistributionKnown) {
92 wishart = createNewWishartDistribution();
93 wishartDistributionKnown = true;
94 }
95
96 double logLike = wishart.logPdf(x);
97 if (randomMixing) {
98 for (int i = 0; i < dim; ++i) {
99 double mixing = this.mixing.getParameterValue(i);
100 double scale = this.scale.getParameterValue(i);
101 logLike += GammaDistribution.logPdf(mixing, 0.5, scale * scale);
102 }
103 }
104 return logLike;
105 }
106
107 public double[][] getScaleMatrix() {
108 double df = this.df.getParameterValue(0);
109 double[][] scaleMatrix = new double[dim][dim];
110 for (int i = 0; i < dim; ++i) {
111 scaleMatrix[i][i] = 1.0 / (2.0 * df * mixing.getParameterValue(i));
112 }
113 return scaleMatrix;
114 }
115
116 public double getDF() {
117 return df.getParameterValue(0) + dim - 1;
118 }
119
120 public double[] getMean() {
121 return new double[dim];
122 }
123
124 public String getType() {
125 return TYPE;
126 }
127
128 // *****************************************************************
129 // Interface Model
130 // *****************************************************************
131
132 public void handleModelChangedEvent(Model model, Object object, int index) {
133 // no intermediates need to be recalculated...
134 }
135
136 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
137 if (variable == mixing || variable == df) {
138 wishartDistributionKnown = false;
139 }
140
141 if (!randomMixing && variable == mixing) {
142 throw new IllegalArgumentException("WishartGamma distribution is not configured for random mixing");
143 }
144 }
145
146 protected void storeState() {
147 storedWishart = wishart;
148 storedWishartDistributionKnown = wishartDistributionKnown;
149 }
150
151 protected void restoreState() {
152 wishartDistributionKnown = storedWishartDistributionKnown;
153 wishart = storedWishart;
154 }
155
156 protected void acceptState() {
157 } // no additional state needs accepting
158
159
160 // *****************************************************************
161 // Interface DensityModel
162 // *****************************************************************
163
164 @Override
165 public Variable<Double> getLocationVariable() {
166 throw new UnsupportedOperationException("Not implemented");
167 }
168
169 // **************************************************************
170 // Private instance variables and functions
171 // **************************************************************
172
173 private WishartDistribution createNewWishartDistribution() {
174 return new WishartDistribution(getDF(), getScaleMatrix());
175 }
176
177 private final int dim;
178
179 private final Parameter df;
180 private final Parameter mixing;
181 private final Parameter scale;
182
183 private final boolean randomMixing;
184
185 private WishartDistribution wishart;
186 private WishartDistribution storedWishart;
187
188 private boolean wishartDistributionKnown;
189 private boolean storedWishartDistributionKnown;
190
191 public static final String TYPE = "WishartGamma";
192
193 @Override
194 public double[] nextRandom() {
195 // TODO Auto-generated method stub
196 return null;
197 }
198 }