1

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

加权 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);
    }
}
4

1 回答 1

2

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

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

这些确实与您正在观察的内容相吻合。

于 2017-09-25T16:12:12.107 回答