Debian Codegraph @

src/dr/inference/operators/LFMTargetedSearchOperator.java

raw symbols SCIP
1 package dr.inference.operators;
2
3 import dr.inference.distribution.NormalDistributionModel;
4 import dr.inference.model.MatrixParameterInterface;
5 import dr.inference.model.Parameter;
6 import dr.math.MathUtils;
7 import dr.math.distributions.NormalDistribution;
8
9
10 import java.util.ArrayList;
11
12 public class LFMTargetedSearchOperator extends SimpleMCMCOperator {
13 public LFMTargetedSearchOperator(Double weight, MatrixParameterInterface SparseMatrix, ArrayList<MatrixParameterInterface> MatrixList,
14 MatrixParameterInterface FactorsMatrix, ArrayList<MatrixParameterInterface> FactorsMatrixList,
15 MatrixParameterInterface LoadingsMatrix, ArrayList<MatrixParameterInterface> LoadingsMatrixList,
16 MatrixParameterInterface cutoffs){
17 setWeight(weight);
18 this.SparseMatrix = SparseMatrix;
19 this.MatrixList = MatrixList;
20 this.FactorsMatrix = FactorsMatrix;
21 this.FactorsMatrixList = FactorsMatrixList;
22 this.LoadingsMatrix = LoadingsMatrix;
23 this.LoadingsMatrixList = LoadingsMatrixList;
24 // Parameter mean = new Parameter.Default(1);
25 // mean.setParameterValue(0, 0);
26 // Parameter SD = new Parameter.Default(1);
27 // SD.setParameterValue(0, this.VAR);
28 this.error = new NormalDistribution(0, VAR);
29 this.cutoffs = cutoffs;
30 }
31
32 @Override
33 public String getOperatorName() {
34 return "Latent Factor Model Targeted Search Operator";
35 }
36
37 @Override
38 public double doOperation() {
39 final int DRAW = MathUtils.nextInt(MatrixList.size());
40 lastDraw = DRAW;
41 double hastings = getHastings();
42 for (int i = 0; i < SparseMatrix.getDimension(); i++) {
43 if(MathUtils.nextDouble() < PROBABILITY)
44 SparseMatrix.setParameterValue(i, MatrixList.get(DRAW).getParameterValue(i));
45 else
46 SparseMatrix.setParameterValue(i, MathUtils.nextInt(2));
47 }
48 for (int i = 0; i < FactorsMatrix.getDimension(); i++) {
49 FactorsMatrix.setParameterValue(i, FactorsMatrixList.get(DRAW).getParameterValue(i) + (Double) error.nextRandom());
50 }
51 for (int i = 0; i < LoadingsMatrix.getDimension(); i++) {
52 boolean badDraw = true;
53 int count = 0;
54 while(badDraw || count == 10){
55 double draw = LoadingsMatrixList.get(DRAW).getParameterValue(i) + (Double) error.nextRandom();
56 if(-Math.sqrt(cutoffs.getParameterValue(i)) < draw && Math.sqrt(cutoffs.getParameterValue(i)) > draw) {
57 badDraw = false;
58 LoadingsMatrix.setParameterValue(i, draw);
59 }
60 else{
61 count ++;
62 }
63 }
64 }
65
66 hastings -= getHastings();
67
68
69 return hastings;
70 }
71
72 private double getHastings(){
73 int denom = MatrixList.size();
74 double hastings = 0;
75 for (int i = 0; i < SparseMatrix.getDimension(); i++) {
76 int sum = 0;
77 for (int j = 0; j < MatrixList.size(); j++) {
78 if(MatrixList.get(j).getParameterValue(i) != SparseMatrix.getParameterValue(i)){
79 sum ++;
80 }
81 }
82 hastings += Math.log((sum / denom) * PROBABILITY + .5 * (1 - PROBABILITY));
83 }
84 for (int i = 0; i < FactorsMatrix.getDimension(); i++) {
85 double sum = 0;
86 for (int j = 0; j < FactorsMatrixList.size(); j++) {
87 sum += error.logPdf(FactorsMatrix.getParameterValue(i) - FactorsMatrixList.get(j).getParameterValue(i)) - Math.log(FactorsMatrixList.size());
88 }
89 hastings += sum;
90 }
91 for (int i = 0; i < LoadingsMatrix.getDimension(); i++) {
92 double sum = 0;
93 for (int j = 0; j < LoadingsMatrixList.size(); j++) {
94 sum += error.logPdf(LoadingsMatrix.getParameterValue(i) - LoadingsMatrixList.get(j).getParameterValue(i)) -
95 Math.log(error.cdf(Math.sqrt(cutoffs.getParameterValue(i)) - LoadingsMatrixList.get(j).getParameterValue(i))
96 - error.cdf(-Math.sqrt(cutoffs.getParameterValue(i)) - LoadingsMatrixList.get(j).getParameterValue(i)))
97 - Math.log(FactorsMatrixList.size());
98 }
99 hastings += sum;
100 }
101
102
103 return hastings;
104 }
105
106
107 @Override
108 public void accept(double deviation) {
109 super.accept(deviation);
110 System.out.println(lastDraw);
111 }
112
113 ArrayList<MatrixParameterInterface> MatrixList;
114 MatrixParameterInterface SparseMatrix;
115 ArrayList<MatrixParameterInterface> FactorsMatrixList;
116 MatrixParameterInterface FactorsMatrix;
117 ArrayList<MatrixParameterInterface> LoadingsMatrixList;
118 MatrixParameterInterface LoadingsMatrix;
119 NormalDistribution error;
120 MatrixParameterInterface cutoffs;
121 private final double PROBABILITY = .9;
122 private final double VAR = .15;
123 private int lastDraw;
124 }