use of de.lmu.ifi.dbs.elki.math.statistics.KernelDensityEstimator in project elki by elki-project.
the class LogNormalLevenbergMarquardtKDEEstimator method estimate.
@Override
public <A> LogNormalDistribution estimate(A data, NumberArrayAdapter<?, A> adapter) {
// We first need the basic parameters:
final int len = adapter.size(data);
MeanVariance mv = new MeanVariance();
// X positions of samples
double[] x = new double[len];
for (int i = 0; i < len; i++) {
final double val = adapter.getDouble(data, i);
if (!(val > 0)) {
throw new ArithmeticException("Cannot fit logNormal to a data set which includes non-positive values: " + val);
}
x[i] = FastMath.log(val);
mv.put(x[i]);
}
// Sort our copy.
Arrays.sort(x);
double median = (x[len >> 1] + x[(len + 1) >> 1]) * .5;
// Height = density, via KDE.
KernelDensityEstimator de = new KernelDensityEstimator(x, GaussianKernelDensityFunction.KERNEL, 1e-6);
double[] y = de.getDensity();
// Weights:
double[] s = new double[len];
Arrays.fill(s, 1.0);
// Initial parameter estimate:
double[] params = { median, mv.getSampleStddev(), 1 };
boolean[] dofit = { true, true, false };
LevenbergMarquardtMethod fit = new LevenbergMarquardtMethod(GaussianFittingFunction.STATIC, params, dofit, x, y, s);
fit.run();
double[] ps = fit.getParams();
return new LogNormalDistribution(ps[0], ps[1], 0.);
}
use of de.lmu.ifi.dbs.elki.math.statistics.KernelDensityEstimator in project elki by elki-project.
the class KernelDensityFittingTest method run.
private double[] run(double[] data, double[] params) {
FittingFunction func = GaussianFittingFunction.STATIC;
boolean[] dofit = { true, true, true };
KernelDensityEstimator de = new KernelDensityEstimator(data, GaussianKernelDensityFunction.KERNEL, 1e-10);
LevenbergMarquardtMethod fit = new LevenbergMarquardtMethod(func, params, dofit, data, de.getDensity(), de.getVariance());
// for(int i = 0; i < 100; i++) {
// fit.iterate();
// double[] ps = fit.getParams();
// System.out.println("Mean: " + ps[0] + " Stddev: " + ps[1] + " Amp: " +
// ps[2]+" Chi: "+fit.getChiSq());
// }
fit.run();
// "+fit.getChiSq()+" Iter: "+fit.maxruns);
return fit.getParams();
}
use of de.lmu.ifi.dbs.elki.math.statistics.KernelDensityEstimator in project elki by elki-project.
the class NormalLevenbergMarquardtKDEEstimator method estimate.
@Override
public <A> NormalDistribution estimate(A data, NumberArrayAdapter<?, A> adapter) {
// We first need the basic parameters:
final int len = adapter.size(data);
MeanVariance mv = new MeanVariance();
// X positions of samples
double[] x = new double[len];
for (int i = 0; i < len; i++) {
x[i] = adapter.getDouble(data, i);
mv.put(x[i]);
}
// Sort our copy.
Arrays.sort(x);
double median = (x[len >> 1] + x[(len + 1) >> 1]) * .5;
// Height = density, via KDE.
KernelDensityEstimator de = new KernelDensityEstimator(x, GaussianKernelDensityFunction.KERNEL, 1e-6);
double[] y = de.getDensity();
// Weights:
double[] s = new double[len];
Arrays.fill(s, 1.0);
// Initial parameter estimate:
double[] params = { median, mv.getSampleStddev(), 1 };
boolean[] dofit = { true, true, false };
LevenbergMarquardtMethod fit = new LevenbergMarquardtMethod(GaussianFittingFunction.STATIC, params, dofit, x, y, s);
fit.run();
double[] ps = fit.getParams();
return new NormalDistribution(ps[0], ps[1]);
}
Aggregations