随机温度的标准偏差

Standard deviation of random temperatures

我正在尝试根据包含 50,000 多个温度的文本文件计算 x 个随机温度的标准差。

我有一个数组,其中包含我应该加载到的每个索引的样本大小。例如,将 8 个随机温度放入索引 0,将 16 个随机温度放入索引 1,等等。

我已经成功计算了样本均值,但我在 variance/standard 偏差方面遇到了问题。

private static void calcEstimates() {
    double 
        sum,
        mean,
        sampleSize = 0,
        mnSum = 0,
        mnSqrSum = 0;

    double [] means = new double[numTemps]; 

    for ( i = 0; i < sampleSizes.length; i++) {
        sum = 0;
        sampleSize = sampleSizes[i];
        for (j = 0; j < sampleSize; j++)
            sum += allTemps[rng.nextInt(numTemps)];

        mean = sum / sampleSize;
        mnSum += sum * sum;
        mnSqrSum = (sampleSizes[i] * mnSum - sum * sum) / (sampleSizes[i]*(sampleSizes[i]-1));

        sampleMeans[i]  = sum/sampleSize;;
        sampleStdDevs[i] = Math.sqrt(mnSqrSum);
    }
} 

输出:

每个样本大小的样本标准差应该在 ~20 左右。

(算术)平均值(参见 https://en.wikipedia.org/wiki/Mean)的定义是:

以及 标准偏差 (参见 https://en.wikipedia.org/wiki/Standard_deviation):

在下面的代码中,第一步计算均值,第二步计算标准差:

private static void calcEstimates() {
    int sampleSize;
    double sum;
    double mean;
    double sumSqrDev;
    double stdDev;

    System.out.println("size   mean      stdDev");
    System.out.println("------------------------");
    for (int i = 0; i < sampleSizes.length; i++) {

        sum = 0;
        sampleSize = sampleSizes[i];

        // 1. Step: Calculation of the mean
        double[] temps = new double[sampleSize]; // N
        for (int j = 0; j < sampleSize; j++) {
            temps[j] = allTemps[rng.nextInt(numTemps)];
            sum += temps[j];
        }

        mean = sum / sampleSize;

        // 2. Step: Calculation of the standard deviation
        sumSqrDev = 0;
        for (int j = 0; j < sampleSize; j++) {
            sumSqrDev += Math.pow((temps[j] - mean), 2);
        }

        stdDev = Math.sqrt(sumSqrDev / (sampleSize - 1));

        System.out.printf("%5d  %.4f  %.4f\n", sampleSize, mean, stdDev);
    }
}

在以下示例中,温度 均匀分布 ,值介于 a = 450b = 550 之间:

private static int[] sampleSizes = new int[] {8,16,32,64,128,140,160,200,240,280,320,360,400,20000};
private static Random rng = new Random();
private static int numTemps = 20000;
private static double[] allTemps = new double[numTemps]; 
private static double meanTemperature = 500;
private static double deviation = 100;

private static void initTemperatures() {        
    for (int i = 0; i < numTemps; i++) {
        allTemps[i] = meanTemperature + deviation * (rng.nextDouble() - 0.5);
    }
}

public static void main(String[] args) {
    initTemperatures();
    calcEstimates();
}

因此,理论 平均值为

并且理论标准差是

(参见https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) and https://stats.stackexchange.com/questions/35123/whats-the-difference-between-variance-and-standard-deviation) 与代码的结果非常吻合:

size   mean      stdDev
------------------------
    8  504.8617  32.1182
   16  503.5508  31.2777
   32  503.1226  28.3134
   64  504.2420  28.2647
  128  499.5431  27.3515
  140  504.0203  26.6482
  160  501.0673  28.7222
  200  498.4244  28.5140
  240  500.7214  28.6428
  280  497.3849  28.3684
  320  499.5752  28.8653
  360  500.6975  29.1524
  400  500.5515  29.9879
20000  499.7810  28.9035