## **One Proportion Test**
**Tests:** One sample proportion against a known/hypothesized population proportion
### **When to Use:**
- Comparing **one group** to a known standard or benchmark
- Testing if a **single proportion** differs from an expected value
### **Formula:**
```python
z = (p̂ - p₀) / √[p₀(1-p₀)/n]
```
Where:
- p̂ = sample proportion
- p₀ = hypothesized population proportion
- n = sample size
## **Two Proportion Test**
**Tests:** Difference between proportions from two independent groups
### **When to Use:**
- Comparing **two different groups** to each other
- Testing if proportions differ between two populations
### **Formula:**
```python
z = (p̂₁ - p̂₂) / √[p̂_pool(1-p̂_pool)(1/n₁ + 1/n₂)]
```
Where:
- p̂_pool = (x₁ + x₂)/(n₁ + n₂)
---
## **Decision Guide:**
```python
def choose_test():
"""Simple decision guide"""
print("ASK YOURSELF: How many groups am I comparing?")
print()
print("š ONE PROPORTION TEST:")
print(" Q: Is my SINGLE group different from a known standard?")
print(" → Use when: Comparing to historical data/benchmark")
print()
print("š TWO PROPORTION TEST:")
print(" Q: Are these TWO GROUPS different from each other?")
print(" → Use when: Comparing Group A vs Group B")
choose_test()
```
---
## **Real-World Examples:**
### **Example 1: One Proportion Test**
```python
# Scenario: Company Quality Claim
# "We deliver 95% of packages on time"
# Sample: 180 out of 200 packages delivered on time
# Question: "Does our actual performance match the 95% claim?"
# → ONE PROPORTION TEST (one group vs known standard)
from statsmodels.stats.proportion import proportions_ztest
# One proportion test
z_stat, p_value = proportions_ztest(count=180, nobs=200, value=0.95, alternative='two-sided')
print(f"One Proportion Test: z={z_stat:.3f}, p={p_value:.4f}")
```
### **Example 2: Two Proportion Test**
```python
# Scenario: Drug Effectiveness
# Drug A: 45 successes out of 50 patients
# Drug B: 35 successes out of 50 patients
# Question: "Is Drug A more effective than Drug B?"
# → TWO PROPORTION TEST (comparing two groups)
z_stat, p_value = proportions_ztest(count=[45, 35], nobs=[50, 50], value=0, alternative='larger')
print(f"Two Proportion Test: z={z_stat:.3f}, p={p_value:.4f}")
```
---
## **Detailed Comparison Table:**
| Aspect | One Proportion Test | Two Proportion Test |
|--------|---------------------|---------------------|
| **Groups Compared** | One sample vs known value | Two independent samples |
| **Research Question** | "Does our rate equal X%?" | "Are these two rates different?" |
| **Null Hypothesis** | H₀: p = p₀ | H₀: p₁ = p₂ |
| **Data Required** | p̂, n, p₀ | p̂₁, n₁, p̂₂, n₂ |
| **Common Use Cases** | Quality control, claim verification | A/B testing, treatment comparisons |
---
## **Medical Examples:**
### **One Proportion (Medical):**
```python
# Hospital Infection Rates
# National standard: Infection rate should be ≤ 2%
# Our hospital: 8 infections in 300 patients (2.67%)
# Question: "Does our hospital meet the national standard?"
# → ONE PROPORTION TEST
print("ONE PROPORTION TEST - Hospital Quality")
print("H₀: Our infection rate ≤ 2% (meets standard)")
print("H₁: Our infection rate > 2% (exceeds standard)")
z_stat, p_value = proportions_ztest(count=8, nobs=300, value=0.02, alternative='larger')
```
### **Two Proportion (Medical):**
```python
# Smoking by Gender
# Males: 40 smokers out of 150
# Females: 20 smokers out of 100
# Question: "Do smoking rates differ by gender?"
# → TWO PROPORTION TEST
print("TWO PROPORTION TEST - Smoking by Gender")
print("H₀: p_male = p_female (no difference)")
print("H₁: p_male ≠ p_female (rates differ)")
z_stat, p_value = proportions_ztest(count=[40, 20], nobs=[150, 100], value=0, alternative='two-sided')
```
---
## **Business Examples:**
### **One Proportion (Business):**
```python
# E-commerce Conversion Rate
# Industry benchmark: 3% conversion rate
# Our site: 45 conversions from 1200 visitors (3.75%)
# Question: "Is our conversion rate better than industry average?"
# → ONE PROPORTION TEST
z_stat, p_value = proportions_ztest(count=45, nobs=1200, value=0.03, alternative='larger')
```
### **Two Proportion (Business):**
```python
# Marketing Campaign A/B Test
# Version A: 120 clicks from 2000 impressions (6%)
# Version B: 90 clicks from 2000 impressions (4.5%)
# Question: "Which ad version performs better?"
# → TWO PROPORTION TEST
z_stat, p_value = proportions_ztest(count=[120, 90], nobs=[2000, 2000], value=0, alternative='larger')
```
---
## **Key Questions to Determine Which Test:**
### **Ask These Questions:**
#### **For One Proportion Test:**
1. "Am I comparing **one group** to a **known standard**?"
2. "Do I have a **historical benchmark** to compare against?"
3. "Is there a **target value** I'm trying to achieve?"
4. "Am I testing a **claim** about a single population?"
#### **For Two Proportion Test:**
1. "Am I comparing **two different groups**?"
2. "Do I want to know if **Group A differs from Group B**?"
3. "Am I running an **A/B test** or **treatment comparison**?"
4. "Are these **independent samples** from different populations?"
---
## **Complete Decision Framework:**
```python
def proportion_test_selector():
"""Interactive test selector"""
print("PROPORTION TEST SELECTOR")
print("=" * 40)
questions = [
"How many groups are you analyzing? (1/2)",
"Do you have a known benchmark to compare against? (yes/no)",
"Are you comparing two different treatments/conditions? (yes/no)",
"Is this quality control against a standard? (yes/no)",
"Are you testing if two groups differ from each other? (yes/no)"
]
print("\nAnswer these questions:")
for i, question in enumerate(questions, 1):
print(f"{i}. {question}")
print("\nšÆ QUICK DECISION GUIDE:")
print("• Known standard + One group → ONE PROPORTION TEST")
print("• Two groups comparison → TWO PROPORTION TEST")
print("• Quality control → ONE PROPORTION TEST")
print("• A/B testing → TWO PROPORTION TEST")
proportion_test_selector()
```
---
## **When to Use Each - Summary:**
### **✅ Use ONE PROPORTION TEST when:**
- Testing against **industry standards**
- **Quality control** checks
- Verifying **company claims**
- Comparing to **historical data**
- **Regulatory compliance** testing
### **✅ Use TWO PROPORTION TEST when:**
- **A/B testing** (website versions, ads, etc.)
- **Treatment comparisons** (drug A vs drug B)
- **Demographic comparisons** (male vs female, young vs old)
- **Geographic comparisons** (Region A vs Region B)
- **Time period comparisons** (before vs after campaign)
---
## **Statistical Note:**
```python
# Both tests rely on these assumptions:
assumptions = {
'random_sampling': 'Data collected through random sampling',
'independence': 'Observations are independent',
'sample_size': 'np ≥ 10 and n(1-p) ≥ 10 for each group',
'normal_approximation': 'Sample size large enough for normal approximation'
}
```
## **Bottom Line:**
**Choose One Proportion Test when comparing to a known standard. Choose Two Proportion Test when comparing two groups to each other.**
The key distinction is whether you have an **external benchmark** (one proportion) or are making an **internal comparison** (two proportions)!