2
$\begingroup$

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?

$\endgroup$

closed as off-topic by Mike Honeychurch, m_goldberg, Henrik Schumacher, José Antonio Díaz Navas, Coolwater Feb 14 '18 at 13:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Mike Honeychurch, m_goldberg, Henrik Schumacher, José Antonio Díaz Navas, Coolwater
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ You probably set x to be 0 somewhere in your code. could you show the rest of your code? $\endgroup$ – ItamarG3 Feb 13 '18 at 10:08
  • 3
    $\begingroup$ 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. $\endgroup$ – Sjoerd Smit Feb 13 '18 at 10:10
5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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!!!!! $\endgroup$ – AndrewC Feb 13 '18 at 10:36
  • 1
    $\begingroup$ 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. $\endgroup$ – Sjoerd Smit Feb 13 '18 at 10:54
  • 1
    $\begingroup$ @SjoerdSmit that's a good point. I addressed that in an edit, $\endgroup$ – ItamarG3 Feb 13 '18 at 11:41
  • $\begingroup$ @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.) $\endgroup$ – AndrewC Feb 13 '18 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.