Drawing a Gauss Curve in Java

I am coding an interactive Piccolo applet and I need to include a Gaussian curve (aka Normal distribution diagram ) inside.

I assume that any kind of Java implementation will suffice, but I cannot find it. Ideally, I would like to convey a set of values โ€‹โ€‹and draw a diagram on a panel, an image object, or anything that can be embedded in an applet.

Before you deal with my dirty encodings myself, does anyone know about a working piece of code?

Implementations in other languages โ€‹โ€‹are welcome if they are easily ported to Java.

+4
source share
2 answers

I don't know if it works, but Google ran this code to build a Gaussian distribution .

The homepage of this project is here .

If Piccolo didnโ€™t do the plotting for you, I would probably use JFreeChart to actually plot it, as it is widely supported and very capable. (I am not familiar with Piccolo)

+4
source

Edit: It looks like the Apache Commons Math library has a statistics section. In particular, a whole package on General Distributions . Hope some mathematical people are there because I can't remember the basic statistics ... here is my attempt to use their library. I just have a sliding window here and compute P between these values. What is the real way to get a PDF from here? 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 that you need a probability density function for the graph. the equations are in wikipedia , since I donโ€™t know how to include mathematical markup. Just use p (x) as your Y value, and X as your X value, and you can get a pretty simple 2nd plot.

Have you looked at Mathtools under Java ?

Well, how about this ... you give it an array of X points (normalized, of course, you can convert your X-pixels into them by dividing each pixel position by the width of your image), and it will return to the height of the distribution curve (again multiply by the normalization factor). This is for the main case, when the average value is 0, and the 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 is easy enough to implement one that takes an arbitrary mean and standard deviation if it cannot be found on the network.

+2
source

All Articles