Can Mathematica do Bayes Rule conditional probability calculations, without doing the calculation manually? If so how?

I have been searching both the Mathemtaica doco and the web for a hint but cannot find anything. I am not after how to do Bayes Rule manually via Mathematica, I want to know if there is a way to define the conditional probabilities and calculate other ones automagically.

So to use the toy example assuming Bernoulli distributions

```
P(Cancer+) = 0.01
P(Cancer-) = 0.99
P(Test+|Cancer+) = 0.9
P(Test-|Cancer+) = 0.1
P(Test+|Cancer-) = 0.2
P(Test-|Cancer-) = 0.8
```

Is it possible to work out

```
P(Cancer+|Test+) = 0.0434
```

So using the below.

```
Print["P(C+) = ", PCancerT=BernoulliDistribution[0.01]];
Print["P(C-) = ", PCancerF=BernoulliDistribution[0.99]];
Print[]
Print["P(T+|C+) = ", PTestTGivenCancerT=BernoulliDistribution[0.9]];
Print["P(T-|C+) = ", PTestFGivenCancerT=BernoulliDistribution[0.1]];
Print["P(T+|C-) = ", PTestTGivenCancerF=BernoulliDistribution[0.2]];
Print["P(T-|C-) = ", PTestFGivenCancerF=BernoulliDistribution[0.8]];
Print[]
Print["P(T+,C+) = ", PTestTAndCancerT = Probability[vCT&&vTTCT,{vCT\[Distributed]PCancerT,vTTCT\[Distributed]PTestTGivenCancerT}]];
Print["P(T-,C+) = ", PTestFAndCancerT = Probability[vCT&&vTFCF,{vCT\[Distributed]PCancerT,vTFCF\[Distributed]PTestFGivenCancerT}]];
Print["P(T+,C-) = ", PTestTAndCancerF = Probability[vCF&&vTTCF,{vCF\[Distributed]PCancerF,vTTCF\[Distributed]PTestTGivenCancerF}]];
Print["P(T-,C-) = ", PTestFAndCancerF = Probability[vCF&&vTTCF,{vCF\[Distributed]PCancerF,vTTCF\[Distributed]PTestFGivenCancerF}]];
Print[]
Print["P(C+|T+) = ?"];
Print["P(C+|T-) = ?"];
Print["P(C-|T+) = ?"];
Print["P(C-|T-) = ?"];
```

I can work out the joint probabilities by defining all the probability tables manually, but is there a way to get Mathematica to do the heavy lifting? Is there a way to define and calculate these kind of conditional probabilities?

Many thanks for any assistance, even it its “You can’t... stop trying” :)

PS : was this an attempt at doing something along these lines? Symbolic Conditional Expectation in Mathematica

I wouldn't complicate the issue with `Print`

statements and `BernoulliDistribution`

s. You know the probabilities, so the simplest thing to do is to calculate them directly, but perhaps using vectors to get `P(B)`

, and using the fact that pr(cancer) = 1-pr(not cancer) and so on.

Bayes' Theorem states that P(A|B)=(P(A ⋂ B))/(P(B))

The intersection is calculated as the conditional probability (test given cancer) times the probability of cancer.

So something like the following should work:

```
conditionalProb[pC_, pTC_, pTNC_] /;
(0 < pC < 1) && (0 < pTC < 1) && (0 < pTNC < 1) :=
(pTC * pC)/({pTC, pTNC}.{pC, 1 - pC})
conditionalProb[0.01, 0.9, 0.2]
0.0434783
```

And yes, the Probability functionality in version 8 does allow you to calculate conditional probabilities "automagically", but for a problem like this with Bernoulli-distributed events, it's overkill.

Actually... I worked this out symbolically in the past, and it covers a lot of simple (unchained) probabilities. I guess it wouldn't be that hard to add chaining(see below). You're welcome to reply with augmentation. The symbolic approach is far more flexible than working with Bernoulli distributions and creating a proc for Bayes theorem and thinking about the right way to apply it every time.

NOTE: The functions are not bound, like in the post above `((0 < pC < 1) && (0 < pTC < 1) && (0 < pTNC < 1))`

because sometimes you want "unweighted" results, which produce numbers outside of 0-1 range, then you can bring back into the range by dividing by some normalizing probability or product of probabilities. If you do want to add bounds for error checking, do this:

`P[A_ /;0<=A<=1] := some_function_of_A;`

use `Esc+cond+Esc`

to enter `\\[Conditioned]`

symbol in Mathematica.

