Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
added t-test question
  • Loading branch information
Jeet009 committed Nov 11, 2025
commit 98867db36a0d99847fa3b4bac7aa37b2fe9b8758
28 changes: 28 additions & 0 deletions questions/187_one-sample-t-test-hypothesis-testing/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Implement a function to perform a one-sample t-test for a population mean when the population standard deviation is unknown (use sample standard deviation). Your function must auto-detect whether the test is one-tailed or two-tailed by parsing the hypotheses.

Implement a function with the signature:
- one_sample_t_test(sample_mean, sample_std, n, H0, H1)

Where:
- sample_mean: Observed sample mean (float)
- sample_std: Sample standard deviation (float > 0, computed with ddof=1)
- n: Sample size (int > 1)
- H0: Null hypothesis as a string, e.g., "mu = 100"
- H1: Alternative hypothesis as a string, e.g., "mu > 100", "mu < 100", or "mu != 100"

Requirements:
- Extract the hypothesized mean μ0 from H0.
- Determine the tail from H1:
- "mu != μ0" → two-sided
- "mu > μ0" → right-tailed
- "mu < μ0" → left-tailed
- Compute the t-statistic: t = (x̄ − μ0) / (s / √n)
- Degrees of freedom: df = n − 1
- Compute the p-value using the Student's t distribution.

Return a dictionary with:
- "t": computed t-statistic rounded to 4 decimals
- "df": degrees of freedom (int)
- "p_value": p-value rounded to 4 decimals
- "alternative": one of {"two-sided", "greater", "less"}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "sample_mean=103.0, sample_std=10.0, n=25, H0='mu = 100', H1='mu > 100'",
"output": "{'t': 1.5, 'df': 24, 'p_value': 0.0735, 'alternative': 'greater'}",
"reasoning": "SE = 10/sqrt(25)=2.0. t=(103-100)/2=1.5. With df=24 and a right-tailed test, p=1-CDF_t(1.5;24)=0.0735 (approx)."
}

20 changes: 20 additions & 0 deletions questions/187_one-sample-t-test-hypothesis-testing/learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
A one-sample t-test assesses whether a population mean differs from a hypothesized value when the population standard deviation is unknown. It uses the sample standard deviation and Student's t distribution with df = n − 1.

Test statistic:
- t = (x̄ − μ0) / (s / √n)
- x̄: sample mean
- μ0: hypothesized mean under H0
- s: sample standard deviation (ddof = 1)
- n: sample size

Tail selection from hypotheses:
- If H1 is "mu != μ0", it's two-sided: p = 2 · min(Tcdf(t), 1 − Tcdf(t))
- If H1 is "mu > μ0", it's right-tailed: p = 1 − Tcdf(t)
- If H1 is "mu < μ0", it's left-tailed: p = Tcdf(t)

Decision rule at level α:
- Reject H0 if p ≤ α; otherwise, fail to reject H0.

When to use:
- Use the t-test when σ is unknown and the sample is reasonably normal or n is moderate/large.

13 changes: 13 additions & 0 deletions questions/187_one-sample-t-test-hypothesis-testing/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": "187",
"title": "One-Sample t-Test for Mean (One and Two-Tailed)",
"difficulty": "easy",
"category": "Statistics",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{ "profile_link": "https://github.com/Jeet009", "name": "Jeet Mukherjee" }
]
}

59 changes: 59 additions & 0 deletions questions/187_one-sample-t-test-hypothesis-testing/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from math import gamma, sqrt, pi

def _t_pdf(x, df):
c = gamma((df + 1) / 2.0) / (sqrt(df * pi) * gamma(df / 2.0))
return c * (1.0 + (x * x) / df) ** (-(df + 1) / 2.0)

def _t_cdf(x, df):
if x == 0.0:
return 0.5
sign = 1.0 if x > 0 else -1.0
a, b = 0.0, abs(x)
n = 4000 # even number of intervals; higher for better accuracy
h = (b - a) / n
s = _t_pdf(a, df) + _t_pdf(b, df)
for i in range(1, n):
xi = a + i * h
s += (4 if i % 2 == 1 else 2) * _t_pdf(xi, df)
integral = s * h / 3.0
return 0.5 + sign * integral

def _parse_hypotheses(H0, H1):
text = H0.replace(" ", "")
if "=" not in text:
raise ValueError("H0 must specify equality, e.g., 'mu = 100'")
mu0 = float(text.split("=")[1])
alt_text = H1.replace(" ", "").replace("<>", "!=").replace("≠", "!=")
if "!=" in alt_text:
alt = "two-sided"
elif ">" in alt_text:
alt = "greater"
elif "<" in alt_text:
alt = "less"
else:
alt = "two-sided"
return mu0, alt

