Java Guassian Distribution-Bell Curve

I calculated the mean and SD of the set of values. Now I need to draw a bell curve using this value to show the normal distribution in JAVA Swing. How can I continue this situation.

List: 204 297 348 528 681 684 785 957 1044 1140 1378 1545 1818

Total count: 13

Average value (average): 877.615384615385

Standard Deviation (SD): 477.272626245539

If I can get the roots of x and y, I can do it, but how to get these values?

+4
source share
2 answers

First you need to calculate the variance for the set. The variance is calculated as the standard deviation of each number from its average value.

double variance(double[] population) { long n = 0; double mean = 0; double s = 0.0; for (double x : population) { n++; double delta = x – mean; mean += delta / n; s += delta * (x – mean); } // if you want to calculate std deviation return (s / n); } 

Once you do this, you can choose x depending on your graph resolution compared to your value set with the extension, and connect it to the following equation to get y.

 protected double stdDeviation, variance, mean; public double getY(double x) { return Math.pow(Math.exp(-(((x - mean) * (x - mean)) / ((2 * variance)))), 1 / (stdDeviation * Math.sqrt(2 * Math.PI))); } 

To display the result set: let's say we take the set you specified, and you decide to show x = 0 at x = 2000 on a graph with a resolution of x 1000 pixels. Then you connect the loop (int x = 0; x <= 2000; x = 2) and load these values ​​into the equation above to get the y values ​​for the pair. Since the y you want to show is 0-1, you match these values ​​with what you want your resolution y to match the appropriate rounding behavior, so your plot is not too distorted. Therefore, if you want your resolution y to be 500 pixels, you set from 0 to 0 and from 1 to 500 and from 0.5 to 250, etc. This is a contrived example, and you may need a lot more flexibility, but I think this illustrates the point. Most graphics libraries will handle these little things for you.

+8
source

Here's an example of plotting some Gaussian curves using XChart . The code can be found here . Disclaimer: I am the creator of the XChart Java graphics library.

 public class ThemeChart03 implements ExampleChart { public static void main(String[] args) { ExampleChart exampleChart = new ThemeChart03(); Chart chart = exampleChart.getChart(); new SwingWrapper(chart).displayChart(); } @Override public Chart getChart() { // Create Chart Chart_XY chart = new ChartBuilder_XY().width(800).height(600).theme(ChartTheme.Matlab).title("Matlab Theme").xAxisTitle("X").yAxisTitle("Y").build(); // Customize Chart chart.getStyler().setPlotGridLinesVisible(false); chart.getStyler().setXAxisTickMarkSpacingHint(100); // Series List<Integer> xData = new ArrayList<Integer>(); for (int i = 0; i < 640; i++) { xData.add(i); } List<Double> y1Data = getYAxis(xData, 320, 60); List<Double> y2Data = getYAxis(xData, 320, 100); List<Double> y3Data = new ArrayList<Double>(xData.size()); for (int i = 0; i < 640; i++) { y3Data.add(y1Data.get(i) - y2Data.get(i)); } chart.addSeries("Gaussian 1", xData, y1Data); chart.addSeries("Gaussian 2", xData, y2Data); chart.addSeries("Difference", xData, y3Data); return chart; } private List<Double> getYAxis(List<Integer> xData, double mean, double std) { List<Double> yData = new ArrayList<Double>(xData.size()); for (int i = 0; i < xData.size(); i++) { yData.add((1 / (std * Math.sqrt(2 * Math.PI))) * Math.exp(-(((xData.get(i) - mean) * (xData.get(i) - mean)) / ((2 * std * std))))); } return yData; } } 

The resulting plot is as follows:

enter image description here

+1
source

Source: https://habr.com/ru/post/1345462/


All Articles