Last active
October 16, 2019 03:42
-
-
Save ofey404/f3acd2ef2b226749c4c79bb1efe730a4 to your computer and use it in GitHub Desktop.
Winning Streak simulation code of Mr. Yu's ML Lecture week 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.mplot3d import Axes3D | |
def three_continous() -> None: | |
def has_three_continous(possible): | |
return "111" in possible | |
all_possible = ["1", "0"] | |
PLAY_TIMES = 10 | |
for _ in range(PLAY_TIMES-1): | |
tmp_possible = [] | |
for i in all_possible: | |
tmp_possible.append(i + "1") | |
tmp_possible.append(i + "0") | |
all_possible = tmp_possible | |
# print(tmp_possible) | |
# for i in tmp_possible: | |
# print(has_three_continous(i)) | |
all_count = 0 | |
cont_count = 0 | |
for i in all_possible: | |
all_count += 1 | |
if has_three_continous(i): | |
cont_count += 1 | |
print("possibility={}".format(cont_count/all_count)) | |
# n >= 1 | |
def winning_streak(n: int, m: int, p: int, std_out=True): | |
def has_m_streak(pattern: str, m: int): | |
return "1"*m in pattern | |
all_possible = { | |
"1": p, | |
"0": 1-p | |
} | |
for _ in range(n-1): | |
tmp_possible = {} | |
for pattern, poss in all_possible.items(): | |
tmp_possible[pattern+"1"] = poss * p | |
tmp_possible[pattern+"0"] = poss * (1-p) | |
all_possible = tmp_possible | |
possibility = 0 | |
for pattern, poss in all_possible.items(): | |
if has_m_streak(pattern, m): | |
possibility += poss | |
if std_out: | |
print("for {} winning streak in {} matches, winning rate is {}".format(m, n, p)) | |
print("possibility={}".format(possibility)) | |
else: | |
return possibility | |
def simple_test(): | |
three_continous() | |
winning_streak(10, 3, 0.5) | |
def main(): | |
# Fixing random state for reproducibility | |
np.random.seed(19680801) | |
# ================ BELOW IS REFER TO ================== | |
# https://matplotlib.org/gallery/mplot3d/bars3d.html#sphx-glr-gallery-mplot3d-bars3d-py | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
# p = 0.5 | |
# n and m varies | |
colors = ['r', 'g', 'b'] | |
yticks = [15, 10, 5] # n that differs | |
for c, k in zip(colors, yticks): | |
# Generate the random data for the y=k 'layer'. | |
xs = np.arange(k) | |
ys = np.array([winning_streak(k, m, 0.5, std_out=False) for m in xs]) | |
# You can provide either a single color or an array with the same length as | |
# xs and ys. To demonstrate this, we color the first bar of each set cyan. | |
cs = [c] * len(xs) | |
cs[0] = 'c' | |
# Plot the bar graph given by xs and ys on the plane y=k with 80% opacity. | |
ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8) | |
ax.set_xlabel('m') | |
ax.set_ylabel('n') | |
ax.set_zlabel('possibility') | |
# On the y axis let's only label the discrete values that we have data for. | |
ax.set_yticks(yticks) | |
plt.show() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment