为什么我的模拟没有找到无限期望值?

时间:2022-01-19 20:20:36

In the Youtube video Numberphile Infinity (starting at 6:09) there is an experiment described (the St. Petersburg Paradox) in which a game has infinite expected value.

在Youtube视频中,Numberphile Infinity(从6:09开始)有一个实验(圣彼得堡悖论),其中游戏具有无限的期望值。

I tried to write a Java program to find out a long-run expected value (average). The theoretical expectation is infinity, but what I get is some numbers around 10. Is it because my program is wrong? Because I think when the number of experiment is large enough, the experimental average will be very close to the mathematical expectation. Here is my program.

我试着编写一个Java程序来找出长期预期值(平均值)。理论上的期望是无限的,但我得到的是10左右的数字。这是因为我的程序错了吗?因为我认为当实验数量足够大时,实验平均值将非常接近数学期望值。这是我的计划。

public class Main {
    public static final int NUM_EXPERIMENT = 1000000;
    public static void main(String[] args) {
        int total = 0;
        for (int i = 0; i < NUM_EXPERIMENT; i++) {
            int counter = 1;
            int subtotal = 1;
            while ((int) (Math.random() * 2) == 0) {
                subtotal *= 2;
                counter++;
            }
            total += subtotal;
        }
        double expectation = total / (double) NUM_EXPERIMENT;
        System.out.println("The expectation of this experiment is " + expectation);
    }
}

3 个解决方案

#1


4  

I think this has everything to do with math and little to do with ints.

我认为这与数学有关,与int无关。

One thing you might notice is that if you run more and more trials, the expected value will get larger and larger. The expected value is infinite because there are extraordinarily unlikely events that yield extraordinarily large payoffs. You're unlikely to get this to happen because the probability of earning 2n profit is 2-n. For example, this means that the probability of getting $1,000,000 (approximately $220) is about 1 in 220. On expectation, you'd need to run one million trials before you ever see this occur. Consequently, if you only run one hundred thousand trials, you wouldn't expect to see something like this happen. However, as the number of trials increases, the probability that you hit one of these sorts of sequences grows, and so the expected value will increase.

您可能注意到的一件事是,如果您运行越来越多的试验,预期值将变得越来越大。预期值是无限的,因为有非常不可能的事件会产生非常大的收益。你不可能实现这一点,因为获得2n利润的概率是2-n。例如,这意味着获得$ 1,000,000(约220美元)的概率约为220中的1。在预期中,您需要在看到这种情况之前运行一百万次试验。因此,如果你只进行了十万次试验,你就不会发现这样的事情会发生。但是,随着试验次数的增加,您点击其中一种序列的概率会增加,因此预期值会增加。

Think of it this way: you will never, ever see infinite profit because eventually the coin tosses the wrong way and you collect your winnings. Therefore, you won't ever get infinite profit reported by your program. However, as you play more and more games, the average will increase. The law of large numbers does indeed say that as you play more games you'll approach the average more and more closely, but it doesn't say that a small number of trials will suffice. Since your expected value is infinite, you're going to need a lot of trials before you're going to converge to infinity. ^_^

可以这样想:你永远不会看到无限的利润,因为最终硬币以错误的方式抛出并收集你的奖金。因此,您的计划不会获得无限利润。但是,当你玩越来越多的游戏时,平均值会增加。大数法确实说,当你玩更多游戏时,你会越来越接*均值,但并不是说少数试验就足够了。由于您的预期值是无限的,因此在您收敛到无穷大之前,您需要进行大量试验。 ^ _ ^

Other answers have noted that you might be overflowing the maximum value of an int, which is true but I think irrelevant here. You should switch to using something like a double or long to hold the values, but the reason you're not seeing infinite payout is because it's exponentially unlikely that you'll see one of the sequences that yields extreme payouts.

