You will be an engineer in 100 days --Day 63 --Programming --Probability 1

Click here until yesterday

You will become an engineer in 100 days-Day 59-Programming-Algorithms

You will become an engineer in 100 days --- Day 53 --Git --About Git

You will become an engineer in 100 days --Day 42 --Cloud --About cloud services

You will become an engineer in 100 days --Day 36 --Database --About the database

You will be an engineer in 100 days-Day 24-Python-Basics of Python language 1

You will become an engineer in 100 days --Day 18 --Javascript --JavaScript basics 1

You will become an engineer in 100 days --Day 14 --CSS --CSS Basics 1

You will become an engineer in 100 days --Day 6 --HTML --HTML basics 1

About probability

Looking up in the dictionary, it seems to be the ratio to all phenomena.

In programming, this probability calculation is also common.

Also, statistics as well as probability are one of the fields we deal with. If you go as a programmer, about terms and calculation methods I think it's better to keep it down.

Probability term

I picked up the terms of probability that I want to suppress.

the term English meaning
probability Probability Rate of events
permutation Permutation Arrange r pieces in a row in the order of taking out r pieces from n different pieces.
combination Combination Extracting r out of n different ones
Factorial Factorial The product of an integer from a positive integer to 1
Independence The results of each other do not affect each other
Trial The act of trying to find the probability
Repeated trial An attempt to repeat the same action without changing the conditions
Expected value Expected value Mean of values obtained in one trial
Event Event What happens
Product event Of the two events A and B, events where A and B occur at the same time
All events A summary of all the consequences
Elementary event Atomic event Events that cannot be further subdivided
Complementary event Events other than one
Mutual exclusivity Events that do not occur at the same time
Empty event An event that does not exist
Venn diagram A visual diagram of the relationships between multiple sets and the extent of the sets
Odds odds Numerical value showing probability in probability theory
Probability distribution probability distribution Represents the probability of taking each value for a random variable
Random variable random variable The value assigned to what can happen
Discrete type(Random variable) Discrete とびとびの数になるRandom variable
Continuous type(Random variable) Continuous 幾らでも細かく刻むことができるRandom variable

Probability calculation

Python has a library to help you do probability calculations You can calculate the probability relatively easily.

Factorial

The factorial can be calculated with factorial methods such as the math library.

Since the calculation method is the product of a certain positive integer to an integer from 1. If it is the factorial of 4, it becomes 4 * 3 * 2 * 1 (4!) And becomes 24.

import math

print(math.factorial(4))
print(math.factorial(10))

24 3628800

permutation

Permutation is the number when r are selected from n different ones and arranged in a row`.

Permutations can be generated and enumerated from lists (arrays). ʻIt is found by permutations in the itertools` library.

ʻItertools.permutations (list value, number to choose) `

In this case, the returned value itself is not a list If you want to see the values, you need to convert them to a list.

The following is the permutation when 2 out of 3 are selected and arranged.

import itertools

l = ['A', 'B', 'C']

p = itertools.permutations(l, 2)
print(list(p))

[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]

Total number of permutations

If the number is small, it can be listed, but if the number of permutations is too large, it cannot be displayed. If you only want the total number, you can use math.factorial to calculate it.

The formula is n! / (N --r)!, So the total number of permutations to select 5 from 7 is

n , r = 7 , 5
math.factorial(n) // math.factorial(n - r)

2520

Now you can find it.

combination

The combination is the number when selecting r from different n, and does not consider the order like a permutation.

Permutations can be generated and enumerated from lists (arrays). ʻIt is found in combinations of the itertools` library.

ʻItertools.combinations (list value, number to choose) `

The enumeration of combinations to select 2 out of 5 is as follows.

l = ['a', 'b', 'c', 'd','e']
c = itertools.combinations(l, 2)
print(list(c))

[('a', 'b'), ('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e'), ('c', 'd'), ('c', 'e'), ('d', 'e')]

In this case, there is no duplication between elements.

To make a combination by taking into account the duplication of elements Use combinations_with_replacement.

ʻItertools.combinations_with_replacement (list value, number to choose) `

