2DGD-F0TH / 2DGD_F0TH

[CC BY-NC-SA] A compendium of the community knowledge on game design and development
https://therealpenaz91.itch.io/2dgd-f0th/
Other
223 stars 18 forks source link

3.16.3 Probability of mutually exclusive events - coin example #86

Closed abreathingcorpse closed 1 year ago

abreathingcorpse commented 2 years ago

Feature Request

I believe that the "Heads + Tails" or "Tails + Heads" probability could be just a little bit more enriching.

Describe briefly what you would like to see

The current coin example mentions the heads or tails probability of a single coin toss, which I believe is already addressed in the 3.16.2 Probability of independent events section. I think that by this point the reader already understands that that probability is 1.

However, you already posed an interesting question on the Pitfall Warning of the previous section. At least, I was thinking about it while reading this section.

Explain in detail your idea

If I understood this two sections properly, then: H .- Heads T .- Tails

P(H and T) = P (H) * P (T) = 1/4 Similarly: P(T and H) = 1/4 Therefore: P( P(H and T) or P(T and H)) = 1/4 + 1/4 = 1/2

I might be wrong here. If so, I would like to discuss it.

Additional Context

It might not be much but, I believe it could be a slight better example. I had to do the math and I'm still unsure if this is how it works. Perhaps this could help future readers, showing how to mix both independent events & mutually exclusive events. Hope that I'm not being too self-centered and that you can see value on this idea. If not, please let me know.

Penaz91 commented 2 years ago

Hello.

Your idea is essentially right, given the right context.

