## Wednesday, April 10, 2013

### Lunchtime Sports Science: Introducing tanh5

As I mentioned in a previous article on ratings systems, the log5 estimate for participant 1 beating participant 2 given respective success probabilities $$p_1, p_2$$ is
\begin{align}
p &= \frac{p_1 q_2}{p_1 q_2+q_1 p_2}\\
&= \frac{p_1/q_1}{p_1/q_1+p_2/q_2}\\
\frac{p}{q} &= \frac{p_1}{q_1} \cdot \frac{q_2}{p_2}
\end{align} where $$q_1=1-p_1, q_2=1-p_2, q=1-p$$.

Where does this come from? Assume that both participants each played average opposition. In a Bradley-Terry setting, this means
\begin{align}
p_1 &= \frac{R_1}{R_1 + 1}\\
p_2 &= \frac{R_2}{R_2 + 1},
\end{align} where $$R_1$$ and $$R_2$$ are the (latent) Bradley-Terry ratings; the $$1$$ in the denominators is an estimate for the average rating of the participants they've played en route to achieving their respective success probabilities.

In a Bradley-Terry setting, it's true that the product of the ratings in the entire pool is taken to equal 1. But participants don't play themselves! Thus, if participant 1 played every participant but itself, the average opponent would have rating $$R$$, where $$R_1 \cdot R^{n-1} = 1$$. Here $$n$$ is the number of participants in the pool.

Our strength estimate for the average opponent faced is then
\begin{align}
R &= {R_1}^{-\frac{1}{n-1}}.
\end{align}
There are two extreme cases. If $$n=2$$, then $$R = \frac{1}{R_1}$$; as $$n \to +\infty$$, $$R \to 1$$.

The limit extreme case is log5; the $$n=2$$ extreme case I call tanh5. We compute
\begin{align}
p_1 &= \frac{R_1}{R_1 + 1/R_1}\\
p_2 &= \frac{R_2}{R_2 + 1/R_2}\\
\textrm{tanh5} = p &= \frac{ \sqrt{p_1 q_2} }{ \sqrt{p_1 q_2} + \sqrt{q_1 p_2} }.
\end{align}
Why tanh5? We can think of log5 as derived from the logistic function by setting $$\log(R)=0$$ for the opponent's rating; analogously, tanh5 is derived from the hyperbolic tangent function by setting $$\log(R)=0$$ for the opponent's rating.

Note that we have a spectrum of estimates corresponding to each value for $$n$$, but these are the two extremes. This also gives a new spectrum of activation functions for neural networks, but I'll explore this application later.

## Wednesday, April 3, 2013

### Lunchtime Sports Science: Fitting a Bradley-Terry Model

Power rankings are game rankings that also allow you to estimate the likely outcome if two opponents were to face each other. One of the simplest of these models is known as the Bradley-Terry-Luce model (or commonly, Bradley-Terry). The idea is that each player $$i$$ is assumed to have an unknown rating $$R_i$$. If players $$i$$ and $$j$$ compete, the probability that $$i$$ wins under this model is expected to be about $\frac{R_i}{R_i + R_j}.$ This model is very popular for hockey and other games; one commonly seen version is called KRACH.

Let's fit a Bradley-Terry model to the current season of NCAA D1 men's hockey. The Frozen Four starts on Thursday, April 11, so you'll get to see how well your predictions do.

You'll need to have R installed. Once R is installed, install the "BradleyTerry2" package that's freely available for R (thanks to Heather Turner and David Firth). To do this, start R and run the following command; you'll have to pick a source.
install.packages("BradleyTerry2")
Next, download two files from my hockey GitHub - R code that fits a basic Bradley-Terry model and a data file containing the NCAA D1 men's hockey game results going back to 1998.

https://github.com/octonion/hockey/blob/master/lunchtime/uscho_btl.R
https://github.com/octonion/hockey/blob/master/lunchtime/uscho_games.csv

