[PYTHON] [CodeIQ] I wrote the probability distribution of dice (from CodeIQ math course for machine learning [probability distribution])

Introduction

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.

environment

Python 2.7.10

Formal understanding of the probability distribution of dice

official

math-1.jpg

Task

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).

Find the probability distribution

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)

Summary

As you can see in the article, dropping it into the code made it easier to understand math.

Recommended Posts

[CodeIQ] I wrote the probability distribution of dice (from CodeIQ math course for machine learning [probability distribution])
I tried calling the prediction API of the machine learning model from WordPress
[Machine learning] I tried to summarize the theory of Adaboost
I considered the machine learning method and its implementation language from the tag information of Qiita
How to use machine learning for work? 01_ Understand the purpose of machine learning
Note that I understand the algorithm of the machine learning naive Bayes classifier. And I wrote it in Python.
Python learning memo for machine learning by Chainer until the end of Chapter 2
Learning notes from the beginning of Python 1
I wrote the code for Gibbs sampling
Learning notes from the beginning of Python 2
I tried to summarize the relationship between probability distributions starting from the Bernoulli distribution
I tried to predict the presence or absence of snow by machine learning.
Looking back on the machine learning competition that I worked on for the first time
I tried to predict the change in snowfall for 2 years by machine learning
I tried to process and transform the image and expand the data for machine learning
Align the number of samples between classes of data for machine learning with Python
GTUG Girls + PyLadiesTokyo Meetup I went to machine learning for the first time
The story of low learning costs for Python
Machine learning starting from 0 for theoretical physics students # 1
I searched for railway senryu from the data
Defeat the probability density function of the normal distribution
Upgrade the Azure Machine Learning SDK for Python
Overview of machine learning techniques learned from scikit-learn
About the development contents of machine learning (Example)
Machine learning starting from 0 for theoretical physics students # 2
The first step of machine learning ~ For those who want to implement with python ~
[Deep Learning from scratch] I implemented the Affine layer
[Recommended tagging for machine learning # 2] Extension of scraping script
[Recommended tagging for machine learning # 2.5] Modification of scraping script
I tried the MNIST tutorial for beginners of tensorflow.
Impressions of taking the Udacity Machine Learning Engineer Nano-degree
Python learning memo for machine learning by Chainer from Chapter 2
Installation of TensorFlow, a machine learning library from Google
About testing in the implementation of machine learning models
Techniques for understanding the basis of deep learning decisions
Predict the gender of Twitter users with machine learning
Othello ~ From the tic-tac-toe of "Implementation Deep Learning" (4) [End]
[Python] I made a classifier for irises [Machine learning]
Summary of the basic flow of machine learning with Python
Study method for learning machine learning from scratch (March 2020 version)
Record of the first machine learning challenge with Keras
Pip the machine learning library from one end (Ubuntu)
I investigated the reinforcement learning algorithm of algorithmic trading
Check the asymptotic nature of the probability distribution in Python
I searched for the contents of CloudWatch Logs Agent
I tried to compress the image using machine learning
I built an environment for machine learning from scratch (windows10 + Anaconda + VSCode + Tensorflow + GPU version)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (5)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (16)
I tried to compare the accuracy of machine learning models using kaggle as a theme.
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (10)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (2)
An example of a mechanism that returns a prediction by HTTP from the result of machine learning
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (13)
How to find the average amount of information (entropy) of the original probability distribution from a sample
I tried to verify the yin and yang classification of Hololive members by machine learning
I made an API with Docker that returns the predicted value of the machine learning model
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (9)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (4)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (12)