4
$\begingroup$

I am trying to write a function that solves for p in the following equation:

PDF[BinomialDistribution[n, p], x] == op

The variables 'n', 'x', and 'op' are known. Generally using NSolve like so works fine:

NSolve[PDF[BinomialDistribution[10, p], 0] == 0.95 && 0 < p < 1, p, Reals]

(* {{p -> 0.0051162}} *)

N[PDF[BinomialDistribution[10, 0.005116196891823705`], 0]]

(* 0.95 *)

But for certain 'n', NSolve gives wildly inaccurate solutions:

NSolve[PDF[BinomialDistribution[80, p], 0] == 0.95 && 0 < p < 1, p, Reals]

(* {{p -> 0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p -> 
   0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p ->
    0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p -> 
   0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p ->
    0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p -> 
   0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p -> 0.999834}, {p ->
    0.999834}, {p -> 0.999834}} *)

Here we just have 23 duplicate solutions, each of which are very inaccurate. Oddly this seems to be a problem only for certain values of 'n' without an obvious pattern; for example, it seems to affect 51 <= n <= 98 but not 'n' immediately outside that range.

Any Mathematica pros out there that might know how I can fix this?

$\endgroup$
2
  • 2
    $\begingroup$ It may be a numerical precision problem. Try using $95/100$ instead of $0.95$ in your equation. $\endgroup$
    – MarcoB
    Aug 26 '15 at 22:29
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Aug 26 '15 at 23:00
5
$\begingroup$

Increase WorkingPrecision:

NSolve[PDF[BinomialDistribution[80, p], 0] == 0.95`200 && 0 < p < 1,
  p, Reals, WorkingPrecision -> 50]
PDF[BinomialDistribution[80, p], 0] /. %
(*
  {{p -> 0.00064096067673218860969986162632491931947341012861}}
  {0.9500000000000000000000000000000000000000000000000}
*)
$\endgroup$
1
  • $\begingroup$ Thanks so much! I actually found that just increasing the precision of 0.95 solved the problem without manually setting WorkingPrecision, but you also showed that in your answer so I'm marking this. $\endgroup$ Aug 27 '15 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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