Debian Codegraph @

src/dr/inference/operators/FactorIndependenceOperator.java

raw symbols SCIP
1 /*
2 * FactorIndependenceOperator.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 FactorIndependenceOperator extends AbstractAdaptableOperator {
41 private static final String FACTOR_OPERATOR = "factorOperator";
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 private double scaleFactor;
50
51 public FactorIndependenceOperator(LatentFactorModel LFM, double weight, boolean randomScan, DiagonalMatrix diffusionPrecision, double scaleFactor, AdaptationMode mode) {
52 super(mode);
53 this.scaleFactor = scaleFactor;
54 this.LFM = LFM;
55 setWeight(weight);
56 this.randomScan = randomScan;
57 this.diffusionPrecision = diffusionPrecision;
58 setupParameters();
59
60 }
61
62 private void setupParameters() {
63 if (numFactors != LFM.getFactorDimension()) {
64 numFactors = LFM.getFactorDimension();
65 mean = new double[numFactors];
66 midMean = new double[numFactors];
67 precision = new double[numFactors][numFactors];
68 }
69 }
70
71 private void getPrecision(double[][] precision) {
72 MatrixParameterInterface Loadings = LFM.getLoadings();
73 MatrixParameter Precision = LFM.getColumnPrecision();
74 int outerDim = Loadings.getRowDimension();
75 int innerDim = Loadings.getColumnDimension();
76 for (int i = 0; i < outerDim; i++) {
77 for (int j = i; j < outerDim; j++) {
78 double sum = 0;
79 for (int k = j; k < innerDim; k++) {
80 sum += Loadings.getParameterValue(i, k) * Loadings.getParameterValue(j, k) * Precision.getParameterValue(k, k);
81 }
82 if (i == j) {
83 precision[i][j] = sum + diffusionPrecision.getParameterValue(i, j);
84 } else {
85 precision[i][j] = sum;
86 precision[j][i] = sum;
87 }
88 }
89
90 }
91 }
92
93 private void getMean(int column, double[][] variance, double[] midMean, double[] mean) {
94 MatrixParameterInterface scaledData = LFM.getScaledData();
95 MatrixParameterInterface Precision = LFM.getColumnPrecision();
96 MatrixParameterInterface Loadings = LFM.getLoadings();
97 for (int i = 0; i < Loadings.getRowDimension(); i++) {
98 double sum = 0;
99 for (int j = i; j < Loadings.getColumnDimension(); j++) {
100 sum += Loadings.getParameterValue(i, j) * Precision.getParameterValue(j, j) * scaledData.getParameterValue(j, column);
101 }
102 midMean[i] = sum;
103 }
104 for (int i = 0; i < numFactors; i++) {
105 double sum = 0;
106 for (int j = 0; j < numFactors; j++) {
107 sum += variance[i][j] * midMean[j];
108 }
109 mean[i] = sum;
110 }
111
112 // try {
113 // answer=getPrecision().inverse().product(new Matrix(LFM.getLoadings().getParameterAsMatrix())).product(new Matrix(LFM.getColumnPrecision().getParameterAsMatrix())).product(data);
114 // } catch (IllegalDimension illegalDimension) {
115 // illegalDimension.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
116 // }
117 }
118
119 private void copy(double[] put, int i) {
120 Parameter working = LFM.getFactors().getParameter(i);
121 for (int j = 0; j < working.getSize(); j++) {
122 working.setParameterValueQuietly(j, put[j]);
123 }
124 working.fireParameterChangedEvent();
125 }
126
127 public int getStepCount() {
128 return 0; //To change body of implemented methods use File | Settings | File Templates.
129 }
130
131 @Override
132 public String getOperatorName() {
133 return FACTOR_OPERATOR; //To change body of implemented methods use File | Settings | File Templates.
134 }
135
136 public void randomDraw(int i, double[][] variance) {
137 double[] nextValue;
138 getMean(i, variance, midMean, mean);
139
140 nextValue = MultivariateNormalDistribution.nextMultivariateNormalVariance(mean, variance, scaleFactor);
141
142 copy(nextValue, i);
143 }
144
145 @Override
146 public double doOperation() {
147 setupParameters();
148 getPrecision(precision);
149 double[][] variance = (new SymmetricMatrix(precision)).inverse().toComponents();
150 if (randomScan) {
151 int i = MathUtils.nextInt(LFM.getFactors().getColumnDimension());
152 randomDraw(i, variance);
153 }
154 for (int i = 0; i < LFM.getFactors().getColumnDimension(); i++) {
155 randomDraw(i, variance);
156 }
157 LFM.getFactors().fireParameterChangedEvent();
158
159 return 0; //To change body of implemented methods use File | Settings | File Templates.
160 }
161
162
163 @Override
164 public double getAdaptableParameter() {
165 return Math.log(scaleFactor);
166 }
167
168 @Override
169 public void setAdaptableParameter(double value) {
170 scaleFactor = Math.exp(value);
171 }
172
173 @Override
174 public double getRawParameter() {
175 return scaleFactor;
176 }
177
178 @Override
179 public String getAdaptableParameterName() {
180 return "scaleFactor";
181 }
182 }