```
Remove[P];
Unprotect@Intersection;
Intersection[A_Symbol, B_Symbol] := {A, B}
Intersection[A_Not, B_Symbol] := {A, B}
Intersection[A_Symbol, B_Not] := {A, B}
P[Int_List/; Length@Int == 2] := P[Int[[2]] \[Conditioned] Int[[1]]] P[Int[[1]]]
(*// P(B) given knowledge of P(A) //*)
P[B_, A_] := If[NumericQ@B, B,
P[B \[Conditioned] A] P[A] + P[B \[Conditioned] Not@A] P[Not@A]]
P[Not@B_, A_: 1] := If[NumericQ@A, 1 - P[B], 1 - P[B, A]]
P[A_ \[Conditioned] B_] := P[A \[Intersection] B]/P[B, A]
P[Not@A_ \[Conditioned] B_] := 1 - P[A \[Conditioned] B];
```

You then use it as such:

```
P[Cancer]=0.01;
```

Don't need "not cancer" since `P[!Cancer]`

yields `0.99`

(`Esc+not+Esc`

types a very pretty logical not symbol, but `Not[A]`

, `!A`

or `\[Not]A`

work just fine too)

```
P[Test \[Conditioned] Cancer] = 0.9
P[Test \[Conditioned] ! Cancer] = 0.2
```

again: `P[!Test \\[Conditioned] Cancer]`

will be `1-P[Test \\[Conditioned] Cancer]`

by definition, unless you override it.

Now let's query this model:

```
P[Test, Cancer]
P[!Test, Cancer]
```

returns

```
0.207
0.793
```

and

```
P[Cancer \[Conditioned] Test]
P[!Cancer \[Conditioned] Test]
P[Cancer \[Conditioned] !Test]
P[!Cancer \[Conditioned] !Test]
```

returns

```
0.0434783
0.956522
0.00126103
0.998739
```

I guess it would be a nice idea to define `P(B|A1,A2,A3,...,An)`

, anyone up for coding the chain rule using NestList or something like it? I didn't need it for my project, but it wouldn't be that difficult to add, should someone need it.

Similar Questions

I need to create a new CA RULE in Mathematica. How can I do? I mean, I need to create a CA with 3 colors (Black, White and Grey). May you help me?

When I try to change the ASP.NET version to v4 on IIS 6, I receive the following warning: Changing the Framework version requires a restart of the W3SVC service. Alternatively, you can change the Fra

The documentation for MongoClient says that one may say 'host=' and give a full MongoDB URI. Those include a database name. Once I do that, is there some way to extract the db name from the MongoClien

Is it possible that built in functions in Mathematica (like Minimize[expr,{x1,x2,...}]) will start to work via CUDA after installation of CUDA module for Mathematica?