l = ['a', 'b', 'c']
c = itertools.combinations_with_replacement(l, 2)
print(list(c))

[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]

Total number of combinations

If you only want the total number, you can use math.factorial to calculate it.

The formula is n! / (R! * (N --r)!), So the total number of combinations to select 3 from 7 is It will be as follows.

n , r = 7 , 3
math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

35

Cartesian product (direct product of multiple lists)

To create a direct product of multiple lists, use product in the ʻitertools` library.

ʻItertools.product (list value, list value) `

l1 = ['a', 'b', 'c']
l2 = ['X', 'Y']
p = itertools.product(l1, l2)
print(list(p))

[('a', 'X'), ('a', 'Y'), ('b', 'X'), ('b', 'Y'), ('c', 'X'), ('c', 'Y')]

If you specify the number of repetitions in the argument repeat, the iterable object is used repeatedly. You can generate a direct product.

ʻItertools.product (list value, repeat = number of times) `

l1 = ['a', 'b', 'c']
p = itertools.product(l1, repeat=2)
print(list(p))

('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]

Summary

I will touch on probability, but first let's learn terms. Tomorrow is about calculating expected value.

37 days until you become an engineer

Author information

Otsu py's HP: http://www.otupy.net/

Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw

Twitter: https://twitter.com/otupython

Recommended Posts

You will be an engineer in 100 days --Day 63 --Programming --Probability 1
You will be an engineer in 100 days --Day 65 --Programming --Probability 3
You will be an engineer in 100 days --Day 64 --Programming --Probability 2
You will be an engineer in 100 days ――Day 71 ――Programming ――About scraping 2
You will be an engineer in 100 days ――Day 61 ――Programming ――About exploration
You will be an engineer in 100 days ――Day 74 ――Programming ――About scraping 5
You will be an engineer in 100 days ――Day 73 ――Programming ――About scraping 4
You will be an engineer in 100 days ――Day 75 ――Programming ――About scraping 6
You will be an engineer in 100 days --Day 68 --Programming --About TF-IDF
You will be an engineer in 100 days ――Day 70 ――Programming ――About scraping
You will be an engineer in 100 days ――Day 81 ――Programming ――About machine learning 6
You will be an engineer in 100 days ――Day 82 ――Programming ――About machine learning 7
You will be an engineer in 100 days ――Day 79 ――Programming ――About machine learning 4
You will be an engineer in 100 days ――Day 76 ――Programming ――About machine learning
You will be an engineer in 100 days ――Day 80 ――Programming ――About machine learning 5
You will be an engineer in 100 days ――Day 78 ――Programming ――About machine learning 3
You will be an engineer in 100 days ――Day 84 ――Programming ――About machine learning 9
You will be an engineer in 100 days ――Day 83 ――Programming ――About machine learning 8
You will be an engineer in 100 days ――Day 77 ――Programming ――About machine learning 2
You will be an engineer in 100 days ――Day 85 ――Programming ――About machine learning 10
You will be an engineer in 100 days --Day 27 --Python --Python Exercise 1
You will be an engineer in 100 days --Day 34 --Python --Python Exercise 3
You will be an engineer in 100 days --Day 31 --Python --Python Exercise 2
You will be an engineer in 100 days --Day 86 --Database --About Hadoop
You will be an engineer in 100 days ――Day 24 ―― Python ―― Basics of Python language 1
You will be an engineer in 100 days ――Day 30 ―― Python ―― Basics of Python language 6
You will be an engineer in 100 days ――Day 25 ―― Python ―― Basics of Python language 2
You will be an engineer in 100 days ――Day 60 ――Programming ――About data structure and sorting algorithm
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
You will be an engineer in 100 days --Day 35 --Python --What you can do with Python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
You become an engineer in 100 days ――Day 67 ――Programming ――About morphological analysis
You become an engineer in 100 days ――Day 66 ――Programming ――About natural language processing
If you draw an Omikuji with a probability of 1% 100 times in a row, will you win once?
If you write the View decorator in urls.py in Django, the list will be higher.
If an exception occurs in the function, it will be transmitted to the caller 2
If an exception occurs in the function, it will be transmitted to the caller 1