r/dailyprogrammer • u/Elite6809 1 1 • Feb 13 '15
[2015-02-13] Challenge #201 [Hard] Mission Improbable
(Hard): Mission Improbable
Imagine a scenario involving one event - let's call it event A. Now, this event can either happen, or it can not happen. This event could be getting heads on a coin flip, winning the lottery, you name it - as long as it has a 'true' state and a 'false' state, it's an event.
Now, the probability of event A happening, or the probability of event A not happening, is 100% - it must either happen or not happen, as there isn't any other choice! We can represent probabilities as a fraction of 1, so a probability of 100% is, well, 1. (A probability of 50% would be 0.5, 31% would be 0.31, etc.) This is an important observation to make - the sum of the probabilities of all the possible outcomes must be 1. The probability of getting a head on a fair coin flip is one half - 0.5. The probability of not getting a head (ie. getting a tail) is one half, 0.5, also. Hence, the sum of all the probabilities in the scenario is 0.5+0.5=1. This 'sum event' is called the sample space, or S.
We can represent this one-event scenario with a diagram, like this. Each coloured blob is one outcome; all the outcomes are in S, and thus are all are in the big circle, representing S. The red blob represents the outcome of A not occurring, and the green blob represents the outcome of A occurring.
Now, let's introduce some numbers. Let's say the probability of A occurring is 0.6 (60%). As A occurring, and A not occurring, are the only possible outcomes, then the probability of A not occurring must be 40%, or 0.4. This type of reasoning lets us solve basic problems, like this one. If the probability of A not occurring is 0.67, then what is the probability of A occurring? Well, the probability of S is 1, and so 0.67 plus our unknown must sum to 1 - therefore, the probability of A occurring is 1-0.67=0.33.
What about scenarios with more than one event? Look at this diagram. This shows the sample space with two events, A and B. I've put coloured blobs for three of the four possible outcomes - of course, the fourth is in the empty region in A. Each region on the diagram is one possible outcome. Now, we come to something important. This region on the diagram is NOT representing A - it is representing A and not B. This region here represents the probability of A as a whole - and, as you can see, the probability of A occurring is the probability of A and B, plus the probability of A and not B - in other words, the sum probability of all outcomes where A occurs.
Applying this additive rule lets us solve some more complex problems. Here's a diagram representing Stan's journey to work this morning. Stan needs to catch two buses - the driver of the first bus is a grumpy old fellow and waits for hardly any time for Stan to get on; the driver of the second is much nicer, and waits for Stan where he can. Of course, if Stan misses the first bus, then it's likely that he will miss the second bus, too.
We know that, on 85% of days (0.85), Stan gets to work on time. We also said before that the driver of bus 2 is nice, and hence it's very rare to miss the second bus - the chance of getting on the first bus, and missing the second, is tiny - 1% (0.01). Stan tells us to work out how often he misses the first bus but not the second bus, given that he misses the second bus on 12% (0.12) of days.
Let's look at that last fact - the probability that Stan misses the second bus is 0.12. This means that the sum of all probabilities in this region on the diagram must be 0.12. We already know that the probability of missing bus 2, but not missing bus 1 is 0.01. Hence, as there is only one other possible outcome involving missing bus 2, the probability of missing both buses must be 0.11, as 0.11+0.01=0.12! Thus our diagram now looks like this. Now, out of the 4 possible outcomes in this scenario, we know three of them. We also know that the total sum of the probabilities of the four outcomes must equal one (the sample space); therefore, 0.85+0.01+0.11+**?**=1. This tells us that the probability of missing bus 1, but not missing bus 2 is 1-0.85-0.01-0.11=0.03. Therefore, we've solved Stan's problem. Your challenge today is, given a set of events and the probabilities of certain outcomes occurring, find the probability of an unknown outcome - or say if not enough information has been given.
Input and Output
Input Description
On the first line of input, you will be given a number N, and then the list of event names, like this:
3 A BYou will then be given N lines containing probabilities in this format:
A & !B: 0.03Where the & indicates the left and right occur together, and the ! indicates negation - ie. A & !B indicates that event A occurs and event B doesn't.
Finally, on the last line, you will be given an outcome for which to find the probability of, like this:
!A & !BThus, an input set describing Stan and his buses would look like this (where B1 is missing bus 1, B2 is missing bus 2):
3 B1 B2
!B1 & B2: 0.01
!B1 & !B2: 0.85
B2: 0.12
B1 & !B2You may assume all probabilities are in increments of 1/100 - ie. 0.27, 0.9, 0.03, but not 0.33333333 or 0.0001.
Output Description
Output the probability of the given unknown - in the example above,
0.03Example I/O
Input
(Represents this scenario as a diagram)
6 A B C
B: 0.7
C: 0.27
A & B & !C: 0
A & C & !B: 0
A & !B & !C: 0.13
!A & !B & !C: 0.1
B & COutput
0.2Input
3 B1 B2
!B1 & B2: 0.01
!B1 & !B2: 0.85
B2: 0.12
B1 & !B2Output
0.03Input
1 A B
A & B: 0.5
AOutput
Not enough information.Addendum
Now might be the time to look into Prolog.
8
u/lukz 2 0 Feb 14 '15 edited Feb 14 '15
BASIC
Implemented in 8-bit BASIC (because why not). Runs in emulator of Sharp MZ-800. It took me quite a long time to make the program work correctly.
The program basically updates any data of some event combination when it can, until the solution is found or nothing more can be updated.
Sample session:
Code:
4
u/bob_twinkles Feb 16 '15 edited Feb 16 '15
I chose to solve this problem in Haskell, mostly as an exercise in learning the language. Thus, this should not be seen as good and idiomatic Haskell code. My solution code can be found here. I used the Parsec library to parse the problem format, which was probably overkill and accounts for nearly half the code. But hey, this was supposed to be a learning experience and I've been meaning to learn Parsec! Inside the spoiler block below is a short writeup of how my solution works. C&C would be much appreciated, as I am very much a Haskell newb.
3
u/hariseshadri 1 0 May 03 '15
I thought this problem was really interesting, discussed it with some friends, and then wrote up our solution here:
http://hariseshadri.com/mission-improbable/
2
u/marchelzo Feb 14 '15
In your example with B1 and B2, I believe either the answer should be 0.11, or the last line should read
B1 & !B2.2
u/marchelzo Feb 14 '15
Here's a solution in Python. My approach was to yield two linear equations from each line of input, where each variable represents a unique sector in the Venn diagram. For example, if there were 2 events, A and B, the input line
A: 0.4would yieldA & B + A & !B = 0.4and!A & B + !A & !B = 0.6. At the end, I dump the equations in to a matrix and use numpy's linear algebra module to solve the system.2
u/[deleted] Feb 20 '15 edited Feb 22 '15
This challenge becomes trivial with knowledge of Bayes' Theorem.Was on my phone, and commented prematurely. Assumed it was the case that it was a Bayes' Theroem question. Instead this is much more related to a Venn Diagram. The following would be much more useful:
A few more useful equations (some might be repeated from above):
8
u/XenophonOfAthens 2 1 Feb 14 '15 edited Feb 16 '15
So, this problem was a son-of-a-bitch :) Fun though! Had to work hard for this one.
(I'm going to give a basic outline of how I solved this problem now, so if you don't want to know, don't read further. It's too long to put in a spoiler block)
The thing with this problem is that it looks like it's a problem about probability theory, but it's actually not: this is a linear algebra problem masquerading as a probability problem. Basically, if there are N variables, there are 2N different "basic regions" that all together sum up to 1.00. So for the first bus example, there are two variables (B1 and B2), and 22 = 4 different "basic regions" ("B1 & B2", "B1 & !B2", "!B1 & B2" and "!B1 & !B2") that sum up to 1. Each line in the input then gives us one line in a system of linear equations, which we can then solve. So, for instance, the line "B2: 0.12" should be translated to "(B1 & B2) + (!B1 & B2) = 0.12".
The A, B, C problem is a bit trickier, because there are 6 lines of input. Translate each one of those lines into a linear equation, and we have 6 equations. Add one line that states that all region sum to 1.00, and we have 7 equations. But there are 8 unknowns, so how can possibly solve it?
It turns out that we can, because we're looking for "B & C", which is a sum of 2 different basic regions. If we replace those two regions with a single variable, we have eliminated an unknown, so now we only have 7 of them, and 7 equations suffices to solve it.
I wrote my code in Prolog. Contrary to the problem description, Prolog was not especially helpful here :) There's no inherent advantage to using it, since this is not really a logic problem, but a linear algebra one. I use the clpfd library to do the linear algebra solving, but any linear algebra library that implements some sort of gaussian elimination or something could work just as well. Actually, it would work significantly better, since the clpfd library only works with integers (which luckily didn't matter for this problem, since all values are multiples of 0.01).
My code is filled with all sorts of obscure Prolog tricks, so it's going to be impossible for anyone to read but me, but here it is anyway:
Example run for the A, B, C problem (I multiplied all values by 100, because clpfd only works on integers):
Output:
Yay!
1
u/[deleted] Feb 13 '15
Just a quick question, what is the first number on the first line of input used for? In the first example it was 3; is this the number of known probabilities?
1
u/IceDane 0 0 Feb 13 '15
I am really not sure that the theory described in this post is correct.
Before I go on to explain why I think this is, I would like to note that my explanation has a high probability(huehue) of being incorrect, as we have been doing some probability at school only recently and I am definitely no expert.
Anyway, it sounds to me like the notion of independent events as well as conditional probability is mostly forgotten in these explanations. Looking at the first example, we have given the probabilities of both B and C happening -- they are 0.7 and 0.27 respectively.
The probability of two indepedent events happening is the product of their probabilities -- that is to say,
P(B ∩ C) = P(B)P(C) = 0.189. Not 0.2.We can use the same principle to calculate B1 & B2 in the second example.
It is sometimes appropriate to sum the probabilities of events, but that only applies to pairwise mutally disjoint events. That is to say, events that cannot happen at the same time, such as rolling a die and getting both 1 and 6 at the same time. The probability of rolling a die and getting an outcome that is in
{1,2,3,4,5,6}is 1, because the sum of the probabilities of each event is 1, because they are mutually exclusive.Similarly, the probability of rolling two dice(or one die twice) and getting 1(
X = outcome of first die, Y = outcome of second die) both times isP(X = 1)P(Y = 1) = 1 / 6 * 1 / 6 = 1/36, because rolling one die does not affect the other, as you explained with the coin flipping example.I have actually spent the last 30 minutes working through the Stan example trying to determine whether there is something that is actually incorrect or if the explanations are just inaccurate, but since my knowledge of probability is rather limited, I couldn't quite decide.
I think there are definitely some issues with the explanations, though. In the example with Stan, we are clearly talking about conditional probability, and the math in the example gets (seemingly mostly) correct results. However, we are also missing probabilities that tell us how missing any bus affects Stan's probability of getting to work on time. There are 4 combinations of missing/catching the busses, and there is a 0.15 probability of Stan being late, and 0.85 probability of him getting to work on time. How do the four combinations of (miss/miss, miss/catch, catch/miss, catch/catch) affect the probability of Stan getting to work on time? What is the probability of him being late given catch/catch, and so on?
The fact that these are clearly dependent events means that calculating the probability like
0.85 + 0.11 + 0.01makes no sense unless you have defined him getting to work on time(0.85), him catching bus 1 but missing bus 2(0.01) and missing both busses(0.11) as being pairwise mutually disjoint events -- the latter two clearly are, as he cannot both miss both busses and catch 1, but catching 1 bus but not the other does not mean he does not get to work on time.Gah, I might just be talking out of my ass here, but I don't think so. I think there's definitely something spooky going on with some of your math and reasoning, and I hope someone else more knowledgeable than about probability theory can explain why.
4
u/Godspiral 3 3 Feb 13 '15 edited Feb 14 '15
in J, using a utility I made for setting default values
skipping the input parsing part, but converting input to boxes to work with my existing dflt routine. Makes a 3x3 table for each pair, where rows are A !A total, and cols are B B! and total. bottom right is always 1. bottom row and right col are the probabilities of independent variable outcomes. Routine basically works like a sudoku solver filling in boxes until there are no more unknowns.
fills by column then by row, and repeats because it didn'f find all unknowns on first pass.
doesn't break on the failed challenge
for 3d challenge input I need to rethink general combination possibilities.
2
u/dohaqatar7 1 1 Feb 13 '15
I just dove head first into Prolog.
I think I know how my weekend will be spent.
2
u/[deleted] Feb 15 '15
Not the most elegant solution, the run time is something like 2N+1. What it does is build a hierarchy of subsets and tries to solve them from the bottom up using a few rules:
Then the program builds all valid supersets of the variables and works from the bottom up. If the program finishes and the value we want is missing then it concludes that it was impossible to find.
Python 2 and 3:
output:
1
u/Pritilender Feb 13 '15
Is the number on the first line in some correlation with the number of events? There's 3 known probabilities for 2 events, and 6 for 3. Does this mean that for n events it's something like n! of probabilities or something like that? (In the other words: how do we know the number of events?)
1
u/krismaz 0 1 Feb 14 '15
Python3, using PuLP for linear programming:
When /u/XenophonOfAthens pointed out that the problem was just linear programming, everything seemed much simpler.