Step 1: Took 5 packets of gems.¶
- Each packet had 9 gems
- Resisted from eating them (this was the hardest part)

In [76]:
import pandas as pd
from scipy.stats import chi2,chisquare
Step 2: Arrange the colors¶
- Sorted and counted how many of each color appeared
- This gives us the observed data from our sample

In [77]:
observed_gems = pd.DataFrame([{
"Blue": 8,
"Light Pink": 9,
"Green": 5,
"Purple": 6,
"Yellow": 2,
"Dark Pink": 7,
"Orange": 8
}])
In [78]:
observed_gems
Out[78]:
Blue | Light Pink | Green | Purple | Yellow | Dark Pink | Orange | |
---|---|---|---|---|---|---|---|
0 | 8 | 9 | 5 | 6 | 2 | 7 | 8 |
In [79]:
observed_gems = observed_gems.transpose()
In [80]:
observed_gems
Out[80]:
0 | |
---|---|
Blue | 8 |
Light Pink | 9 |
Green | 5 |
Purple | 6 |
Yellow | 2 |
Dark Pink | 7 |
Orange | 8 |
In [82]:
observed_gems.columns = ["Observed"]
observed_gems
Out[82]:
Observed | |
---|---|
Blue | 8 |
Light Pink | 9 |
Green | 5 |
Purple | 6 |
Yellow | 2 |
Dark Pink | 7 |
Orange | 8 |
Step 3: Stating My Hypothesis¶
- H0 (Null Hypothesis): All colors are equally likely
- H1 (Alternative Hypothesis): At least one color is not equally likely

In [83]:
#Uniform distribution
obs_sum = observed_gems["Observed"].sum()
obs_count = observed_gems["Observed"].count()
In [84]:
# Since uniform distribution
expected_gems = pd.DataFrame([{
"Blue": obs_sum/obs_count ,
"Light Pink": obs_sum/obs_count ,
"Green": obs_sum/obs_count ,
"Purple": obs_sum/obs_count ,
"Yellow": obs_sum/obs_count ,
"Dark Pink": obs_sum/obs_count ,
"Orange": obs_sum/obs_count
}])
In [85]:
expected_gems
Out[85]:
Blue | Light Pink | Green | Purple | Yellow | Dark Pink | Orange | |
---|---|---|---|---|---|---|---|
0 | 6.428571 | 6.428571 | 6.428571 | 6.428571 | 6.428571 | 6.428571 | 6.428571 |
In [86]:
expected_gems=expected_gems.transpose()
In [87]:
expected_gems = expected_gems.rename(columns={0: "Expected"})
expected_gems
Out[87]:
Expected | |
---|---|
Blue | 6.428571 |
Light Pink | 6.428571 |
Green | 6.428571 |
Purple | 6.428571 |
Yellow | 6.428571 |
Dark Pink | 6.428571 |
Orange | 6.428571 |
Step 4: Calculate Chi2¶
- Found the expected frequency for each color assuming equal probability
- Compared observed vs expected values
- Calculated:
- Chi-Square Statistic

In [67]:
calc_table = pd.merge(observed_gems, expected_gems, left_index=True, right_index=True)
In [68]:
calc_table
Out[68]:
Observed | Expected | |
---|---|---|
Blue | 8 | 6.428571 |
Light Pink | 9 | 6.428571 |
Green | 5 | 6.428571 |
Purple | 6 | 6.428571 |
Yellow | 2 | 6.428571 |
Dark Pink | 7 | 6.428571 |
Orange | 8 | 6.428571 |
In [71]:
ChiSq_calc,P_value_calc = chisquare(f_obs=calc_table["Observed"], f_exp=calc_table["Expected"])
In [72]:
ChiSq_calc,P_value_calc
Out[72]:
(5.2444444444444445, 0.5128652198163051)
In [73]:
alpha = 0.05
dof = len(observed_gems) - 1
ChiSq_critical = chi2.ppf(1-alpha, df=dof)
print(ChiSq_critical)
12.591587243743977
Step 5: Reject / Fail to Reject the Null Hypothesis¶
- Calculated:
- Degrees of Freedom
- Critical value
- p-value
Chi-Square Statistic Method¶
- ChiSq_calc > ChiSq_critical: Reject the null hypothesis
→ The result is unlikely to occur if H₀ is true. - ChiSq_calc ≤ ChiSq_critical: Fail to reject the null hypothesis
→ The result is not unusual enough to reject H₀.
p-value Method¶
- p_value < α: Reject the null hypothesis
→ The probability of getting this kind of result is too low assuming H₀ is true. - p_value ≥ α: Fail to reject the null hypothesis
→ The probability of getting this result is high enough that it doesn't seem unusual.

In [74]:
if ChiSq_calc > ChiSq_critical:
print("Reject the null hypothesis")
else:
print("Fail to reject the null hypothesis")
Fail to reject the null hypothesis
In [ ]:
if P_value_calc < alpha:
print("Reject the null hypothesis")
else:
print("Fail to reject the null hypothesis")
Fail to reject the null hypothesis
Conclusion¶
- In this sample, the Chi-Square value was not large enough to reject the null hypothesis.
- So, we fail to reject H₀ — meaning we don't have strong evidence that the gem colors are unequally distributed.
- It doesn't prove that all colors are perfectly equal in the entire population — just that based on this small sample of 5 packets (45 gems), we can't say otherwise.
In [ ]: