 One of my favorite uses of programming is through something known as or building something known as Simulations and the entire idea is we have mathematical models And we may want to ask questions on those models and we have to sort of carefully monitor in them the entire time Well, guess what that is where we can use something like Python to build simulations something like Approximating things like probability or something known as the Monte Carlo simulation So at least to start let's kind of do that quick refresher you saw this in the previous videos But the entire idea of approximation Something that is close enough, but not exactly correct or if as we used in our conditional statements Something that is close enough Okay. Well, how could we use something like conditions and loops to Do approximation and so here is a great example of approximation Well, we've seen this but something known as Newton's method now if I were to ask you a super simple question What's the square root of 25? Oh? Well that one's easy you learned that one in I don't know when but you know it's five and you're done But what happens if I were to ask you something like 50? Oh, well that becomes a bigger slight issue You're gonna, you know do in your head math and you're gonna say something like oh, well It's maybe like somewhere between seven and eight The issue is because there's no quick and easy way to know the apro the square root of 50 But Newton's method and that's a nice little wiki leak for a wiki pedia link for you But the entire idea is this formula Will allow you to Approximate what that square root would be Starting with some level of precision again We're just saying when we get close enough and then we make some initial guess Well, we know that the square root of 50 is not going to be one and one squared is one So it's wrong, but it gives us that initial base case that again We can work off our loop just like when we said Something like we'll start with play again Equaling yes. Oh, well, it just means start our loop and Then we continue to run through that so oh in this case our guess is one one is less than 50 So we ask it to run through this simple question first If our guess squared minus in very similar to what we saw here if you notice If that is less than our epsilon Then we are now Close enough to our approximation Then we can do something known as a break and a break is just saying exit The while loop you've reached a point where your your loop doesn't need to continue running get out of it Else else modify our guess with this new number. So in this case, you know half times What is that guess guess was one plus? 50 divided by guess so in that case Take our guess that we have and so in this case 50 divided by one. That's one The plus one that becomes One half times 51 roughly speaking. What is that? That's like Yeah, that's like twenty five point five Think that's right. Don't quote me on that But okay. Well, we would ask that same question again. Is that twenty five point five Less than in if it is then do the entire process again in twenty five Squared is definitely too big. It's still outside of our epsilon. So we don't break but we iterate and refine and just as you can sort of see and So you don't have to sort of guess this on your own Here's this same example. So I take that approximation and let's say for our sake. I want to Approximate our Sqrt 50 Now you'll see I am also going to include new guess and this is going to just keep an iterative process Inside of our while loop telling us what this guess is each time so if we were to take this and Run it new guess while guess is less if Hmm There we go now if we run it Let's modify that there we are. So okay good twenty five point five is the What fifty one divided by two is but as you can see well twenty five squared twenty five squared is way too much So we then Modified the guess to become 13.73 Okay, well we make the evaluation again. That's still less than 50. We printed that new guess So it now we see that print while we do our calculation 13 squared 13 squared is still Well above our epsilon. So we skip and we modify our Guess again, and we went with Eight point six all right well eight point six squared eight squared is roughly sixty four You know, whatever the point six adds to that still too high modify it again So we get to seven point two seven point two. Oh, okay seven squared is forty nine But I guess that point two is adding a little too much where we're not quite there And so if we modify it. Oh, well, we're still not quite there with this Point zero seven two six, but as we continue to refine you see that we get this seven point seven one zero and if we Square that We'd see yeah, okay. Well, we look at that. That's actually very close to our fifty and so again We're using the looping structure to approximate and run in this case Not a simulation, but we're utilizing it to Effectively approximate some values for us Now this is where we can think about something like probability Let's say very even simply we want to just flip a coin not even Playing with dice yet. I just want to take My coin and here's my George Washington quarter Very accurate representation of a quarter and then I I think there's a building on the other end So here's a building to represent my tails Well, the entire idea here is again. What's the probability of landing on heads? Okay? Well, if we think about that, there's the mathematical way where oh, you know Well, it's a one in two chance of doing it and then you know, you can make that deduction or We could actually once again use Probability and simulations to get that same approximation So once again if we think about this I now come in and I have two functions flip coin and dice roll and I'll focus on just that first one of flipping a coin Again, I'm going to generate a random number because I don't want to know what the Solution would be it could be heads could be tails But I've made just a simple rule if it's above a 0.5. We're calling that heads 50% roughly speaking and then if it's not we're calling that tails So if I wanted to run a trial of this numb trials Let's say I want to run 10,000 trials. I want to flip that coin 10,000 times and then I'm going to also call something total or successful trials or something like that, but basically this is going to keep track of heads every time we roll ahead we've successfully added to our total number of Corrects and we can do a comparison of that. Okay, so now We'll start so I once again our stepper is going to start at zero and while I is less than our numb of trials We want to flip a coin so Coin is going to equal flip coin Again, that's going to flip a coin and either give us heads or tails okay, if coin is Heads a.k.a. We landed on a heads Total is going to go Plus one Okay, all right last little bit. We need to do here is I is going to equal I plus one I Increments the entire time So if we run through this, okay, we didn't see anything, but let's check out what our total is now Okay, well if you see that's a very interesting number four thousand nine hundred eighty seven very close to Five thousand and the reason why I say that is our We'll call it probability Would it equal our total divided by our numb trials? print our Probability And you see we're very close to fifty percent if we ran through that trial again Okay, now we're above the fifty percent, but you can see we're very very close and interestingly enough if I Increased my trials and just now I'm going up an order of magnitude I'm getting actually very close to that approximation because it to go from Fifty one fifty thousand to fifty one thousand it's actually much more difficult than five thousand to five hundred one Or fifty one hundred if you will But we can do the same thing now instead of doing Coin flipping. Let's actually go back sort of to our Slide and say well, what's the probability of? Landing on here's a die There There's your die We'll say that's the one the two and three. What's the probability of rolling a three again? Yes mathematically and theoretically It would be one and six, but once again, we can show that through Python we can take that same code once again, and now instead of doing flip coin. Let's dice roll dice Roll and we'll work off of a six-sided die because we're we're not playing D&D or any random board game But okay, well die Does that equal three in our case? We're working off of the three idea here and just to kind of take a look at dice roll It's gonna generate a random number from one to however many sides plus one reason why is because Randint would go up to six but not include six so we are just explicitly going through that and then just to sort of take a look at what the one divided by six is So we're roughly expecting a one a 0.16 as Our probability. Okay, so we take it we run our trials We don't need that anymore, so I'll just get rid of it and Okay, let's take a look at what our probability is of rolling a three Roughly speaking 14. Okay, not terrible run it again 14 Run it again 14. Maybe I don't need that Yeah, I did my apologies. So I didn't need the the plus one there That was on me my apologies, but as you can see Now to roll a three. We can effectively see we are going to get roughly speaking 16% each time we can even go even further What if I wanted to do something like ask these types of questions if I rolled five dice is one one one one One more random than six three one five five Well, they're actually the same because each die roll is independent of each other And so you could play some mathematical operations of looking at them But one of the things that we could really look at is something like Pascal So Pascal funny enough is the creator of probability because of gambling. That's great the entire idea is Pierre de Fermant was Getting super pissed that he was losing in gambling and so he Hit up his buddy Pascal and effectively asked, you know, why am I losing all this money in this game? Given 24 rolls bet against there being one with two sixes So again Pascal built the probability of doing just that And so just to see that without doing the 24 rolls kind of thing We can see a few different things. What's the probability of rolling one six in two rolls? Okay, that's one in 36 and we're not probability class where we're not statistics where computer programming So we're just kind of going through sort of the iterations And so not rolling one six in two rolls In 35 so now not rolling six in 24 rolls is roughly speaking 35 over 36 to the 24th power or roughly speaking 0.5 so Again, we could Build this. Okay, so we have 24 rolls that we need to do and we need to roll two sixes So let's take our probability and let's again use our dice roll We're gonna still keep this as a hundred trials. We'll do this 100,000 times so the first thing we're going to do is we're going to keep an inner loop track So let's see. So we'll call this count equals zero while count is Less than 24 24 rolls. Okay, so count will plus equal one So in this case roll two dice Roll two dice Die one die two. So just to kind of reiterate what they're looking for Given 24 rolls bet against there being one with two sixes So I roll two sixes and I'm basically saying that out of these rolls two sixes will not happen. Okay, so if die one equals six and Die two equals six Total is going to increment by one Now this is where we get into the fact that we now can effectively quit this Iteration because if we think about sort of what's going on here if I roll two sixes in any of that 24 simulation It's done right. Oh six has happened. Don't need to check anymore. So if that occurs We are going to break out of our Secondary loop. This is not going to exit out of the entire looping structure. Just our innermost loop and so the last little bit here will do is I is Going to increment by one Okay, so we run this That's gonna take some time because we're doing 100,000 times 24 so it ran and you know Sometimes that happens, but we take our probability roughly speaking you can see oh look at that Roughly speaking if we did the Pascal Pierre de fermont Gamble we have a 50% chance in our 24s and again, we're again working off of this is one iteration of it and Then we're running through it a few different times to say let me just Confirm that that one time I did this wasn't a fluke. So again if we ran through and just let's see Now I'm just going to do a little bit of printing and this is mostly just to add some visualizations for our sake or our sake if I modulo 10,000 or 10,000 is equal to zero effectively every time we hit 10,000 10,000 iterations prints Iteration I Like to do this when I have to deal with Long simulations because again it just tells me how far off I am so in this case we run it again iteration zero ten thousand three thousand or forty thousand fifty so it's going through and it Finished up because we never technically Hit 100,000 or we never do the 100,000 comparison. That's perfectly fine, but Once again, we can see that that probability is very close to our theoretical Model of roughly 50% so some really fun things that we can do inside of Python using something known as Monte Carlo