189. One-Sample Z-Test for Mean

Warning: Pipulation standard deviation, 𝜎, is known.

𝐻0:𝜇𝐻𝑎:𝜇
Example

Your favorite candy company claims their chocolate bars have a mean weight of 200 grams, and their population standard deviation is 5 grams. You’ve noticed that they’ve seemed lighter than usual. You take a simple random sample of 35 candy bars and find they have a mean weight of 198 grams. At 𝛼 = 0.05 test the company’s claim.

𝐻0:𝜇200𝐻𝑎:𝜇<200𝑧=𝑥̄𝜇𝜎/𝑛

Where:

  • 𝑥̄=198

  • 𝜇=200

  • 𝜎=5

  • 𝑛=35

  • 𝜎/𝑛=5/35=0.8452

𝑧=1982005/35=−2.3663𝑃(𝑧−2.3663)=0.009𝑧-value<critical-value−2.3663<−1.6449𝑝-value<𝛼-value0.009<0.05

Reject 𝐻0. At 𝛼=0.05 there is sufficient evidence that the mean weight of the chocolate bars is less than 200 grams.

Code
  import numpy as np
  from scipy.stats import norm

  mu = 200.0
  sigma = 5.0
  xbar = 198
  n = 35
  alpha = 0.05

  cv = norm.ppf(alpha)

  z = (xbar - mu) / (sigma / np.sqrt(n))
  p = norm.cdf(z)

  if z <= cv:
      print("Reject null hypothesis")
  else:
      print("Fail to reject null hypothesis")