18
Given a sequences of events with probabilities between 0.0 and 1.0, generate and derive the probability of each combination occurring. You may presume that a sequence of numbers is provided in whatever construct your chosen language provides.
Here's an example; you may presume that the length of the sequence's combinations fit into memory:
{ 0.55, 0.67, 0.13 }
The program shall print each combination and the associated probability of that sequence occurring. A 1 shall denote that the event in that index of the input sequence occurred and a 0 shall denote that that event did not occur. The desired output is below (I don't care about printing the work, that's just for informational purposes of the algorithm):
[0,0,0] = (1 - 0.55) * (1-0.67) * (1-0.13) = 0.129195
[0,0,1] = (1 - 0.55) * (1-0.67) * (0.13) = 0.019305
[0,1,0] = (1 - 0.55) * (0.67) * (1-0.13) = 0.262305
[0,1,1] = (1 - 0.55) * (0.67) * (0.13) = 0.039195
[1,0,0] = (0.55) * (1-0.67) * (1-0.13) = 0.157905
[1,0,1] = (0.55) * (1-0.67) * (0.13) = 0.023595
[1,1,0] = (0.55) * (0.67) * (1-0.13) = 0.320595
[1,1,1] = (0.55) * (0.67) * (0.13) = 0.047905
This problem is tangentially related to calculating a "Cartesian product".
Remember, this is code-golf, so the code with the fewest number of bytes wins.
3Welcome to Programming Puzzles & Code Golf, and nice first challenge! – Doorknob – 2016-11-06T00:55:53.680
Would
[0.129195, 0.019305, 0.262305, ..., 0.047905]
be enough as output or are the[0,0,0], [0,0,1], ...
necessary? – Laikoni – 2016-11-06T01:00:49.927@Laikoni That output is fine. The output portion isn't the meat of the problem. – Mark Johnson – 2016-11-06T02:17:00.783
Can the output be in reverse order? – Luis Mendo – 2016-11-06T05:07:13.210
@LuisMendo Sure, why not. – Mark Johnson – 2016-11-06T05:12:00.530