| 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.oldevomodel.sitemodel; |
| 27 |
|
| 28 |
import dr.inference.model.*; |
| 29 |
import dr.inference.distribution.ParametricDistributionModel; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class DiscretizedLociRates extends AbstractModel { |
| 37 |
|
| 38 |
private CompoundParameter lociRates; |
| 39 |
private Parameter rateCategoryParameter; |
| 40 |
private ParametricDistributionModel distrModel; |
| 41 |
private double normalizeRateTo; |
| 42 |
private double[] rates; |
| 43 |
private boolean normalize; |
| 44 |
private int categoryCount; |
| 45 |
private double scaleFactor; |
| 46 |
private boolean completeSetup; |
| 47 |
|
| 48 |
|
| 49 |
public DiscretizedLociRates( |
| 50 |
CompoundParameter lociRates, |
| 51 |
Parameter rateCategoryParameter, |
| 52 |
ParametricDistributionModel model, |
| 53 |
boolean normalize, |
| 54 |
double normalizeLociRateTo, |
| 55 |
int categoryCount) { |
| 56 |
super("DiscretizedLociRatesModel"); |
| 57 |
this.lociRates = lociRates; |
| 58 |
|
| 59 |
this.rateCategoryParameter = rateCategoryParameter; |
| 60 |
|
| 61 |
Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0, rateCategoryParameter.getDimension()); |
| 62 |
this.rateCategoryParameter.addBounds(bound); |
| 63 |
|
| 64 |
this.distrModel = model; |
| 65 |
this.normalizeRateTo = normalizeLociRateTo; |
| 66 |
this.normalize = normalize; |
| 67 |
this.categoryCount = categoryCount; |
| 68 |
rates = new double[categoryCount]; |
| 69 |
completeSetup = true; |
| 70 |
setupRates(); |
| 71 |
|
| 72 |
|
| 73 |
addModel(distrModel); |
| 74 |
addVariable(this.rateCategoryParameter); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
private void setupRates(){ |
| 79 |
if(completeSetup){ |
| 80 |
|
| 81 |
double categoryIntervalSize = 1.0/categoryCount; |
| 82 |
for(int i = 0; i < categoryCount; i++){ |
| 83 |
rates[i]= distrModel.quantile((i+0.5)*categoryIntervalSize); |
| 84 |
} |
| 85 |
} |
| 86 |
if(normalize){ |
| 87 |
computeFactor(); |
| 88 |
} |
| 89 |
|
| 90 |
completeSetup = false; |
| 91 |
int lociCount = rateCategoryParameter.getDimension(); |
| 92 |
for(int i = 0; i < lociCount; i ++){ |
| 93 |
lociRates.setParameterValue(i,rates[(int)rateCategoryParameter.getParameterValue(i)]*scaleFactor); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 98 |
if (model == distrModel) { |
| 99 |
completeSetup = true; |
| 100 |
setupRates(); |
| 101 |
|
| 102 |
fireModelChanged(); |
| 103 |
}else if (model == rateCategoryParameter) { |
| 104 |
|
| 105 |
setupRates(); |
| 106 |
fireModelChanged(null, index); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 111 |
|
| 112 |
setupRates(); |
| 113 |
fireModelChanged(null, index); |
| 114 |
} |
| 115 |
|
| 116 |
protected void storeState() { |
| 117 |
} |
| 118 |
protected void acceptState() { |
| 119 |
} |
| 120 |
protected void restoreState() { |
| 121 |
|
| 122 |
} |
| 123 |
private void computeFactor(){ |
| 124 |
double sumRates = 0.0; |
| 125 |
int lociCount = rateCategoryParameter.getDimension(); |
| 126 |
for(int i = 0; i < lociCount; i++){ |
| 127 |
sumRates += rates[(int)rateCategoryParameter.getParameterValue(i)]; |
| 128 |
} |
| 129 |
scaleFactor = normalizeRateTo/(sumRates/lociCount); |
| 130 |
} |
| 131 |
|
| 132 |
} |