from scipy.misc import comb import numpy as np import matplotlib.pyplot as plt SCORES = ('0:6', '1:6', '2:6', '3:6', '4:6', '5:6', '6:5', '6:4', '6:3', '6:2', '6:1', '6:0') def calc_p(stat): Rz, Rt = 0, 0 for score, count in stat.items(): z, t = map(int, score.split(':')) Rz += z * count Rt += t * count return float(Rz) / (Rz + Rt) def predict(score, P): z, t = map(int, score.split(':')) return comb(z + t - 1, min(z, t)) * P**z * (1 - P)**t if __name__ == "__main__": stat = {score:0 for score in SCORES} with open('letopis.csv', 'r') as f: for line in f: s = line.strip().split(',')[-2:] score = s[0] if not s[1] else s[1] if score in stat.keys(): stat[score] += 1 P = calc_p(stat) S = sum(stat.values()) ind = np.arange(len(SCORES)) width = 0.25 vals = [stat[score] for score in SCORES] vals = [float(v) / S for v in vals] vals_h = [predict(score, P) for score in SCORES] stds = [2 * (val / S)**0.5 for val in vals_h] vals_r = [v * S for v in vals_h] vals_r = [np.random.poisson(v) for v in vals_r] S_r = sum(vals_r) vals_r = [float(v) / S_r for v in vals_r] fig, ax = plt.subplots() rects1 = ax.bar(ind, vals, width, color='r') rects2 = ax.bar(ind + width, vals_h, width, color='y', yerr=stds) rects3 = ax.bar(ind + 2 * width, vals_r, width, color='c') ax.set_xticks(ind + 1.5 * width) ax.set_xticklabels(SCORES) plt.title("Chto Gde Kogda, probability distribution") plt.xlabel('Scores') plt.ylabel('Probability') plt.grid(True) plt.legend(['Actual', 'Predicted', 'Random case']) fig.savefig('pic.png')