Python x Bradley-Visualize the transition of the strength values of both the Interleague League with Terry's Model

Bradley-Terry's Model This is a model for "strength". Simply put, the concept is that each "strength" can be quantified from the result of the match. The following papers were helpful, so please see here for details. Quantitative evaluation method for "strength" and its application

There are n elements (teams and individuals) and some kind of match is played. A match is a one-element to one-element match, and the result is only a victory or defeat against one element. The "strength" of each element is measured from the results of several battles. Here, assuming that the probability that element i wins element j is Pij, for all combinations, Pij = πi / πi + πj (1) Introduce πi. The relational expression of equation (1) is called the Bradley-Terry (BT) model. In the BT model, πi can be thought of as representing the strength of element i.

Referenced

I refer to the lecture contents at Data mining study session based on statistical processing and machine learning # 03 that I attended the other day. Here is a Python implementation of Bradley-Terry's Model. (The numbers assigned to ws are a list of the total number of wins for the six Central League teams in 2014, excluding the results of the interleague game. For convenience, the draw is counted as 0.5.) The theta obtained here is the numerical value of the "strength" of each team.

import numpy as np
theta = np.ones(6)/6.0 # \theta initial value
t = np.zeros(6)
r = 24.0 #Number of matches
ws = np.array([66.5, 66.5, 66, 56.5, 55, 50.5])  #Total number of wins
for iloop in range(0, 100):
    for i in range(0,6):
        acc = 0 #Accumulator for summation
        for j in range(0,6):
            if (j == i):
                pass
            else:
                acc += r / (theta[i]+theta[j])
        t[i] = ws[i]/acc
    s = sum(t)
    for i in range(0, 6):
        theta[i] = t[i] / s

Strength transition for the past 5 years

With reference to the above Bradley-Terry's Model, I tried to graph the transition of the Central League over 5 years with matplotlib.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
import os

theta = np.ones(6) / 6.0
t = np.zeros(6)
r = 24.0

teams = [u"Giant", u"Hanshin", u"Hiroshima", u"Chunichi", u"Yokohama", u"Yakult"]
colors = ["#f27b00", "#ffff00", "#ff0000", "#002468", "#044a90", "#111c3c"]
scores = [[67.5, 68, 48, 68.5, 42.5, 65.5], [66, 61, 57, 66, 43.5, 66.5], [76.5, 51.5, 55.5, 69, 43, 64.5], [74, 62.5, 58.5, 57.5, 55, 51.5], [66.5, 66.5, 66, 56.5, 55, 50.5]]
years = ["2010", "2011", "2012", "2013", "2014"]
points = [[], [], [], [], [], []]


def reset():
    global theta
    theta = np.ones(6) / 6.0

def bradley_terry(ws):
    for iloop in range(0, 100):
        for i in range(0, 6):
            acc = 0
            for j in range(0, 6):
                if i == j:
                    pass
                else:
                    acc += r / (theta[i] + theta[j])
            t[i] = ws[i] / acc
        s = sum(t)
        for i in range(0, 6):
            theta[i] = t[i] / s

if __name__ == '__main__':
    for score in scores:
        reset()
        bradley_terry(score)
        for i in range(0, 6):
            points[i].append(theta[i])

    for i in range(0, 6):
        data = np.loadtxt(points[i])
        plt.plot(
            data,
            linestyle="-",
            color=colors[i],
            label=teams[i],
            linewidth=2
        )

    plt.ylim(0, 0.5)
    X = np.arange(len(years))
    plt.xticks(X, years)
    plt.tick_params(labelleft="off")
    plt.xlabel("Year")
    plt.ylabel("BradleyTerry Rate")
    plt.title("Central League")
    plt.grid(True)
    plt.legend(loc="best")
    plt.rcParams.update({"font.size": 20})
    os.chdir("graph/")
    plt.savefig("central.eps")

    plt.show()

central.png

It turned out to be something like this. ・ Stable strength of the giant ・ Chunichi, Yakult's right shoulder down ・ Hiroshima and Yokohama are rising I think you can read ...

If you try the same in the Pacific League, the graph will be as follows.

pacific.png

Compared to the Central League graph, it is characteristic that the six line charts are messed up. You can often express the "mixed pa" that happens when the AB class is completely replaced in 2013 and 2014!

Recommended Posts

Python x Bradley-Visualize the transition of the strength values of both the Interleague League with Terry's Model
Check the existence of the file with python
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Let's simulate the transition of infection rate with respect to population density with python
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
Check the date of the flag duty with Python
Solving the Lorenz 96 model with Julia and Python
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
[Blender x Python] Think of code with symbols
[Python & SQLite] I analyzed the expected value of a race with horses with a win of 1x ②
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
Find the sum of unique values with pandas crosstab
Destroy the intermediate expression of the sweep method with Python
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
Install the latest stable Python with pyenv (both 2 and 3)
Summary of the basic flow of machine learning with Python
Get the operation status of JR West with Python
Extract the band information of raster data with python
Numpy creates a matrix with only the columns whose total values of the columns of the matrix are the top X