为什么平均值不会更接近原始权重?

时间:2017-09-25 13:33:21

标签: java random weighting

我运行以下程序,典型的控制台输出如下。

  

加权0的平均百分点是:57.935590153643616
  意思   权重1的百分点是:42.06440984635654

为什么这些印刷品不太接近60和40?

public static void main(String[] args) {
    Random rand = new Random();

    int numCycles = 5000;

    double[] weightings = {60.0, 40.0};
    double[] weightedRandoms = new double[weightings.length];
    double[] totPercentagePoints = {0.0, 0.0};

    for (int j = 0; j < numCycles; j++) {

        for (int k = 0; k < weightings.length; k++) {
            weightedRandoms[k] = (rand.nextInt(10) + 1) * weightings[k]; // +1 to the random integer to ensure that the weighting is not multiplied by 0
        }

        for (int k = 0; k < weightings.length; k++) {
            totPercentagePoints[k] += weightedRandoms[k] / DoubleStream.of(weightedRandoms).sum() * 100;
        }
    }

    for (int i = 0; i < weightings.length; i++) {
        System.out.println("Mean percentage points for weighting " + i + " is: " + totPercentagePoints[i] / numCycles);
    }
}

1 个答案:

答案 0 :(得分:2)

您正在估算100*E(X/(X+Y)]100*E(Y/(X+Y)]其中X = 60*U(1,10)Y = 40*U(1,10)(其中U(1,10)是1,...,10上的离散均匀分布)。由于只有10 * 10 = 100种可能的方法来生成两个这样的统一变量,因此您可以计算每个这样的对的表达式,然后直接计算这些期望值。在Python中定义:

def f(x,y): return 60*x/(60*x + 40*y)

然后:

>>> sum(f(x,y) for x in range(1,11) for y in range(1,11))
58.36068355253924

请注意,您乘以的100取消了计算期望所需的1/100因子。

同样,如果你定义:

def g(x,y): return 40*y/(60*x + 40*y)

然后:

>>> sum(g(x,y) for x in range(1,11) for y in range(1,11))
41.639316447460756

这些与您观察的内容相符。