Search in sources :

Example 1 with ExponentialFamilyMixture

use of smile.stat.distribution.ExponentialFamilyMixture in project smile by haifengl.

the class ExponentialFamilyMixtureDemo method main.

public static void main(String[] args) {
    // Mixture of Gaussian, Exponential, and Gamma.
    double[] data = new double[2000];
    GaussianDistribution gaussian = new GaussianDistribution(-2.0, 1.0);
    for (int i = 0; i < 500; i++) data[i] = gaussian.rand();
    ExponentialDistribution exp = new ExponentialDistribution(0.8);
    for (int i = 500; i < 1000; i++) data[i] = exp.rand();
    GammaDistribution gamma = new GammaDistribution(2.0, 3.0);
    for (int i = 1000; i < 2000; i++) data[i] = gamma.rand();
    List<Mixture.Component> m = new ArrayList<>();
    Mixture.Component c = new Mixture.Component();
    c.priori = 0.25;
    c.distribution = new GaussianDistribution(0.0, 1.0);
    m.add(c);
    c = new Mixture.Component();
    c.priori = 0.25;
    c.distribution = new ExponentialDistribution(1.0);
    m.add(c);
    c = new Mixture.Component();
    c.priori = 0.25;
    c.distribution = new GammaDistribution(1.0, 2.0);
    m.add(c);
    ExponentialFamilyMixture mixture = new ExponentialFamilyMixture(m, data);
    System.out.println(mixture);
    JFrame frame = new JFrame("Mixture of Exponential Family Distributions");
    PlotCanvas canvas = Histogram.plot(data, 50);
    frame.add(canvas);
    double width = (Math.max(data) - Math.min(data)) / 50;
    double[][] p = new double[400][2];
    for (int i = 0; i < p.length; i++) {
        p[i][0] = -10 + i * 0.1;
        p[i][1] = mixture.p(p[i][0]) * width;
    }
    canvas.line(p, Color.RED);
    frame.add(QQPlot.plot(data, mixture));
    frame.setVisible(true);
}
Also used : GaussianDistribution(smile.stat.distribution.GaussianDistribution) ExponentialFamilyMixture(smile.stat.distribution.ExponentialFamilyMixture) ExponentialDistribution(smile.stat.distribution.ExponentialDistribution) ArrayList(java.util.ArrayList) ExponentialFamilyMixture(smile.stat.distribution.ExponentialFamilyMixture) Mixture(smile.stat.distribution.Mixture) JFrame(javax.swing.JFrame) GammaDistribution(smile.stat.distribution.GammaDistribution) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

ArrayList (java.util.ArrayList)1 JFrame (javax.swing.JFrame)1 PlotCanvas (smile.plot.PlotCanvas)1 ExponentialDistribution (smile.stat.distribution.ExponentialDistribution)1 ExponentialFamilyMixture (smile.stat.distribution.ExponentialFamilyMixture)1 GammaDistribution (smile.stat.distribution.GammaDistribution)1 GaussianDistribution (smile.stat.distribution.GaussianDistribution)1 Mixture (smile.stat.distribution.Mixture)1