Make sure both files are in the same directory and run the R code. That's it, you've built a power ranking using a Bradley-Terry model. You should get output that looks like this:

ability      s.e.
Quinnipiac              1.687042594 0.5678939
Massachusetts-Lowell    1.480098569 0.5872701
Minnesota               1.428503522 0.5638946
Yale                    1.115338226 0.5641414
Miami                   1.114307264 0.5479346
Notre Dame              1.109912670 0.5523657
Boston College          1.091836391 0.5815233
St. Cloud State         1.079314965 0.5573018

The order should be the same as USCHO's KRACH rankings.

How do we use these ability estimates to predict game outcomes? These values are the logarithms of the ratings I've mentioned above, so first apply the exponential to get the rating, then the estimated winning probability is the team's rating divided by the sum of the team and opponent ratings. For the teams in the Frozen Four we get a power rating of $$e^{1.687} = 5.40$$ for Quinnipiac and $$e^{1.079} = 2.94$$ for St. Cloud State, so we estimate the probability of Quinnipiac beating St. Cloud State to be about $\frac{5.40}{5.40+2.94} = 0.65.$ What's your estimate for Massachusetts-Lowell beating Yale?

## Monday, April 1, 2013

### Lunchtime Sports Science: Cracking a New Sport

This is the first and what will be a series of relatively short pieces on sports analytics. I'll be using a variety of sports for examples, including both team sports and single-player sports, and I'll also make my code available through my GitHub account.

Here are my recommended tools. If you're unfamiliar with some of these, don't worry. You'll pick them up as you go along, and they form a powerful suite that will keep you on the cutting edge even as a professional data scientist.
1. Hardware - Ideally you want at least 4GB of RAM for larger data sets, but you'll be able to do high-level analysis with almost any modern computing hardware.
2. Linux operating system - You can certainly do top-notch data analysis using any operating system, but Linux is an excellent (and free) working environment. There are a variety of ways to install and use Linux, but I'd recommend Ubuntu's Windows installer. This will allow you to easily install Ubuntu alongside Windows, and it also makes it easy to uninstall Ubuntu later (if you choose). Ubuntu is just one of the (many) Linux distributions available, but it's very popular and well supported.
3. R programming language - R is a powerful statistical programming language, and it has thousands of available packages available. If you're using Ubuntu, installing R is simple - sudo apt-get install r-base. That's it!
4. Python programming language -Python is a powerful and relatively easy to use programming language. One of the most common tasks for sports analytics is web scraping, and Python is an excellent choice thanks to libraries such as Mechanize, Beautiful Soup and lxml. It's also a great language for data cleansing. Installing Python under Ubuntu - sudo apt-get install python.
5. PostgreSQL database server - There are many ways to store and analyze data sets, but a dedicated relational database server is necessary tool for high-level analytics. PostgreSQL is my personal recommendation, but there are other good choices (such as MySQL). PostgreSQL is free, fast, powerful and has a huge variety of procedural languages available (including R and Python). Installing PostgreSQL under Ubuntu - sudo apt-get install postgresql-9.2.
6. GitHub account - Setting up a GitHub account is free and will allow you to automatically following any changes to various sports analytics GitHub projects (such as mine). Later, you can set up your own repositories if you'd like to share your own work with other people. Don't forget to install git under Ubuntu - sudo apt-get install git.
7. We'll start with analyzing hockey. If you'd like to take a look at some of my hockey code and data that I've scraped, you can find them in my hockey GitHub repository. If you've set up Ubuntu and have installed git you can execute the command git clone https://github.com/octonion/hockey.git to make a local copy of my repository.
Here's a basic outline for tackling a new sport.
1. Understand how teams win - Build a model to project the likely outcome for a team when facing a particular opponent. Example - Krach for hockey (which is based on the Bradley-Terry model).
2. Understand how teams score - Build a model to project how many goals/points/runs teams are likely to score or allow when facing a particular opponent. Exampe - Poisson distribution and hockey.
3. Relate the two - Characterize the relationship between scoring and winning or losing. Example - Pythagorean win expectation.
4. Understand how players score/prevent scoring - Determine which aspects of player performance impact team offense and defense and by how much.
5. Understand player contribution to winning/losing - This is nearly automatic once you understand the relationship between team offense/defense and team winning/losing.
In my next article we'll build a basic power ranking model for hockey to predict likely game outcomes.

