Python complete tutorial
  • Python Complete Tutorial
  • About this book
  • What you need to prepare
  • 1️⃣Try python for the first time
    • Install python
    • Hello world!
    • Hello world in a nutshell
    • The first simple python project
    • most useful libraries
    • Recommended websites
  • 2️⃣Data structure and basic operations
    • Python data structure
    • Data structure without hash table
    • Data structure with hash table
    • Variability and address
    • basic python programming
    • basic python programming 2
    • basic python programming 3
    • some additions
    • Fibonacci sequence
    • Judging prime numbers
    • txt/csv file operation
  • 🐍Practice program
    • 🚩fancy print
    • 🚩Remove duplicate elements
    • 🚩Palindrome detection
  • 😎leetcode
    • what is leetcode
  • 3️⃣Data mining and machine learning
    • What is data mining
    • iris data set
    • Mean median mode
    • Harmonic mean
    • Histogram
    • Correlation algorithm
    • Gaussian distribution data set
    • projection
    • PCA
    • MDS
    • Bayesian and Frequentist
    • Data normalization
    • binary SVM
    • One Hot Encoding
    • Multi-class SVM
    • Accuracy and error rate
    • Confusion matrix & Accuracy, Precision, Recall
    • F1 score
    • ROC and AUC
  • 4️⃣big data and data visualization
    • line chart
    • Parallel coordinates
    • Histogram chart
  • 5️⃣Mathematical algorithm and digital signal processing
    • Mathematical constants and basic operations
    • Normal distribution
    • Permutation and combination
    • Bernoulli distribution
    • Chaotic system
  • 6️⃣Classes and design patterns
    • Classes and design patterns
  • 7️⃣Operate the database with python
    • MySQL
      • Install MySQL
      • First try MySQL
      • MySQL Architecture
      • database operations
      • database
  • 8️⃣Cryptography
    • beginning of Cryptography
  • 9️⃣deep learning
    • What is Deep Learning
    • basic
  • 💔algorithm
    • Algorithms and Data Structures
Powered by GitBook
On this page
  • binomial distribution
  • Statistics
  1. Mathematical algorithm and digital signal processing

Bernoulli distribution

binomial distribution

Bernoulli distribution or binomial distribution is A common distribution in our lives.

For example: I now have an uneven coin, the probability of flipping heads is 0.6, and the probability of flipping tails is 0.4.

I toss a coin five times in a row. What is the probability of three heads?

We list five coin toss possibilities: True for heads up and False for tails up.

Then one of the coin tosses is True or False, and five consecutive coin tosses can be represented by an ordered list: [True, False, True, True, False], The probability of this happening is: 0.6*0.4*0.6*0.6*0.4 = 0.03456

So how many outcomes are there in total when we flip a coin five times in a row?

ans = []

for a in [True, False]:
    for b in [True, False]:
        for c in [True, False]:
            for d in [True, False]:
                for e in [True, False]:
                    ans.append([a,b,c,d,e])

print(len(ans))
print(ans)

Each coin toss has either heads or tails, so there are 2**5=32 different situations for five tosses.

All cases can be printed in full using the above code.

32

[[True, True, True, True, True], [True, True, True, True, False], 
[True, True, True, False, True], [True, True, True, False, False], 
[True, True, False, True, True], [True, True, False, True, False], 
[True, True, False, False, True], [True, True, False, False, False], 
[True, False, True, True, True], [True, False, True, True, False], 
[True, False, True, False, True], [True, False, True, False, False], 
[True, False, False, True, True], [True, False, False, True, False], 
[True, False, False, False, True], [True, False, False, False, False], 
[False, True, True, True, True], [False, True, True, True, False], 
[False, True, True, False, True], [False, True, True, False, False], 
[False, True, False, True, True], [False, True, False, True, False], 
[False, True, False, False, True], [False, True, False, False, False], 
[False, False, True, True, True], [False, False, True, True, False], 
[False, False, True, False, True], [False, False, True, False, False], 
[False, False, False, True, True], [False, False, False, True, False], 
[False, False, False, False, True], [False, False, False, False, False]]
>>> 

We can filter out the cases where three of them are positive:

ans = []

for a in [True, False]:
    for b in [True, False]:
        for c in [True, False]:
            for d in [True, False]:
                for e in [True, False]:
                    ans.append([a,b,c,d,e])

# print(len(ans))
# print(ans)

thrice = []
for i in ans:
    if sum(i)==3:
        thrice.append(i)

print(len(thrice))
print(thrice)

In this way, we filter out all the results with three heads and two tails, a total of 10:

10

[[True, True, True, False, False], [True, True, False, True, False], 
[True, True, False, False, True], [True, False, True, True, False], 
[True, False, True, False, True], [True, False, False, True, True], 
[False, True, True, True, False], [False, True, True, False, True], 
[False, True, False, True, True], [False, False, True, True, True]]
>>> 

A total of 10 different throws resulted in three heads and two tails.

And the probability of getting [True, True, True, False, False] result is 0.6*0.6*0.6*0.4*0.4=0.03456

So the probability of getting three heads and two tails is the product of the above two results: 10*0.03456=0.3456

binomial distribution

The calculation method of the binomial distribution uses permutations and combinations:

If you follow the example just now, then in the binomial distribution formula, p is the probability of heads of the coin, and q is the probability of tails of the coin. The preceding c(n, x) is the frequency of three heads and two tails.

So the probability of three heads and two tails is: c(5, 2)*(0.6**3)*(0.4**2) or c(5, 3)*(0.6**3)*(0.4**2) (One is counted according to the head of the coin, the other is counted according to the tail of the coin, and the result is the same)

Statistics

Start time of this page: January 9, 2022

Completion time of this page: January 9, 2022

PreviousPermutation and combinationNextChaotic system

Last updated 3 years ago

5️⃣
Page cover image
binomial distribution