 Hi. In this video, we're going to go through how to do a hypothesis test for a single mean in Python using a randomization distribution. And we're going to continue on with our record example from the 2021 Texas power crisis. And we're going to focus on wind here. The reason why we're focusing on wind is because, well, if you're looking at this visualization that we left off in the last video. You know, it's kind of on, you know, it goes up and down around its forecast peak capacity. Natural gas is consistently below it. So the more questionable case here is, is wind, whether or not. Whether or not the mean power generation, the curve here is above or below the dash line, the forecast peak capacity. Okay. So let's get into how we would execute the code to perform this hypothesis test. So recall from last time we left off with Jen Piv and it's organized as such we've got date time as one column fuel as another generation as another here. So we're going to ask ourselves in this hypothesis test is whether the mean generation from when was consistently or statistically significantly below the forecasted peak capacity of 6.1 gigawatts. Now we could just calculate the mean with the solid curve and see whether it's above or below this dash curve. So if it was how much below it, or above it, would it need to be to be deemed significantly different than 6.1. If the actual mean was only say 6.01 is that statistically significantly below 6.1. If it was five, is that statistically significantly below it. So that's the real question here is, is how extreme was the actual power generation there. So if we were to do a hypothesis test for a single mean using a randomization distribution, we have to compute this shifted x sample. And so we're going to do that before we right before we do our randomization distribution step two here. The very first thing we need to do is calculate our sample statistic. So, since we're focusing on wind let's pull out our wind sample here from Jen Piv, just using a simple filter. We've got fuel equals wind. So now all of our wind generation values are just going to be in wind here. Actually, this will contain our three columns and then if we want to just have the generation values you can say wind. And then from wind we'll get our generation. And so that'll be our, those will be all the wind values. Our sample statistic though is the mean. So we want to then compute the mean of that. Okay, now there's other ways to do this we could have just done this in one line and filtered and only gotten the generation but but it doesn't hurt to do it multiple steps either. So, let's see what our mean wind is so 5.15 gigawatts. Well it's certainly less than 6.1. The question is, well is that extremely below it. Is it enough below it to say that wind, unequivocally fail as a power source during the Texas Colts net. So that's what we're going to answer with our hypothesis test here. So let's move on to the randomization distribution we need to import from NumPy. We need to import our NumPy package. And first thing we're going to do is do this shift. Now the whole point of doing this calculate the shifted wind speed is to see. Well what would our wind samples have looked like. If they were meeting the forecast to be capacity. In other words, what would the data look like if it were meeting the milk condition, if the actual mean was 6.1. What would that look like. Okay, so wind shift is basically the generation that we might have seen. Had or no condition to net. Okay. And it's preserving the very the same amount of variability that we actually saw in the real data. So how we do this is we take our actual wind sample from the data set. And then we're going to shift it. So we're going to add on the amount between our no value. So 6.1. So whatever is on the right hand side of the equal sign in our null hypothesis. Okay, 6.1. And we're going to subtract our wind mean value here. So the difference between the null and the actual mean. We're going to add that on to our wind samples here. Okay. So that's our wind shift. Next, we want to dictate how many randomizations we want to do. So capital N equals 1000. So we'll do 1000 randomization samples. And they're all going to have the same sample size as wind shift. So our little end is going to be the length of wind shift. Wind shift here is a series and we can use this link function. So it's going to have the same sample size as our original data set. That's important. And then we need an object in which to store our randomization sample statistics. So we'll use this empty function. Empty is just going to create an empty series in this case. It's going to be of size N. So it's going to basically have 1000 blank spaces here. And as we go through our for loop. Let's use J as our index here as we go through our for loop, we're going to fill in that blank series. So ran mean the J place. So we go through our for loop, the J spot in this empty vector or empty series. We're going to fill with something and that something is going to be a randomized sample. So we're going to use this random choice function. So NP random choice random choice is just going to randomly draw values from whatever we provide as the first argument here. So it's going to randomly draw values from wind shift. And we need to tell it how many values to draw. So the size should be equal to end. So it needs to be the same size as the original data set for a randomization distribution to hold true. And we need to do this with replacement. That is, we draw one value out of wind shift. And then we put it back in that same value can be drawn again. So we replace the value. Now this will, this alone will create a new sample of size N. But we want the statistic of that. So on this we can calculate our mean. So we'll have our new sample and we'll calculate the mean from it. So we just have one value and we'll store it in that single blank spot dictated by this J value. So we'll put it in the J spot of this empty random object here. Okay. And so that's our randomization distribution and go ahead and run that. We want to inspect this. Let's visualize it. Let's take our ran mean, convert it to a data frame so we can visualize it in GG plot. Take our ran mean series. And we'll just give it a column name of means here. So then in GG plot, we can take a look at this as say for example a histogram. And for the histogram, we just need to supply, tell it what what to use for X values in this case the means. So you take a look at that. So here's our histogram of the randomization statistics here. So this is our randomization distribution, all the different 1000 different mean values from scrambling up one shift. So this is the sort of variability that we'd expect to see in the sample statistic. If the null hypothesis were true. So if, if that forecast be capacity was actually being met to a statistically significant degree, we'd expect our mean values to fluctuate in this range here. Okay. That's great. Let's see how this compares to what we actually have so where does our 5.15 lie within this. And here we can visualize this with a vertical line. And so our X intercept is going to be mean. This is our sample statistic. Let's give it a color red, and let's make it dashed. Here we go. So this is our actual sample statistics. So this is the mean of our actual data and we see that it's well below the randomization distribution. What does this mean means that what we saw in reality is extremely different from what we should have seen if the null hypothesis were true. So we can also state the same result as a p value, or the probability of observing a value at least as extreme as this, given the assumption that the null hypothesis was true. Here we see that there's very little if no chance of seeing any part of the randomization distribution below this dash line. Let's write this out. We would then encounter calculate our p value here. I'll put it in a couch it in a print statement, although we could store it as a value if we wanted to. So our p value equals p value is going to be the number of our randomization distribution values that are at least as extreme as our dashed line. So we can write that as a length of the ran mean values that meet some criteria and that criteria is ran mean being less than or equal to our sample statistic. And so that'll be the number of the samples that are that are less than at least as extreme as when mean we want the proportion so we'll divide this by the big end. So there's a thousand values in here by that. Now the reason why we do less than is because that's the same as our alternative hypothesis. The mean is less than 6.1. So we need to make that agree down here we're calculating the p value has to be less than the reason why we include the equals to is because the definition of p value is that the probability of observing value at least as extreme. So at least so at least would include the equals sign. Alright, so let's go ahead and run this and we see that our p value is zero that there is zero chance of any element of our randomization distribution falling below our actual sample statistic. So, in other words, our, the data do not support the conclusion that the forecast capacity was met. So we can write this as a concluding statement. Always good to in the concluding statement. Okay, what the hot know what the p value is so we can say our p value is zero point zero if we want to be a little bit more precise about it. Because none of the randomization distribution falls below the sample statistic mean when right. So. So therefore, we can reject the null hypothesis. And we can say with null hypothesis we can reject null hypothesis that the four pasted peak capacity was met. But the key language here is that we reject the null hypothesis when we put that in bold. And the reason why we can do that is because our p value of zero is, well, it's less than point or five. We can make that clear we can say p value is less than 0.05. And we can, it's always nice to then put this into the context of the problem. We can say the average. Sorry, the average wind power generated during the Texas. Cold snap was statistically significantly less than the forecasted peak capacity. So here we have statistic proof that that wind also failed that we didn't meet the criteria here and I say also fail because well if the data for wind. We failed on null hypothesis, then most certainly that for natural gas would also fail since it is considerably below its forecasted peak capacity here. Okay. All right. So that concludes our test for a single me.