| 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.distribution; |
| 27 |
|
| 28 |
import dr.math.distributions.Distribution; |
| 29 |
import dr.math.matrixAlgebra.Vector; |
| 30 |
import dr.util.Attribute; |
| 31 |
import org.w3c.dom.Document; |
| 32 |
import org.w3c.dom.Element; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class DistributionLikelihood extends AbstractDistributionLikelihood { |
| 43 |
|
| 44 |
public final static boolean DEBUG = false; |
| 45 |
|
| 46 |
public static final String DISTRIBUTION_LIKELIHOOD = "distributionLikelihood"; |
| 47 |
|
| 48 |
private int from = -1; |
| 49 |
private int to = Integer.MAX_VALUE; |
| 50 |
private final boolean evaluateEarly; |
| 51 |
|
| 52 |
public DistributionLikelihood(Distribution distribution) { |
| 53 |
this(distribution, 0.0, false, 1.0); |
| 54 |
} |
| 55 |
|
| 56 |
public DistributionLikelihood(Distribution distribution, double offset) { |
| 57 |
this(distribution, offset, offset > 0.0, 1.0); |
| 58 |
} |
| 59 |
|
| 60 |
public DistributionLikelihood(Distribution distribution, double offset, double scale){ |
| 61 |
this(distribution, offset, offset>0.0, scale); |
| 62 |
} |
| 63 |
|
| 64 |
public DistributionLikelihood(Distribution distribution, boolean evaluateEarly) { |
| 65 |
this(distribution, 0.0, evaluateEarly, 1.0); |
| 66 |
} |
| 67 |
|
| 68 |
public DistributionLikelihood(Distribution distribution, double offset, boolean evaluateEarly, double scale) { |
| 69 |
super(null); |
| 70 |
this.distribution = distribution; |
| 71 |
this.offset = offset; |
| 72 |
this.evaluateEarly = evaluateEarly; |
| 73 |
this.scale=scale; |
| 74 |
} |
| 75 |
|
| 76 |
public DistributionLikelihood(ParametricDistributionModel distributionModel) { |
| 77 |
super(distributionModel); |
| 78 |
this.distribution = distributionModel; |
| 79 |
this.offset = 0.0; |
| 80 |
this.evaluateEarly = false; |
| 81 |
this.scale=1.0; |
| 82 |
} |
| 83 |
|
| 84 |
public Distribution getDistribution() { |
| 85 |
return distribution; |
| 86 |
} |
| 87 |
|
| 88 |
public void setRange(int from, int to) { |
| 89 |
this.from = from; |
| 90 |
this.to = to; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public double calculateLogLikelihood() { |
| 103 |
|
| 104 |
if (DEBUG) { |
| 105 |
System.err.println("Calling DistributionLikelihood.calculateLogLikelihood()"); |
| 106 |
System.err.println(distribution.toString()); |
| 107 |
System.err.println(dataList.toString() + "\n"); |
| 108 |
} |
| 109 |
|
| 110 |
double logL = 0.0; |
| 111 |
int count = 0; |
| 112 |
|
| 113 |
for( Attribute<double[]> data : dataList ) { |
| 114 |
|
| 115 |
|
| 116 |
final double[] attributeValue = data.getAttributeValue(); |
| 117 |
|
| 118 |
if (DEBUG) { |
| 119 |
System.err.println("\t" + new Vector(attributeValue)); |
| 120 |
} |
| 121 |
|
| 122 |
for (int j = Math.max(0, from); j < Math.min(attributeValue.length, to); j++) { |
| 123 |
|
| 124 |
final double value = attributeValue[j] - offset; |
| 125 |
|
| 126 |
if (offset > 0.0 && value < 0.0) { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
return Double.NEGATIVE_INFINITY; |
| 133 |
} |
| 134 |
logL += getLogPDF(value, count); |
| 135 |
count += 1; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
return logL; |
| 140 |
} |
| 141 |
|
| 142 |
protected double getLogPDF(double value, int i){ |
| 143 |
return distribution.logPdf(value/scale) - Math.log(scale); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public boolean evaluateEarly() { |
| 148 |
return evaluateEarly; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public Element createElement(Document d) { |
| 156 |
throw new RuntimeException("Not implemented yet!"); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public String prettyName() { |
| 161 |
String s = distribution.getClass().getName(); |
| 162 |
String[] parts = s.split("\\."); |
| 163 |
s = parts[parts.length - 1]; |
| 164 |
if( s.endsWith("Distribution") ) { |
| 165 |
s = s.substring(0, s.length() - "Distribution".length()); |
| 166 |
} |
| 167 |
s = s + '('; |
| 168 |
for( Attribute<double[]> data : dataList ) { |
| 169 |
String name = data.getAttributeName(); |
| 170 |
if( name == null ) { |
| 171 |
name = "?"; |
| 172 |
} |
| 173 |
s = s + name + ','; |
| 174 |
} |
| 175 |
s = s.substring(0,s.length()-1) + ')'; |
| 176 |
|
| 177 |
return s; |
| 178 |
} |
| 179 |
|
| 180 |
protected Distribution distribution; |
| 181 |
private final double offset; |
| 182 |
private final double scale; |
| 183 |
} |
| 184 |
|