使用 t 检验进行 A/B 测试

问题描述 投票:0回答:1

我正在运行一些模拟来测试对当前模型的更改,我们将这些更改称为新算法。 我的模型预测两条可能路线之间的特定交易的路线。成功率定义为总成功率/总交易数。 下面的数据框有两列旧的和新的。旧的有通过旧算法 14 天的每日成功率,新的有根据新算法的 14 天的每日成功率,

Q1。我想得出新算法是否比旧算法更好的结论,我可以只比较 14 天的平均值并进行比较,但我想运行一些统计措施。我已经编写了下面的代码,但如果我交换新旧列,它仍然会产生相同的 p 值。我基本上想得出新的比旧的更好的结论,但我认为这个测试告诉我两种算法的结果彼此显着不同。需要一些帮助才能得出结论

Q2。我可以说出我的结果(新旧差异)所在的置信区间吗?

import pandas as pd
from scipy import stats

data = pd.DataFrame({
    'old': [74.9254,73.7721,73.6018,68.6855,63.4666,63.9204,70.6977,62.6488,67.8088,70.2274,71.1197,64.8925,73.1113,70.7065],  # Replace with your old algorithm results
    'new': [74.8419,73.7548,73.6677,68.9352,63.8387,64.1143,70.9533,62.6026,67.9586,70.7,71.1263,65.1053,72.9996,70.5899],
})

# Perform a paired t-test
t_statistic, p_value = stats.ttest_rel(data['new'], data['old'])

# Define your significance level (alpha)
alpha = 0.05

# Print the t-statistic and p-value
print(f"Paired t-statistic: {t_statistic}")
print(f"P-value: {p_value}")

# Compare p-value to the significance level
if p_value < alpha:
    print("Reject the null hypothesis. The new algorithm is performing significantly better.")
else:
    print("Fail to reject the null hypothesis. There is no significant difference between the algorithms.")
python pandas statistics simulation ab-testing
1个回答
0
投票

您已经正确地确定应该使用配对 t 检验进行此比较,因为您正在比较两个相关组。我将按顺序回答您的问题:

Q1:交换“新”和“旧”时获得相同 p 值的原因是因为 p 值测试了均值相同的原假设。 t 统计量将更改符号,但 p 值保持不变。在您的情况下重要的是 t 统计量的符号。如果为正,则表明新算法的均值高于旧算法。如果为负数,则表明旧算法的均值高于新算法。

所以,这样修改决策逻辑:

if p_value < alpha:
    if t_statistic > 0:
        print("Reject the null hypothesis. The new algorithm is performing significantly better.")
    else:
        print("Reject the null hypothesis. The old algorithm is performing significantly better.")
else:
    print("Fail to reject the null hypothesis. There is no significant difference between the algorithms.")

Q2:是的,您可以计算差异的置信区间。例如:

# Compute the difference and its standard error
difference = data['new'] - data['old']
se = difference.std() / (len(difference)**0.5)

# Compute the confidence interval
ci_low = difference.mean() - (se * stats.t.ppf(1 - (alpha / 2), len(difference) - 1))
ci_high = difference.mean() + (se * stats.t.ppf(1 - (alpha / 2), len(difference) - 1))

print(f"Confidence Interval (95%): ({ci_low:.4f}, {ci_high:.4f})")

在本例中,您计算了 95% 置信区间,对应于 0.05 的 alpha 水平。如果此区间不包含 0,则提供了拒绝原假设的进一步证据(类似于 p 值检验)。此外,该区间为您提供了两种算法均值之间差异的一系列合理值。

© www.soinside.com 2019 - 2024. All rights reserved.