Debian Codegraph @

src/dr/inference/distribution/MomentDistributionModel.java

raw symbols SCIP
1 /*
2 * MomentDistributionModel.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
29 import dr.inference.model.*;
30 import dr.inference.model.Parameter;
31 import dr.inferencexml.distribution.MomentDistributionModelParser;
32 import dr.math.distributions.RandomGenerator;
33
34 //@author Max Tolkoff
35 public class MomentDistributionModel extends AbstractModelLikelihood implements ParametricMultivariateDistributionModel, RandomGenerator {
36
37 public MomentDistributionModel(String id, Parameter mean, Parameter precision, Parameter cutoff, Parameter data) {
38 super(id);
39
40 this.mean=mean;
41 this.precision=precision;
42 // this.mean = new DuplicatedParameter(mean);
43 // this.mean.addDuplicationParameter(new Parameter.Default(cutoff.getDimension()));
44 // DuplicatedParameter precTemp= new DuplicatedParameter(precision);
45 // precTemp.addDuplicationParameter(new Parameter.Default(cutoff.getDimension()));
46 // this.precision=new DiagonalMatrix(precTemp);
47 addVariable(mean);
48 mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
49 addVariable(precision);
50 // precision.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
51 this.cutoff=cutoff;
52 if(cutoff!=null){
53 addVariable(cutoff);
54 int boundsDim;
55 if(cutoff.getBounds()!=null)
56 boundsDim = cutoff.getBounds().getBoundsDimension();
57 else boundsDim = cutoff.getDimension();
58 cutoff.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, boundsDim));}
59 addVariable(data);
60 this.data=data;
61 untruncated=new NormalDistributionModel(mean, precision, true);
62 sumKnown=false;
63 untruncatedKnown=false;
64 }
65
66 private final Parameter mean;
67 private final Parameter precision;
68 // private final DuplicatedParameter mean;
69 // private final DiagonalMatrix precision;
70 private final Parameter cutoff;
71 private NormalDistributionModel untruncated;
72 private double sum;
73 private boolean sumKnown;
74
75 private boolean storedSumKnown;
76 private double storedSum;
77 private boolean untruncatedKnown;
78 private boolean storedUntruncatedKnown;
79 private NormalDistributionModel storedUntruncated;
80 private Parameter data;
81
82 public double logPdf(Parameter data) {
83 // untruncatedKnown=false;
84 // sumKnown=false;
85 checkDistribution();
86 if(sumKnown)
87 return sum;
88 else
89 {
90 sum=0;
91 }
92
93 if(cutoff!=null){
94 if(data.getDimension()!=cutoff.getDimension()){
95 throw new RuntimeException("Incorrect number of cutoffs");
96 }
97 for (int i = 0; i <data.getDimension() ; i++) {
98 if (Math.sqrt(cutoff.getParameterValue(i)) - .05 > Math.abs(data.getParameterValue(i)) && data.getParameterValue(i)!=0){
99 return Double.NEGATIVE_INFINITY; }
100 else if(data.getParameterValue(i)==0){
101 sum += 0;
102 }
103 // sum+=-1000-Math.log(precision.getParameterValue(0));
104 else {
105 sum+=untruncated.logPdf(data.getParameterValue(i))
106 + getNormalizingConstant(i)
107 ;
108 }
109 }
110 }
111 else{
112 for (int i = 0; i <data.getDimension() ; i++) {
113 sum+= untruncated.logPdf(data.getParameterValue(i)) + 2 * StrictMath.log(Math.abs(data.getParameterValue(i))) + StrictMath.log(precision.getParameterValue(0));
114 }
115 }
116 sumKnown=true;
117 return sum;
118
119 }
120
121 public double getNormalizingConstant(int i){
122 return -Math.log(1 - (untruncated.cdf(Math.sqrt(cutoff.getParameterValue(i))) - untruncated.cdf(-Math.sqrt(cutoff.getParameterValue(i)))));
123 }
124
125 @Override
126 public double logPdf(double[] x) {
127 return 0;
128 }
129
130 public Parameter getCutoff(){return cutoff;}
131
132 @Override
133 public double[][] getScaleMatrix() {
134 double[][] temp=new double[1][1];
135 temp[0][0]=precision.getParameterValue(0);
136 return temp;
137 // return precision.getParameterAsMatrix();
138 }
139
140 @Override
141 public double[] getMean() {
142 return mean.getParameterValues();
143 }
144
145 @Override
146 public String getType() {
147 return "Moment Distribution Model";
148 }
149
150 @Override
151 protected void handleModelChangedEvent(Model model, Object object, int index) {
152
153 }
154
155 @Override
156 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
157 sumKnown=false;
158 if(variable==mean || variable==precision)
159 {untruncatedKnown=false;}
160
161 }
162
163 @Override
164 protected void storeState() {
165 storedSumKnown=sumKnown;
166 storedSum=sum;
167 storedUntruncated=untruncated;
168 storedUntruncatedKnown=untruncatedKnown;
169 }
170
171 @Override
172 protected void restoreState() {
173 sumKnown=storedSumKnown;
174 sum=storedSum;
175 untruncated=storedUntruncated;
176 untruncatedKnown=storedUntruncatedKnown;
177
178 }
179
180 @Override
181 protected void acceptState() {
182
183 }
184
185 private NormalDistributionModel createNewDistribution() {
186 return new NormalDistributionModel(mean, precision, true);
187 // return new NormalDistributionModel(new Parameter.Default(mean.getParameterValue(0)), new Parameter.Default(precision.getParameterValue(0)), true);
188 }
189
190 private void checkDistribution() {
191 if (!untruncatedKnown) {
192 untruncated = createNewDistribution();
193 untruncatedKnown = true;
194 }
195 }
196
197 @Override
198 public double[] nextRandom() {
199 return new double[0];
200 }
201
202 @Override
203 public double logPdf(Object x) {
204 if(x instanceof Parameter)
205 return logPdf((Parameter) x);
206 else
207 return 0;
208 }
209
210 @Override
211 public Model getModel() {
212 return this;
213 }
214
215 @Override
216 public double getLogLikelihood() {
217 return logPdf(data);
218 }
219
220 @Override
221 public void makeDirty() {
222 sumKnown=false;
223 untruncatedKnown=false;
224
225 }
226
227 // *****************************************************************
228 // Interface DensityModel
229 // *****************************************************************
230
231 @Override
232 public Variable<Double> getLocationVariable() {
233 throw new UnsupportedOperationException("Not implemented");
234 }
235
236 }