## Tuesday, March 26, 2013

### Solving Recurrences with Difference Equations

Here's an example from Robert Sedgewick's course on analytic combinatorics.

Solve the recurrence $a_n = 3 a_{n−1} − 3 a_{n−2} + a_{n−3}$ for $$n>2$$ with $$a_0=a_1=0$$ and $$a_2=1$$.

Let $$f(n) = a_n$$; in the language of difference equations the above becomes simply
$\frac{{\Delta^3} f}{{\Delta n}^3 } = 0 .$ Immediately,
$f(n) = c_2 n^2 + c_1 n + c_0 .$ Applying the initial conditions we get
$c_0 = 0, c_2 + c_1 = 0, 4 c_2 + 2 c_1 = 1,$ and so the solution is $$a_n = \frac{1}{2} n^2 - \frac{1}{2} n$$.

Now what if the initial conditions are changed so $$a_1 = 1$$?

## Thursday, March 14, 2013

### Baseball, Chess, Psychology and Pychometrics: Everyone Uses the Same Damn Rating System

Here's a short summary of the relationship between common models used in baseball, chess, psychology and education. The starting point for examining the connections between various extended models in these areas. The next steps include multiple attempts, guessing, ordinal and multinomial outcomes, uncertainty and volatility, multiple categories and interactions. There are also connections to standard optimization algorithms (neural  nets, simulated annealing).

Baseball

Common in baseball and other sports, the log5 method provides an estimate for the probability $$p$$ of participant 1 beating participant 2 given respective success probabilities $$p_1, p_2$$. Also let $$q_* = 1 -p_*$$ in the following. The log5 estimate of the outcome is then:

\begin{align}
p &= \frac{p_1 q_2}{p_1 q_2+q_1 p_2}\\
&= \frac{p_1/q_1}{p_1/q_1+p_2/q_2}\\
\frac{p}{q} &= \frac{p_1}{q_1} \cdot \frac{q_2}{p_2}
\end{align}

The final form uses the odds ratio, $$\frac{p}{q}$$. Additional factors can be easily chained using this form to provide more complex estimates. For example, let $$p_e$$ be an environmental factor, then:

\begin{align}
\frac{p}{q} &= \frac{p_1}{q_1} \cdot \frac{q_2}{p_2} \cdot \frac{q_e}{p_e}
\end{align}

Chess

The most common rating system in chess is the Elo rating system. This has also been adopted for various other uses, e.g. hot or not'' websites. This system assigns ratings $$R_1, R_2$$ to players 1 and 2 such that the probability of player 1 beating player 2 is approximately:

\begin{align}
p &= \frac{e^{R_1/C}}{e^{R_1/C}+e^{R_2/C}}
\end{align}

Here $$C$$ is just a scaling factor (typically $$400/\ln{10}$$ ). The Elo rating is connected to log5 via setting $$e^{R/C} = p/q$$. We then recover:

\begin{align}
\frac{p}{q} &= e^{R/C}\\
p &= \frac{e^{R/C}}{1+e^{R/C}}\\
R &= C\cdot \ln(p/q)
\end{align}

Note that $$p$$ is also the probability of this player beating another player with Elo rating 0. The Elo system generally includes enhancements accounting for ties, first-move advantage and also an online algorithm for updating ratings. We'll revisit these features later.

Psychology

The Bradley-Terry-Luce (BTL) model is commonly used in psychology. Given two items, the probability $$p$$ that item 1 is ranked over item 2 is approximately:

