Debian Codegraph @

src/dr/inference/distribution/MultivariateNormalDistributionModel.java

raw symbols SCIP
1 /*
2 * MultivariateNormalDistributionModel.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.*;
29 import dr.inferencexml.distribution.MultivariateNormalDistributionModelParser;
30 import dr.math.distributions.GaussianProcessRandomGenerator;
31 import dr.math.distributions.MultivariateNormalDistribution;
32
33 /**
34 * A class that acts as a model for multivariate normally distributed data.
35 *
36 * @author Marc Suchard
37 * @author Max Tolkoff
38 */
39
40 public class MultivariateNormalDistributionModel extends AbstractModel implements ParametricMultivariateDistributionModel,
41 GaussianProcessRandomGenerator, GradientProvider {
42
43 public MultivariateNormalDistributionModel(Parameter meanParameter, MatrixParameter precParameter) {
44 super(MultivariateNormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL);
45 this.mean = meanParameter;
46 addVariable(meanParameter);
47
48 if (!(meanParameter instanceof DuplicatedParameter)) {
49 meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY,
50 meanParameter.getDimension()));
51 }
52 this.precision = precParameter;
53 addVariable(precParameter);
54
55 Parameter single = null;
56 if (precParameter instanceof DiagonalMatrix) {
57 DiagonalMatrix dm = (DiagonalMatrix) precParameter;
58 if (dm.getDiagonalParameter() instanceof DuplicatedParameter) {
59 single = dm.getDiagonalParameter();
60 }
61 }
62 hasSinglePrecision = (single != null);
63 singlePrecision = single;
64
65 distribution = createNewDistribution();
66 distributionKnown = true;
67 }
68
69 public MatrixParameter getPrecisionMatrixParameter() {
70 return precision;
71 }
72
73 public Parameter getMeanParameter() {
74 return mean;
75 }
76
77 // *****************************************************************
78 // Interface MultivariateDistribution
79 // *****************************************************************
80
81
82 private void checkDistribution() {
83 if (!distributionKnown) {
84 distribution = createNewDistribution();
85 distributionKnown = true;
86 }
87 }
88
89 public double logPdf(double[] x) {
90 checkDistribution();
91 return distribution.logPdf(x);
92 }
93
94 public double[][] getScaleMatrix() {
95 return precision.getParameterAsMatrix();
96 }
97
98 public double[] getMean() {
99 return mean.getParameterValues();
100 }
101
102 public String getType() {
103 return distribution.getType();
104 }
105
106 // *****************************************************************
107 // Interface Model
108 // *****************************************************************
109
110 public void handleModelChangedEvent(Model model, Object object, int index) {
111 // no intermediates need to be recalculated...
112 }
113
114 public Likelihood getLikelihood() {
115 return null;
116 }
117
118 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
119 distributionKnown = false;
120 }
121
122 protected void storeState() {
123 storedDistribution = distribution;
124 storedDistributionKnown = distributionKnown;
125 }
126
127 protected void restoreState() {
128 distributionKnown = storedDistributionKnown;
129 distribution = storedDistribution;
130 }
131
132 protected void acceptState() {
133 } // no additional state needs accepting
134
135 @Override
136 public int getDimension() {
137 return mean.getDimension();
138 }
139
140 @Override
141 public double[][] getPrecisionMatrix() {
142 return precision.getParameterAsMatrix();
143 }
144
145 // *****************************************************************
146 // Interface DensityModel
147 // *****************************************************************
148
149 @Override
150 public Variable<Double> getLocationVariable() {
151 return mean;
152 }
153
154 // **************************************************************
155 // Private instance variables and functions
156 // **************************************************************
157
158 private MultivariateNormalDistribution createNewDistribution() {
159 if (hasSinglePrecision) {
160 return new MultivariateNormalDistribution(getMean(), singlePrecision.getParameterValue(0));
161 } else {
162 return new MultivariateNormalDistribution(getMean(), getScaleMatrix());
163 }
164 }
165
166 private final Parameter mean;
167 private final MatrixParameter precision;
168 private final boolean hasSinglePrecision;
169 private final Parameter singlePrecision;
170 private MultivariateNormalDistribution distribution;
171 private MultivariateNormalDistribution storedDistribution;
172
173 private boolean distributionKnown;
174 private boolean storedDistributionKnown;
175
176 // RandomGenerator interface
177 public double[] nextRandom() {
178 checkDistribution();
179 return distribution.nextMultivariateNormal();
180 }
181
182 public double logPdf(Object x) {
183 checkDistribution();
184 return distribution.logPdf(x);
185 }
186
187 // GradientWrtParameterProvider interface
188 @Override
189 public double[] getGradientLogDensity(Object x) {
190 checkDistribution();
191 return distribution.getGradientLogDensity(x);
192 }
193
194 }