(Part 4)
(Refer to the page that appears in the text Statistics is the strongest study [Practice] Thoughts and methods for data analysis-by Hiromu Nishiuchi This is the page of the book /9784478028230.html).
The result of throwing a coin 16 times, which says "For reference" on P63.
The author emphasizes that "the sum of the data converges to a normal distribution" even when throwing an asymmetric coin with a 1/3 chance of appearing in the table.
The reason why it is shaped like a "mountain" is that the easiest thing to understand is that if the data is represented as a 2x2, 3x3, or 8x8 table, "another pattern that produces the same result" from the upper right to the lower left. There are many "", and by adding them together, the closer to the diagonal, the larger the total value = the highest point of the mountain.
This time, I will make only a bar graph, so I will not prepare the data to make it a table.
from random import randint
from decimal import Decimal
import numpy as np
def tossBiasedCoin():
""" Returns 0 or 1 with 0 having 2/3 chance """
return randint(0,2) % 2
# Prepare counters
tossCount = 16
counts = [0]*tossCount
# Toss a coin many times to get counts
sampleCount = 50000
for num in range(sampleCount):
headCount = 0
for i in range(tossCount): # Toss the coin 16 times
headCount += tossBiasedCoin()
counts[headCount] += 1
# Conert all counts to perentage
TWOPLACES = Decimal(10) ** -2
for i in range(tossCount):
value = counts[i]
counts[i] = (100 * Decimal(counts[i])/Decimal(sampleCount)).quantize(TWOPLACES)
print("Converted the value {} to percentage {}".format(value, counts[i]))
# Draw a bar chart
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
rects = plt.bar(np.arange(tossCount),
counts,
0.5,
alpha=0.4,
align="center",
color='b')
plt.xlabel('Number of heads for 16-toss')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(np.arange(tossCount))
plt.tight_layout()
plt.show()
Obediently, it creates a list with 16 elements.
# Prepare counters
tossCount = 16
counts = [0]*tossCount
Throw 16 times and execute 500,000 times to see how many sheets are on the front (though you don't have to turn this much)
## Toss a coin many times to get counts
sampleCount = 50000
for num in range(sampleCount):
headCount = 0
for i in range(tossCount): # Toss the coin 16 times
headCount += tossBiasedCoin()
counts[headCount] += 1
# Conert all counts to perentage
TWOPLACES = Decimal(10) ** -2
for i in range(tossCount):
value = counts[i]
counts[i] = (100 * Decimal(counts[i])/Decimal(sampleCount)).quantize(TWOPLACES)
print("Converted the value {} to percentage {}".format(value, counts[i]))
I haven't done anything special.
# Draw a bar chart
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
rects = plt.bar(np.arange(tossCount),
counts,
0.5,
alpha=0.4,
align="center",
color='b')
plt.xlabel('Number of heads for 16-toss')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(np.arange(tossCount))
plt.tight_layout()
plt.show()
A graph similar to Chart 1-21 on page 64 was created.
It's a knack for being able to analyze data with Python, or something that I feel I have to do in the future.
--Although it has nothing to do with Python, a fundamental review and study of statistics and probabilities --I saw one of the key points of Python as a sequence type --Familiarize yourself with libraries for plots and graphs such as matplotlib
Recommended Posts