Debian Codegraph @

src/dr/inference/operators/FactorGibbsOperator.java

raw symbols SCIP
1 /*
2 * FactorGibbsOperator.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.model.*;
29 import dr.math.MathUtils;
30 import dr.math.distributions.MultivariateNormalDistribution;
31 import dr.math.matrixAlgebra.SymmetricMatrix;
32
33 /**
34 * Created with IntelliJ IDEA.
35 * User: max
36 * Date: 5/22/14
37 * Time: 12:49 PM
38 * To change this template use File | Settings | File Templates.
39 */
40 public class FactorGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
41 private static final String FACTOR_GIBBS_OPERATOR = "factorGibbsOperator";
42 private LatentFactorModel LFM;
43 private MatrixParameter diffusionPrecision;
44 double[][] precision;
45 double[] mean;
46 double[] midMean;
47 private int numFactors;
48 private boolean randomScan;
49
50 public FactorGibbsOperator(LatentFactorModel LFM, double weight, boolean randomScan, DiagonalMatrix diffusionPrecision) {
51 this.LFM = LFM;
52 setWeight(weight);
53 this.randomScan = randomScan;
54 this.diffusionPrecision = diffusionPrecision;
55 setupParameters();
56
57 }
58
59 private void setupParameters() {
60 if (numFactors != LFM.getFactorDimension()) {
61 numFactors = LFM.getFactorDimension();
62 mean = new double[numFactors];
63 midMean = new double[numFactors];
64 precision = new double[numFactors][numFactors];
65 }
66 }
67
68 private void getPrecision(double[][] precision) {
69 MatrixParameterInterface Loadings = LFM.getLoadings();
70 MatrixParameter Precision = LFM.getColumnPrecision();
71 int outerDim = Loadings.getRowDimension();
72 int innerDim = Loadings.getColumnDimension();
73 for (int i = 0; i < outerDim; i++) {
74 for (int j = i; j < outerDim; j++) {
75 double sum = 0;
76 for (int k = j; k < innerDim; k++) {
77 sum += Loadings.getParameterValue(i, k) * Loadings.getParameterValue(j, k) * Precision.getParameterValue(k, k);
78 }
79 if (i == j) {
80 precision[i][j] = sum + diffusionPrecision.getParameterValue(i, j);
81 } else {
82 precision[i][j] = sum;
83 precision[j][i] = sum;
84 }
85 }
86
87 }
88 }
89
90 private void getMean(int column, double[][] variance, double[] midMean, double[] mean) {
91 MatrixParameterInterface scaledData = LFM.getScaledData();
92 MatrixParameterInterface Precision = LFM.getColumnPrecision();
93 MatrixParameterInterface Loadings = LFM.getLoadings();
94 for (int i = 0; i < Loadings.getRowDimension(); i++) {
95 double sum = 0;
96 for (int j = i; j < Loadings.getColumnDimension(); j++) {
97 sum += Loadings.getParameterValue(i, j) * Precision.getParameterValue(j, j) * scaledData.getParameterValue(j, column);
98 }
99 midMean[i] = sum;
100 }
101 for (int i = 0; i < numFactors; i++) {
102 double sum = 0;
103 for (int j = 0; j < numFactors; j++) {
104 sum += variance[i][j] * midMean[j];
105 }
106 mean[i] = sum;
107 }
108
109 // try {
110 // answer=getPrecision().inverse().product(new Matrix(LFM.getLoadings().getParameterAsMatrix())).product(new Matrix(LFM.getColumnPrecision().getParameterAsMatrix())).product(data);
111 // } catch (IllegalDimension illegalDimension) {
112 // illegalDimension.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
113 // }
114 }
115
116 private void copy(double[] put, int i) {
117 Parameter working = LFM.getFactors().getParameter(i);
118 for (int j = 0; j < working.getSize(); j++) {
119 working.setParameterValueQuietly(j, put[j]);
120 }
121 working.fireParameterChangedEvent();
122 }
123
124 public int getStepCount() {
125 return 0; //To change body of implemented methods use File | Settings | File Templates.
126 }
127
128 @Override
129 public String getOperatorName() {
130 return FACTOR_GIBBS_OPERATOR; //To change body of implemented methods use File | Settings | File Templates.
131 }
132
133 public void randomDraw(int i, double[][] variance) {
134 double[] nextValue;
135 getMean(i, variance, midMean, mean);
136
137 nextValue = MultivariateNormalDistribution.nextMultivariateNormalVariance(mean, variance);
138
139 copy(nextValue, i);
140 }
141
142 @Override
143 public double doOperation() {
144 setupParameters();
145 getPrecision(precision);
146 double[][] variance = (new SymmetricMatrix(precision)).inverse().toComponents();
147 if (randomScan) {
148 int i = MathUtils.nextInt(LFM.getFactors().getColumnDimension());
149 randomDraw(i, variance);
150 }
151 for (int i = 0; i < LFM.getFactors().getColumnDimension(); i++) {
152 randomDraw(i, variance);
153 }
154 LFM.getFactors().fireParameterChangedEvent();
155
156 return 0; //To change body of implemented methods use File | Settings | File Templates.
157 }
158
159
160 }