Bayesian decision analysis shows why you have to flip USB connectors twice
Flipping USB Connectors - Towards Data Science
Hands-on TutorialsBayesian decision analysis shows why you have to flip USB connectors twiceAllen B. DowneyI am not the first person to observe that it sometimes takes several tries to plug in a USB connector (specifically the rectangular Type A connector, which is not reversible). There are memes about it, there are cartoons about it, and on sites like Quora, people have asked about it more than a few times.Photo by Jess Bailey on UnsplashBut I might be the first to use Bayesian decision analysis to figure out the optimal strategy for plugging in a USB connector. Specifically, I have worked out how long you should try on the first side before flipping, how long you should try on the second side before flipping again, how long you should try on the third side, and so on.Of course, my analysis is based on some modeling decisions:Let’s assume that the initial probability is 0.5 that the connector is in the right orientation;And if it is, the time it takes to succeed follows an exponential distribution with a mean of 1.1 seconds;And flipping the connector takes 0.1 seconds.With those assumptions, we are ready to get started.Bayes’s TheoremThe first step is to figure out the probability that the connector is in the right orientation as a function of how long you have been trying.For example, suppose that the prior probability is 0.5 that the orientation of the connector is correct, and you have been trying for 0.9 seconds. You might start to doubt that you’ve got it right.To compute the posterior probability, we’ll start with the likelihood that you would fail to connect for 0.9 seconds, given the orientation:If you are on the wrong side, the likelihood that you would fail is 100%.If you are on the right side, the likelihood is given by the survival function of the exponential distribution, which is exp(−λt), where λ is the rate parameter and t is time.So, for the hypothesis that the orientation of the connector is correct, the likelihood ratio is exp(−λt) : 1.Now it will be convenient to use Bayes’s theorem in log-odds form:log O(H|D)= log O(H)+log LR(D)wherelog O(H) is the prior log odds of the hypothesis, before we see the data,log O(H|D) is the posterior log odds of the hypothesis, after we see the data, andlog LR(D) is the log likelihood ratio of the data, under the two hypotheses.Since the likelihood ratio is exp(−λt), the log likelihood ratio is −λt, so we can write:log O(H|D)= log O(H) − λtAnd given the posterior log odds, we can compute the probability that you’re on the correct side as a function of how long you’ve been trying. The following figure shows the result:After a few seconds of fiddling, you should be reasonably convinced that the orientation of the connector is wrong.We can read this graph in either direction: given how long you’ve been fiddling, you can look up the probability you are on the correct side. Or, given a target probability, you can look up how long it will take to get there. This will be useful for the next step.StrategyNow let’s think about turning belief into action. I’ll start with a conjecture: I suspect that the best strategy is to try on the first side until the probability of correct orientation drops below some threshold, which I’ll call r, then try on the second side until the probability drops below that threshold again, and repeat as needed.To implement this strategy, we can use the result from the previous section to figure out how long it will take the posterior probability to drop below the threshold, given the prior probability, which I’ll call p.I wrote a Python function that simulates the process while keeping track of the total time and the number of flips. Here are the steps:Based on p and r, compute the maximum time to try before flipping.If we’re on the wrong side, we won’t succeed, so add the maximum time to the total, flip the connector and go to Step 1.Otherwise, draw a random connect time from an exponential distribution.If the connect time is less than the maximum time, that means we succeeded, so add the connect time to the total, then return the total time and number of flips.Otherwise, add the maximum time to the total, flip the connector, and go to Step 1.The details of the simulation are in a Jupyter notebook you can read on the Think Bayes site or run on Colab.For a range of values of r, I ran the simulation many times, starting in the correct orientation 50% of the time, and computed the average time to connect. Here are the results:The dots show simulation results for different threshold probabilities. The line shows a local regression through the results.The average time to connect is minimized when r is close to 0.3. That means we should keep trying on the first side until we think there is a 30% chance the orientation is correct — which takes about 0.9 seconds — then try on the second side.After the flip, we think there is a 70% chance the orientation is correct. We should keep trying until it drops to 30% again — which takes about 1.8 seconds — then flip again, and repeat until we connect.How many flips?Now let’s see how long it takes to connect and how many times we have to flip, using the optimal value of r.I ran simulations starting in both orientations and recorded the total time and the number of flips. The following figure shows the distribution of total time for each orientation, represented as a cumulative distribution function (CDF).The average is about 2.2 seconds if we start on the right side and 2.6 seconds if we start on the wrong size. But sometimes it takes twice as long or more: the 90th percentile is about 4.6 seconds if we start on the right side, and 5.6 seconds otherwise.Here’s the distribution for the total number of flips.The probability of connecting on the first try is only 28%, which implies that when we start in the correct orientation, we flip almost half the time (22 out of 50).The most common outcome is that we flip once, about 40% of the time. And the probability of the notorious double flip is about 18%.Fortunately, it is rare to flip three or more times.SummaryWith that, I think we have solved the USB connector problem.We used Bayes’s theorem (in log-odds form) to compute the posterior probability that the orientation of the connector is right, depending on how long you’ve been fiddling.We defined a strategy based on my conjecture that you should keep fiddling until the posterior probability falls below a given threshold.Given the average time to connect (in the right orientation) and the time it takes to flip, we found the threshold probability that minimizes the total connection time.Given this optimal value, we estimated distributions for the total time and the number of times we have to flip.These results explain why it is so common to flip a USB connector more than once, even if you start in the right orientation.Sadly, our fun is being spoiled by the increasing prevalence of the USB-C connector, which works in both orientations.This example is based on methods in the new second edition of Think Bayes. For other examples of Bayesian decision analysis, see Chapter 9 and Chapter 10.Allen Downey is a professor at Olin College and author of Think Python, Think Stats, and other books about software and data science — all available from Green Tea Press. His blog, Probably Overthinking It, features articles about Bayesian statistics and data science.Copyright 2021 Allen Downey.License: Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)