def test_combination(self):
        combination = CombinationCalc()

        number_of_objects = 7
        objects_chosen = 2
        expected_output = 21
        actual_output = CombinationCalc.calc_combination(
            combination, number_of_objects, objects_chosen)
        self.assertEqual(expected_output, actual_output,
                         'Combination of 7 choose 2 is 42')
    def test_combination6(self):
        combination = CombinationCalc()

        number_of_objects = 'waluigi'
        objects_chosen = 0
        expected_output = None
        actual_output = CombinationCalc.calc_combination(
            combination, number_of_objects, objects_chosen)
        self.assertEqual(expected_output, actual_output,
                         'Nonsense test failed')
    def test_combination5(self):
        combination = CombinationCalc()

        number_of_objects = 18
        objects_chosen = 20
        expected_output = None
        actual_output = CombinationCalc.calc_combination(
            combination, number_of_objects, objects_chosen)
        self.assertEqual(expected_output, actual_output,
                         'Chosen larger than available test failed')
    def test_combination4(self):
        combination = CombinationCalc()

        number_of_objects = 18
        objects_chosen = 0
        expected_output = 1
        actual_output = CombinationCalc.calc_combination(
            combination, number_of_objects, objects_chosen)
        self.assertEqual(expected_output, actual_output,
                         'Combination of 18 choose 0 is 1')
    def test_combination2(self):
        combination = CombinationCalc()

        number_of_objects = 13
        objects_chosen = 1
        expected_output = 13
        actual_output = CombinationCalc.calc_combination(
            combination, number_of_objects, objects_chosen)
        self.assertEqual(expected_output, actual_output,
                         'Combination of 13 choose 1 is 13')
Ejemplo n.º 6
0
    def binomial_calc(self, success_prob, trials, successes):
        result = []
        comb = CombinationCalc()
        if success_prob < 0 or trials < 0 or successes < 0:
            print 'None of the numbers can be negative.'
            return
        elif successes > trials:
            print 'The number of success cannot be larger than the number of trials.'
            return
        else:
            try:
                #X = number of successes
                equal = (comb.calc_combination(trials, successes)) * math.pow(success_prob,successes) * math.pow(1 - success_prob, trials - successes)
                result.append(equal)

                #X < number of success
                less = 0
                for i in range(int(successes)):
                    less += (comb.calc_combination(trials, i)) * math.pow(success_prob, i) * math.pow(1 - success_prob, trials - i)
                result.append(less)

                #X <= number of successes
                result.append(less + equal)

                #X > number of successes
                result.append(1 - (less + equal))

                #X >= number of sucesses
                result.append(equal + (1 - (less + equal)))

                #round all numbers to fourth decimal place
                for i in range(len(result)):
                    result[i] = round(result[i], 4)

            except:
                print 'An error occurred. Enter only numbers for the parameters. Binomial'
                return

        return result

# bin = BinomialCalc()
# print bin.binomial_calc(0.5, 5.0, 3)
    def neg_binomial_calc(self, success_prob, failures, successes):
        comb = CombinationCalc()
        if success_prob < 0 or failures < 0 or successes < 0:
            print 'None of the numbers can be negative.'
            return
        elif success_prob == 0 or successes == 0:
            print 'Successes and success_prob cannot be zero.'
            return
        else:
            try:
                return round(
                    comb.calc_combination(failures + successes - 1,
                                          successes - 1) *
                    math.pow(success_prob, successes) *
                    math.pow(1 - success_prob, failures), 4)
            except:
                print 'An error occurred. Enter numbers for parameters.'


#neg = NegativeBinomialDist()
#print neg.neg_binomial_calc(0.7, 0, 6)