1

With n number of trials and p probability of successes, I need to find the probability of having less than or equal to x successes

I have built a function to find x number of successes.

I have tried to use this function with MAP

def bin_dist(n, p, x):
    if x > n:
        raise ValueError('the number of success cannot be greater than the number of trials')
    else:     
        def fact2(x):
            if x < 0: 
                return ValueError("number of successes cannot be less than 1")
            else:
                fact = 1
                for i in range(1, x + 1):
                    fact = fact * i
            return fact

        def combination2(n, x):
            return(fact2(n)/fact2(n-x)*fact2(x))
        print("The probability of x successes, followed by the total number of arrangements/possible combinations used to find x:") 
        print(combination2(n, x) * (p)**(x) * ((1-p)**(n-x)))  

def bin_cdf(n, p, x):
        li = range(0, x)
        print map(bin_dist, li)
        return map(bin_dist, li)

bin_cdf(3, 0.5, 2)

Unfortunately, I either receive an invalid syntax error or simply a MAP object

  File "<ipython-input-1-e333d90fddea>" , line 21 
    print map (bin_dist, li) 
            ^ 
SyntaxError : invalid syntax

I have also been informed the function used to return p of x successes, bin_dist, is missing two positional arguments when converting MAP to a list



----> 3          print ( list ( map ( bin_dist , li ) ) ) 
      4          return  ( list ( map ( bin_dist , li ) ) ) 
      5 

TypeError : bin_dist () missing 2 required positional arguments: 'p' and 'x' 

I have also tried iterating from 0 to x and finding the sum of all values, but seem to be finding dubious results

def bin_cdf(n, p, x):
    if x > n:
        raise ValueError('the number of success cannot be greater than the number of trials')
    else: 
     def fact2(x):
            if x < 0: 
                return ValueError("number of successes cannot be less than 1")
            else:
                fact = 1
                for i in range(1, x + 1):
                    fact = fact * i
            return fact

    def combination2(n, x):
        return(fact2(n)/(fact2(n-x)*fact2(x)))
    bin_dist = (combination2(n, x) * (p)**(x) * ((1-p)**(n-x)))
    li = range(0, x)
    bin_cdf = sum([element * bin_dist for element in li])
    print(bin_cdf)
    return bin_cdf
       

bin_cdf(3, 0.5, 3)


Out: 0.375

The stack overflow community appears, at least in my humble view, the brightest on the internet. Any advice much recommended

1 Answers1

0

Sorry for asking the classic question: do you need to develop your own function? There's plenty of functions to do it in Python already:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html

  • this is wrong answer, looks like @Hamster is asking about [negative binomial](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.nbinom.html) – Severin Pappadeux Aug 31 '20 at 02:37
  • Hamster is asking: "With n number of trials and p probability of successes, I need to find the probability of having less than or equal to x successes" which is a binomial random variable, not a negative binomial. In a negative binomial the number of trials is not fixed, it is actually the random variable. But in Hamster 's case, the number of trials is fixed. – Carlos Hernández-Suárez Aug 31 '20 at 23:09