Search in sources :

Example 1 with ExponentialDistribution

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

the class ExponentialDistributionDemo method stateChanged.

@Override
public void stateChanged(ChangeEvent e) {
    if (e.getSource() == lambdaSlider) {
        lambda = lambdaSlider.getValue() / 10.0;
        if (lambda == 0)
            lambda = 0.01;
        ExponentialDistribution dist = new ExponentialDistribution(lambda);
        double[][] p = new double[100][2];
        double[][] q = new double[100][2];
        for (int i = 0; i < p.length; i++) {
            p[i][0] = i / 20.0;
            p[i][1] = dist.p(p[i][0]);
            q[i][0] = i / 20.0;
            q[i][1] = dist.cdf(p[i][0]);
        }
        pdf.clear();
        pdf.line(p, Line.Style.SOLID, Color.BLUE);
        cdf.clear();
        cdf.line(q, Line.Style.SOLID, Color.BLUE);
        double[] data = new double[500];
        for (int i = 0; i < data.length; i++) {
            data[i] = dist.rand();
        }
        histogram.clear();
        histogram.histogram(data, 20, Color.BLUE);
        qqplot.clear();
        qqplot.add(new QQPlot(data, dist));
        canvas.repaint();
    }
}
Also used : QQPlot(smile.plot.QQPlot) ExponentialDistribution(smile.stat.distribution.ExponentialDistribution)

Example 2 with ExponentialDistribution

use of smile.stat.distribution.ExponentialDistribution 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

ExponentialDistribution (smile.stat.distribution.ExponentialDistribution)2 ArrayList (java.util.ArrayList)1 JFrame (javax.swing.JFrame)1 PlotCanvas (smile.plot.PlotCanvas)1 QQPlot (smile.plot.QQPlot)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