\begin{align}
p &= \frac{Q_1}{Q_1+Q_2}
\end{align}

In this context $$Q_*$$ typically reflects the amount of a certain quality. That this model is equivalent to the previous models is immediate:

\begin{align}
Q &= e^{R/C} = p/q\\
R &= C\cdot \ln(Q) = C\cdot \ln(p/q)\\
p &= \frac{Q}{1+Q}
\end{align}

Psychometrics

The dichotomous (two-response) Rasch and item response models are commonly used in psychometrics. For the Rasch model, let $$r_1$$ represent a measurement of ability and $$r_2$$ the difficulty of the test item. The Rasch model estimates the probability of correct response $$p$$ as:

\begin{align}
p &= \frac{e^{r_1-r_2}}{1+e^{r_1-r_2}}
\end{align}

The one-parameter item response model estimates:

\begin{align}
p &= \frac{1}{1+e^{r_2-r_1}}
\end{align}

These are clearly equivalent to each other and to the previous models.

## Monday, February 25, 2013

### Rejoining the San Diego Padres

Good news - I'm rejoining the San Diego Padres as a consulting analyst!

## Wednesday, February 20, 2013

### Mining the First 3.5 Million California Unclaimed Property Records

As I mentioned in my previous article the state of California has over $6 billion in assets listed in its unclaimed property databaseThe search interface that California provides is really too simplistic for this type of search, as misspelled names and addresses are both common and no doubt responsible for some of these assets going unclaimed. There is an alternative, however - scrape the entire database and mine it at your leisure using any tools you want. Here's a basic little scraper written in Ruby. It's a slow process, but I've managed to pull about 10% of the full database in the past 24 hours (3.5 million out of about 36 million). What does the distribution of these unclaimed assets look like? Among those with non-zero cash reported amounts: • Total value -$511 million
• Median value - $15 • Mean value -$157
• 90th percentile - $182 • 95th percentile -$398
• 98th percentile - $1,000 • 99th percentile -$1,937
• 99.9th percentile - $14,203 • 99.99th percentile -$96,478
Visually, it looks like this:

• 548309 have value >= $100 • 67452 have value >=$1,000
• 4954 have value >= $10,000 • 304 have have value >=$100,000
• 4 have value >= $1,000,000 • The largest value was$8,050,000
The top 10 by value:
1. $8,050,000 to Jasmine Holdco. Entered as Hold Co. Sent CEO Alexander Slusky a note on LinkedIn. 2.$2,183,062 to someone associated with Procket Networks. Procket was bought by Cisco. Sent former Procket CFO Curtis Mason a note on LinkedIn.
3. $1,669,561 to Wyle Systems. Address and city misspelled. Sent email to Wyle, was told this money belongs to Wyle Electronics, which in turn was purchased by Arrow Electronics. 4.$1,419,929 to Anne Baronia. Appears to have moved. Sent her notes on Facebook and LinkedIn.
5. $777,856 to Citi Residential Lending. Premium refund. 6.$639,000 to Martin Peter Wright. Funds for liquidation.
7. $611,460 to Jeanne and Melvin Hing. Mature CD or savings certificate. 8.$520,761 to Loretta Nisewander. Checking account.
9. $507,077 to Payroll (no address). Now you know what to name your next kid. "This here's my son Payroll, and this one's my daughter Unknown". 10.$450,000 Joseph Mallon. Funds for liquidation.
People who should be easy to contact:
1. $58,946 to Ehren Maedge. COO of Scale Computing, Foundation Capital. 2.$408,105 and $94,001 to Dane Prenovitz. Director of the Dani Investment Collection, appears to go by DJ Dane Mitchell now. 3.$160,825 to the Vernon Otto Wahrenbrock Trust. Wahrenbrock's was the biggest used bookstore here in San Diego - it was very sad when Vernon passed away and Wahrenbrock's closed. His grandson, Craig Maxwell, owns a bookstore in La Mesa.