Debian Codegraph @

src/dr/inference/operators/FactorTreeGibbsOperator.java

raw symbols SCIP
1 package dr.inference.operators;
2
3 import dr.evomodel.continuous.GibbsSampleFromTreeInterface;
4 import dr.inference.model.LatentFactorModel;
5 import dr.inference.model.MatrixParameterInterface;
6 import dr.inference.model.Parameter;
7 import dr.math.MathUtils;
8 import dr.math.distributions.MultivariateNormalDistribution;
9 import dr.math.matrixAlgebra.Matrix;
10 import dr.math.matrixAlgebra.SymmetricMatrix;
11
12 /**
13 * Created by max on 5/16/16.
14 */
15 public class FactorTreeGibbsOperator extends SimpleMCMCOperator implements PathDependentOperator, GibbsOperator {
16
17 private final LatentFactorModel lfm;
18 private double pathParameter = 1;
19 private final GibbsSampleFromTreeInterface tree;
20 private final GibbsSampleFromTreeInterface workingTree;
21 private final MatrixParameterInterface factors;
22 private final MatrixParameterInterface errorPrec;
23 private final boolean randomScan;
24 private final Parameter missingIndicator;
25
26 public FactorTreeGibbsOperator(double weight, LatentFactorModel lfm, GibbsSampleFromTreeInterface tree, Boolean randomScan){
27 setWeight(weight);
28 this.tree = tree;
29 this.lfm = lfm;
30 this.factors = lfm.getFactors();
31 errorPrec = lfm.getColumnPrecision();
32 this.randomScan = randomScan;
33 this.workingTree = null;
34 missingIndicator = lfm.getMissingIndicator();
35 }
36
37 @Override
38 public String getOperatorName() {
39 return "Factor Tree Gibbs Operator";
40 }
41
42 @Override
43 public double doOperation() {
44 if(randomScan){
45 int column = MathUtils.nextInt(factors.getColumnDimension());
46 MultivariateNormalDistribution mvn = getMVN(column);
47 double[] draw = (double[]) mvn.nextRandom();
48 for (int i = 0; i < factors.getRowDimension(); i++) {
49 factors.setParameterValue(i, column, draw[i]);
50 }
51 }
52 else{
53 for (int i = 0; i < factors.getColumnDimension(); i++) {
54 MultivariateNormalDistribution mvn = getMVN(i);
55 double[] draw = (double[]) mvn.nextRandom();
56 for (int j = 0; j < factors.getRowDimension(); j++) {
57 factors.setParameterValue(j, i, draw[j]);
58 }
59 }
60 }
61
62 return 0;
63 }
64
65 MultivariateNormalDistribution getMVN(int column){
66 double[][] precision = getPrecision(column);
67 double[] mean = getMean(column, precision);
68 return new MultivariateNormalDistribution(mean, precision);
69 }
70
71 double[][] getPrecision(int column){
72 double [][] treePrec = getTreePrec(column);
73 for (int i = 0; i < lfm.getLoadings().getColumnDimension(); i++) {
74 for (int j = i; j < lfm.getLoadings().getColumnDimension(); j++) {
75 for (int k = 0; k < lfm.getLoadings().getRowDimension(); k++) {
76 if(missingIndicator == null || missingIndicator.getParameterValue(column * lfm.getLoadings().getRowDimension() + k) != 1){
77 treePrec[i][j] += lfm.getLoadings().getParameterValue(k, i) * errorPrec.getParameterValue(k, k) * lfm.getLoadings().getParameterValue(k, j) * pathParameter;}
78 }
79 treePrec[j][i] = treePrec[i][j];
80 }
81 }
82 return treePrec;
83 }
84
85 double[] getMean(int column, double[][] precision){
86 Matrix variance = (new SymmetricMatrix(precision)).inverse();
87 double[] midMean = new double[lfm.getLoadings().getColumnDimension()];
88 double[] condMean = getTreeMean(column);
89 double[][] condPrec = getTreePrec(column);
90 for (int i = 0; i < midMean.length; i++) {
91 // for (int j = 0; j < midMean.length; j++) {
92 midMean [i] += condPrec[i][i] * condMean[i];
93 // }
94 }
95 for (int i = 0; i < lfm.getLoadings().getRowDimension(); i++) {
96 for (int j = 0; j < lfm.getLoadings().getColumnDimension(); j++) {
97 if(missingIndicator == null || missingIndicator.getParameterValue(column * lfm.getScaledData().getRowDimension() + i) != 1)
98 midMean[j] += lfm.getScaledData().getParameterValue(i, column) * errorPrec.getParameterValue(i,i) * lfm.getLoadings().getParameterValue(i, j) * pathParameter;
99 }
100 }
101 double[] mean = new double[midMean.length];
102 for (int i = 0; i < mean.length; i++) {
103 for (int j = 0; j < mean.length; j++) {
104 mean[i] += variance.component(i, j) * midMean[j];
105 }
106
107 }
108 return mean;
109 }
110
111 public double[][] getTreePrec(int column){
112 double answerFactor = tree.getPrecisionFactor(column);
113 double[][] answer = new double[factors.getRowDimension()][factors.getRowDimension()];
114
115 for (int i = 0; i < factors.getRowDimension(); i++) {
116 answer[i][i] = answerFactor;
117 }
118
119 if (workingTree != null) {
120 double[][] temp = workingTree.getConditionalPrecision(column);
121 for (int i = 0; i < answer.length; i++) {
122 for (int j = 0; j < answer.length; j++) {
123 answer[i][j] = answer[i][j] * pathParameter + temp[i][j] * (1 - pathParameter);
124 }
125
126 }
127 }
128
129 return answer;
130 }
131
132 public double[] getTreeMean(int column){
133 double[] answer = tree.getConditionalMean(column);
134
135 if (workingTree != null) {
136 double[] temp = workingTree.getConditionalMean(column);
137 for (int i = 0; i < answer.length; i++) {
138 answer[i] = answer[i] * pathParameter + temp[i] * (1 - pathParameter);
139 }
140 }
141
142 return answer;
143 }
144
145 @Override
146 public void setPathParameter(double beta) {
147 pathParameter = beta;
148 }
149 }