Probability in Discrete Mathematics, and Data Science

What is the possibility of an event to happen? Probability in discrete mathematics and data science is all about this.

Nothing more. Or less.

For example, if we toss a coin in the air, the probability of getting head or tail is the same. 

It’s because there are only two sides there.

On the contrary, consider an example of a dice. There are six numbers. From 1 to 6. 

What is the probability of getting any number that belongs to this data set?

{1, 2, 3, 4, 5, 6}

It’s difficult to predict.

Certainly probability theory belongs to discrete mathematics which in recent times has numerous applications in computer science.

Data science is no exception. 

Machine learning also uses probability theory.

However, we don’t make this discussion too academic.

Why? 

Because we intend to make it for the beginners.

We will show some Python code to show how it is quite easy to predict the probability of getting a number out of many numbers.

In addition, that will also explain the probability theory in detail.

Let’s take a look at the following code.

After that we will discuss the code.

def find_probability(the_array, the_length_of_array, the_element):
    count = the_array.count(the_element)
    # find probability up to 4 decimal places
    return round(count / the_length_of_array, 4)
    
the_array_variable = [22, 22, 22, 22, 22, 22, 22]
the_element_to_find = 22
the_length_of_array = len(the_array_variable)

print(f'The probability is: {find_probability(the_array_variable, the_length_of_array , the_element_to_find)}')

# The probability is: 1.0

Firstly, in the above code, we are to find the probability of finding an element in an array. 

Secondly, according to the code, every element of the array equals one number. 

Consider a dice where six sides equal to a certain number, such as 1.

In that case, the probability of getting 1 is 100 percent. Right? 

In the above code, the same thing happens.

Probability depends on two factors

In a data set or in an array, finding an element depends on two factors. 

First is the number of favorable outcomes. 

And the second is the number of possible outcomes.

The number of favourable outcomes depends on the total number of elements present in the array.

On the other hand, the number of possible outcomes depends on the length of the array.

As a result, our task becomes easier.

But the probability of finding 22 will become difficult if we change this part of the code.

the_array_variable = [22, 720, 34, 89, 113, 56, 71]
the_element_to_find = 22

Now the output is no longer 100 percent.

The output is as follows.

# The probability is: 0.1429

We can change any algorithm whatsoever. Therefore, we would like to write the above code in the following manner.

def count_number_of_elements_in_array(the_array, match_element):
    count_numbers = 0
    for val in the_array:
        if val == match_element:
            count_numbers = count_numbers + 1
    return count_numbers

dice = [1, 2, 3, 4, 5, 6]
the_element = 2
length = len(dice)

total_number_of_elements = count_number_of_elements_in_array(dice, the_element)
the_probability = round(total_number_of_elements / length, 6)
print(f'The probability of getting 2 is: {the_probability}')

# The probability of getting 2 is: 0.166667

Firstly, we have counted the number of a particular element present in the array.

Quite naturally, in a dice, 1 or 2 is present for once. Not twice. Or thrice. Right? 

Secondly, finding the length of the array is not difficult though. A dice consists of 6 elements.

Finally we can get the result quite easily. We will divide 1 by 6. And that’s the result.

For more discrete mathematics and python related code please visit the respective GitHub Repository.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

Leave a Reply