MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Calculate n in binomial distribution

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101637] Re: Calculate n in binomial distribution
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Sun, 12 Jul 2009 05:49:30 -0400 (EDT)
  • References: <h229dp$i0v$1@smc.vnet.net> <h27fe4$cmb$1@smc.vnet.net>

If you have only a few of these to do then a simple linear search,
which is easily understood and can be coded inline, may suffice:

  n = k; While[cf[n,p,k] > test, n++];

But if you have many to do then an adaptation of Carl Woll's
binarysearch may save enough time to offset the additional complexity:

  binomsearch[p_,k_,test_] := Module[{lo = k, mid, hi = k+1, c},
  While[cf[hi,p,k] > test, hi *= 2];
  While[lo <= hi,
    If[(c = cf[mid = Quotient[lo+hi,2],p,k]) === test, Return[mid=
]];
    If[c < test, hi = mid-1, lo = mid+1]];
  lo]

On Jun 28, 3:08 am, Peter Breitfeld <ph... at t-online.de> wrote:
> Thank you Valerie, Bob and Sj=F6rd for your answers.
>
> With your help I came up with the following solution. I had to make
> CDF[BinomialDistribution[n,p],k] a continuous function:
>
> Options[binomialN] = Options[FindRoot];
>
> binomialN::usage =
> "binomialN[p_, k_, \[Alpha]_] gives the smallest n, such that
> CDF[BinomialDistribution[n,p],k]\[LessEqual] \[Alpha] ist.
> Result is the list {n, {cdf[n-1],cdf[n],cdf[n+1]}}";
>
> binomialN[p_, k_Integer, \[Alpha]_, opts:OptionsPattern[]] :=
> Module[{n, q = Rationalize[p], a = Rationalize[\[Alpha]]},
>   n = Ceiling[ n /. FindRoot[
>         BetaRegularized[1 - q, n - k, 1 + k] == a, {n, k, 100 k}, opt=
s]];
>   {n, CDF[BinomialDistribution[#, p], k] & /@ Range[n - 1, n + 1]}]
>
> this seems to work even for extreme values. You have to increase
> WorkingPrecision sometimes.
>
>
>
> Peter Breitfeld wrote:
>> Suppose I have the following distribution:
>>
>> cf[n_,p_,k_]=CDF[BinomialDistribution[n,p],k]
>>
>> Now I want to calculate n so that the biggest n such that e.g.
>>
>> cf[n,0.2,7]<0.3
>>
>> I made a ListPlot
>>
>> ListPlot[Abs[cf[#,0.2,7]-0.3]&/@Range[60]], where I see, that a value of
>> about n=46 gives an approximation nearest to 0.3
>>
>> To get this value of n I tried
>>
>> Minimize[{Abs[cf[n,0.2,7]-0.3],n>7},n,Integers]
>>
>> Out:  {0.699765, {n->11}}
>>
>> which is obviously wrong.
>>
>> Why?
>>
>> Is it, because Abs isn't differentiable at the peak?
>>
>> I tried other ways too, like Reduce NMinimize, FindMinimum, but no succe=
ss.
>>
>> --
>> _________________________________________________________________
>> Peter Breitfeld, Bad Saulgau, Germany --http://www.pBreitfeld.de
>
> --
> _________________________________________________________________
> Peter Breitfeld, Bad Saulgau, Germany --http://www.pBreitfeld.de


  • Prev by Date: Re: sparsearray bug?
  • Next by Date: four parametric projection ifs types
  • Previous by thread: Re: Calculate n in binomial distribution
  • Next by thread: binomial expansion of quantity raised to power of 1/2