Debian Codegraph @

src/dr/inference/operators/LatentFactorModelPrecisionGibbsOperator.java

raw symbols SCIP
1 /*
2 * LatentFactorModelPrecisionGibbsOperator.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.*;
30 import dr.math.MathUtils;
31 import dr.math.distributions.GammaDistribution;
32
33 /**
34 * Created by max on 6/12/14.
35 */
36 public class LatentFactorModelPrecisionGibbsOperator extends SimpleMCMCOperator implements PathDependentOperator, GibbsOperator {
37 // private double[] FacXLoad;
38 // private double[] residual;
39 private LatentFactorModel LFM;
40 private GammaDistribution prior;
41 private boolean randomScan;
42 private double shape;
43 double pathWeight = 1.0;
44 final Parameter missingIndicator;
45
46 public LatentFactorModelPrecisionGibbsOperator(LatentFactorModel LFM, DistributionLikelihood prior, double weight, boolean randomScan) {
47 setWeight(weight);
48 this.LFM = LFM;
49 this.prior = (GammaDistribution) prior.getDistribution();
50 this.randomScan = randomScan;
51 this.missingIndicator = LFM.getMissingIndicator();
52
53 // FacXLoad=new double[LFM.getFactors().getColumnDimension()];
54 // residual=new double[LFM.getFactors().getColumnDimension()];
55 // setShape();
56 }
57
58 private double getShape(int i) {
59 return this.prior.getShape() + LFM.getRowCount(i) * .5 * pathWeight;
60 }
61
62 private void setPrecision(int i) {
63 MatrixParameterInterface factors = LFM.getFactors();
64 MatrixParameterInterface loadings = LFM.getLoadings();
65 DiagonalMatrix precision = (DiagonalMatrix) LFM.getColumnPrecision();
66 MatrixParameterInterface data = LFM.getScaledData();
67 double di = 0;
68 for (int j = 0; j < factors.getColumnDimension(); j++) {
69 double sum = 0;
70 if(missingIndicator == null || missingIndicator.getParameterValue(j * LFM.getScaledData().getRowDimension() + i) != 1) {
71 for (int k = 0; k < factors.getRowDimension(); k++) {
72 sum += factors.getParameterValue(k, j) * loadings.getParameterValue(i, k);
73 }
74 double temp = data.getParameterValue(i, j) - sum;
75 di += temp * temp;
76 }
77 // FacXLoad[j]=sum;
78 // residual[j]=data.getParameterValue(i,j)-FacXLoad[j];
79 // }
80 // double sum=0;
81 // for (int j = 0; j <factors.getColumnDimension() ; j++) {
82 // sum+=residual[j]*residual[j];
83 }
84 double scale = 1.0 / (1.0 / prior.getScale() + pathWeight * di * .5);
85 double nextPrecision = GammaDistribution.nextGamma(getShape(i), scale);
86 precision.setParameterValueQuietly(i, nextPrecision);
87 }
88
89 public void setPathParameter(double beta) {
90 pathWeight = beta;
91 }
92
93 @Override
94 public String getOperatorName() {
95 return "Latent Factor Model Precision Gibbs Operator";
96 }
97
98 @Override
99 public double doOperation() {
100
101 if (!randomScan) for (int i = 0; i < LFM.getColumnPrecision().getColumnDimension(); i++) {
102 if (LFM.getContinuous().getParameterValue(i) != 0)
103 setPrecision(i);
104 }
105 else {
106 int i = MathUtils.nextInt(LFM.getColumnPrecision().getColumnDimension());
107 while (LFM.getContinuous().getParameterValue(i) == 0)
108 i = MathUtils.nextInt(LFM.getColumnPrecision().getColumnDimension());
109 setPrecision(i);
110 }
111 LFM.getColumnPrecision().getParameter(0).fireParameterChangedEvent();
112
113
114 return 0;
115 }
116 }