其他答案已经注意到你可能会溢出int的最大值,这是真的,但我认为这里无关紧要。你应该转而使用像double或long这样的东西来保存这些值,但是你没有看到无限支付的原因是因为你看到其中一个产生极端支出的序列是指数级的。

Hope this helps!

希望这可以帮助!

#2


1  

You are overflowing the integer data-types. They hit a max value and cycle back to the minimum value. Can't do infinite equations literally like that.

您正在溢出整数数据类型。它们达到最大值并循环回到最小值。不能做那样的无限方程式。

#3


0  

Range of integer value is ~ -2 147 000 000 to 2 147 000 000, if you exceed the max value of an integer, it is overflowed to the min value of an integer.

整数值的范围是〜-2 147 000 000到2 147 000 000,如果超过整数的最大值,它将溢出到整数的最小值。

#1


4  

I think this has everything to do with math and little to do with ints.

我认为这与数学有关,与int无关。

One thing you might notice is that if you run more and more trials, the expected value will get larger and larger. The expected value is infinite because there are extraordinarily unlikely events that yield extraordinarily large payoffs. You're unlikely to get this to happen because the probability of earning 2n profit is 2-n. For example, this means that the probability of getting $1,000,000 (approximately $220) is about 1 in 220. On expectation, you'd need to run one million trials before you ever see this occur. Consequently, if you only run one hundred thousand trials, you wouldn't expect to see something like this happen. However, as the number of trials increases, the probability that you hit one of these sorts of sequences grows, and so the expected value will increase.

您可能注意到的一件事是,如果您运行越来越多的试验,预期值将变得越来越大。预期值是无限的,因为有非常不可能的事件会产生非常大的收益。你不可能实现这一点,因为获得2n利润的概率是2-n。例如,这意味着获得$ 1,000,000(约220美元)的概率约为220中的1。在预期中,您需要在看到这种情况之前运行一百万次试验。因此,如果你只进行了十万次试验,你就不会发现这样的事情会发生。但是,随着试验次数的增加,您点击其中一种序列的概率会增加,因此预期值会增加。

Think of it this way: you will never, ever see infinite profit because eventually the coin tosses the wrong way and you collect your winnings. Therefore, you won't ever get infinite profit reported by your program. However, as you play more and more games, the average will increase. The law of large numbers does indeed say that as you play more games you'll approach the average more and more closely, but it doesn't say that a small number of trials will suffice. Since your expected value is infinite, you're going to need a lot of trials before you're going to converge to infinity. ^_^

可以这样想:你永远不会看到无限的利润,因为最终硬币以错误的方式抛出并收集你的奖金。因此,您的计划不会获得无限利润。但是,当你玩越来越多的游戏时,平均值会增加。大数法确实说,当你玩更多游戏时,你会越来越接*均值,但并不是说少数试验就足够了。由于您的预期值是无限的,因此在您收敛到无穷大之前,您需要进行大量试验。 ^ _ ^

Other answers have noted that you might be overflowing the maximum value of an int, which is true but I think irrelevant here. You should switch to using something like a double or long to hold the values, but the reason you're not seeing infinite payout is because it's exponentially unlikely that you'll see one of the sequences that yields extreme payouts.

其他答案已经注意到你可能会溢出int的最大值,这是真的,但我认为这里无关紧要。你应该转而使用像double或long这样的东西来保存这些值,但是你没有看到无限支付的原因是因为你看到其中一个产生极端支出的序列是指数级的。

Hope this helps!

希望这可以帮助!

#2


1  

You are overflowing the integer data-types. They hit a max value and cycle back to the minimum value. Can't do infinite equations literally like that.

您正在溢出整数数据类型。它们达到最大值并循环回到最小值。不能做那样的无限方程式。

#3


0  

Range of integer value is ~ -2 147 000 000 to 2 147 000 000, if you exceed the max value of an integer, it is overflowed to the min value of an integer.

整数值的范围是〜-2 147 000 000到2 147 000 000,如果超过整数的最大值,它将溢出到整数的最小值。