Your calculation works because the event of tossing a coin ticks both boxes of "independence" (having a "Heads" result coming out doesn't influence how the next "throws" will behave) and "mutual exclusivity" (if a coin lands on "heads", it cannot land on "tails" on the same throw).

There is a formula for "non mutually exclusive events", which is P(A or B) = P(A) + P(B) - P(A and B).

That could apply on stuff like cards, where you want to see the probability of, let's say, "get a face card (J,Q,K) or a 'hearts' card".


There is a big probability (no pun intended) that my example with coins created some confusion because the probability of throwing a coin twice is the same as throwing two coins in a single event (probably the latter would have made for a more "sane" example). Thus what I wrote kind of "mixed" the concept of "event" and "outcome".

I can't find a way to explain it better at the moment, but maybe with further questions I'll be able to come to a more sane series of examples.

Thank you, as always.

abreathingcorpse commented 2 years ago

Your idea is essentially right, given the right context.

Thank you for clarifying this.

There is a formula for "non mutually exclusive events", which is P(A or B) = P(A) + P(B) - P(A and B).

That's the one that I remember back from uni, this is when stuff starts to get tricky.

There is a big probability (no pun intended) that my example with coins created some confusion because the probability of throwing a coin twice is the same as throwing two coins in a single event (probably the latter would have made for a more "sane" example). Thus what I wrote kind of "mixed" the concept of "event" and "outcome".

This paragraph is somewhat confusing to me. Can one throw two coins in a single event? Are events and outcomes different terms then?! According to this website:

Events in probability are outcomes of random experiments.

I still believe that they are kind of the same concept.

Penaz91 commented 2 years ago

I have always thought of "events" and "outcomes" as two different things.

An "event" is something that I'm expecting to happen, thus "coin lands on heads" as an event is the subject I'm trying to calculate the probability of. As an outcome it can be seen as the "coin actually landing on heads in that specific trial".

For the purposes of calculation they are the same.


I think I see now why you mentioned this as a feature proposal, I think you meant to expand the section to include "mixing independent and mutually exclusive events".

Am I getting this right? (Language barriers are hard!)

abreathingcorpse commented 2 years ago

Yes, that was my thought! You may even include a "non mutually exclusive events" section with your deck of cards example. Unless you do not see the value on that from a Game Development perspective.

Regarding the language, I believe that you speak italian as a mother tongue. I can't do that but, I can do spanish. Not sure if that would help?

Penaz91 commented 2 years ago

Regarding the language, I believe that you speak italian as a mother tongue. I can't do that but, I can do spanish. Not sure if that would help?

Don't worry, it's just me being way too distracted for my own good 😆

Thank you kindly for the thought, it's really appreciated

Yes, that was my thought! You may even include a "non mutually exclusive events" section with your deck of cards example. Unless you do not see the value on that from a Game Development perspective.

I have been adding both examples, I'm thinking if the concept of "Conditional Probability" could be useful as well, but I can't really find a use for that yet.

abreathingcorpse commented 2 years ago

So, I read this Conditional Formatting webpage. First of all, I think that it helped me understand your following statement earlier bette, with the help of this image:

An "event" is something that I'm expecting to happen, thus "coin lands on heads" as an event is the subject I'm trying to calculate the probability of. As an outcome it can be seen as the "coin actually landing on heads in that specific trial".

I thought about a Conditional Probability example: Not sure if you know the rules of DnD but, in order to hit an enemy, one needs to throw a 20 sided die (called d20). If it is bigger than a specific threshold, then one may throw a damage die, depending on the attack. For instance, a 6 sided die (called d6).

Now imagine that you are playing DnD, you are fighting against an enemy, let's say it's an orc. You and your enemy are both close to fatal damage, you can attempt to run or you can attempt to kill it. You calculate that the orc has about 3 HP left. But, you can only roll the damage die if you get a nice roll on the d20. You figure about a 13 gets you a hit. Remember that you need to kill it on this turn, otherwise it may kill you. Just hitting it won't suffice which is why one needs the P (A | B).

With that out of the way, we can focus on the probability section: A.- event of d6 being higher than 2. B .- event of d20 being higher than 12.

A = { 3, 4, 5, 6 } B = { 13, 14, 15, 16, 17, 18, 19, 20 }

S.- Sample Space

S would be something like: S = { 1 - 1, 1 - 2, ... 1-6, ... 13 - 1, 13 - 2, ... 13 - 6, 14-1, ... 20 - 1 } I mean, it would be hard to write it all down here but, I'm pretty sure that: |S| = 20 * 6 = 120 Note: I might be wrong on the above S set, since technically the d6 isn't thrown unless one hits 13 on the d20. But, I'm pretty sure that really shouldn't matter.

We have to find the probability of getting at least 3 on the d6, given that it is at least a 13 on the d20.

We already know the formula for P( A | B) = P(A intersection B) / P(B)

I .- Intersection set

I = { 13 - 3, 13 - 4, 13 - 5, 13 - 6, 14 - 1, ... 20 - 6 } This looks an awful lot to: |A| * |B|

The P(B) is easy to get: P(A) = 4/6 = 2/3

I attempted to get this another way: SA .- Sample Space for the A set. ( Not sure if this wording is correct )

| A | / | SA | = 4 / 6 = P(A)

If the above statement is true then, perhaps the following statement is also true?

P(A intersection B) = |A| |B| / |S| = (4 8) / 120 = 32 / 120 = 4 / 15

We can finally get: P( A | B) = P(A intersection B) / P(B) = 4 / 15 / 2 / 3 = 4 / 10

If I'm correct then, the probability of killing this orc is 40%. I do not think it's the worst bet ever. I attack!

Let me know your thoughts and please help me verify my math, as well as my assumptions. I could be awfully wrong here!

Penaz91 commented 2 years ago

Not sure if you know the rules of DnD

I had the chance to play Dungeons&Dragons, even managed to DM for a while 😄


In your example, P(A|B) could be called "The probability of hitting the orc for more than 2HP, given that the d20 roll is higher than 12".

The sample space represents all the possible outcomes of throwing a D20 and a D6, thus its size is |S| = 120 is correct. We can see this experiment as being equivalent to "throwing the D20 and D6 at the same time, and then applying the rules we decided", the fact that the outcome is part of what we're trying to count doesn't influence the number of possible outcomes.

| A | / | SA | = 4 / 6 = P(A)

This is essentially the definition of P(A), P(A) is given by the number of "wanted events" over the "total possible outcomes", just that they're shown as cardinality of sets. SA is the sample space given by a D6, since the event "A" has nothing to do with the possible outcomes, I think a more correct name for it would be Sd6.

P(A intersection B) = |A| |B| / |S| = (4 8) / 120 = 32 / 120 = 4 / 15

This is essentially the definition of P(A and B), just using set cardinalities, joined with the definition of probability, a more complete calculation would be

P(A intersection B) = P(A) P(B) = |A| / |S| |B| / |S| = |A||B|/S = (4 8) / 120 = 32 / 120 = 4 / 15

Thus P(A|B) = P(A and B) / P(B) = (4/15) / (2/3) = (4/15) * (3/2) = 2/5 = 0.4 => 40%


In the end your calculations are correct, and this could open the door to a "probabilistic approach" to enemy AI. Good one!

abreathingcorpse commented 2 years ago

I had the chance to play Dungeons&Dragons, even managed to DM for a while smile

Nice! I'm glad that you liked the idea. One more question, though. You have the following equation:

P(A intersection B) = P(A) P(B) = |A| / |S| |B| / |S| = |A|*|B|/S

But, shouldn't it be? |A| / |S| |B| / |S| = |A| |B| / |S|2

Unless that: P(A) = |A| / Sd6 P(B) = |B| / Sd20

Therefore: P(A intersection B) = P(A) P(B) = |A| / |Sd6| |B| / |Sd20| = |A| * |B| / |S|

Penaz91 commented 2 years ago

But, shouldn't it be? |A| / |S| |B| / |S| = |A| |B| / |S|2

Unless that: P(A) = |A| / Sd6 P(B) = |B| / Sd20

Yes, apologies. I'm really out of it today.

I forgot to put the subscripts where appropriate.

abreathingcorpse commented 2 years ago

Hey, don't worry man! You are teaching me a lot! Thank you kindly!

Penaz91 commented 2 years ago

We can finally get: P( A | B) = P(A intersection B) / P(B) = 4 / 15 / 2 / 3 = 4 / 10

Just noticed a minor mistake, you used P(A) instead of P(B) in the denominator.


I'm glad to be of use, it's nice being able to pass knowledge on. I'm working on adding the new examples!

abreathingcorpse commented 2 years ago

Just noticed a minor mistake, you used P(A) instead of P(B) in the denominator.

Oh wow! Let's try to fix that with my new knowledge:

P(A|B) = P (A and B) / P(B) = (|A| / |Sd6|) (|B| / |Sd20|) 1/P(B) = ( ( |A| |B| ) / |S| ) 1/ P(B) = ( ( |A| |B| ) / |S| ) ( |Sd20| / |B| ) = |A| |Sd20| / |S| = 4 20 / 120 = 2/3

2/3 > 2/5, which is now a safer bet.

Penaz91 commented 2 years ago

Your results match with mine. Fantastic.

The new examples will be available in the next release, as soon as i manage to translate a couple of code listings in lua,python,c++ and javascript. If you want I can send you the newest "unofficial build"

abreathingcorpse commented 2 years ago

That's fine, I'll wait.

Penaz91 commented 1 year ago

I have released a new revision the ebook, I will close the issue for now. Thank you again!

If there's anything you'd like to add, feel free to reopen this issue or make a new one.

abreathingcorpse commented 1 year ago

Awesome! Thank you! I have the following points:

By the way, I like that this is addresed on the notes of the 3.16.3 Probability of mutually exclusive events

Thank you for these additions!

Penaz91 commented 1 year ago
  • 3.16.2 Probability of independent event I think that the example for P (H and T) (or P(T and H)) whatever was better. Since it opens the door for the Pitfall Warning.

I understand what you mean, but that would need to know how to calculate the probability of a union of events. I don't really like referring readers to upcoming chapters or paragraphs in order to understand something fully. This is why I put the example in a note under "Probability of mutually exclusive events".

Alternatively I would need to make a section where all examples are gathered together, but I fear the reader may lose context if I did so.

I'll try thinking of a way to fix this.

abreathingcorpse commented 1 year ago

Let me try to rephrase this, since I'm thinking I confused you. I seem to recall that on the 3.16.2 Probability of independent events you had the following equation: P(H and T) = P(H) * P (T) = 1/2 + 1/2 = 1/4 (I can not recall if it was H and T or T and H) Then the Pitfall Warning explains why "Heads + Tails" is different than "Tails + Heads". Then, on the 3.16.3 Probability of mutually exclusive events section you have a note that addresses both cases: "Heads + Tails" and "Tails + Heads"

Whereas your current equation is: P(H and H) = P(H) * P (H) = 1/2 + 1/2 = 1/4 Then the Pitfall Warning explains why "Heads + Tails" is different than "Tails + Heads" but, none of the examples have been used before, which could be somewhat confusing to the reader. Then, on the 3.16.3 Probability of mutually exclusive events section you have a note that says:

Considering the previous "tossing two coins" example ...

Which was: P(H and H) = P(H) * P (H) = 1/2 + 1/2 = 1/4

I guess that, ultimately what I'm thinking is to update the following equation in 3.16.2 Probability of independent events: P(H and H) = P(H) P (H) = 1/2 + 1/2 = 1/4 into: P(H and T) = P(H) P (T) = 1/2 + 1/2 = 1/4

Unless that you think that would be confusing for the reader.

Penaz91 commented 1 year ago

Oh, I see what you mean now!

Yeah, making the "heads+heads" example and referring to a "heads+tails" example (that doesn't really exist) will definitely create confusion.

I am fixing that issue right now. Sorry for the delay in the response!

abreathingcorpse commented 1 year ago

What delay? Hahah! Thank you for all of your work, man.

Penaz91 commented 1 year ago

I have added the "heads + tails" example as a second example to reinforce the concept and "lead in" to the pitfall that follows. Let me know what you think about it!

Thank you!

abreathingcorpse commented 1 year ago

It reads good to me. Thank you very much, man.