本文介绍了Java 高斯分布-钟形曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计算了一组值的均值和标准差.现在我需要使用这些值绘制钟形曲线以显示 JAVA Swing 中的正态分布.我该如何处理这种情况.

I have calculated mean and SD of a set of values. Now I need to draw a bell curve using those value to show the normal distribution in JAVA Swing. How do i proceed with this situation.

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

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

总数:13

平均值(Mean):877.615384615385

Average value (Mean): 877.615384615385

标准差 (SD) : 477.272626245539

Standard deviation (SD) : 477.272626245539

如果我能得到 x 和 y 坐标,我就能做到,但我如何得到这些值?

If i can get the x and y cordinates I can do it, but how do i get those values?

推荐答案

首先你需要计算集合的方差.方差计算为每个数字与其平均值的平均平方偏差.

First you need to calculate the variance for the set. The variance is computed as the average squared deviation of each number from its mean.

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);
}

一旦你有了它,你就可以根据你的图形分辨率与你的值集散布相比选择 x,并将其代入以下方程得到 y.

Once you have that you can choose x depending on your graph resolution compared to your value set spread and plug it in 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)));

    }

要显示结果集:假设我们采用您布置的总体集,并决定要在 x 分辨率为 1000 像素的图形上显示 x=0 到 x=2000.然后你会插入一个循环 (int x = 0; x

To display the resulting set: say we take the population set you laid out and decide you want to show x=0 to x=2000 on a graph with an x resolution of 1000 pixels. Then you would plug in a loop (int x = 0; x <= 2000; x = 2) and feed those values into the equation above to get your y values for the pair. Since the y you want to show is 0-1 then you map these values to whatever you want your y resolution to be with appropriate rounding behavior so your graph doesn't end up too jaggy. So if you want your y resolution to be 500 pixels then you set 0 to 0 and 1 to 500 and .5 to 250 etc. etc. This is a contrived example and you might need a lot more flexibility but I think it illustrates the point. Most graphing libraries will handle these little things for you.

这篇关于Java 高斯分布-钟形曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:39