Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * LoadingsHamiltonianMC.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.inference.distribution.MomentDistributionModel;
29 import dr.inference.model.*;
30 import dr.inference.operators.AdaptationMode;
31
32 /**
33 * Created by max on 1/11/16.
34 */
35 @Deprecated
36 public class LoadingsHamiltonianMC extends AbstractHamiltonianMCOperator {
37 private LatentFactorModel lfm;
38 private MomentDistributionModel prior;
39 private MatrixParameterInterface factors;
40 private MatrixParameterInterface loadings;
41 private MatrixParameterInterface Precision;
42 private int nfac;
43 private int ntaxa;
44 private int ntraits;
45 private double stepSize;
46 private int nSteps;
47
48
49 public LoadingsHamiltonianMC(LatentFactorModel lfm, MomentDistributionModel prior, double weight, AdaptationMode mode, double stepSize, int nSteps, double momentumSd, MatrixParameterInterface loadings){
50 super(mode , momentumSd);
51 setWeight(weight);
52 this.lfm = lfm;
53 this.prior = prior;
54 this.factors = lfm.getFactors();
55 this.loadings = loadings;
56 this.Precision = lfm.getColumnPrecision();
57 nfac = lfm.getFactorDimension();
58 ntaxa = lfm.getFactors().getColumnDimension();
59 ntraits = Precision.getRowDimension();
60 this.stepSize = stepSize;
61 this.nSteps = nSteps;
62 }
63
64 @Override
65 public double getAdaptableParameter() {
66 return 0;
67 }
68
69 @Override
70 public void setAdaptableParameter(double value) {
71
72 }
73
74 @Override
75 public double getRawParameter() {
76 return 0;
77 }
78
79 @Override
80 public String getOperatorName() {
81 return "LoadingsHamiltonianMC";
82 }
83
84 @Override
85 public double doOperation() {
86
87
88 double[][] derivative = getGradient();
89 drawMomentum(lfm.getFactorDimension() * ntraits);
90 double functionalStepSize = stepSize;
91
92 double prop=0;
93 for (int i = 0; i < momentum.length ; i++) {
94 prop += momentum[i] * momentum[i] / (2 * getMomentumSd() * getMomentumSd());
95 }
96
97 for (int i = 0; i < lfm.getFactorDimension() ; i++) {
98 for (int j = 0; j < ntraits ; j++) {
99 momentum[i * ntraits + j] = momentum[i * ntraits + j] - functionalStepSize / 2 * derivative[j][i];
100 }
101
102 }
103
104 for (int i = 0; i <nSteps ; i++) {
105 for (int j = 0; j <lfm.getFactorDimension() ; j++) {
106 for (int k = 0; k <ntraits ; k++) {
107 loadings.setParameterValueQuietly(k, j, loadings.getParameterValue(k, j) + functionalStepSize * momentum[j * ntraits + k]); }
108
109 }
110 loadings.fireParameterChangedEvent(-1, Parameter.ChangeType.ALL_VALUES_CHANGED);
111
112
113 if(i != nSteps){
114 derivative = getGradient();
115
116 for (int j = 0; j < lfm.getFactorDimension() ; j++) {
117 for (int k = 0; k <ntraits ; k++) {
118 momentum[j * ntraits + k] = momentum[j * ntraits + k] - functionalStepSize * derivative[k][j];
119 }
120
121 }
122 }
123 }
124
125 derivative=getGradient();
126 for (int i = 0; i < lfm.getFactorDimension() ; i++) {
127 for (int j = 0; j < ntraits ; j++) {
128 momentum[i * ntraits + j] = momentum[i * ntraits + j ] - functionalStepSize / 2 * derivative[j][i];
129 }
130
131 }
132 double res=0;
133 for (int i = 0; i <momentum.length ; i++) {
134 res+=momentum[i] * momentum[i] / (2 * getMomentumSd() * getMomentumSd());
135 }
136 return prop - res;
137 }
138
139
140
141 private double[][] getLFMDerivative(){
142 double[] residual=lfm.getResidual();
143 double[][] answer= new double[ntraits][lfm.getFactorDimension()];
144 for (int i = 0; i < ntaxa; i++) {
145 for (int j = 0; j < ntraits; j++) {
146 for (int k = 0; k < lfm.getFactorDimension() ; k++) {
147 answer[j][k] -= residual[i * ntaxa + j] * factors.getParameterValue(k , i);
148 }
149 }
150
151 }
152 for (int i = 0; i < ntraits ; i++) {
153 for (int j = 0; j < lfm.getFactorDimension() ; j++) {
154 answer[i][j] *= Precision.getParameterValue(i , i);
155 }
156
157 }
158 return answer;
159 }
160
161 private double[][] getGradient(){
162 double[][] answer = getLFMDerivative();
163 for (int i = 0; i < loadings.getRowDimension(); i++) {
164 for (int j = 0; j < loadings.getColumnDimension(); j++) {
165 answer[i][j] += 2 / loadings.getParameterValue(i, j) + (loadings.getParameterValue(i, j) - prior.getMean()[0]) / prior.getScaleMatrix()[0][0];
166 }
167
168 }
169 return answer;
170 }
171
172 }