1
$\begingroup$

I am new to Mathematica. I would like to understand why Mathematica returns redundant cases, and whether it is possible to simplify them to the generic case $(n>1)$ that covers everything $(n\ge0)$.

Assuming[n>=0 && p>=0 && p<=1,Sum[k*PDF[BinomialDistribution[n,p],k],{k, 0, n}]]

It returns

$$ \begin{cases} np & n>1\\ -\frac{n(1-p)^np}{-1+p} & n=1\\ 0 & \text{True} \end{cases} $$

This is not wrong, but redundant: The $n=1$ case simplifies to $p$ which is covered by the generic case $n>1$ (I also don't understand why Mathematica doesn't substitute and simplify it, but that's for another question ...). The $\rm True$ case which I think should stand for the only remaining possibility $n=0$, since I added $n\ge0$ assumption, is also covered by the generic case. So I expect to see just $np$ from this calculation given my assumptions...

Can I simplify this somehow? Or should I? If I assign this to a variable and use it in subsequent calculations manipulating with $n$ in the range $n\ge0$, can I expect valid results?

$\endgroup$
9
  • $\begingroup$ Apologies everyone, if I take out the backticks from around the LaTeX formula it does not allow me to post it... no idea why. Would appreciate if someone fixed it and let me know what the issue was, thanks $\endgroup$ Jun 7, 2023 at 17:08
  • 3
    $\begingroup$ You can post it as a bitmap (which is OK for output just not desirable for input). But if you use FullSimplify rather than Assuming, I think you get what you want: FullSimplify[Sum[k*PDF[BinomialDistribution[n, p], k], {k, 0, n}], Assumptions -> 0 < p <= 1 && n >= 1]. You could also use Mean[BinomialDistribution[n, p]] or Expectation[x, x \[Distributed] BinomialDistribution[n, p]] to get n p. $\endgroup$
    – JimB
    Jun 7, 2023 at 17:21
  • 1
    $\begingroup$ I suspect it is because the answer is constructed in the form Piecewise[cases], which auto-evaluates to Piecewise[cases, 0]. E.g. Piecewise[{{1, x > 2}, {2, x <= 2}}] // InputForm. $\endgroup$
    – Michael E2
    Jun 7, 2023 at 17:54
  • 1
    $\begingroup$ Just add Simplify, i.e., Assuming[n >= 0 && p >= 0 && p <= 1, Sum[k*PDF[BinomialDistribution[n, p], k], {k, 0, n}] // Simplify] $\endgroup$
    – Bob Hanlon
    Jun 7, 2023 at 19:26
  • 1
    $\begingroup$ If you need 3rd and 4th moments, then Expectation and Moment should be helpful: Moment[BinomialDistribution[n, p], 3] results in n p - 3 (1 - n) n p^2 + (1 - n) (2 - n) n p^3. But if your need has to do with something more complex than a binomial distribution, giving a more desired example would be helpful to us. $\endgroup$
    – JimB
    Jun 7, 2023 at 20:59

1 Answer 1

1
$\begingroup$

Perhaps this:

PiecewiseExpand[
 Assuming[n >= 0 && p >= 0 && p <= 1, 
  Sum[k*PDF[BinomialDistribution[n, p], k], {k, 0, n}]],
 n \[Element] PositiveIntegers]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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