use of de.lmu.ifi.dbs.elki.math.StatisticalMoments in project elki by elki-project.
the class BestFitEstimator method estimate.
@Override
public <A> Distribution estimate(A data, NumberArrayAdapter<?, A> adapter) {
int numlmm = 0;
for (LMMDistributionEstimator<?> est : lmmests) {
numlmm = Math.max(numlmm, est.getNumMoments());
}
final int len = adapter.size(data);
// Build various statistics:
StatisticalMoments mom = new StatisticalMoments(), logmom = new StatisticalMoments();
double[] x = new double[len], scratch = new double[len], logx = new double[len];
if (LOG.isDebuggingFine()) {
LOG.debugFine("Computing statistical moments and L-Moments.");
}
for (int i = 0; i < len; i++) {
final double val = x[i] = adapter.getDouble(data, i);
if (Double.NEGATIVE_INFINITY < val && val < Double.POSITIVE_INFINITY) {
mom.put(val);
}
}
if (mom.getMax() <= mom.getMin()) {
LOG.warning("Constant distribution detected. Cannot fit.");
return new UniformDistribution(mom.getMin() - 1., mom.getMax() + 1.);
}
// Sort: for L-Moments, but getting the median is now also cheap.
Arrays.sort(x);
double[] lmm;
try {
lmm = (numlmm > 0) ? ProbabilityWeightedMoments.samLMR(x, DoubleArrayAdapter.STATIC, numlmm) : null;
} catch (ArithmeticException e) {
lmm = null;
}
final double min = x[0], median = .5 * (x[len >> 1] + x[(len + 1) >> 1]), max = x[len - 1];
if (LOG.isDebuggingFine()) {
LOG.debugFine("Computing statistical moments in logspace.");
}
// Build logspace copy:
double shift = Math.min(0., min - (max - min) * 1e-10);
for (int i = 0; i < len; i++) {
double val = x[i] - shift;
val = val > 0. ? FastMath.log(val) : Double.NEGATIVE_INFINITY;
logx[i] = val;
if (Double.NEGATIVE_INFINITY < val && val < Double.POSITIVE_INFINITY) {
logmom.put(val);
}
}
double logmedian = .5 * (logx[len >> 1] + logx[(len + 1) >> 1]);
if (LOG.isDebuggingFine()) {
LOG.debugFine("Computing MADs.");
}
double mad = MADDistributionEstimator.computeMAD(x, len, median, scratch);
double logmad = MADDistributionEstimator.computeMAD(logx, len, logmedian, scratch);
BestFit best = new BestFit(x, scratch);
for (MOMDistributionEstimator<?> est : momests) {
try {
best.test(est, est.estimateFromStatisticalMoments(mom));
} catch (ArithmeticException e) {
warnIfDebugging(e, est);
}
}
for (MADDistributionEstimator<?> est : madests) {
try {
best.test(est, est.estimateFromMedianMAD(median, mad));
} catch (ArithmeticException e) {
warnIfDebugging(e, est);
}
}
for (LMMDistributionEstimator<?> est : lmmests) {
if (lmm != null) {
try {
best.test(est, est.estimateFromLMoments(lmm));
} catch (ArithmeticException e) {
warnIfDebugging(e, est);
}
}
}
for (LogMOMDistributionEstimator<?> est : logmomests) {
try {
best.test(est, est.estimateFromLogStatisticalMoments(logmom, shift));
} catch (ArithmeticException e) {
warnIfDebugging(e, est);
}
}
for (LogMADDistributionEstimator<?> est : logmadests) {
try {
best.test(est, est.estimateFromLogMedianMAD(logmedian, logmad, shift));
} catch (ArithmeticException e) {
warnIfDebugging(e, est);
}
}
{
// Uniform estimators.
final UniformMinMaxEstimator est = UniformMinMaxEstimator.STATIC;
best.test(est, est.estimate(min, max));
}
{
// Uniform estimators.
final UniformEnhancedMinMaxEstimator est = UniformEnhancedMinMaxEstimator.STATIC;
best.test(est, est.estimate(min, max, len));
}
if (LOG.isVeryVerbose()) {
LOG.veryverbose("Best distribution fit: " + best.score + " " + best.toString() + " via " + best.est);
}
return best.dist;
}
use of de.lmu.ifi.dbs.elki.math.StatisticalMoments in project elki by elki-project.
the class MOMDistributionEstimator method estimate.
@Override
default <A> D estimate(A data, NumberArrayAdapter<?, A> adapter) {
final int size = adapter.size(data);
StatisticalMoments mv = new StatisticalMoments();
for (int i = 0; i < size; i++) {
final double val = adapter.getDouble(data, i);
if (Double.isInfinite(val) || Double.isNaN(val)) {
continue;
}
mv.put(val);
}
return estimateFromStatisticalMoments(mv);
}
use of de.lmu.ifi.dbs.elki.math.StatisticalMoments in project elki by elki-project.
the class LogMOMDistributionEstimator method estimate.
@Override
default <A> D estimate(A data, NumberArrayAdapter<?, A> adapter) {
final int len = adapter.size(data);
double min = min(data, adapter, 0., 1e-10);
StatisticalMoments mv = new StatisticalMoments();
for (int i = 0; i < len; i++) {
final double val = adapter.getDouble(data, i) - min;
if (Double.isInfinite(val) || Double.isNaN(val) || val <= 0.) {
continue;
}
mv.put(FastMath.log(val));
}
return estimateFromLogStatisticalMoments(mv, min);
}
Aggregations