import pytest
import numpy as np
import scirs2
class TestLeveneTest:
def test_levene_basic_two_groups(self):
g1 = np.array([8.88, 9.12, 9.04, 8.98, 9.00])
g2 = np.array([8.88, 8.95, 9.29, 9.44, 9.15])
result = scirs2.levene_py(g1, g2)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_levene_three_groups(self):
g1 = np.array([8.88, 9.12, 9.04, 8.98, 9.00])
g2 = np.array([8.88, 8.95, 9.29, 9.44, 9.15])
g3 = np.array([8.95, 9.12, 8.95, 8.85, 9.03])
result = scirs2.levene_py(g1, g2, g3)
assert "statistic" in result
assert "pvalue" in result
def test_levene_center_mean(self):
g1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
g2 = np.array([2.0, 3.0, 4.0, 5.0, 6.0])
result = scirs2.levene_py(g1, g2, center="mean")
assert "statistic" in result
assert "pvalue" in result
def test_levene_center_median(self):
g1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
g2 = np.array([2.0, 3.0, 4.0, 5.0, 6.0])
result = scirs2.levene_py(g1, g2, center="median")
assert "statistic" in result
assert "pvalue" in result
def test_levene_center_trimmed(self):
g1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
g2 = np.array([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0])
result = scirs2.levene_py(g1, g2, center="trimmed", proportion_to_cut=0.1)
assert "statistic" in result
assert "pvalue" in result
def test_levene_equal_variances(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 50)
g2 = np.random.normal(1, 1, 50)
result = scirs2.levene_py(g1, g2)
assert result["pvalue"] > 0.05
def test_levene_different_variances(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 50)
g2 = np.random.normal(0, 5, 50)
result = scirs2.levene_py(g1, g2)
assert result["pvalue"] < 0.05
def test_levene_four_groups(self):
g1 = np.array([1.0, 2.0, 3.0, 4.0])
g2 = np.array([2.0, 3.0, 4.0, 5.0])
g3 = np.array([3.0, 4.0, 5.0, 6.0])
g4 = np.array([4.0, 5.0, 6.0, 7.0])
result = scirs2.levene_py(g1, g2, g3, g4)
assert "statistic" in result
assert "pvalue" in result
class TestBartlettTest:
def test_bartlett_basic_two_groups(self):
g1 = np.array([8.88, 9.12, 9.04, 8.98, 9.00])
g2 = np.array([8.88, 8.95, 9.29, 9.44, 9.15])
result = scirs2.bartlett_test_py(g1, g2)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_bartlett_three_groups(self):
g1 = np.array([8.88, 9.12, 9.04, 8.98, 9.00])
g2 = np.array([8.88, 8.95, 9.29, 9.44, 9.15])
g3 = np.array([8.95, 9.12, 8.95, 8.85, 9.03])
result = scirs2.bartlett_test_py(g1, g2, g3)
assert "statistic" in result
assert "pvalue" in result
def test_bartlett_equal_variances(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 50)
g2 = np.random.normal(1, 1, 50)
result = scirs2.bartlett_test_py(g1, g2)
assert result["pvalue"] > 0.05
def test_bartlett_different_variances(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 50)
g2 = np.random.normal(0, 5, 50)
result = scirs2.bartlett_test_py(g1, g2)
assert result["pvalue"] < 0.05
def test_bartlett_multiple_groups(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 30)
g2 = np.random.normal(0, 1, 30)
g3 = np.random.normal(0, 1, 30)
g4 = np.random.normal(0, 1, 30)
result = scirs2.bartlett_test_py(g1, g2, g3, g4)
assert "statistic" in result
assert "pvalue" in result
assert result["pvalue"] > 0.05
def test_bartlett_different_sample_sizes(self):
g1 = np.array([1.0, 2.0, 3.0])
g2 = np.array([2.0, 3.0, 4.0, 5.0, 6.0])
g3 = np.array([3.0, 4.0])
result = scirs2.bartlett_test_py(g1, g2, g3)
assert "statistic" in result
assert "pvalue" in result
class TestBrownForsytheTest:
def test_brown_forsythe_basic_two_groups(self):
g1 = np.array([8.88, 9.12, 9.04, 8.98, 9.00])
g2 = np.array([8.88, 8.95, 9.29, 9.44, 9.15])
result = scirs2.brown_forsythe_py(g1, g2)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_brown_forsythe_three_groups(self):
g1 = np.array([8.88, 9.12, 9.04, 8.98, 9.00])
g2 = np.array([8.88, 8.95, 9.29, 9.44, 9.15])
g3 = np.array([8.95, 9.12, 8.95, 8.85, 9.03])
result = scirs2.brown_forsythe_py(g1, g2, g3)
assert "statistic" in result
assert "pvalue" in result
def test_brown_forsythe_equal_variances(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 50)
g2 = np.random.normal(1, 1, 50)
result = scirs2.brown_forsythe_py(g1, g2)
assert result["pvalue"] > 0.05
def test_brown_forsythe_different_variances(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 50)
g2 = np.random.normal(0, 5, 50)
result = scirs2.brown_forsythe_py(g1, g2)
assert result["pvalue"] < 0.05
def test_brown_forsythe_robust_to_outliers(self):
g1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
g2 = np.array([1.0, 2.0, 3.0, 4.0, 100.0])
result = scirs2.brown_forsythe_py(g1, g2)
assert "statistic" in result
assert "pvalue" in result
class TestHomogeneityEdgeCases:
def test_levene_insufficient_groups(self):
g1 = np.array([1.0, 2.0, 3.0])
with pytest.raises(RuntimeError, match="Need at least 2 groups"):
scirs2.levene_py(g1)
def test_bartlett_insufficient_groups(self):
g1 = np.array([1.0, 2.0, 3.0])
with pytest.raises(RuntimeError, match="Need at least 2 groups"):
scirs2.bartlett_test_py(g1)
def test_brown_forsythe_insufficient_groups(self):
g1 = np.array([1.0, 2.0, 3.0])
with pytest.raises(RuntimeError, match="Need at least 2 groups"):
scirs2.brown_forsythe_py(g1)
def test_levene_with_small_samples(self):
g1 = np.array([1.0, 2.0])
g2 = np.array([3.0, 4.0])
result = scirs2.levene_py(g1, g2)
assert "statistic" in result
assert "pvalue" in result
def test_bartlett_with_small_samples(self):
g1 = np.array([1.0, 2.0])
g2 = np.array([3.0, 4.0])
result = scirs2.bartlett_test_py(g1, g2)
assert "statistic" in result
assert "pvalue" in result
class TestHomogeneityConsistency:
def test_brown_forsythe_equals_levene_median(self):
g1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
g2 = np.array([2.0, 3.0, 4.0, 5.0, 6.0])
g3 = np.array([3.0, 4.0, 5.0, 6.0, 7.0])
bf_result = scirs2.brown_forsythe_py(g1, g2, g3)
lev_result = scirs2.levene_py(g1, g2, g3, center="median")
assert abs(bf_result["statistic"] - lev_result["statistic"]) < 1e-6
assert abs(bf_result["pvalue"] - lev_result["pvalue"]) < 1e-6
def test_homogeneity_order_invariance(self):
g1 = np.array([1.0, 2.0, 3.0])
g2 = np.array([4.0, 5.0, 6.0])
g3 = np.array([7.0, 8.0, 9.0])
lev1 = scirs2.levene_py(g1, g2, g3)
lev2 = scirs2.levene_py(g3, g1, g2)
lev3 = scirs2.levene_py(g2, g3, g1)
assert abs(lev1["statistic"] - lev2["statistic"]) < 1e-6
assert abs(lev1["statistic"] - lev3["statistic"]) < 1e-6
assert abs(lev1["pvalue"] - lev2["pvalue"]) < 1e-6
assert abs(lev1["pvalue"] - lev3["pvalue"]) < 1e-6
bar1 = scirs2.bartlett_test_py(g1, g2, g3)
bar2 = scirs2.bartlett_test_py(g3, g1, g2)
bar3 = scirs2.bartlett_test_py(g2, g3, g1)
assert abs(bar1["statistic"] - bar2["statistic"]) < 1e-6
assert abs(bar1["statistic"] - bar3["statistic"]) < 1e-6
assert abs(bar1["pvalue"] - bar2["pvalue"]) < 1e-6
assert abs(bar1["pvalue"] - bar3["pvalue"]) < 1e-6
class TestHomogeneityComparison:
def test_bartlett_vs_levene_normal_data(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 100)
g2 = np.random.normal(0, 1.5, 100)
bartlett_result = scirs2.bartlett_test_py(g1, g2)
levene_result = scirs2.levene_py(g1, g2)
assert bartlett_result["pvalue"] < 0.05
assert levene_result["pvalue"] < 0.05
def test_levene_centers_comparison(self):
g1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
g2 = np.array([2.0, 3.0, 4.0, 5.0, 6.0])
mean_result = scirs2.levene_py(g1, g2, center="mean")
median_result = scirs2.levene_py(g1, g2, center="median")
assert "statistic" in mean_result
assert "statistic" in median_result
assert "pvalue" in mean_result
assert "pvalue" in median_result