| 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.evomodel.coalescent.operators; |
| 27 |
|
| 28 |
import dr.evomodel.coalescent.GMRFSkyrideLikelihood; |
| 29 |
import dr.evomodelxml.coalescent.operators.GMRFSkyrideFixedEffectsGibbsOperatorParser; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.operators.GibbsOperator; |
| 32 |
import dr.inference.operators.SimpleMCMCOperator; |
| 33 |
import dr.math.distributions.MultivariateDistribution; |
| 34 |
import no.uib.cipr.matrix.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class GMRFSkyrideFixedEffectsGibbsOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 46 |
|
| 47 |
private GMRFSkyrideLikelihood gmrfLikelihood; |
| 48 |
|
| 49 |
private DenseVector mean; |
| 50 |
private DenseMatrix precision; |
| 51 |
|
| 52 |
private int fieldLength; |
| 53 |
private int dim; |
| 54 |
|
| 55 |
public GMRFSkyrideFixedEffectsGibbsOperator(Parameter param, |
| 56 |
GMRFSkyrideLikelihood gmrfLikelihood, MultivariateDistribution prior, double weight) { |
| 57 |
this.gmrfLikelihood = gmrfLikelihood; |
| 58 |
mean = new DenseVector(prior.getMean()); |
| 59 |
precision = new DenseMatrix(prior.getScaleMatrix()); |
| 60 |
|
| 61 |
this.fieldLength = gmrfLikelihood.getPopSizeParameter().getDimension(); |
| 62 |
this.dim = param.getDimension(); |
| 63 |
|
| 64 |
this.gmrfLikelihood = gmrfLikelihood; |
| 65 |
|
| 66 |
setWeight(weight); |
| 67 |
} |
| 68 |
|
| 69 |
public double doOperation() { |
| 70 |
|
| 71 |
DenseMatrix X = new DenseMatrix(gmrfLikelihood.getDesignMatrix().getParameterAsMatrix()); |
| 72 |
SymmTridiagMatrix Q = gmrfLikelihood.getScaledWeightMatrix(gmrfLikelihood.getPrecisionParameter().getParameterValue(0), |
| 73 |
gmrfLikelihood.getLambdaParameter().getParameterValue(0)); |
| 74 |
DenseVector gamma = new DenseVector(gmrfLikelihood.getPopSizeParameter().getParameterValues()); |
| 75 |
|
| 76 |
Parameter.Abstract beta = (Parameter.Abstract) gmrfLikelihood.getBetaParameter(); |
| 77 |
|
| 78 |
|
| 79 |
DenseMatrix gibbsPrecision = precision.copy(); |
| 80 |
UpperSPDDenseMatrix gibbsVariance; |
| 81 |
DenseVector gibbsMean = new DenseVector(dim); |
| 82 |
DenseMatrix workingMatrix = new DenseMatrix(dim, fieldLength); |
| 83 |
DenseVector workingVector = new DenseVector(dim); |
| 84 |
|
| 85 |
|
| 86 |
X.transAmultAdd(Q, workingMatrix); |
| 87 |
workingMatrix.multAdd(X, gibbsPrecision); |
| 88 |
|
| 89 |
precision.mult(mean, workingVector); |
| 90 |
workingMatrix.multAdd(gamma, workingVector); |
| 91 |
|
| 92 |
workingMatrix = Matrices.identity(dim); |
| 93 |
|
| 94 |
gibbsPrecision.solve(Matrices.identity(dim), workingMatrix); |
| 95 |
gibbsVariance = new UpperSPDDenseMatrix(workingMatrix); |
| 96 |
gibbsVariance.mult(workingVector, gibbsMean); |
| 97 |
|
| 98 |
|
| 99 |
DenseVector betaNew = GMRFSkyrideBlockUpdateOperator.getMultiNormal(gibbsMean, gibbsVariance); |
| 100 |
|
| 101 |
for (int i = 0; i < dim; i++) { |
| 102 |
beta.setParameterValueQuietly(i, betaNew.get(i)); |
| 103 |
} |
| 104 |
|
| 105 |
beta.fireParameterChangedEvent(); |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
public int getStepCount() { |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
public String getPerformanceSuggestion() { |
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public final String getOperatorName() { |
| 122 |
return GMRFSkyrideFixedEffectsGibbsOperatorParser.GMRF_GIBBS_OPERATOR; |
| 123 |
} |
| 124 |
|
| 125 |
} |