Message Boards Message Boards

1
|
6418 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Combine probabilities in Wolfram Language?

Posted 8 years ago

I am trying to use Mathematica to check my results to questions in the book "Probability and Statistics for Computer Science 2ed." There are no answers in the book and so no way to check that I have the correct result. At present I am trying to combine probabilities and can't find a function in mathematica that does this automagically. I am sure it is simple but my searches in the documentation have not revealed the type of function I am looking for. Below is a sample of the type of question I am trying to answer: 2.10 Three computer viruses arrived as an e-mail attachment. Virus A damages the system with probability 0.4. Independently of it, virus B damages the system with probability 0.5.
Independently of A and B, virus C damages the system with probability 0.2. What is the probability that the system gets damaged? And my Mathematica code to answer this Question:

va=0.4;
vb=0.5;
vc=0.2;
va+vb+vc-va*vb-va*vc - vb vc+va*vb*vc

My question is, is there a function that will take a list of the probabilities and return the combined probability?

POSTED BY: Mark Henwood
4 Replies
Posted 8 years ago

After much trial and error, I'm really good at error, I have found that Probability[] does combined probabilities automagically. The key is choosing the right distribution and specifying the distribution correctly. For the purposes of chapter 2 in "Probability and Statistics for Computer Science 2ed." the distribution to use is UniformDistribution[]. To specify the variables as being independent specify each variable's distribution separately in a list.

u = UniformDistribution[];

dist = u;

vA = x <= 0.4;

vB = y <= 0.5;

vC = z <= 0.2;

(* v = or ^ = and *)

Probability[ vA v vB v vC, {x [Distributed] dist, y [Distributed] dist, z [Distributed] dist}] `

Attachments:
POSTED BY: Mark Henwood
Posted 8 years ago

I had responded to this earlier but it must not have gone through... First of all, Thanks for both of your inputs. I will have to look at Michael's as I have been looking for a reason to use Fold. For both answers though I don't have the combinedProbs[] function. Since it starts with a lowercase letter I am assuming it is a user defined function. Is this function available? Thank you Marco for giving me an example of how to generate random inputs. I will need them in the next chapter I am sure but the present chapter is looking for exact answers to the questions posed and so I am assuming that there is one number that will correctly answer the question. I know I could run the experiment several times and take the average, as you did above, but I would not necessarily get the same answer as what the book is looking for.

POSTED BY: Mark Henwood
Posted 8 years ago

Hello,

very similiar to Marco's solution ist this one which avoids explicit usage of indices:

combinedProbs[ list_] := 1 - Fold[#1 (1 - #2) &, 1, list]

Regards,

Michael

POSTED BY: Michael Helmle

Hi,

yes there is indeed. This is the function:

combindedProbs[lis_List] := 1. - Product[1 - lis[[i]], {i, 1, Length[lis]}]

in your case that is 0.76. You can call this function like so:

combinedProbs[{0.4,0.5,0.2}]

With Mathematica you can of course also simulate that experiment. You can use RandomChoice with probabilities to simulate the "infections":

Table[Total[{RandomChoice[{0.6, 0.4} -> {0, 1}], RandomChoice[{0.5, 0.5} -> {0, 1}],  RandomChoice[{0.8, 0.2} -> {0, 1}]}], {10}]

You get a triple of three numbers for each simulation, which is made up of zeros and ones. Zero at position one means "no infection by virus one", a one at position two means "infection with virus two", etc. The 10 at the end simulates 10 computers.

Now we can run this 1000 times and check how many computers have no infection, i.e. the sum of the three entries is zero.

M = 1000; N[Length[Select[Table[Total[{RandomChoice[{0.6, 0.4} -> {0, 1}], RandomChoice[{0.5, 0.5} -> {0, 1}], RandomChoice[{0.8, 0.2} -> {0, 1}]}], {M}], # != 0 &]]/M]

This gives something close to 76%.

Cheers,

M.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract