def test_mean_diff_with_pop_variance(n1: int, mean1: float, pop_variance1: float, n2: int, mean2: float, pop_variance2: float, significance: float) -> (bool, float): """ 母分散既知において平均値の差に関するz検定を行う. ## Parameters `n1`: 1つめの標本の大きさ `mean1`: 1つめの標本の標本平均 `pop_variance1`: 1つめの標本の既知の母分散 `n2`: 2つめの標本の大きさ `mean2`: 2つめの標本の標本平均 `pop_variance2`: 2つめの標本の既知の母分散 `significance`: 有意水準 ## Returns `is_reject`: 帰無仮説が棄却されるかどうか `z`: 実現値 """ z = (mean1 - mean2) / math.sqrt(pop_variance1 / n1 + pop_variance2 / n2) bottom = stats.norm.ppf(significance / 2) top = stats.norm.ppf(1 - significance / 2) side = "double" is_reject = stats_test.is_reject(z, stats_test.side_from_str(side), bottom, top, None, None) return (is_reject, z)
def test_var_ratio(n1: int, sample_variance1: float, n2: int, sample_variance2: float, significance: float) -> (bool, float): """ 分散比に関するF検定を行う. ## Parameters `n1`: 1つめの標本の大きさ `sample_variance1`: 1つめの標本の既知の母分散 `n2`: 2つめの標本の大きさ `sample_variance2`: 2つめの標本の既知の母分散 `significance`: 有意水準 ## Returns `is_reject`: 帰無仮説が棄却されるかどうか `z`: 実現値 """ f = n1 * (n2 - 1) * sample_variance1 / (n2 * (n1 - 1) * sample_variance2) df1 = n1 - 1 df2 = n2 - 1 bottom = stats.f.ppf(significance / 2, df1, df2) top = stats.f.ppf(1 - significance / 2, df1, df2) side = "double" is_reject = stats_test.is_reject(f, stats_test.side_from_str(side), bottom, top, None, None) return (is_reject, f)
def test_mean_diff_without_pop_variance(n1: int, mean1: float, variance1: float, n2: int, mean2: float, variance2: float, significance: float) -> (bool, float): """ 平均値の差に関するt検定を行う. ## Parameters `n1`: 1つめの標本の大きさ `mean1`: 1つめの標本の標本平均 `variance1`: 1つめの標本の標本分散 `n2`: 2つめの標本の大きさ `mean2`: 2つめの標本の標本平均 `variance2`: 2つめの標本の標本分散 `significance`: 有意水準 ## Returns `is_reject`: 帰無仮説が棄却されるかどうか `t`: 実現値 """ df = n1 + n2 - 2 u = (n1 * variance1 + n2 * variance2) / df t = (mean1 - mean2) / math.sqrt(u * (1 / n1 + 1 / n2)) bottom = stats.t.ppf(significance / 2, df) top = stats.t.ppf(1 - significance / 2, df) side = "double" is_reject = stats_test.is_reject(t, stats_test.side_from_str(side), bottom, top, None, None) return (is_reject, t)
def cmd(z_test: bool, n: int, hypothesis: float, mean: float, variance: float, stdev: float, level: float, side: str): if level <= 0 or level >= 1: import sys print("信頼係数は(0, 1)の範囲で指定してください.", file=sys.stderr) exit() if stdev <= 0: stdev = math.sqrt(variance) if z_test: (is_reject, test_stat) = test_mean_with_pop_variance( n, hypothesis, mean, stdev, level, stats_test.side_from_str(side)) else: (is_reject, test_stat) = test_mean_without_pop_variance( n, hypothesis, mean, stdev, level, stats_test.side_from_str(side)) print(stats_test.show_result(is_reject, test_stat, "μ", hypothesis))
def test_stats_test_mean_with_pop_var1(self): n = 30 hypothesis = 60 mean = 56.75 pop_stdev = 15 level = 0.05 side = "double" (is_reject, test_stat) = stats_test_mean.test_mean_with_pop_variance( n, hypothesis, mean, pop_stdev, level, stats_test.side_from_str(side)) self.assertFalse(is_reject) self.assertAlmostEqual(test_stat, -1.187, places=3)
def test_stats_test_mean_without_pop_var1(self): n = 10 hypothesis = 12 mean = 12.36 stdev = math.sqrt(0.910) level = 0.05 side = "double" (is_reject, test_stat) = stats_test_mean.test_mean_without_pop_variance( n, hypothesis, mean, stdev, level, stats_test.side_from_str(side)) self.assertFalse(is_reject) self.assertAlmostEqual(test_stat, 1.132, places=3)
def test_stats_test_mean_with_pop_var2(self): n = 50 hypothesis = 30 mean = 28.1 pop_stdev = math.sqrt(60) level = 0.05 side = "left" (is_reject, test_stat) = stats_test_mean.test_mean_with_pop_variance( n, hypothesis, mean, pop_stdev, level, stats_test.side_from_str(side)) self.assertTrue(is_reject) self.assertAlmostEqual(test_stat, -1.734, places=3)
def test_no_correl(n: int, r: float, significance: float) -> (bool, float): """ 無相関かどうかのt検定を行う. ## Parameters `n`: 標本の大きさ `r`: 標本相関係数 `significance`: 有意水準 ## Returns `is_reject`: 帰無仮説が棄却されるかどうか `t`: 実現値 """ df = n - 2 t = r * math.sqrt(df) / math.sqrt(1 - math.pow(r, 2)) bottom = stats.t.ppf(significance / 2, df) top = stats.t.ppf(1 - significance / 2, df) side = stats_test.side_from_str("double") is_reject = stats_test.is_reject(t, side, bottom, top, None, None) return (is_reject, t)
def test_variance(n: int, target_variance: float, sample_variance: float, significance: float) -> (bool, float): """ 分散に関するカイ2乗検定を行う. ## Parameters `n`: 標本の大きさ `target_variance`: 帰無仮説において等しいと仮定する分散 `sample_variance`: 標本分散 `significance`: 有意水準 ## Returns `is_reject`: 帰無仮説が棄却されるかどうか `x`: 実現値 """ x = n * sample_variance / target_variance df = n - 1 bottom = stats.chi2.ppf(significance / 2, df) top = stats.chi2.ppf(1 - significance / 2, df) side = "double" is_reject = stats_test.is_reject(x, stats_test.side_from_str(side), bottom, top, None, None) return (is_reject, x)
def test_correl_diff(n1: int, n2: int, r1: float, r2: float, significance: float) -> (bool, float): """ 相関係数に関するz検定を行う. ## Parameters `n`: 標本の大きさ `target_r`: 帰無仮説で等しいと仮定する相関係数 `r`: 標本相関係数 `significance`: 有意水準 ## Returns `is_reject`: 帰無仮説が棄却されるかどうか `z`: 実現値 """ x1 = math.log((1 + r1) / (1 - r1)) / 2.0 x2 = math.log((1 + r2) / (1 - r2)) / 2.0 z = (x1 - x2) / math.sqrt(1 / (n1 - 3) + 1 / (n2 - 3)) bottom = stats.norm.ppf(significance / 2) top = stats.norm.ppf(1 - significance / 2) side = stats_test.side_from_str("double") is_reject = stats_test.is_reject(z, side, bottom, top, None, None) return (is_reject, z)
def test_correl(n: int, target_r: float, r: float, significance: float) -> (bool, float): """ 相関係数に関するz検定を行う. ## Parameters `n`: 標本の大きさ `target_r`: 帰無仮説で等しいと仮定する相関係数 `r`: 標本相関係数 `significance`: 有意水準 ## Returns `is_reject`: 帰無仮説が棄却されるかどうか `z`: 実現値 """ x = math.log((1 + r) / (1 - r)) / 2.0 xi = math.log((1 + target_r) / (1 - target_r)) / 2.0 z = (x - xi) * math.sqrt(n - 3) bottom = stats.norm.ppf(significance / 2) top = stats.norm.ppf(1 - significance / 2) side = stats_test.side_from_str("double") is_reject = stats_test.is_reject(z, side, bottom, top, None, None) return (is_reject, z)