import numpy as np
## x contains the sample of Tesla Model S ranges, in miles
x = np.array([240, 241, 236, 240, 240])
## STEP 1: Calculate the sample statistic
## STEP 2: Randomization distribution
## STEP 3: Calculate p-value. Direction of inequality agrees with Ha above!!!
import numpy as np
## x contains the sample of Tesla Model S ranges, in miles
x = np.array([240, 241, 236, 240, 240])
## STEP 1: Calculate the sample statistic
xbar = x.mean()
print("Sample mean = ", xbar)
## STEP 2: Randomization distribution
x_shift = x + (240 - xbar) # shift the sample so that the mean is equal to the null hypothesis mean value
N = 1000 # number of bootstrap samples
n = len(x) # sample size
randmean = np.empty(N)
for j in range(N):
randmean[j] = np.random.choice(x_shift, size = n, replace = True).mean()
## STEP 3: Calculate p-value. Direction of inequality agrees with Ha above!!!
print("p-value =", len(randmean[randmean <= xbar])/N)