tags:

views:

97

answers:

5

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;
        }


    }
A: 

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.

Loïc Février
@Loic I'm trying to simulate flipping an unfair coin several times versus 1 time. Shouldn't out of 100 flips we likely get at least 70 wins? I'm just saying okay if we get at least 70 wins or better, I count it as a win.
Curtis White
@Loic See I'm running the second loop 100 times. I should be getting 70% and I'm saying okay if I get 70% or better I count it a win, does it make sense?
Curtis White
@Curtis White: Indeed. I've read too fast. What difference of result have you ? What are the two probabilities ? (try also with 10.000 instead of 100 and give that result too)
Loïc Février
@Loic I get 70% when I run with the default and 55% when I use the replacement. ;( The p(x) should approach p(x) as we go toward infinity. I'm very confused.
Curtis White
Answer updated. 55% explained ;)
Loïc Février
@Loic Thanks, I believe you but I'm confused. Shouldn't I be able to determine that X has 70% probability? from this
Curtis White
Nice, came up with the same conclusion shortly after my initial thinking 100 was not large enough a number. But this is correct :). 2 mins later im doubting again =].
bastijn
You agree that pHeadsCount will be ~0.7 ? Then if can be more or less than 0.7 with equal probability, it's basically "noise" arround 0.7.
Loïc Février
A: 

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

vs

if (pHeadsCount >= .7 )

Daniel Mošmondor
+1  A: 

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.

Steve Haigh
+1  A: 

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.

I. J. Kennedy
Yes and if total number increase this will tends to 50%.
Loïc Février
+2  A: 

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.

Lasse V. Karlsen
+1 for this explanation, very well written :). Might consider a final conclusion in the end to complete the answer. Something like fixing (pHeadsCount >= .7 ) ==> (pHeadsCount >= .5 ) and increasing your N to 10000 would solve your problem.
bastijn
@Lasse This is pretty good. Some mention of various distribution would add something too, i.e binomial, normal, etc. Also something about how the odds decrease as more runs are made, i.e from 100 to 1000 it becomes closer to 50%. I may ask this at the stats overflow to get a better handle on it.
Curtis White
Yeah, well, read the last sentence of my answer again, in full :)
Lasse V. Karlsen