def one_sample_t_test(sample_mean, sample_std, n, H0, H1):
"""
Perform a one-sample t-test for a population mean with unknown population std.
Auto-detect tail from H1 and extract μ0 from H0.
Returns dict with keys: "t", "df", "p_value", "alternative".
"""
mu0, alternative = _parse_hypotheses(H0, H1)
df = n - 1
se = sample_std / sqrt(n)
t = (sample_mean - mu0) / se
cdf = _t_cdf(t, df)

if alternative == "two-sided":
p = 2.0 * min(cdf, 1.0 - cdf)
elif alternative == "greater":
p = 1.0 - cdf
elif alternative == "less":
p = cdf
else:
p = 2.0 * min(cdf, 1.0 - cdf)

return {"t": round(t, 4), "df": df, "p_value": round(p, 4), "alternative": alternative}

55 changes: 55 additions & 0 deletions questions/187_one-sample-t-test-hypothesis-testing/starter_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from math import gamma, sqrt, pi

def _t_pdf(x, df):
c = gamma((df + 1) / 2.0) / (sqrt(df * pi) * gamma(df / 2.0))
return c * (1.0 + (x * x) / df) ** (-(df + 1) / 2.0)

def _t_cdf(x, df):
# Numerical integration using Simpson's rule, leveraging symmetry
if x == 0.0:
return 0.5
sign = 1.0 if x > 0 else -1.0
a, b = 0.0, abs(x)
n = 2000 # even number of intervals
h = (b - a) / n
s = _t_pdf(a, df) + _t_pdf(b, df)
for i in range(1, n):
xi = a + i * h
s += (4 if i % 2 == 1 else 2) * _t_pdf(xi, df)
integral = s * h / 3.0
return 0.5 + sign * integral

def _parse_hypotheses(H0, H1):
# Extract mu0 from H0 like "mu = 100" (allow spaces)
text = H0.replace(" ", "")
if "=" not in text:
raise ValueError("H0 must specify equality, e.g., 'mu = 100'")
mu0 = float(text.split("=")[1])
alt_text = H1.replace(" ", "").replace("<>", "!=").replace("≠", "!=")
if "!=" in alt_text:
alt = "two-sided"
elif ">" in alt_text:
alt = "greater"
elif "<" in alt_text:
alt = "less"
else:
alt = "two-sided"
return mu0, alt

def one_sample_t_test(sample_mean, sample_std, n, H0, H1):
"""
Perform a one-sample t-test for a population mean with unknown population std.
Auto-detect tail from H1 and extract μ0 from H0.
Returns dict with keys: "t", "df", "p_value", "alternative".
"""
# TODO: implement
# Steps:
# 1) mu0, alternative = _parse_hypotheses(H0, H1)
# 2) df = n - 1
# 3) t = (sample_mean - mu0) / (sample_std / sqrt(n))
# 4) p-value using _t_cdf:
# - two-sided: p = 2 * min(CDF(t), 1 - CDF(t))
# - greater: p = 1 - CDF(t)
# - less: p = CDF(t)
return {"t": 0.0, "df": n - 1, "p_value": 1.0, "alternative": "two-sided"}

27 changes: 27 additions & 0 deletions questions/187_one-sample-t-test-hypothesis-testing/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"test": "one_sample_t_test(103.0, 10.0, 25, 'mu = 100', 'mu != 100')",
"expected_output": "{'t': 1.5, 'df': 24, 'p_value': 0.1469, 'alternative': 'two-sided'}"
},
{
"test": "one_sample_t_test(103.0, 10.0, 25, 'mu = 100', 'mu > 100')",
"expected_output": "{'t': 1.5, 'df': 24, 'p_value': 0.0735, 'alternative': 'greater'}"
},
{
"test": "one_sample_t_test(103.0, 10.0, 25, 'mu = 100', 'mu < 100')",
"expected_output": "{'t': 1.5, 'df': 24, 'p_value': 0.9265, 'alternative': 'less'}"
},
{
"test": "one_sample_t_test(97.0, 12.0, 36, 'mu = 100', 'mu != 100')",
"expected_output": "{'t': -1.5, 'df': 35, 'p_value': 0.1423, 'alternative': 'two-sided'}"
},
{
"test": "one_sample_t_test(97.0, 12.0, 36, 'mu = 100', 'mu < 100')",
"expected_output": "{'t': -1.5, 'df': 35, 'p_value': 0.0712, 'alternative': 'less'}"
},
{
"test": "one_sample_t_test(97.0, 12.0, 36, 'mu = 100', 'mu > 100')",
"expected_output": "{'t': -1.5, 'df': 35, 'p_value': 0.9288, 'alternative': 'greater'}"
}
]