This entry is from CodeIQ's Learn by programming with Ayaka Ikezawa! Mathematics course for machine learning [Probability distribution] I could not understand the code in the article on the probability distribution of dice in the latter half. So, it is a reminder when I made the code as far as I can understand.
Since python is a beginner, there may be some strange writing styles.
Python 2.7.10
Q. Find the probability distribution of the number of dice thrown 5 times. The probability distribution is a list of probabilities.
The formula for the probability distribution of dice is the above formula. The official explanation and probability distribution are described in detail in the article, so here I will write the information necessary to make the code.
N â Since it is the number of trials, the task is to "throw the dice 5 times", so N = 5
Nīŧ â Factorial of 5. Use the factorial method of the math library
k1 k2 k3 k4 k5 k6 â When throwing the dice N times, the combination of the number of times each dice rolls For example, if you throw it 5 times
If 1 is given all 5 times, k1 = 5 k2 = 0 k3 = 0 k4 = 0 k5 = 0 k6 = 0 If all but 6 out of 5 appear once, k1 = 1 k2 = 1 k3 = 1 k4 = 1 k5 = 1 k6 = 0
It will be. In the probability distribution, we will find the probability for each combination (understanding).
First, find out the combination of the rolls when you throw the dice 5 times.
When k1 = 5 k2 = 0 k3 = 0 k4 = 0 k5 = 0 k6 = 0 When k1 = 4 k2 = 1 k3 = 0 k4 = 0 k5 = 0 k6 = 0 When k1 = 4 k2 = 0 k3 = 1 k4 = 0 k5 = 0 k6 = 0 ,,,
Since it is difficult to write the code considering, I will put out all the combinations of k1 ~ k6 once kX is all 0 ~ 5.
For example, like this
#k1-k2-k3-k4-k5-k6
0-0-0-0-0-5
0-0-0-0-1-4
0-0-0-0-2-3
...
5-5-5-5-5-3
5-5-5-5-5-4
5-5-5-5-5-5
All combinations are created with the recursive function (create_pattern (k, list)) in the code below. After that, only the combinations for which the total of k1 to k6 is 5, and the probability is calculated according to the formula.
python
#!/usr/bin/env python
import math
#Make all combinations with recursive functions
#Make a combination of hyphen delimiters
def create_pattern(k, list):
if k == 1:
for i in xrange(0, 6):
list.append(str(i))
else:
new_list = []
for pattern in list:
for j in xrange(0, 6):
new_list.append(pattern + '-' + str(j))
list = new_list
if k == 6:
return list
else:
k = k + 1
return create_pattern(k, list)
#Number of times to throw the dice
n = 5
all_pattern_str_list = create_pattern(1, [])
#Break down the hyphen-separated combinations and calculate the probabilities only for combinations with a total of 5.
for all_pattern_str in all_pattern_str_list:
tmp_list = all_pattern_str.split('-')
pattern_sum = 0
pattern_list = []
for tmp_str in tmp_list:
pattern_list.append(int(tmp_str))
if sum(pattern_list) == 5:
print pattern_list
#Official denominator(k1!k2!k3!k4!k5!k6!)Calculate
denom = 1
for p in pattern_list:
denom = denom * math.factorial(p)
#Probability calculation
print (math.factorial(n) / denom) * (1.0 / 6.0 ** n)
As you can see in the article, dropping it into the code made it easier to understand math.
Recommended Posts