| 1 | package dr.inference.distribution; |
| 2 | |
| 3 | import dr.inference.model.Parameter; |
| 4 | import dr.inference.operators.GibbsOperator; |
| 5 | import dr.inference.operators.SimpleMCMCOperator; |
| 6 | import dr.math.distributions.InverseGammaDistribution; |
| 7 | |
| 8 | public class ShrinkageGibbsOperator extends SimpleMCMCOperator implements GibbsOperator{ |
| 9 | IndependentInverseGammaDistributionModel localPrior; |
| 10 | IndependentInverseGammaDistributionModel globalPrior; |
| 11 | Parameter data; |
| 12 | double pathParameter; |
| 13 | final boolean usePathParameter = true; |
| 14 | |
| 15 | public ShrinkageGibbsOperator(double weight, IndependentInverseGammaDistributionModel localPrior, IndependentInverseGammaDistributionModel globalPrior, Parameter data){ |
| 16 | setWeight(weight); |
| 17 | this.localPrior = localPrior; |
| 18 | this.globalPrior = globalPrior; |
| 19 | this.data = data; |
| 20 | this.pathParameter = 1; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | public int getStepCount() { |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | public String getOperatorName() { |
| 30 | return "ShrinkageGibbsOperator"; |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public double doOperation() { |
| 35 | Parameter local = localPrior.getData(); |
| 36 | Parameter localShape = localPrior.getShape(); |
| 37 | Parameter localAugmented = localPrior.getScale(); |
| 38 | |
| 39 | Parameter global = globalPrior.getData(); |
| 40 | Parameter globalShape = globalPrior.getShape(); |
| 41 | Parameter globalAugmented = globalPrior.getScale(); |
| 42 | |
| 43 | for (int i = 0; i < local.getDimension(); i++) { |
| 44 | double scale = localAugmented.getParameterValue(i) + data.getParameterValue(i) * data.getParameterValue(i) / (2 * global.getParameterValue(0)); |
| 45 | double draw = InverseGammaDistribution.nextInverseGamma(localShape.getParameterValue(i) + .5, scale); |
| 46 | local.setParameterValueQuietly(i, draw); |
| 47 | } |
| 48 | local.fireParameterChangedEvent(); |
| 49 | |
| 50 | double shape = local.getDimension() / 2 + globalShape.getParameterValue(0); |
| 51 | double scale = globalAugmented.getParameterValue(0); |
| 52 | for (int i = 0; i < local.getDimension(); i++) { |
| 53 | scale += .5 * (data.getParameterValue(i) * data.getParameterValue(i)) / local.getParameterValue(i); |
| 54 | } |
| 55 | double draw = InverseGammaDistribution.nextInverseGamma(shape, scale); |
| 56 | global.setParameterValue(0, draw); |
| 57 | |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | } |