 So today I'm going to talk about estimating the value of pi with a dartboard and maybe or maybe not luck. So here's a picture of what the game of darts is supposed to look like. If you've never played darts, that's actually not important. The only important part of this picture is that the darts are actually on the dartboard. Here's a picture of what it looks like when I play. Once again, if you don't know the rules, it's fine, but you can probably infer that this is not the correct way to play. Okay, so I'm not going to be a dart champion, I won't be on ESPN, but we can pivot. So there's this really interesting thing that happens that if I truly have the ability to throw uniformly at random at this dartboard, there are some really interesting consequences that arise because of this. One of which is actually I can use this randomness to estimate pi. You might ask why would I ever want to do that? I don't really have a good answer, but this is BangBangCon, so we're going to go forward with this. All right, so how is this going to work? The basic principle is just going to be a simple comparison of area between a square and a circle. So if you bear with me here, here I have a picture of a square and there's a circle inside it. And basically the area of the circle, if you remember the formulas, is something like pi r squared where r is the radius of the circle. The area of the square that surrounds it is force r squared. And so we just look at the ratio of the area of the circle to the ratio of the area of the, sorry, if we look at the area of the circle to the area of the square, this proportion is pi over four. And that's an important number that we're going to want to remember. So the reason is is that if I basically throw uniformly at random at the square, the chance I have of actually landing inside the circle is exactly pi over four. So how are we going to turn this key inside into an algorithm? Well, effectively, we're just going to throw a bunch of darts at the square. We're going to just count the number of darts that land inside the circle. And our estimate is just going to be four times the number of darts inside a circle over n, which is the number of darts that we threw total. And that's going to give us an estimate of pi. Okay. So here I, this is actually a really simple algorithm so I can implement it in two lines of numPi. I use numPi because I don't like for loops, so this is very concise. And so if I actually run this, and so I ran this, and here I ran it 1,000 trials, and each trial I set n throws to be 100k, so that's actually quite a big number. And you see here the black vertical line is the floating point representation of pi. And you see, you know, this algorithm does roughly well. There's like this nice bell curve shape around pi. There are a couple outliers though, right? So there's a couple of runs that were something like 3.15, 3.16, which we know is not quite the right answer. But, you know, most of them, the most of the mass of the, of this probability distribution is lying very close to pi. Okay. Oh, and also if you see this bell shape curve and you're thinking that it looks, you know, kind of like a Gaussian distribution, that's not a coincidence. That's the central limit theorem at play, so, okay. So before we sort of think of like how much we need to run this algorithm to get a good estimate, I want to make a quick aside here that this phenomenon of just throwing things at the dartboard can be more generally thought of as this instance of Monte Carlo integration. And so this is really the idea of trying to estimate an integral by sampling. And this is a very popular technique in science and engineering when you have some integral expression that you're interested in for whatever reason. And it turns out that it's too difficult to plug into Mathematica. So you have to just, you know, and also you don't remember your freshman level calculus course or you maybe never took one. So then, you know, you just try a bunch of values. And to convince you that this is important, here's the book. And this is a book that I was told to read and never did. And now I'm telling you about it. So that means it's really important. Okay. Great. So let's go back to the question of how many darts do we need to throw more generally how accurate is this algorithm? So the cop-out answer here is if we threw forever, right? So if we could just set it up bar forever and throw darts, we would get perfect accuracy and this is a consequence of the law of large numbers. But that's not really quite an answer because, you know, when the event like the earth implodes into the sun happens, we still need to be throwing darts because we won't have our estimate, right? So this is, you know, we would like to do better than throw forever. Okay. So if we're willing to tolerate a little bit of inaccuracy, so here we're going to have this parameter called epsilon, which is going to parameterize how much inaccuracy we're going to allow and we're going to measure the inaccuracy of our solution by just the deviation of the estimate from pi in absolute value. And so that's going to be our error metric here that we're going to look at. And the question is, well, since our estimator is random, we're going to also need to allow some chance so that we just, you know, completely fail because we could get really, really, really unlucky. So, you know, we'll fix some chance of failing, say something like 5%. And we want to ask the question, well, how many darts do I need to throw in order to get with an epsilon with, you know, 95% chance of success, right? So that's the question we want to ask. And so here's a table. So it turns out you can actually compute this value to, you know, pretty somewhat arbitrary precision. There's like no nice close form for it, but you can get pretty close on a computer. And so roughly speaking, if the error is .1, I'm going to need about, say, 2,000 samples or 2,000 throws. If the error is .01, I'm going to actually need something around like 200,000. And if the error is .001, I'm going to need 20 million, which is quite a big number. And the general trend here is if for every decimal place extra of error that I'm, you know, I want to push down, I'm going to actually have to add two zeros to the number of samples. So this isn't the most sample efficient algorithm, but it is an algorithm. And if you like the picture, so this is the scaling of how epsilon actually, how the number of samples we need actually grows with epsilon. So the y-axis is just, and the number of darts we need to throw in the x-axis is the epsilon. And the scaling turns out to be something like 1 on epsilon squared. And I'm not going to convince you why that this is the correct asymptotic scaling, but it's pretty close. Okay, so that was a good day, right? So a good day, I can throw darts in this small square that perfectly touches a circle at four points. On a bad day, I might actually not throw like that, and, you know, I might have some more tolerance or more error, right? So maybe there's this parameter h that parameterizes how big this square is, and maybe h is really big, like 20 or 2,000. And so the question is, well, can we still estimate pi by using the same trick, right? So it'd be really weird if it only worked when h was two. So unsurprisingly, the answer is yes, we can still estimate pi, and let's think about how we can do that. So we're going to go through the same line of reasoning maybe a little quicker this time. So I'm going to set the radius to be one, because it was actually unimportant in the calculation, so we might as well set it to one. So the area of the circle now is just pi. The area of the square is now h squared, and so the ratio is pi over h squared. And remember before we had h is two, so, you know, pi over two squared is pi over four, so this is actually more general than the analysis that we did earlier. And the new algorithm we're going to use is just a simple modification of what we were doing. So instead of the four, we're just going to change that to h squared, and we're just going to plug it into the simple formula. And that's it. And that'll give us the same answer to pi. And we can go back, oh, and also, yeah, right, like I said before, setting h is two recovers our old estimate. So we can actually go in and ask this question again, right, like so, you know, on a good day, a small h, on a bad day, a big h, how does this actually affect the number of samples I need to throw to achieve some level of accuracy? And here are three lines just denoting different levels of accuracy, 0.1, 0.2, 0.4. And on the x-axis now, instead of epsilon, it's actually h, remember the height of the square, and the y-axis is still the number of throws we need. And we see that the scaling is actually quadratic, so it's something like h squared. So we have the same phenomenon before where every extra decimal point you add in h, you're going to have to need two extra zeros to number samples. And also, it turns out that h squared over epsilon squared is roughly the correct asymptotic formula, but I won't be able to convince you of that because I think I am roughly out of time. So thanks. That's it, the code for generating a plot site put on GitHub, and that's it.