| 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.math.distributions; |
| 27 |
|
| 28 |
import dr.math.UnivariateFunction; |
| 29 |
import org.apache.commons.math.FunctionEvaluationException; |
| 30 |
import org.apache.commons.math.MaxIterationsExceededException; |
| 31 |
import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; |
| 32 |
import org.apache.commons.math.analysis.UnivariateRealFunction; |
| 33 |
import org.apache.commons.math.analysis.integration.SimpsonIntegrator; |
| 34 |
import org.apache.commons.math.analysis.solvers.NewtonSolver; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public abstract class KernelDensityEstimatorDistribution implements Distribution { |
| 40 |
|
| 41 |
KernelDensityEstimatorDistribution(Double[] sample, Double lowerBound, Double upperBound, Double bandWidth) { |
| 42 |
|
| 43 |
this.sample = new double[sample.length]; |
| 44 |
for (int i = 0; i < sample.length; i++) { |
| 45 |
this.sample[i] = sample[i]; |
| 46 |
} |
| 47 |
this.N = sample.length; |
| 48 |
processBounds(lowerBound, upperBound); |
| 49 |
setBandWidth(bandWidth); |
| 50 |
} |
| 51 |
|
| 52 |
abstract protected double evaluateKernel(double x); |
| 53 |
|
| 54 |
abstract protected void processBounds(Double lowerBound, Double upperBound); |
| 55 |
|
| 56 |
abstract protected void setBandWidth(Double bandWidth); |
| 57 |
|
| 58 |
abstract public double getFromPoint(); |
| 59 |
|
| 60 |
abstract public double getToPoint(); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public double pdf(double x) { |
| 69 |
return evaluateKernel(x); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public double logPdf(double x) { |
| 79 |
return Math.log(pdf(x)); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public double cdf(double x) { |
| 89 |
|
| 90 |
double cdf; |
| 91 |
try { |
| 92 |
cdf = cdfFunction.value(x); |
| 93 |
} catch (FunctionEvaluationException e) { |
| 94 |
throw new RuntimeException(e.getMessage()); |
| 95 |
} |
| 96 |
|
| 97 |
return cdf; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public double quantile(final double y) { |
| 107 |
|
| 108 |
final DifferentiableUnivariateRealFunction root = |
| 109 |
new DifferentiableUnivariateRealFunction() { |
| 110 |
|
| 111 |
@Override |
| 112 |
public UnivariateRealFunction derivative() { |
| 113 |
return pdfFunction; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public double value(double x) throws FunctionEvaluationException { |
| 118 |
|
| 119 |
return cdfFunction.value(x) - y; |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
NewtonSolver solver = new NewtonSolver(root); |
| 124 |
|
| 125 |
double q; |
| 126 |
try { |
| 127 |
q = solver.solve(getFromPoint(), getToPoint()); |
| 128 |
} catch (MaxIterationsExceededException e) { |
| 129 |
throw new RuntimeException(e.getMessage()); |
| 130 |
} catch (FunctionEvaluationException e) { |
| 131 |
throw new RuntimeException(e.getMessage()); |
| 132 |
} |
| 133 |
|
| 134 |
return q; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public double mean() { |
| 143 |
throw new RuntimeException("Not Implemented."); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public double variance() { |
| 152 |
throw new RuntimeException("Not Implemented."); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public UnivariateFunction getProbabilityDensityFunction() { |
| 159 |
throw new RuntimeException("Not Implemented."); |
| 160 |
} |
| 161 |
|
| 162 |
public double getBandWidth() { |
| 163 |
return bandWidth; |
| 164 |
} |
| 165 |
|
| 166 |
public enum Type { |
| 167 |
GAUSSIAN("Gaussian"), |
| 168 |
GAMMA("Gamma"), |
| 169 |
LOG_TRANSFORMED_GAUSSIAN("LogTransformedGaussian"), |
| 170 |
BETA("Beta"); |
| 171 |
|
| 172 |
Type(String text) { |
| 173 |
this.text = text; |
| 174 |
} |
| 175 |
|
| 176 |
public String getText() { |
| 177 |
return text; |
| 178 |
} |
| 179 |
|
| 180 |
public static Type parseFromString(String text) { |
| 181 |
for (Type format : Type.values()) { |
| 182 |
if (format.getText().compareToIgnoreCase(text) == 0) |
| 183 |
return format; |
| 184 |
} |
| 185 |
return null; |
| 186 |
} |
| 187 |
|
| 188 |
private final String text; |
| 189 |
} |
| 190 |
|
| 191 |
protected int N; |
| 192 |
protected double lowerBound; |
| 193 |
protected double upperBound; |
| 194 |
double bandWidth; |
| 195 |
protected double[] sample; |
| 196 |
|
| 197 |
private final SimpsonIntegrator integrator = new SimpsonIntegrator(); |
| 198 |
|
| 199 |
private final UnivariateRealFunction pdfFunction = new UnivariateRealFunction() { |
| 200 |
|
| 201 |
@Override |
| 202 |
public double value(double x) throws FunctionEvaluationException { |
| 203 |
return pdf(x); |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
private final UnivariateRealFunction cdfFunction = new UnivariateRealFunction() { |
| 208 |
|
| 209 |
@Override |
| 210 |
public double value(double x) throws FunctionEvaluationException { |
| 211 |
double rangeMin = getFromPoint(); |
| 212 |
|
| 213 |
double cdf; |
| 214 |
try { |
| 215 |
cdf = integrator.integrate(pdfFunction, rangeMin, x); |
| 216 |
} catch (MaxIterationsExceededException e) { |
| 217 |
throw new RuntimeException(e.getMessage()); |
| 218 |
} |
| 219 |
|
| 220 |
return cdf; |
| 221 |
} |
| 222 |
}; |
| 223 |
} |