Search in sources :

Example 1 with KernelDensityEstimator

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.);
}
Also used : MeanVariance(de.lmu.ifi.dbs.elki.math.MeanVariance) LevenbergMarquardtMethod(de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.LevenbergMarquardtMethod) KernelDensityEstimator(de.lmu.ifi.dbs.elki.math.statistics.KernelDensityEstimator) LogNormalDistribution(de.lmu.ifi.dbs.elki.math.statistics.distribution.LogNormalDistribution)

Example 2 with KernelDensityEstimator

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();
}
Also used : FittingFunction(de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.FittingFunction) GaussianFittingFunction(de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.GaussianFittingFunction) LevenbergMarquardtMethod(de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.LevenbergMarquardtMethod) KernelDensityEstimator(de.lmu.ifi.dbs.elki.math.statistics.KernelDensityEstimator)

Example 3 with KernelDensityEstimator

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]);
}
Also used : MeanVariance(de.lmu.ifi.dbs.elki.math.MeanVariance) NormalDistribution(de.lmu.ifi.dbs.elki.math.statistics.distribution.NormalDistribution) LevenbergMarquardtMethod(de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.LevenbergMarquardtMethod) KernelDensityEstimator(de.lmu.ifi.dbs.elki.math.statistics.KernelDensityEstimator)

Aggregations

LevenbergMarquardtMethod (de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.LevenbergMarquardtMethod)3 KernelDensityEstimator (de.lmu.ifi.dbs.elki.math.statistics.KernelDensityEstimator)3 MeanVariance (de.lmu.ifi.dbs.elki.math.MeanVariance)2 FittingFunction (de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.FittingFunction)1 GaussianFittingFunction (de.lmu.ifi.dbs.elki.math.linearalgebra.fitting.GaussianFittingFunction)1 LogNormalDistribution (de.lmu.ifi.dbs.elki.math.statistics.distribution.LogNormalDistribution)1 NormalDistribution (de.lmu.ifi.dbs.elki.math.statistics.distribution.NormalDistribution)1