EME 210
Data Analytics for Energy Systems

Single Mean One-Line Test

PrintPrint

Single Mean One-Line Test

Read It: Single Mean One-Line Test

To conduct the one-line test for a single mean hypothesis test, you will use a one sample t-test. Below, we demonstrate how to implement this code.

 Watch It: Video - Hypothesis Test for Single Mean (5:45 minutes)

Click here for a transcript.

All right, in this series of videos we're going to shift back into hypothesis testing. And in particular, we're going to focus on the one-line tests that you can do to do all of the hypothesis testing that we learned in the previous lesson, but in just a single line. And while a lot of times we'll still want to do the randomization procedures, these one-line tests can be a good way to to check your answer. Because although they may be slightly different, a lot of times they should actually follow the same pattern as your randomization distribution.

So, let's go ahead and jump right in. So, we're here in the code for hypothesis testing. And in particular, I'm going to focus on the hypothesis test for a single mean in this video. And so, when we write our hypotheses, we say that mu equals in this case 6.1, the capacity. And then our alternative is that mu is less than that. So, in essence, we're going to test whether or not wind actually met the capacity in, during the Texas 2021 cold snap. And so, in the previous lesson we went through these steps: we shifted the data, we initialized the variable, and we implemented this randomized random choice function where we set replace equal to true and focused on the shifted data. And then, eventually, we calculated the p-value as the proportion of data that was less than our original mean. And we got a p-value of zero, which leads us to reject the null hypothesis in favor of the alternative, that the capacity of wind, that the average wind was less than the capacity.

And so, this one-liner test, we're going to use a library, the stats library in Python. And in particular, this library is the stat SCI Pi stats, which is the same library that we used when we were writing our normal or working with the normal distributions in the previous videos. And so, in order to do the single mean hypothesis test, I'm going to put the results into a variable called results. And we say stats dot ttest underscore one - the number one - samp. So effectively, a one sample ttest. Then we give it our data, which is wind - which is the observed wind generation. We give it our population mean, which is what we expect. So this is our capacity in this case, or whatever value you have set in the null hypothesis. And then we give it the alternative. And in this case, we specify the alternative as less. Meaning that we are doing a test in which we're looking at less than, we're using a left-tailed test. And then to actually see the p-value we can just say results dot p-value at the end. And so, we can run this. And we can see that the p-value is still less than our significance level. It's very, very close to zero. But as what often happens in these one-liner tests, that is more specific. So, instead of just staying zero, we've got you know quite a few digits, but still very, very, very close to zero given the e to the negative seven. And this is a very common result when we're using these one-liners and comparing them to randomization procedures, because what this one-liner test is actually doing is conducting that same randomization procedure that we went over, but with a lot more data. And so, essentially, it does something very similar to this. And instead of being a thousand, is maybe a million, 10 million. It essentially generates a lot more iterations, which allow it to become a lot more specific in the results.

Credit: © Penn State is licensed under CC BY-NC-SA 4.0

Try It: Google Colab

  1. Click the Google Colab file used in the video here.
  2. Go to the Colab file and click "File" then "Save a copy in Drive", this will create a new Colab file that you can edit in your own Google Drive account.
  3. Once you have it saved in your Drive, try to edit the following code to run the single mean one-line test. Remember to import the libraries and run the code to create some data. 

Note: You must be logged into your PSU Google Workspace in order to access the file. 

# Libraries
import numpy as np
import scipy.stats as stats

# create some data
x = np.random.randint(0,100,1000)

# run one-line test
results = ...
results.pvalue

Once you have implemented this code on your own, come back to this page to test your knowledge.


 Assess It: Check Your Knowledge

Knowledge Check 

FAQ