R' s rbeta()函数的python等效是什么?
我正在尝试找到相当于R的rbeta()
功能的Python,以进行一些A/B测试。这是r
中的代码:
a <- 50
notA <- 200
b <- 200
notb <-400
trials <- 100000
alpha <- 1
beta <- 4
a.samples <- rbeta(trials, a+alpha, notA+beta)
b.samples <- rbeta(trials, b+alpha, notB+beta)
a_wins <- sum(a.samples > b.samples) / trials
b_wins <- sum(b.samples > a.samples) / trials
print(a_wins)
print(b_wins)
在此r
示例中, b 赢得了99%以上的时间。
这就是我作为Python等效的尝试:
import scipy.stats as stats
a = 50
notA = 200
b = 200
notB =400
trials = 100000
alpha = 1
beta = 4
# Random data on which to calculate probability density
inputs = []
inputs = stats.beta(alpha, beta).rvs(size=trials)
aSamples = stats.beta(a+alpha, notA+beta).pdf(inputs)
bSamples = stats.beta(b+alpha, notB+beta).pdf(inputs)
aWins = sum(aSamples > bSamples) / trials
bWins = sum(bSamples > aSamples) / trials
print("A", aWins)
print("B", bWins)
在Python等效中, a 赢得了75%的时间。
我的猜测是问题是输入
,即计算概率密度的随机统计信息。它们在此处使用.rvs()
scipy.stats.beta
的方法生成。我还尝试了随机数模块和np.linspace()
无用。有人可以告诉我我想念什么吗?
@user20650在评论中解决了它。调用.pdf()
是一个冗余步骤。函数scipy.stats.beta()。rvs()
是r rbeta()
函数的python。然后应阅读正确的代码:
aSamples = stats.beta(a+alpha, notA+beta).rvs(trials)
bSamples = stats.beta(b+alpha, notB+beta).rvs(trials)
感谢所有回答的人。昨天整天都融化了我的头。
I'm trying to find the Python equivalent of R's rbeta()
function in order to do some A/B testing. This is the code in R
:
a <- 50
notA <- 200
b <- 200
notb <-400
trials <- 100000
alpha <- 1
beta <- 4
a.samples <- rbeta(trials, a+alpha, notA+beta)
b.samples <- rbeta(trials, b+alpha, notB+beta)
a_wins <- sum(a.samples > b.samples) / trials
b_wins <- sum(b.samples > a.samples) / trials
print(a_wins)
print(b_wins)
In this R
example, B wins more than 99% of the time.
This is what I'm trying as the Python equivalent:
import scipy.stats as stats
a = 50
notA = 200
b = 200
notB =400
trials = 100000
alpha = 1
beta = 4
# Random data on which to calculate probability density
inputs = []
inputs = stats.beta(alpha, beta).rvs(size=trials)
aSamples = stats.beta(a+alpha, notA+beta).pdf(inputs)
bSamples = stats.beta(b+alpha, notB+beta).pdf(inputs)
aWins = sum(aSamples > bSamples) / trials
bWins = sum(bSamples > aSamples) / trials
print("A", aWins)
print("B", bWins)
In the Python equivalent, A wins 75% of the time.
My guess is that the problem arises with inputs
, the random stats on which the probability density is calculated. They're generated here with the .rvs()
method of scipy.stats.beta
. I've also tried the random number module and np.linspace()
to no avail. Can someone tell me what I'm missing?
@user20650 solved it in the comments. Calling .pdf()
is a redundant step. The function scipy.stats.beta().rvs()
is the Python equivalent of R's rbeta()
function. The correct code then should read:
aSamples = stats.beta(a+alpha, notA+beta).rvs(trials)
bSamples = stats.beta(b+alpha, notB+beta).rvs(trials)
Thank you everyone who replied. This was melting my head all day yesterday.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论