(Part 2)
(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).
In Part 2, consider the result of throwing two sets (that is, throwing a coin four times), assuming that "throw twice" = "one set" as in the example of P61. Since coins are thrown four times, the pattern that appears is
--Table 0 times --Table once --Table twice --Table 3 times --Table 4 times
5 ways.
from random import randint
from decimal import Decimal
from prettytable import PrettyTable
import numpy as np
def tossBiasedCoin():
""" Returns 0 or 1 with 0 having 2/3 chance """
return randint(0,2) % 2
# Make a 3x3 array
counts = [[0 for j in range(3)] for i in range(3)]
# Toss a coin many times to get counts
sampleCount = 50000
for num in range(sampleCount):
firstSet = [tossBiasedCoin(),tossBiasedCoin()]
secondSet = [tossBiasedCoin(),tossBiasedCoin()]
counts[sum(secondSet)][sum(firstSet)] += 1
# Conert all counts to perentage
TWOPLACES = Decimal(10) ** -2
for i in range(3):
for j in range(3):
value = counts[i][j]
counts[i][j] = (100 * Decimal(counts[i][j])/Decimal(sampleCount)).quantize(TWOPLACES)
print("Converted the value {} to percentage {}".format(value, counts[i][j]))
# Make summaries of number of heads.
keys = np.arange(5)
values = [counts[0][0], # 0
counts[0][1]+counts[1][0], # 1
counts[0][2]+counts[2][0]+counts[1][1],
counts[1][2]+counts[2][1],
counts[2][2]]
# Add row descriptions
counts[0].insert(0, '2nd set 0 head')
counts[1].insert(0, '2nd set 1 head')
counts[2].insert(0, '2nd set 2 heads')
# Create table with column descriptions, add rows, then show it.
table = PrettyTable(["", "1st set 0 head", "1st set 1 head", "1st set 2 heads"])
table.padding_width = 1
table.add_row(counts[0])
table.add_row(counts[1])
table.add_row(counts[2])
print table
# Draw a bar chart
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
rects = plt.bar(keys,
values,
0.5,
alpha=0.4,
align="center",
color='b')
plt.xlabel('Number of heads with two sets')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(keys, np.arange(5))
plt.tight_layout()
plt.show()
The program structure is basically the same as last time.
Create a 3x3 list to make it the same as Figure 1-19.
# Make a 3x3 array
counts = [[0 for j in range(3)] for i in range(3)]
Loop 500,000 times and throw coins a total of 4 times each time.
# Toss a coin many times to get counts
sampleCount = 50000
for num in range(sampleCount):
firstSet = [tossBiasedCoin(),tossBiasedCoin()]
secondSet = [tossBiasedCoin(),tossBiasedCoin()]
counts[sum(secondSet)][sum(firstSet)] += 1
It's getting a little troublesome. The pattern that "table is twice (sheets)" is out of 2 sets 2 sheets for the 1st set, 0 sheets for the 2nd set 0 for the 1st set, 2 for the 2nd set 1 piece for the 1st set, 1 piece for the 2nd set There are 3 ways, so add them all.
# Make summaries of number of heads.
keys = np.arange(5)
values = [counts[0][0], # 0
counts[0][1]+counts[1][0], # 1
counts[0][2]+counts[2][0]+counts[1][1],
counts[1][2]+counts[2][1],
counts[2][2]]
Add a third line to change from a 2x2 structure to a 3x3 structure.
# Add row descriptions
counts[0].insert(0, '2nd set 0 head')
counts[1].insert(0, '2nd set 1 head')
counts[2].insert(0, '2nd set 2 heads')
Basically, changes are made in response to the increase.
# Create table with column descriptions, add rows, then show it.
table = PrettyTable(["", "1st set 0 head", "1st set 1 head", "1st set 2 heads"])
table.padding_width = 1
table.add_row(counts[0])
table.add_row(counts[1])
table.add_row(counts[2])
print table
# Draw a bar chart
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
rects = plt.bar(keys,
values,
0.5,
alpha=0.4,
align="center",
color='b')
plt.xlabel('Number of heads with two sets')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(keys, np.arange(5))
plt.tight_layout()
plt.show()
It looks like a "standard deviation" graph! That's what this chapter is about to demonstrate in the first place.
Furthermore, it can be seen that the probability of "one table" is the highest. This means that the "probability of throwing 4 times and the table appears (1/3)" is "4 x 1/3" = 1.333. In other words, it means that "throwing four cards will usually result in one table."
Continue to (Part 3).
Recommended Posts