| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 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; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public String getOperatorName() { |
| 133 |
return FACTOR_OPERATOR; |
| 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; |
| 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 |
} |