# How to get Mathematica to Return an Actual Numerical Result For Probability? [closed]

This is a painfully basic question. It's probably been answered before (or might be in one of the documentation packages), but I've been looking for some time and can't find it! (Though it could very easily be that I'm not exactly sure what to look for, so I might have missed it!)

My question is basically this: I'd like to get Mathematica to compute a simple probability given a distribution and return the result as a number.

Specifically, let's say I'm interested in getting the probability that $x=0$ when $x$ is distributed according to a Poisson Distribution with $\mu = 1$. So I enter (basically straight from the Wolfram website):

Probability[x == 0, x \[Distributed] PoissonDistribution[1.]]


But instead of getting something like $.368$, that returns:

Probability[True, 0 \[Distributed] PoissonDistribution[1.]]


Which is puzzling, since on the wolfram website, the input and output examples are:

Probability[x == 20, x \[Distributed] PoissonDistribution[20.]]


returns

.0888353


Can anyone explain why one returns a numerical result and the other doesn't? And how can I get my functions to actually return/store numerical results?

• You probably set x to be 0 somewhere in your code. could you show the rest of your code? Feb 13, 2018 at 10:08
• The fact that you get Probability[True, ...] as a return value tells you that x has already been assigned a value. Check the coloring of your variables: if they're blue, they're unassigned. If they're black, they have a value and expressions like x == 0 can evaluate to booleans. Either use Clear[x] or Block[{x}, Probability[x == 0, ...]] to avoid this issue. Feb 13, 2018 at 10:10

The difference probably rises from a definition you gave x.

I'm assuming you set

x=0;


somewhere in your code. Mathematica would then have a value of 0 for x when you run

Probability[x == 0, x \[Distributed] PoissonDistribution[1.]]


So the x == 0 evaluates to True.

Wolfram, on the other hand, does not have a pre-existing definition for x, so it actually does the calculation.

In fact, when looking at the output you get, one can see that:

Probability[True, 0 \[Distributed] PoissonDistribution[1.]]


See how x became 0 (before the \[Distributed])? Mathematica substituted the value x has, which is 0.

To solve this, simply remove your definition for x (probably a line like the one I mentioned in the beginning of my answer)

As a side-note, if you define x=0 somewhere in the notebook, and then later delete that line, but don't reassign (or Clear) x, then x is still 0.

So you should include a

Clear[x]


At the beginning (or just before using x) of the notebook.

• Thank you so much for your help!!!! I had included a "ClearAll" at the beginning, but I think I accidentally kept re-declaring x=0 through my attempts at calculating the probability. (The only line in the notebook was the one I included in the question, which was what was really baffling me.) At the risk of violating stackexhange rules- quick clarification question. Do variables start with a value before they're defined in a given workbook? I think that might have been what was throwing me off. Thanks again for your help!!!!! Feb 13, 2018 at 10:36
• What exactly did you put in ClearAll? Just having ClearAll by itself doesn't do anything, you need to tell it what symbol to clear. To elaborate: ClearAll does NOT "clear all symbols". ClearAll[a, b, c, ...] clears all definitions attributes, messages, and defaults associated with symbols a, ... etc. So you need to call ClearAll[x] to clear x. Feb 13, 2018 at 10:54
• @SjoerdSmit that's a good point. I addressed that in an edit, Feb 13, 2018 at 11:41
• @Sjoerd Smit That explains it!! I did not realize that at the time- thank you for clarifying that!! (I just included ClearAll without specifying the symbols to clear.) Feb 13, 2018 at 12:15