Sorry, I am pretty new to php. Is something like this possible? if ( if ($clauseA) { A } if ($clauseB) { || B } ) { // Do Stuff } What I want to do is if $clauseA is true, include A in condition and

I wrote some code that can spit out a dna sequence of specified length, number of copies, specified probabilities, etc. In IDLE the program works fine as I preset the probabilities I want. I want this

It's easy to see what modules a specific module depends on but I can't seem to find out how to see what modules depend on a specific module. Is there a simple way to do this? If so, is there a way to

I know that using the Insert menu, you can create a matrix with vertical and horizontal lines, but not a more generic partition, such as dividing a 4x4 matrix into 4 2x2 partitions. Nor, can MatrixFor

I would like to call a matlab function from mathematica. How best to do that? I have found an ancient post on Wolfram site describing a way to do this, is this still the way to connect the two?

How would you print() out or find out the index of an object? For example, if I spawned 20 random rock objects on screen into an array RockTable = {}; Like this RockTable[#RockTable + 1] = rock; And

We are interested in finding out some statistics of various frameworks Mailing list activity on say richfaces. Much similar to what is available on http://code.google.com (Low, Medium, High) + avera

This NHibernate blog entry notes how detached QueryOver queries (analogous to DetachedCriteria) can be created (using QueryOver.Of<T>()). However, looking this over, it doesn't look analogous to

As far as autotest is concerned, how do you do autotest for C++ programs? are there any autotest framework that can be utilized to do unit test and integration test?

Does StoryTest (http://storytestiq.solutionsiq.com/wiki/Main_Page) work? Everything on the wiki is under construction. If you have gotten it to work, can you share how you did it?

Why is it that if I call a conditional method without a block, I can do if foo? bar.baz, but with a block I have to do: if foo?; bar.each { |n| n.baz } end And how would I do the same with a multi-li

How do I eliminate the ternary operator from the code below? I'm looking for something using only integer operators (shifts, additions, etc). a = (a < 0) ? (-a * 2) - 1 : (a * 2)

I have a situation that's similar to what goes on in a job search engine where you type in the zipcode where you're searching for a job and the app returns jobs in that zipcode as well as in zipcodes

I've had some issues lately with PHP memory limits lately: Out of memory (allocated 22544384) (tried to allocate 232 bytes) These are quite the nuisance to debug since I'm not left with a lot of in

Given the tuple: let tuple = (true, 1) How do i use this tuple in a conditional? Something like this: if tuple.first then //doesnt work or if x,_ = tuple then // doesnt work I don't want to do this

I'm using Jasmine 2.0. I register an event handler, and wait for it to fire so I can do my test: $(document).on(pagechange, function() { expect(1).toBe(1); /* times out before it can evaluate this *

I've been attempting move a directory structure from one location to another in Subversion and I've run up against a brick wall which is the Item '*' is out of date commit error. I have the latest ve

<!-- Additional IE/Win specific style sheet (Conditional Comments) --> <!--[if IE]> <style type=text/css media=all>@import files/tabs-ie.css;</style> <script type=tex

I know that most programming languages have functions built in for doing that for you, but how do those functions work?

I need to create a 3 by 3 real orthonormal symbolic matrix in Mathematica. How can I do so?

In working on side projects, I typically write code best around 7pm-8pm, with a good cup of coffee, and I find that I tend to be more productive than I do at work during this time. What is the best w

I wrote an algorithm and tried to implement it in Prolog, but what I found out is that the parenthesis do not work as expected: what is written in is not all done before exiting the parenthesis. Here

I looked for this question before on stack, and for some reason the answer provided did not work. I am wondering if there is an update to this, to check either in the command-line or else where.

I am new to Core Image and am trying out some of the filters. I can get some of the filters to work but others just refused to work. For example, I can get the CIGaussianGradient filter to work using

I am looking to use Mathematica to print equations in TraditionalForm. While I have been successful for some simple ones, I have not for the 2 below.

Suppose I have a class like this, containing a generic method with an out parameter: public class C { public static void M<T>(IEnumerable<T> sequence, out T result) { Console.WriteLine(Te

My work PC, Home PC, and laptop all have different keyboards. I find my hands wanting to do the wrong thing all the time. Also I have to log into multiple servers with different versions of vi, vim, e

I am new to Mathematica and currently working with time series data. I have the following input data in Mathematica 9.0 data = {{{2011, 3, 13}, 10}, {{2011, 3, 14}, NA}, {{2011, 3, 15}, 20}, {{2011,

How do you position a text outside a plot in mathematica? A quick google search will lead you to http://reference.wolfram.com/mathematica/howto/AddTextOutsideThePlotArea.html This is not enough since

I'm having a sum like this: Sum[1 + x[i], {i, 1, n}] Mathematica doesn't simplify it any more. What would I need to do so it translates it into: n + Sum[x[i],{i,1,n}]

If you have an object file, how do you get the initialized value of a global variable in that object file's data segment? For example, say I've done the following: # I'm interested in the variable foo

So, I've recently converted from Mathematica to Matlab, and while Matlab has most of Mathematica's useful features, I can't figure out how to perform the equivalent of Mathematica's delayed set operat

I am thinking of process an image to generate in Mathematica given its powerful image processing capabilities. Could anyone give some idea as to how to do this? Thanks a lot.

I am learning assembly language at the moment and in the book assembly language by kip irvine he talks about conditional jump instructions and gives the following example Example 2: mov bx,1234h sub b

I would like to write a Mathematica function that constructs a list of all Fibonacci numbers less than n. Moreover, I would like to do this as elegantly and functionally as possible(so without an expl

I have a client with an event planning site asking his pages to fade into one another. I have no idea how I can accomplish this. Any ideas? Is there anyway to do this without flash?

In the build.gradle files, sometimes (I'm not sure when) you'll have to specify what version of gradle is used. The last few updates of Android Studio kept breaking some build files which listed the g

When editing Lisp code, occasionally it's useful to entirely comment out a top-level definition, like this: ;(defun some-fn-which-is-broken (x) ; ...) ... or comment out only part of an s-expression,

If you have javascript being written in HAML, with a ruby array being interpolated into javascript, how do you get ruby to print out NULL? :javascript $().ready(function() { $(document).ready(function

Is it possible to use Mathematica's computing capabilities from other languages? I need to do some complex operations (not necessarily symbolic, btw), and it'd be pretty sweet to be able to just call

I've literally looked everywhere and cannot figure out how to find someones userid that I can use inside their embed code. Anyone know how to find it out?

What do you call this arrow looking -> operator found in PHP? It's either a minus sign, dash or hyphen followed by a greater than sign (or right chevron). How do you pronounce it when reading cod

Mathematica offers the Notation package to define custom notation, and it seems to work great until I save a notebook with custom notation as a package and try to use Needs[...] to import the new no

How to use a correct function for arg, re and im in order to obtain correct wok on mathematica. For example When inputting Simplify[D[r^2 Re[e^(i t) r]^2 ,r],r>0 && t>0] One has 2 r^3

How do you check if a person has got an avatar? How do you unset it? How do you load an avatar from a file? From web, Facebook, gravatar? What image types can AppleScript read? Is it possible to copy

Just for fun I would like to count the conditional probabilities that a word (from a natural language) appears in a text, depending on the last and next to last word. I.e. I would take a huge bunch of