Debian Codegraph @

src/dr/inference/operators/MultivariateNormalGibbsOperator.java

raw symbols SCIP
1 /*
2 * MultivariateNormalGibbsOperator.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.operators;
27
28 import dr.inference.distribution.MultivariateDistributionLikelihood;
29 import dr.inference.distribution.MultivariateNormalDistributionModel;
30 import dr.inference.model.MatrixParameter;
31 import dr.inference.model.Parameter;
32 import dr.math.distributions.MultivariateNormalDistribution;
33 import dr.math.matrixAlgebra.IllegalDimension;
34 import dr.math.matrixAlgebra.Matrix;
35 import dr.math.matrixAlgebra.Vector;
36 import dr.util.Attribute;
37
38 import java.util.List;
39
40 /**
41 @author Max Tolkoff
42 */
43 public class MultivariateNormalGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
44 private Matrix priorPrecision;
45 private Vector priorMean;
46 private MatrixParameter likelihoodPrecision;
47 private Parameter likelihoodMean;
48 private MultivariateDistributionLikelihood likelihood;
49 private int dim;
50 public static final String MVN_GIBBS="multivariateNormalGibbsOperator";
51
52
53 public MultivariateNormalGibbsOperator(MultivariateDistributionLikelihood likelihood, MultivariateDistributionLikelihood prior, Double weight) throws IllegalDimension {
54
55
56 MultivariateNormalDistribution tempPrior=(MultivariateNormalDistribution) prior.getDistribution();
57 this.priorMean=new Vector(tempPrior.getMean());
58 this.priorPrecision=new Matrix(tempPrior.getScaleMatrix());
59 MultivariateNormalDistributionModel tempLikelihood=(MultivariateNormalDistributionModel) likelihood.getDistribution();
60 this.likelihoodMean=tempLikelihood.getMeanParameter();
61 this.likelihoodPrecision=tempLikelihood.getPrecisionMatrixParameter();
62 this.likelihood=likelihood;
63 this.dim=likelihoodMean.getValues().length;
64 // if(dataTemp.contains(MatrixParameter.class))
65 // {System.err.print("Well, at least you know it's there...\n");}
66 // else{System.err.print("Nope, you screwed up\n");}
67
68 setWeight(weight);
69 }
70
71
72 private void setParameterValue(Parameter set, double[] value){
73 set.setDimension(value.length);
74 for(int i=0; i<value.length; i++)
75 {set.setParameterValueQuietly(i,value[i]);}
76 set.fireParameterChangedEvent();
77 }
78
79 private double[] getMeanSum(){
80 double[] answer=new double[dim];
81 List<Attribute<double[]>> dataList = likelihood.getDataList();
82 for(Attribute<double[]> d: dataList){
83 for(int i=0; i<d.getAttributeValue().length; i++)
84 {
85 answer[i]+=d.getAttributeValue()[i];
86 }
87 }
88 /*
89 for(int i=0; i<dim; i++){
90 System.err.print(answer[i]);
91 System.err.print("\n");}
92 */
93 return answer;}
94
95 private Matrix getPrecision() throws IllegalDimension {
96 Matrix currentPrecision=new Matrix(likelihoodPrecision.getParameterAsMatrix());
97 currentPrecision=currentPrecision.product(likelihood.getDataList().size());
98
99 /*
100 for(int i=0; i<currentPrecision.columns(); i++){
101 for(int j=0; j<currentPrecision.rows(); j++){
102 System.err.print(currentPrecision.toComponents()[i][j]);
103 System.err.print(" ");}
104 System.err.print("\n"); }
105 */
106 return priorPrecision.add(currentPrecision);
107 }
108
109 private Vector getMean() throws IllegalDimension {
110 Vector meanSum=new Vector(getMeanSum());
111 Matrix workingPrecision=new Matrix(likelihoodPrecision.getParameterAsMatrix());
112 Vector meanPart=workingPrecision.product(meanSum);
113 meanPart=meanPart.add(priorPrecision.product(priorMean));
114 Matrix varPart=getPrecision().inverse();
115 Vector answer=varPart.product(meanPart);
116 /*
117 for(int i=0; i<varPart.columns(); i++){
118 for(int j=0; j<varPart.rows(); j++){
119 System.err.print(varPart.toComponents()[i][j]);}
120 System.err.print("\n"); }
121 this.priorPrecision=new Matrix(prior.getDistribution().getScaleMatrix());
122 System.err.print(answer.toComponents()[0]);
123 System.err.print("\n");
124 System.out.print(answer.toComponents()[0]);
125 for(int i=0; i<answer.dimension(); i++){
126 System.err.print(answer.toComponents()[i]);}
127 System.err.print("\n");
128 */
129
130 return answer;
131 }
132
133 // private Vector getDraws() throws IllegalDimension{
134 // double[] rUniform=new double[dim];
135 // for(int i=0; i<dim; i++)
136 // {rUniform[i]=}
137 // Vector draws=new Vector(MultivariateNormalDistribution.);
138 // return draws;
139 // }
140
141 @Override
142 public String getOperatorName() {
143 return MVN_GIBBS; //To change body of implemented methods use File | Settings | File Templates.
144 }
145
146 @Override
147 public double doOperation() {
148 double[] draws=null;
149
150
151 // try {
152 // for(int i=0; i<getPrecision().columns(); i++){
153 // for(int j=0; j<getPrecision().rows(); j++){
154 // System.err.print(getPrecision().toComponents()[i][j]);
155 // System.err.print(", ");}}
156 //// System.err.print(" ");}
157 //// System.err.print("\n"); }
158 // } catch (IllegalDimension illegalDimension) {
159 // illegalDimension.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
160 // }
161
162
163
164 try {
165 draws=MultivariateNormalDistribution.nextMultivariateNormalPrecision(getMean().toComponents(), getPrecision().toComponents());
166 } catch (IllegalDimension illegalDimension) {
167 illegalDimension.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
168 }
169
170 // for(int i=0; i<dim; i++){
171 // System.err.print(draws[i]);
172 // System.err.print("\n");}
173 setParameterValue(likelihoodMean, draws);
174
175 return 0; //To change body of implemented methods use File | Settings | File Templates.
176 }
177
178 }