Debian Codegraph @

src/dr/inference/operators/hmc/deprecated/LatentFactorHamiltonianMC.java

raw symbols SCIP
1 /*
2 * LatentFactorHamiltonianMC.java
3 *
4 * Copyright (c) 2002-2017 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.hmc.deprecated;
27
28 import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood;
29 import dr.inference.model.*;
30 import dr.inference.operators.AdaptationMode;
31 import dr.math.MathUtils;
32
33 /**
34 * Created by max on 12/2/15.
35 */
36 @Deprecated
37 public class LatentFactorHamiltonianMC extends AbstractHamiltonianMCOperator{
38 private LatentFactorModel lfm;
39 private FullyConjugateMultivariateTraitLikelihood tree;
40 private MatrixParameterInterface factors;
41 private MatrixParameterInterface loadings;
42 private MatrixParameterInterface Precision;
43 private int nfac;
44 private int ntaxa;
45 private int ntraits;
46 private double stepSize;
47 private int nSteps;
48 private boolean diffusionSN=true;
49 private Parameter missingIndicator;
50
51
52 public LatentFactorHamiltonianMC(LatentFactorModel lfm, FullyConjugateMultivariateTraitLikelihood tree, double weight, AdaptationMode mode, double stepSize, int nSteps, double momentumSd){
53 super(mode, momentumSd);
54 setWeight(weight);
55 this.lfm = lfm;
56 this.tree = tree;
57 this.factors = lfm.getFactors();
58 this.loadings = lfm.getLoadings();
59 this.Precision = lfm.getColumnPrecision();
60 nfac = lfm.getFactorDimension();
61 ntaxa = lfm.getFactors().getColumnDimension();
62 ntraits = Precision.getRowDimension();
63 this.stepSize = stepSize;
64 this.nSteps = nSteps;
65 this.missingIndicator = lfm.getMissingIndicator();
66 }
67
68
69
70 @Override
71 public double getAdaptableParameter() {
72 return Math.log(stepSize);
73 }
74
75 @Override
76 public void setAdaptableParameter(double value) {
77 stepSize = Math.exp(value);
78 }
79
80 @Override
81 public double getRawParameter() {
82 return stepSize;
83 }
84
85 @Override
86 public String getOperatorName() {
87 return "Latent Factor Hamiltonian Monte Carlo";
88 }
89
90 private double[][] getMatrix(double[] residual){
91 double answer[][] = new double[this.nfac][this.ntaxa];
92 for (int i = 0; i < this.nfac ; i++) {
93 for (int j = 0; j < ntraits; j++) {
94 for (int k = 0; k < ntaxa; k++) {
95 if(missingIndicator == null || missingIndicator.getParameterValue(k * ntraits + j) != 1){
96 answer[i][k] -= loadings.getParameterValue(j, i) * Precision.getParameterValue(j, j) *
97 residual[k * ntraits + j];
98 }
99 }
100 }
101 }
102 return answer;
103 }
104
105 private double[][] getGradient(double[][] mean,
106 // double[][] prec,
107 double[] precfactor){
108 double[] residual = lfm.getResidual();
109 double[][] derivative = getMatrix(residual);
110
111 // if(diffusionSN){
112 for (int i = 0; i < nfac ; i++) {
113 for (int j = 0; j < ntaxa; j++) {
114 derivative[i][j] += (factors.getParameterValue(i, j) - mean[j][i]) * precfactor[j];
115 }
116
117 }
118 // }
119 // else{
120 // for (int i = 0; i <mean.length ; i++) {
121 // double sumi = 0;
122 // for (int j = 0; j <mean.length ; j++) {
123 // sumi += prec[i][j]*(factors.getParameterValue(j, randel) - mean[j]);
124 // }
125 // derivative[i] -= sumi;
126 // }
127 // }
128 return derivative;
129 }
130
131 @Override
132 public double doOperation() {
133
134
135
136 double[][] mean = tree.getConditionalMeans();
137 double precfactor[];
138 // double[][] prec = null;
139 double rand = MathUtils.nextDouble();
140 // System.out.println(rand);
141 double functionalStepSize = stepSize;
142
143 // if(diffusionSN){
144 precfactor = tree.getPrecisionFactors();
145 // }
146 // else {
147 // prec = tree.getConditionalPrecision(randel);
148 // }
149
150 double[][] derivative = getGradient(mean,
151 // prec,
152 precfactor);
153 drawMomentum(lfm.getFactorDimension() * ntaxa);
154
155 double prop=0;
156 for (int i = 0; i < momentum.length ; i++) {
157 prop += momentum[i] * momentum[i] / (2 * getMomentumSd() * getMomentumSd());
158 }
159
160
161 for (int i = 0; i <lfm.getFactorDimension() ; i++) {
162 for (int j = 0; j < ntaxa; j++) {
163 momentum[i * ntaxa + j] = momentum[i * ntaxa + j] - functionalStepSize / 2 * derivative[i][j];
164 }
165
166 }
167
168 for (int i = 0; i <nSteps ; i++) {
169 for (int j = 0; j <lfm.getFactorDimension() ; j++) {
170 for (int k = 0; k < ntaxa; k++) {
171 factors.setParameterValueQuietly(j, k, factors.getParameterValue(j, k) + functionalStepSize * momentum[j * ntaxa + k] / (getMomentumSd() * getMomentumSd()));
172 }
173 }
174 factors.fireParameterChangedEvent();
175
176 if(i != nSteps){
177 derivative = getGradient(mean, precfactor);
178
179 for (int j = 0; j < lfm.getFactorDimension() ; j++) {
180 for (int k = 0; k < ntaxa; k++) {
181 momentum[j * ntaxa + k] = momentum[j * ntaxa + k] - functionalStepSize * derivative[j][k];
182 }
183 }
184 }
185 }
186
187 derivative = getGradient(mean , precfactor);
188 for (int i = 0; i <lfm.getFactorDimension() ; i++) {
189 for (int j = 0; j < ntaxa; j++) {
190 momentum[i * ntaxa + j] = momentum[i * ntaxa + j] - functionalStepSize / 2 * derivative[i][j];
191 }
192 }
193
194 double res=0;
195 for (int i = 0; i <momentum.length ; i++) {
196 res += momentum[i] * momentum[i] / (2 * getMomentumSd() * getMomentumSd());
197 }
198 return prop - res;
199 }
200 }