Debian Codegraph @

src/dr/inference/operators/LoadingsIndependenceOperator.java

raw symbols SCIP
1 /*
2 * LoadingsIndependenceOperator.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.DistributionLikelihood;
29 import dr.inference.model.LatentFactorModel;
30 import dr.inference.model.MatrixParameterInterface;
31 import dr.inference.model.Parameter;
32 import dr.math.MathUtils;
33 import dr.math.distributions.MultivariateNormalDistribution;
34 import dr.math.distributions.NormalDistribution;
35 import dr.math.matrixAlgebra.CholeskyDecomposition;
36 import dr.math.matrixAlgebra.IllegalDimension;
37 import dr.math.matrixAlgebra.SymmetricMatrix;
38
39 import java.util.ArrayList;
40 import java.util.ListIterator;
41
42 /**
43 * Created with IntelliJ IDEA.
44 * User: max
45 * Date: 5/23/14
46 * Time: 2:23 PM
47 * To change this template use File | Settings | File Templates.
48 */
49 public class LoadingsIndependenceOperator extends AbstractAdaptableOperator {
50 NormalDistribution prior;
51 LatentFactorModel LFM;
52 ArrayList<double[][]> precisionArray;
53 ArrayList<double[]> meanMidArray;
54 ArrayList<double[]> meanArray;
55 boolean randomScan;
56 double scaleFactor;
57
58
59 double priorPrecision;
60 double priorMeanPrecision;
61
62 public LoadingsIndependenceOperator(LatentFactorModel LFM, DistributionLikelihood prior, double weight, boolean randomScan, double scaleFactor, AdaptationMode mode) {
63 super(mode);
64 setWeight(weight);
65
66 this.scaleFactor = scaleFactor;
67 this.prior = (NormalDistribution) prior.getDistribution();
68 this.LFM = LFM;
69 precisionArray = new ArrayList<double[][]>();
70 double[][] temp;
71 this.randomScan = randomScan;
72
73
74 meanArray = new ArrayList<double[]>();
75 meanMidArray = new ArrayList<double[]>();
76 double[] tempMean;
77 if (!randomScan) {
78 for (int i = 0; i < LFM.getFactorDimension(); i++) {
79 temp = new double[i + 1][i + 1];
80 precisionArray.add(temp);
81 }
82 for (int i = 0; i < LFM.getFactorDimension(); i++) {
83 tempMean = new double[i + 1];
84 meanArray.add(tempMean);
85 }
86
87 for (int i = 0; i < LFM.getFactorDimension(); i++) {
88 tempMean = new double[i + 1];
89 meanMidArray.add(tempMean);
90 }
91 } else {
92 for (int i = 0; i < LFM.getFactorDimension(); i++) {
93 temp = new double[LFM.getFactorDimension() - i][LFM.getFactorDimension() - i];
94 precisionArray.add(temp);
95 }
96 for (int i = 0; i < LFM.getFactorDimension(); i++) {
97 tempMean = new double[LFM.getFactorDimension() - i];
98 meanArray.add(tempMean);
99 }
100
101 for (int i = 0; i < LFM.getFactorDimension(); i++) {
102 tempMean = new double[LFM.getFactorDimension() - i];
103 meanMidArray.add(tempMean);
104 }
105 }
106
107 // vectorProductAnswer=new MatrixParameter[LFM.getLoadings().getRowDimension()];
108 // for (int i = 0; i <vectorProductAnswer.length ; i++) {
109 // vectorProductAnswer[i]=new MatrixParameter(null);
110 // vectorProductAnswer[i].setDimensions(i+1, 1);
111 // }
112
113 // priorMeanVector=new MatrixParameter[LFM.getLoadings().getRowDimension()];
114 // for (int i = 0; i <priorMeanVector.length ; i++) {
115 // priorMeanVector[i]=new MatrixParameter(null, i+1, 1, this.prior.getMean()/(this.prior.getSD()*this.prior.getSD()));
116 //
117 //
118 // }
119 priorPrecision = 1 / (this.prior.getSD() * this.prior.getSD());
120 priorMeanPrecision = this.prior.getMean() * priorPrecision;
121 }
122
123 private void getPrecisionOfTruncated(MatrixParameterInterface full, int newRowDimension, int row, double[][] answer) {
124
125 // MatrixParameter answer=new MatrixParameter(null);
126 // answer.setDimensions(this.getRowDimension(), Right.getRowDimension());
127 // System.out.println(answer.getRowDimension());
128 // System.out.println(answer.getColumnDimension());
129
130 int p = full.getColumnDimension();
131 for (int i = 0; i < newRowDimension; i++) {
132 for (int j = i; j < newRowDimension; j++) {
133 double sum = 0;
134 for (int k = 0; k < p; k++)
135 sum += full.getParameterValue(i, k) * full.getParameterValue(j, k);
136 answer[i][j] = sum * LFM.getColumnPrecision().getParameterValue(row, row);
137 if (i == j) {
138 answer[i][j] += priorPrecision;
139 } else {
140 answer[j][i] = answer[i][j];
141 }
142 }
143 }
144 }
145
146
147 private void getTruncatedMean(int newRowDimension, int dataColumn, double[][] variance, double[] midMean, double[] mean) {
148
149 // MatrixParameter answer=new MatrixParameter(null);
150 // answer.setDimensions(this.getRowDimension(), Right.getRowDimension());
151 // System.out.println(answer.getRowDimension());
152 // System.out.println(answer.getColumnDimension());
153 MatrixParameterInterface data = LFM.getScaledData();
154 MatrixParameterInterface Left = LFM.getFactors();
155 int p = data.getColumnDimension();
156 for (int i = 0; i < newRowDimension; i++) {
157 double sum = 0;
158 for (int k = 0; k < p; k++)
159 sum += Left.getParameterValue(i, k) * data.getParameterValue(dataColumn, k);
160 sum = sum * LFM.getColumnPrecision().getParameterValue(dataColumn, dataColumn);
161 sum += priorMeanPrecision;
162 midMean[i] = sum;
163 }
164 for (int i = 0; i < newRowDimension; i++) {
165 double sum = 0;
166 for (int k = 0; k < newRowDimension; k++)
167 sum += variance[i][k] * midMean[k];
168 mean[i] = sum;
169 }
170
171 }
172
173 private void getPrecision(int i, double[][] answer) {
174 int size = LFM.getFactorDimension();
175 if (i < size) {
176 getPrecisionOfTruncated(LFM.getFactors(), i + 1, i, answer);
177 } else {
178 getPrecisionOfTruncated(LFM.getFactors(), size, i, answer);
179 }
180 }
181
182 private void getMean(int i, double[][] variance, double[] midMean, double[] mean) {
183 // Matrix factors=null;
184 int size = LFM.getFactorDimension();
185 // double[] scaledDataColumn=LFM.getScaledData().getRowValues(i);
186 // Vector dataColumn=null;
187 // Vector priorVector=null;
188 // Vector temp=null;
189 // Matrix data=new Matrix(LFM.getScaledData().getParameterAsMatrix());
190 if (i < size) {
191 getTruncatedMean(i + 1, i, variance, midMean, mean);
192 // dataColumn=new Vector(data.toComponents()[i]);
193 // try {
194 // answer=precision.inverse().product(new Matrix(priorMeanVector[i].add(vectorProductAnswer[i]).getParameterAsMatrix()));
195 // } catch (IllegalDimension illegalDimension) {
196 // illegalDimension.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
197 // }
198 } else {
199 getTruncatedMean(size, i, variance, midMean, mean);
200 // dataColumn=new Vector(data.toComponents()[i]);
201 // try {
202 // answer=precision.inverse().product(new Matrix(priorMeanVector[size-1].add(vectorProductAnswer[size-1]).getParameterAsMatrix()));
203 // } catch (IllegalDimension illegalDimension) {
204 // illegalDimension.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
205 // }
206 }
207
208 }
209
210 private void copy(int i, double[] random) {
211 Parameter changing = LFM.getLoadings().getParameter(i);
212 for (int j = 0; j < random.length; j++) {
213 changing.setParameterValueQuietly(j, random[j]);
214 }
215 }
216
217 private void drawI(int i, ListIterator<double[][]> currentPrecision, ListIterator<double[]> currentMidMean, ListIterator<double[]> currentMean) {
218 double[] draws = null;
219 double[][] precision = null;
220 double[][] variance;
221 double[] midMean = null;
222 double[] mean = null;
223 double[][] cholesky = null;
224 if (currentPrecision.hasNext()) {
225 precision = currentPrecision.next();
226 }
227
228 if (currentMidMean.hasNext()) {
229 midMean = currentMidMean.next();
230 }
231 if (currentMean.hasNext()) {
232 mean = currentMean.next();
233 }
234 getPrecision(i, precision);
235 variance = (new SymmetricMatrix(precision)).inverse().toComponents();
236
237 try {
238 cholesky = new CholeskyDecomposition(variance).getL();
239 } catch (IllegalDimension illegalDimension) {
240 illegalDimension.printStackTrace();
241 }
242
243 getMean(i, variance, midMean, mean);
244
245 draws = MultivariateNormalDistribution.nextMultivariateNormalCholesky(mean, cholesky, scaleFactor);
246 // if(i<draws.length)
247 //
248 // {
249 // while (draws[i] < 0) {
250 // draws = MultivariateNormalDistribution.nextMultivariateNormalCholesky(mean, cholesky);
251 // }
252 // }
253 if (i < draws.length) {
254 if (draws[i] > 0) {
255 copy(i, draws);
256 // LFM.computeResiduals();
257 }
258 } else {
259 copy(i, draws);
260 // LFM.computeResiduals();
261 }
262
263 // copy(i, draws);
264
265 }
266
267 // @Override
268 // public int getStepCount() {
269 // return 0; //To change body of implemented methods use File | Settings | File Templates.
270 // }
271
272 @Override
273 public String getOperatorName() {
274 return "loadingsGibbsOperator"; //To change body of implemented methods use File | Settings | File Templates.
275 }
276
277 @Override
278 public double doOperation() {
279
280 int size = LFM.getLoadings().getColumnDimension();
281 if (!randomScan) {
282 ListIterator<double[][]> currentPrecision = precisionArray.listIterator();
283 ListIterator<double[]> currentMidMean = meanMidArray.listIterator();
284 ListIterator<double[]> currentMean = meanArray.listIterator();
285 for (int i = 0; i < size; i++) {
286 drawI(i, currentPrecision, currentMidMean, currentMean);
287 }
288 LFM.getLoadings().fireParameterChangedEvent();
289 } else {
290 int i = MathUtils.nextInt(LFM.getLoadings().getColumnDimension());
291 ListIterator<double[][]> currentPrecision;
292 ListIterator<double[]> currentMidMean;
293 ListIterator<double[]> currentMean;
294 if (i < LFM.getFactorDimension()) {
295 currentPrecision = precisionArray.listIterator(LFM.getFactorDimension() - i - 1);
296 currentMidMean = meanMidArray.listIterator(LFM.getFactorDimension() - i - 1);
297 currentMean = meanArray.listIterator(LFM.getFactorDimension() - i - 1);
298 } else {
299 currentPrecision = precisionArray.listIterator();
300 currentMidMean = meanMidArray.listIterator();
301 currentMean = meanArray.listIterator();
302 }
303 drawI(i, currentPrecision, currentMidMean, currentMean);
304 LFM.getLoadings().fireParameterChangedEvent();
305 }
306 return 0;
307 }
308
309 public double getAdaptableParameter() {
310 return Math.log(scaleFactor);
311 }
312
313 @Override
314 public void setAdaptableParameter(double value) {
315 scaleFactor = Math.exp(value);
316 }
317
318 @Override
319 public double getRawParameter() {
320 return scaleFactor;
321 }
322
323 public String getAdaptableParameterName() {
324 return "scaleFactor";
325 }
326 }