| 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.evolution.continuous; |
| 27 |
|
| 28 |
import dr.math.*; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class DiffusionLikelihood implements UnivariateFunction, MultivariateFunction { |
| 37 |
|
| 38 |
Tree tree; |
| 39 |
String traitName; |
| 40 |
|
| 41 |
public DiffusionLikelihood(Tree tree, String traitName) { |
| 42 |
this.tree = tree; |
| 43 |
this.traitName = traitName; |
| 44 |
} |
| 45 |
|
| 46 |
public double optimize(double[] p) { |
| 47 |
|
| 48 |
|
| 49 |
if (p.length > 1) { |
| 50 |
p[0] = 1.0; |
| 51 |
p[1] = 0.01; |
| 52 |
|
| 53 |
MultivariateMinimum optimizer = new ConjugateDirectionSearch(); |
| 54 |
|
| 55 |
optimizer.optimize(this,p,1e-6, 1e-6); |
| 56 |
return -evaluate(p); |
| 57 |
} else if (p.length == 1) { |
| 58 |
UnivariateMinimum optimizer = new UnivariateMinimum(); |
| 59 |
p[0] = optimizer.optimize(this,1e-6); |
| 60 |
return -evaluate(p[0]); |
| 61 |
} else throw new IllegalArgumentException(""); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public double getLogLikelihood(double distance, double time, double D, double bias) { |
| 68 |
|
| 69 |
|
| 70 |
double Dtime = D * time; |
| 71 |
|
| 72 |
double d = distance - (bias*time); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
return -(Math.log(Math.sqrt(Dtime*2*Math.PI))) - ((d*d)/(2*Dtime)); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public double evaluate(double[] argument) { |
| 83 |
|
| 84 |
double D = argument[0]; |
| 85 |
double bias = argument[1]; |
| 86 |
|
| 87 |
double logLkl = 0.0; |
| 88 |
for (int i = 0; i < tree.getNodeCount(); i++) { |
| 89 |
|
| 90 |
NodeRef child = tree.getNode(i); |
| 91 |
|
| 92 |
if (!tree.isRoot(child)) { |
| 93 |
|
| 94 |
NodeRef parent = tree.getParent(child); |
| 95 |
|
| 96 |
Contrastable parentValue = (Contrastable)tree.getNodeAttribute(parent,traitName); |
| 97 |
Contrastable childValue = (Contrastable)tree.getNodeAttribute(child,traitName); |
| 98 |
|
| 99 |
double distance = parentValue.getDifference(childValue); |
| 100 |
|
| 101 |
|
| 102 |
logLkl += getLogLikelihood(distance,tree.getBranchLength(child),D, bias); |
| 103 |
|
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return -logLkl; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public int getNumArguments() { |
| 114 |
return 2; |
| 115 |
} |
| 116 |
|
| 117 |
public double getLowerBound(int n) { |
| 118 |
if (n == 0) return 1e-12; |
| 119 |
return -Double.MAX_VALUE; |
| 120 |
} |
| 121 |
|
| 122 |
public double getUpperBound(int n) { |
| 123 |
return 2000; |
| 124 |
} |
| 125 |
|
| 126 |
public double evaluate(double argument) { |
| 127 |
return evaluate(new double[] {argument, 0.0}); |
| 128 |
} |
| 129 |
|
| 130 |
public double getLowerBound() { |
| 131 |
return getLowerBound(0); |
| 132 |
} |
| 133 |
|
| 134 |
public double getUpperBound() { |
| 135 |
return getUpperBound(0); |
| 136 |
} |
| 137 |
} |