Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Implement a function to perform a one-sample Z-test for a population mean when the population standard deviation is known. Your function must support both one-tailed and two-tailed alternatives.

Implement a function with the signature:
- one_sample_z_test(sample_mean, population_mean, population_std, n, alternative="two-sided")

Where:
- sample_mean: The observed sample mean (float)
- population_mean: The hypothesized population mean under H0 (float)
- population_std: The known population standard deviation (float > 0)
- n: Sample size (int > 0)
- alternative: One of {"two-sided", "greater", "less"}

Return a dictionary with:
- "z": the computed Z statistic rounded to 4 decimals
- "p_value": the corresponding p-value rounded to 4 decimals

Use the standard normal distribution for the p-value. Handle invalid inputs minimally by assuming valid types and values.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "sample_mean=103.0, population_mean=100.0, population_std=15.0, n=36, alternative='greater'",
"output": "{'z': 1.2, 'p_value': 0.1151}",
"reasoning": "Standard error = 15/sqrt(36)=2.5. Z=(103-100)/2.5=1.2. For a 'greater' test, p=1-CDF(1.2)=0.1151."
}

20 changes: 20 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
A one-sample Z-test assesses whether the mean of a population differs from a hypothesized value when the population standard deviation is known. It is appropriate for large samples (by CLT) or when normality is assumed and the population standard deviation is known.

Test statistic:
- z = (x̄ − μ0) / (σ / √n)
- x̄: sample mean
- μ0: hypothesized mean under H0
- σ: known population standard deviation
- n: sample size

P-value computation uses the standard normal distribution:
- Two-sided (H1: μ ≠ μ0): p = 2 · min(Φ(z), 1 − Φ(z))
- Right-tailed (H1: μ > μ0): p = 1 − Φ(z)
- Left-tailed (H1: μ < μ0): p = Φ(z)

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

Notes:
- If σ is unknown, use a one-sample t-test with the sample standard deviation instead.

13 changes: 13 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": "186",
"title": "One-Sample Z-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" }
]
}

40 changes: 40 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from math import erf, sqrt

def _standard_normal_cdf(x):
return 0.5 * (1.0 + erf(x / sqrt(2.0)))

def one_sample_z_test(sample_mean, population_mean, population_std, n, alternative="two-sided"):
"""
Perform a one-sample Z-test for a population mean with known population std.

Parameters
----------
sample_mean : float
population_mean : float
population_std : float
n : int
alternative : str
One of {"two-sided", "greater", "less"}

Returns
-------
dict with keys:
- "z": Z-statistic rounded to 4 decimals
- "p_value": p-value rounded to 4 decimals
"""
standard_error = population_std / sqrt(n)
z = (sample_mean - population_mean) / standard_error
cdf = _standard_normal_cdf(z)

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:
# Fallback to two-sided if unexpected input
p = 2.0 * min(cdf, 1.0 - cdf)

return {"z": round(z, 4), "p_value": round(p, 4)}

33 changes: 33 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/starter_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from math import erf, sqrt

def _standard_normal_cdf(x):
return 0.5 * (1.0 + erf(x / sqrt(2.0)))

def one_sample_z_test(sample_mean, population_mean, population_std, n, alternative="two-sided"):
"""
Perform a one-sample Z-test for a population mean with known population std.

Parameters
----------
sample_mean : float
population_mean : float
population_std : float
n : int
alternative : str
One of {"two-sided", "greater", "less"}

Returns
-------
dict with keys:
- "z": Z-statistic rounded to 4 decimals
- "p_value": p-value rounded to 4 decimals
"""
# TODO: Implement the Z statistic and p-value computation
# z = (sample_mean - population_mean) / (population_std / sqrt(n))
# Use _standard_normal_cdf for CDF of standard normal.
# For alternative:
# - "two-sided": p = 2 * min(P(Z<=z), P(Z>=z)) = 2 * min(cdf(z), 1-cdf(z))
# - "greater": p = 1 - cdf(z)
# - "less": p = cdf(z)
return {"z": 0.0, "p_value": 1.0}

27 changes: 27 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"test": "one_sample_z_test(103.0, 100.0, 15.0, 36, alternative='two-sided')",
"expected_output": "{'z': 1.2, 'p_value': 0.2296}"
},
{
"test": "one_sample_z_test(103.0, 100.0, 15.0, 36, alternative='greater')",
"expected_output": "{'z': 1.2, 'p_value': 0.1151}"
},
{
"test": "one_sample_z_test(103.0, 100.0, 15.0, 36, alternative='less')",
"expected_output": "{'z': 1.2, 'p_value': 0.8849}"
},
{
"test": "one_sample_z_test(97.0, 100.0, 10.0, 25, alternative='two-sided')",
"expected_output": "{'z': -1.5, 'p_value': 0.1336}"
},
{
"test": "one_sample_z_test(97.0, 100.0, 10.0, 25, alternative='less')",
"expected_output": "{'z': -1.5, 'p_value': 0.0668}"
},
{
"test": "one_sample_z_test(97.0, 100.0, 10.0, 25, alternative='greater')",
"expected_output": "{'z': -1.5, 'p_value': 0.9332}"
}
]

14 changes: 0 additions & 14 deletions questions/187_mlops-etl-pipeline/description.md

This file was deleted.

5 changes: 0 additions & 5 deletions questions/187_mlops-etl-pipeline/example.json

This file was deleted.

24 changes: 0 additions & 24 deletions questions/187_mlops-etl-pipeline/learn.md

This file was deleted.

43 changes: 0 additions & 43 deletions questions/187_mlops-etl-pipeline/solution.py

This file was deleted.

9 changes: 0 additions & 9 deletions questions/187_mlops-etl-pipeline/starter_code.py

This file was deleted.

14 changes: 0 additions & 14 deletions questions/187_mlops-etl-pipeline/tests.json

This file was deleted.

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.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"id": "187",
"title": "Build a Simple ETL Pipeline (MLOps)",
"difficulty": "medium",
"category": "MLOps",
"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" }
]
}

Loading