Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw Gaussian curve in Java

I'm coding an interactive applet with Piccolo and I need to include a Gaussian curve (aka Normal distribution chart) inside it.

I imagine any kind of Java implementation would be enough, but I can't find any. Ideally, I'd like to pass a set of values and have the chart drawn in a panel, an image object or anything that can be embedded in an applet.

Before getting my hands dirty coding it myself, does anybody know of a working piece of code to do it?

Implementations in other languages are welcomed, as long as they are easily portable to Java.

like image 561
Seb Avatar asked Mar 31 '09 21:03

Seb


People also ask

How do you draw a Gaussian curve?

Begin by drawing a horizontal line (axis). Next, draw a normal (bell-shaped) curve centered on the horizontal axis. Then draw a vertical line from the horizontal axis through the center of the curve, cutting it in half.

What is Java Gaussian?

The nextGaussian() method of Random class returns the next pseudorandom, Gaussian(normally) distributed double value with mean 0.0 and standard deviation 1.0 from the random number generator's sequence. Syntax: public double nextGaussian() Parameters: The function does not accepts any parameter.

How do you generate a random number from a normal distribution in Java?

double r = generator. nextDouble(); To generate a random number from a normal distribution, use "nextGaussian". This message takes no parameters and returns a random number from a normal distribution with mean 0 and standard deviation 1.


2 Answers

Don't know if it works, but Google threw up this code to plot a Gaussian distribution.

The home page for this project is here.

If Piccolo doesn't perform the plotting for you, I would perhaps use JFreeChart for the actual plotting, since it's widely supported and very capable. (I'm not familiar with Piccolo)

like image 71
Brian Agnew Avatar answered Sep 26 '22 17:09

Brian Agnew


Edit: It looks like the Apache Commons Math library has a statistics section. Specifically, a whole package on common Distributions. Hopefully theres some math people out there because I can't remember basic statistics... here's my attempt at using the their library. I just have a sliding window here and calculate the P between those values. Whats the real way to get a PDF from this? They only have a CDF function.

public void testNormalDist() throws MathException {
    DistributionFactory f = DistributionFactory.newInstance();
    NormalDistribution n = f.createNormalDistribution(0.0d, 1.0d);
    double lastx = Double.NEGATIVE_INFINITY;
    double nextx = Double.NEGATIVE_INFINITY;
    for (int i=-100; i < 100; i++) {
        nextx = i / 100d;
        System.out.println(n.cumulativeProbability(lastx, nextx));
        lastx = nextx;
    }
}

I assume you want the probability density function for the graph. The equations are on wikipedia, since I don't know how to include math markup here. Just use p(x) as your Y value and X as your X value and you can get a pretty easy 2-d graph from that.

Have you looked at Mathtools under Java?

Ok, how about this... you give it an array of X points (normalized, of course, you can convert your X pixels to these by dividing each pixel position by the width of your image), and it will return the heights of the distribution curve (again, multiply by your normalization factor). This is for the basic case where mean is 0 and standard deviation is 1.

public double[] normalDistBasic(double[] xarray, double mu) {
    double[] yarray = new double[xarray.length];
    double rad2pi = 2.50662827d;
    for (int off = 0; off < yarray.length; off++) {
        double x = xarray[off];
        double ss = -1d * x * x / 2d;
        yarray[off] = (-1f / rad2pi) * Math.exp(ss);
    }
    return yarray;
}

It should be pretty easy to implement one that takes arbitrary mean and standard deviation if one can't be found on the net.

like image 40
John Ellinwood Avatar answered Sep 22 '22 17:09

John Ellinwood