Posts

Exploring multi-input einsums in JAX

The JAX documentation says : einsum is a powerful and generic API for computing various reductions, inner products, outer products, axis reorderings, and combinations thereof across one or more input arrays. In this blog post, we are going to explore the einsum API and utilize it solve marginal inference-type problems.  Basic Examples Suppose we have two $ n \times n $ arrays $A$ and $B$ and we want to compute $$ Y[i, k] = \sum_{j=1}^n A[i, j] B[j, k] $$ This is the basic definition of matrix multiplication, and we can compute it in JAX via jnp.einsum('ij,jk->ik', A, B).  Here the formula 'ij,jk->ik' defines the computation.  Each letter corresponds to a name for an axis of an array.  In this case we have only 2D arrays so each part of the formula contains two axes.  The letters on the RHS of the expression determine which axes will be defined for the output.  Any axes names not appearing in the RHS of the expression will be "marginalized out" via th...

Beat the Streak Day 17: Coordinated Pick Selection

Image
My current best pick selection model for BTS achieves roughly 78% accuracy.  Looking at the table from Day Six , we can see that with an optimal pick selection strategy, my odds of beating the streak are roughly 11,800 to 1, or roughly a 0.01% chance of winning.  In this blog post, I will explore methods for boosting this probability by coordinating picks across multiple accounts (e.g., friends, family, other BTS enthusiasts, etc.) To the non-mathematically inclined, one might think that with $k$ accounts our probability of beating the streak as a group would simply multiply by $k$.  This simple formula is not correct, however, although it is an upper bound.  If each account has a probability $p$ of beating the streak, and there are $k$ accounts, the probability that at least one account beats the streak would be $1 - (1 - p)^k$ if we (incorrectly)  assume independence between accounts .  For small $p$ and small $k$, this is pretty close to to the upper bou...

Beat the Streak Day 16: Vegas Odds and Sports Betting

Image
I recently have been seeing many advertisements for sports betting platforms like DraftKings and Fanduel, and since those are somewhat related to beat the streak, I thought it would be interesting to look into these things a little more. In my search I came across a website that lists Vegas odds for various sports books on various bets.  Among those bets is one highly related to beat the streak: a bet that a given batter will "record a hit" in a given game.  This is exactly the essence of the beat the streak: identifying a batter most likely to record a hit.  In my effort to develop models for BTS, I have several approaches to estimate the probability that a player will record a hit in a given game.   So three natural questions arise: 1. Is Vegas good at BTS?  That is, is the implied probability of a hit given the Vegas odds a better estimate of the true probability than some of the models I've talked about in this blog? 2. Can the Vegas odds be a useful f...

Beat the Streak Day Fifteen: A back-testing framework

Image
In this blog post, I will talk about a back-testing framework I developed to evaluate the quality of different Beat the Streak pick selection strategies on historical data, including the data sources I use, the evaluation metrics I look at, and some of the baseline models I've considered.  The source code for my back-testing framework is available at  https://github.com/ryan112358/beat-the-streak .  Everything is written in python, and the source code heavily relies on the pandas package for data processing. Data Sources I draw on data from multiple sources, most importantly is statcast data, which I obtain using  pybaseball .  This dataset contains information about every pitch, including the outcome (ball/strike/hit/etc) as well as other characteristics that have expanded over time (pitch type, velocity, spin, etc.).  From this pitch-level data, I derive at-bat level data and (batter, game)-level data.  This data includes many context features that c...

Beat the Streak Day Fourteen: Singlearity

Image
After a fortnight of work, I am back with another blog post on MLBs beat the streak contest. And before you ask, yes that is still a thing in 2023. No one has won it yet, and this year the longest streak was 44, 13 shy of of the number needed and 7 short of the previous all time leader in BTS. In short, it doesn't seem like we're any closer to winning it now than we were 10 years ago.  In the last few days, I have been thinking about new algorithms, models, and approaches to this longstanding problem. But before I can or should test these new approaches, it's important to better understand the limitations of simpler approaches when done very carefully. Singlearity was the first approach to this problem I've seen that convincingly demonstrated solid performance where it matters.  The idea is to apply standard neural network training techniques on a dataset of (batter, game, outcome) data. The neural network is trained on carefully feature engineered data. Specif...

Deriving Perfect Strategy for Blackjack

Image
In this blog post, I will discuss the algorithmic techniques needed to derive perfect strategy for the game of blackjack. Perfect strategy utilizes knowledge of the exact composition of cards remaining in the deck. No human can execute perfect strategy in practice since it requires keeping track of the count of 13 different cards simultaneously. It’s theoretically possible to use a technique like this to gain a slight advantage in online live dealer blackjack via software, and this strategy does provide a positive EV game, although as we will see later, the advantage is quite small. Nevertheless, it is quite an interesting algorithmic problem, and I had fun solving it. This will be the topic of this blog post. The Wizard of Odds has a great YouTube video showing how you can derive basic strategy for a simplified game with an infinite deck of cards. This is a great starting point for understanding this way of thinking. There are two main questions we ultimately want to answer...

Beat the Streak: Day 13

Image
Recall from Beat the Streak: Day 12 , that my grand vision requires building a collection of several models for different sub problems, which will all be combined to get a model for the probability that a batter will get a hit in a given game.  In this blog post, my aim is to tackle the first subproblem.  Specifically, I'd like to build a model to predict the probability that a ball put into play results in a hit, given it's launch angle, spray angle, launch speed, and any other relevant context (like the ballpark).  Note that statcast data already has a column called "estimated_ba_from_speedangle".  This only looks at launch angle and launch velocity, and ignores spray angle.  It therefore acts as a good baseline for this problem that we can hopefully improve upon.  An even more naive baseline is to assume the probability of a hit is constant given it was put into play, ignoring all other context.  Evaluating these models gives a negative log likeliho...