| 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.treedatalikelihood.continuous; |
| 27 |
|
| 28 |
import dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType; |
| 29 |
import dr.inference.model.*; |
| 30 |
|
| 31 |
import java.util.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class ContinuousTraitDataModel extends AbstractModel implements ContinuousTraitPartialsProvider { |
| 37 |
|
| 38 |
private final CompoundParameter parameter; |
| 39 |
private final List<Integer> missingIndices; |
| 40 |
private final List<Integer> originalMissingIndices; |
| 41 |
|
| 42 |
private final int numTraits; |
| 43 |
private final int dimTrait; |
| 44 |
private final PrecisionType precisionType; |
| 45 |
|
| 46 |
public ContinuousTraitDataModel(String name, |
| 47 |
CompoundParameter parameter, |
| 48 |
List<Integer> missingIndices, |
| 49 |
boolean useMissingIndices, |
| 50 |
final int dimTrait, PrecisionType precisionType) { |
| 51 |
super(name); |
| 52 |
this.parameter = parameter; |
| 53 |
this.originalMissingIndices = missingIndices; |
| 54 |
this.missingIndices = (useMissingIndices? missingIndices : new ArrayList<Integer>()); |
| 55 |
addVariable(parameter); |
| 56 |
|
| 57 |
this.dimTrait = dimTrait; |
| 58 |
this.numTraits = getParameter().getParameter(0).getDimension() / dimTrait; |
| 59 |
this.precisionType = precisionType; |
| 60 |
} |
| 61 |
|
| 62 |
public boolean bufferTips() { return true; } |
| 63 |
|
| 64 |
public int getTraitCount() { return numTraits; } |
| 65 |
|
| 66 |
public int getTraitDimension() { return dimTrait; } |
| 67 |
|
| 68 |
public PrecisionType getPrecisionType() { |
| 69 |
return precisionType; |
| 70 |
} |
| 71 |
|
| 72 |
public String getName() { return super.getModelName(); } |
| 73 |
|
| 74 |
public CompoundParameter getParameter() { return parameter; } |
| 75 |
|
| 76 |
public List<Integer> getMissingIndices() { return missingIndices; } |
| 77 |
|
| 78 |
List<Integer> getOriginalMissingIndices() { return originalMissingIndices; } |
| 79 |
|
| 80 |
@Override |
| 81 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 87 |
if (variable == parameter) { |
| 88 |
if (type == Parameter.ChangeType.VALUE_CHANGED) { |
| 89 |
fireModelChanged(this, getTaxonIndex(index)); |
| 90 |
} else if (type == Parameter.ChangeType.ALL_VALUES_CHANGED){ |
| 91 |
|
| 92 |
fireModelChanged(this); |
| 93 |
|
| 94 |
|
| 95 |
} else { |
| 96 |
throw new RuntimeException("Unhandled parameter change type"); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private int getTaxonIndex(int parameterIndex) { |
| 104 |
return parameterIndex / (dimTrait * numTraits); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
protected void storeState() { |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
protected void restoreState() { } |
| 114 |
|
| 115 |
@Override |
| 116 |
protected void acceptState() { } |
| 117 |
|
| 118 |
private double[] getScalarTipPartial(int taxonIndex) { |
| 119 |
double[] partial = new double[numTraits * (dimTrait + 1)]; |
| 120 |
final Parameter p = parameter.getParameter(taxonIndex); |
| 121 |
int offset = 0; |
| 122 |
for (int i = 0; i < numTraits; ++i) { |
| 123 |
boolean missing = false; |
| 124 |
for (int j = 0; j < dimTrait; ++j) { |
| 125 |
final int index = i * dimTrait + j; |
| 126 |
final int missingIndex = index + dimTrait * numTraits * taxonIndex; |
| 127 |
partial[offset + j] = p.getParameterValue(index); |
| 128 |
if (missingIndices != null && missingIndices.contains(missingIndex)) { |
| 129 |
missing = true; |
| 130 |
} |
| 131 |
} |
| 132 |
partial[offset + dimTrait] = missing ? 0.0 : Double.POSITIVE_INFINITY; |
| 133 |
offset += dimTrait + 1; |
| 134 |
} |
| 135 |
return partial; |
| 136 |
} |
| 137 |
|
| 138 |
private static final boolean OLD = false; |
| 139 |
|
| 140 |
public double[] getTipPartial(int taxonIndex, boolean fullyObserved) { |
| 141 |
if (fullyObserved) { |
| 142 |
|
| 143 |
final PrecisionType precisionType = PrecisionType.SCALAR; |
| 144 |
final int offsetInc = dimTrait + precisionType.getMatrixLength(dimTrait); |
| 145 |
final double precision = PrecisionType.getObservedPrecisionValue(false); |
| 146 |
|
| 147 |
double[] tipPartial = getTipPartial(taxonIndex, precisionType); |
| 148 |
|
| 149 |
for (int i = 0; i < numTraits; ++i) { |
| 150 |
precisionType.fillPrecisionInPartials(tipPartial, i * offsetInc, 0, precision, dimTrait); |
| 151 |
} |
| 152 |
|
| 153 |
return tipPartial; |
| 154 |
} else { |
| 155 |
return getTipPartial(taxonIndex, precisionType); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
private double[] getTipPartial(int taxonIndex, final PrecisionType precisionType) { |
| 160 |
|
| 161 |
if (OLD) { |
| 162 |
return getScalarTipPartial(taxonIndex); |
| 163 |
} |
| 164 |
|
| 165 |
final int offsetInc = dimTrait + precisionType.getMatrixLength(dimTrait); |
| 166 |
final double[] partial = new double[numTraits * offsetInc]; |
| 167 |
final Parameter p = parameter.getParameter(taxonIndex); |
| 168 |
|
| 169 |
int offset = 0; |
| 170 |
for (int i = 0; i < numTraits; ++i) { |
| 171 |
for (int j = 0; j < dimTrait; ++j) { |
| 172 |
|
| 173 |
final int pIndex = i * dimTrait + j; |
| 174 |
final int missingIndex = pIndex + dimTrait * numTraits * taxonIndex; |
| 175 |
|
| 176 |
partial[offset + j] = p.getParameterValue(pIndex); |
| 177 |
|
| 178 |
final boolean missing = missingIndices != null && missingIndices.contains(missingIndex); |
| 179 |
final double precision = PrecisionType.getObservedPrecisionValue(missing); |
| 180 |
|
| 181 |
precisionType.fillPrecisionInPartials(partial, offset, j, precision, dimTrait); |
| 182 |
} |
| 183 |
|
| 184 |
offset += offsetInc; |
| 185 |
} |
| 186 |
|
| 187 |
return partial; |
| 188 |
} |
| 189 |
|
| 190 |
double[] getTipObservation(int taxonIndex, final PrecisionType precisionType) { |
| 191 |
final int offsetInc = dimTrait + precisionType.getMatrixLength(dimTrait); |
| 192 |
|
| 193 |
final double[] partial = getTipPartial(taxonIndex, precisionType); |
| 194 |
final double[] data = new double[numTraits * dimTrait]; |
| 195 |
|
| 196 |
for (int i = 0; i < numTraits; ++i) { |
| 197 |
precisionType.copyObservation(partial, i * offsetInc, data, i * dimTrait, dimTrait); |
| 198 |
} |
| 199 |
|
| 200 |
return data; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
} |