Curtis White
Curtis White

Reputation: 6353

Probability Code Problem

When I switch out isWinDefault with isWinConfidence I get drastically different results. I felt they should be the same. Is there a bug in my code or a property of statistics that I've misunderstood?

This test is meant to simulate flipping a single coin 1x vs a coin many times.

The question is

If P(x) is 70% then should p(x) * 100 be >= 70 70% of the time, no?

Thanks.

    private void TestWin()
    {
        double headsUp = 0;
        double testRuns = 100;
        for (double i = 0; i < testRuns; i++)
        {
            if (IsWinConfidence())
            {
                headsUp++;
            }

        }
        label1.Text = "Probability of Heads is " + headsUp /testRuns;

    }

    private bool IsWinDefault()
    {
        if (r.NextDouble() <= .7)
        {
            return true;
        }
        return false;
    }

    private bool IsWinConfidence()
    {
        int headsCount = 0;
        for (double x = 0; x < 100; x++)
        {
            if (IsWinDefault())
                headsCount++;

        }

        double pHeadsCount = headsCount / 100d;
        if (pHeadsCount >= .7 )
        {
            return true;
        }
        else
        {
            return false;
        }


    }

Upvotes: 6

Views: 1805

Answers (6)

probability is a counting problem. Count the occurrence of each class type (0 heads 1 tails) then divide by the total. The odds are a function of the probability. Odds tell you the likelihood of heads or tails when flipping.

from collections import Counter

flips=10000

data=[]
for i in range(flips):
    data.append(random.choice([0,1]))

dictProbability={}
for key,value in Counter(data).items():
    dictProbability[key]=value/flips

print(dictProbability)

output:

{0: 0.5014, 1: 0.4986}
 .5 probability of heads
 .49 probability of tails

a bernouli binomial distribution

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

Here's the simple answer:

A 70% probability means that on average, 100 coin flips will produce 70 heads up. It will, however, sometimes be more than 70, and sometimes less.

In other words, the number of heads up you will get for each batch of 100 coin flips, will be close to 70. Sometimes below 70, sometimes above 70, sometimes exactly 70.

So if the number swings around 70, it only stands to reason that if you're asking "how often will it swing above 70, or equal to 70", you will get an answer that says "around 50% of the time".

So you're not asking the right question with your code there.

In fact, increasing the number in your loop in IsWinConfidence to something much higher gives you a number close to 50.


Let's pick apart your arguments here.

You're saying that if you have:

A biased coin, that 70% of the time, will land with heads up, and 30% of the time, with heads down

Then you're saying that:

If I flip the coin 100 times, I should get more than 70 heads up

One does not lead to the other, there's a flaw in your arguments here. Probability is not about guarantees, it's about averages.

If probability was absolute, your second statement should be:

If I flip the coin 100 times, I should get 70 heads up

Notice the lack of "more than" here.

Instead, what the first argument means is this:

If I flip the coin 100 times, then flip it 100 more times, then 100 more times, then 100 more times, and so on, then on average each 100 flips will have 70 heads up

Now, I don't know enough about probability calculations to pick apart your loops and counts, but I do know that just following logic, your arguments fail.

Let's try another approach.

If the coin is even, though biased, it means out of a 100 coin flips, you will sometimes get more than 70, and sometimes less than 70.

In my naive mind, this means that... On average, you will only get more than 70 coin flips half the time.

By increasing the numbers in your loop to 100.000, I get the confidence-function to return close to 50. This seems to back up my theory.

But as I said, the chance of me being an expert (or even dabbler) in probability is less than zero.

Upvotes: 2

I. J. Kennedy
I. J. Kennedy

Reputation: 25819

IsWinDefault "wins" 70% of the time, as expected; IsWinConfidence "wins" about 53.77%, so you should see numbers close to that. See binomial distribution for more.

Upvotes: 1

Steve
Steve

Reputation: 8511

If P(x) is 70% then should p(x) * 100 be >= 70 70% of the time, no?

No. The confidence is not related to the probability in this way...

What you are doing is in the second method is tossing a biased coin 100 times and returning true if you get 70 or more heads. As you have fixed your coin so that on average is will give you heads 70% of the time you would expect to get 70 heads out of 100 tosses "sometimes", but that "sometimes" is not 70% of the time.

Upvotes: 1

Lo&#239;c F&#233;vrier
Lo&#239;c F&#233;vrier

Reputation: 7750

Update:

The first function returns true 70% of the time so headsCount will be equal, with very high probability, to ~70 (if 100 is replaced with a bigger number if will tends to be 70% of that number).

Hence

pHeadsCount >= .7

has a probability, of 50%, wince the value will be ~0.7.

Upvotes: 0

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19956

if (r.NextDouble() <= .7)

vs

if (pHeadsCount >= .7 )

Upvotes: 0

Related Questions