Transcription
[Music] hey everybody welcome back.
So most of the videos on this channel, we're talking about some kind of specific data science or machine learning concept and doing a deep dive into that concept. In this video, instead, I want to take a step back and look at the whole pipeline of machine learning and specifically focus on the part near the end of the pipeline where we're making predictions using some model that we've trained.
Now, the typical way we think about the pipeline is, I have some training data, I use it to train some kind of smart model, and then I use this smart model to make predictions about my problem, whether it's a classification or regression or whatever kind of problem it is. And I just keep this model around until it's not doing a good job anymore. I want to kind of fight that notion, kind of think about a different way of doing predictions that's more in line with Bayesian stats or Bayesian reasoning. And so that all sounds a little bit abstract right now, so let's dive into a real-world example to explain this point.
So we've used this example before, but let's say you're the data scientist at a cool new streaming service called StatFlix, where they stream movies and TV shows about stats. Now, let's say you're trying to predict something called churn. Churn, very simply put, is basically when you have a customer who's signed up to your platform and for whatever reason, they unsubscribe from your platform. Maybe they don't find any interesting content, maybe they're not using it enough, could be whatever reason. But it's something that we would like to do a good job of predicting ahead of time because if I know somebody's going to leave the platform, maybe I can send them an email with some kind of cool offer to get them to stick around. So this is why we want to predict this.
And so today, we'll be doing a very simple prediction problem where I want to know whether or not someone's going to churn. I want to predict the probability of that based on the number of days that they've been inactive, the number of days they haven't been using StatFlix. The hypothesis, of course, is the more days you have not been using it, the more likely you are to churn. And so we think that if we were to plot the number of days of inactivity versus probability of churn, it'll go up some kind of increasing function. And let's keep things simple in this video and say we're going to try and model this probability using a logistic regression. And so what that means is that we're going to model probability of churning given the number of days of inactivity as some sigmoid of this linear combination.
So we've seen this all before, and of course, the only question now, the only thing preventing us from having this model is what is the right setting for beta naught and beta one? Those are the only two parameters in the model. And of course, we know that we typically use maximum likelihood estimation or MLE in order to figure out these two coefficients. And MLE amounts to just solving this optimization problem: find the beta naught and beta one. And in general, you can have many of these betas if you have more features. Find the betas who are going to maximize the following probability. That probability, of course, is the probability of observing my training data given that setting of betas. And we've talked about this concept in many previous videos, but just to briefly touch on it, this is a good maximization problem because it says that if I were to find the beta naught and beta one who maximize this probability, that's equivalent to basically saying these are the betas which lead to the highest probability of seeing the data that I actually observed in the real world. In other words, these are the betas that line up most with reality.
Now comes the Bayesian twist. So let's write this probability again. So this is the same probability as here. I've just put the betas into a vector to be more general. Let's write this using Bayes' theorem. Of course, that is writing the reverse conditional probability of beta given the data times the probability of data divided by the probability of the beta vector. Now, it'll be clear why we did this in a second, but first, let's follow this arrow up and try to simplify things a little bit.
So, first things first, we're going to make the assumption, and I want to be very clear here, this is just an assumption to simplify things for this video, it doesn't have to be true. We're going to make the assumption that the probability of observing any beta is equal to 1. In a more intuitive sense, this is what we call setting a flat prior or uninformative prior, where in the context of Bayesian stats, I don't have any preference, any prior preference for one beta versus a different setting of betas. This is putting equal probability, equal prior probability on all those betas. And this is very convenient for us because it lets us do a very nice thing, which is that we can reframe our maximization problem here, which again was probability of data given the betas. We can write that equivalently as this maximization problem, which is the maximum over all possible betas of the reverse conditional.
Now, it's worth talking about why are we allowed to just do this. It's because of this key assumption we've made. If you look back at this formula, the denominator now becomes 1, so you can effectively ignore it. So the only other term would be probability of data. Notice there's no betas in there, which means that this quantity and the reverse conditional are proportional to each other. They differ only by a constant, that constant being the probability of the data. And when two things are proportional to each other, maximizing one is the same as maximizing the other. And therefore, we are allowed to reframe the maximum likelihood estimation to look like this.
But the key question is, of course, why did we go through all this math and try to reframe the problem? It'll be more clear in a second, but we can solve this version of the problem or the original version of the problem. Again, it doesn't really matter, and we can use something like gradient descent, any of our favorite optimization techniques, in order to get the optimal beta naught and beta one, which I'm calling beta naught, beta one with a star on it. This is simply the solution to this maximum likelihood problem.
And so now is where the divergence comes between what we would do typically in order to make predictions versus thinking about predictions in a more Bayesian way. Let's talk about the typical case first. At this point, we have a model, right? We have some beta naught and beta one which are chosen in some kind of intelligent way. So the typical thing we would do is just plug those beta naught, beta one into the logistic regression form and use that as the model for the foreseeable future. And how would that work? Well, we would pick some kind of threshold for the probability of churning, let's just say it's 80%. And so if my logistic regression predicts that the probability that somebody's going to churn is above 80%, I will take some kind of action. So in this case, I will send them an email, here's a free month of subscription, for example, in hopes that they will stick around on the platform a lot longer.
Now, let's graph this. We have the fortune of being able to graph this because we're working in just a couple dimensions here. So the graph would look something like this. Let's say that that probability, that predicted probability of 80% lines up with D, the number of days of inactivity being four or greater. So you can see in this picture that if the number of days of inactivity is greater than or equal to four, that will mean that the predicted probability of churning is above 80%, and therefore we can very conveniently talk about this in kind of a business sense. We can say, if somebody has not been using the subscription platform for four or more days, I'm going to send them an email. And that's what this model would effectively do.
Now, I want to critically think about what is wrong, what could be wrong with this entire prediction process we just talked about. And in order to understand that, let's backtrack a couple of steps and look at this optimization problem that we set up. So we have the probability of some beta given the data. This is effectively a function of beta. That function just tells me that for any beta you give me, I'm going to give you the probability that that was the beta which generated this training data that I see in the real world. And we do the very natural thing of saying, give me the beta which maximizes that probability. Makes sense.
But when you think about it, what are the chances that that beta you chose, that beta naught, beta one star, is truly the coefficients which generated the data that you saw in the real world? It's probably not super likely. In fact, it could have been some other beta naught, beta one which still has a very high posterior probability, which we see here, but it wasn't quite the maximum. Or it could be a different beta naught, beta one which also has a pretty high posterior probability, but it wasn't quite the maximum. And since there's many possible candidates all with pretty high posterior probability, doesn't it feel kind of wrong or crazy to just pick the one that has the highest and say that is the correct answer? Shouldn't we kind of hedge our bets? Shouldn't we kind of say that maybe there's some noise in my training data? Maybe there's some error in the way I'm modeling these probabilities? So maybe I should pick a different beta which still has a high probability, but maybe not quite the highest. And that is very much in the spirit of Bayesian thinking. Bayesian thinking says that there is not really a right answer. Maybe there is something which maximizes a function, but there are many other possibilities which get close to maximizing that function, and perhaps we should sample the model based on or proportional to those probabilities.
And so, let me make it more concrete what I'm trying to say as we talk about the instead way, the way we can do this prediction process in a more Bayesian way. That would go something like this. It might go something like each and every day. So on every new day, instead of just having this fixed model with this beta naught and beta one that maximizes that problem, we are going to sample a new beta vector. So in our case, just a new beta naught and beta one from that posterior probability distribution. So on every given day, there will be some different beta naught, beta one chosen. And the more likely that beta naught, beta one was, or in other words, the higher this quantity, this posterior probability is for that beta naught, beta one, the higher chance it has to get chosen. But the key observation here is that we will definitely be getting a slightly different, or in rare cases, very different beta naught, beta one on every single day. And then on every day, that becomes the model which we're going to be running throughout the entire day.
And so on some days, our model might look like this green sigmoid up here, where the cutoff for 80% would be at three days. On some days, the model will look just like it did before, where the cutoff was four days. On some days, it'll look like this, where the cutoff was five days. And so you can think about this in both ways, right? If you've never thought about this before, it seems kind of crazy. Why would I want a different model every day? Why not just have a static model for the foreseeable future? And there are definitely pros and cons, but let me talk about all the pros first.
And the biggest pro, the one that kind of drove this conversation, would be this insurance for close probabilities. There was probably a better way for me to phrase that, but it's exactly the concept that I've been trying to get across, which is that this is basically kind of a guardrail, a safeguard for us against saying that I'm sure that this solution to the maximum posterior probability problem is the correct answer. I mean, how can you be sure? The real world is so messy. Our training data could have noise. Maybe we didn't clean it in the best possible way. Maybe this logistic regression formulation wasn't the best formulation. There's so many places where things could have gone wrong or a little bit different from reality, where it makes a lot of sense to have some kind of guardrails for us to say that, I think this is the best possible model, but I'm not 100% confident. So let me try a bunch of different ones out throughout the next 30 days, for example, and see which one is performing better. And that kind of gets us into these other points, which is that this serves as a natural form of diversity. If you think about it, now let's say you're doing this procedure, a new model every day, chosen from the posterior distribution for a month, for 30 days. Now, at the end of that month, you can go back and see which days were the most successful in terms of whatever business metric you're calling success. Maybe the number of times people don't unsubscribe or don't churn who otherwise would have churned, something like that. And it kind of gets into the last point too, which is that this setup lets you create a very natural experiment. It kind of says that I'm doing my best job at modeling, but I know I can't capture everything in the world. So let's just kind of try out a bunch of different models and see which ones that people are interacting with the most.
And thinking about it now, I didn't plan for this to be part of the video, and I'll keep this part brief, but this kind of segues really nicely into kind of a multi-armed bandit situation because now, if you're willing to act dynamically on those models which are giving you the best success on any given day and kind of prune out or disregard the ones that aren't, then you can have your model pretty much just evolve naturally over time into something that is giving you the most success. And that maybe is the best case scenario for Bayesian statistics.
Now, of course, on this channel, we talk about things from all possible angles, so I need to talk about the cons of this method. And there's one, I guess, actual con, and the other one is something that I haven't explicitly stated, which you might have missed. So let's talk about the actual con. As you might expect, this requires us, probably more engineering effort and more thought and more kind of code writing for all these things. If you have just a static model, you just put it there, you walk away, you're done. Maybe you retrain the model in a couple months or something. With this, there's a lot more active work going on. You have to basically be able to sample from this distribution every day, set up a new model, be tracking which model is running at any given time. So computationally, a little bit more expensive. But I personally think it's very worth it. And I did not really think it would have been worth it a couple years ago before I had real-world experience with all these sorts of things. And when you're kind of solving these canned problems, you'd get in your university or college, it doesn't seem like you would need anything like this because the data is so clean, the objective is so clear. But in the real world, the data is not clean, the objective is not often clear. So Bayesian stats opens up a door for us to kind of give ourselves a break and say, I'm doing the best I can here, but at the end of the day, let's just sample a bunch of things and see what's doing the best.
And the final thing I'll say in this video is that notice I didn't say anywhere that doing this instead of doing the static model is going to give you more accurate predictions. This is not a matter of accuracy. If you're seeing more accuracy from this, it's not really by design, it's probably more by accident. So we're definitely not doing this because we want stronger predictions in any way. Hopefully, we're not getting weaker predictions. We're hopefully just kind of diversifying the space of predictions and diversifying the model space that we are seeing, so we can kind of look back at all this data and say, which models are performing the best, which are performing the worst, what are the conditions under which they're performing good or bad. So it's really more of a diversity play than it is an accuracy play. And that's important to keep in mind.
So hopefully you learned a little bit about Bayesian prediction. I do think it's an amazing tool in the data scientist toolbelt and something that translates this more academic side of data science where everything is neat and tidy into a more real-world, messy, uh, everything is on fire kind of data science, which is more real-world, more what we're going to be doing on the job. So if you like this video, please, please like and subscribe. Really helps. Um, if you have any questions at all, please leave them in the comments below and catch you next time.