从 bayesAB 包的 bayesTest 函数手动计算后验预期损失
Calculate posterior expected loss manually from bayesTest function of bayesAB package
当我在一些模拟数据上使用该函数时
library(bayesAB)
set.seed(255)
# simulate data
A <- rbinom(250, 1, .25)
B <- rbinom(250, 1, .2)
# apply the function for AB group comparison
AB_test <- bayesTest(A,
B,
priors = c('alpha' = 65, 'beta' = 200),
n_samples = 1e5,
distribution = 'bernoulli')
# obtain the output
summary(AB_test)
我得到以下输出
# Quantiles of posteriors for A and B:
#
# $Probability
# $Probability$A
# 0% 25% 50% 75% 100%
# 0.1775006 0.2469845 0.2598399 0.2730324 0.3506919
#
# $Probability$B
# 0% 25% 50% 75% 100%
# 0.1510354 0.2146442 0.2268472 0.2394675 0.3182802
#
#
# --------------------------------------------
#
# P(A > B) by (0)%:
#
# $Probability
# [1] 0.89305
#
# --------------------------------------------
#
# Credible Interval on (A - B) / B for interval length(s) (0.9) :
#
# $Probability
# 5% 95%
# -0.04278263 0.37454069
#
# --------------------------------------------
#
# Posterior Expected Loss for choosing A over B:
#
# $Probability
# [1] 0.00587424
我知道如何使用后验数据手动获取前 3 个部分
quantile(AB_test$posteriors$Probability$A, c(0, 0.25, 0.50, 0.75, 1))
# 0% 25% 50% 75% 100%
# 0.1775006 0.2469845 0.2598399 0.2730324 0.3506919
quantile(AB_test$posteriors$Probability$B, c(0, 0.25, 0.50, 0.75, 1))
# 0% 25% 50% 75% 100%
# 0.1510354 0.2146442 0.2268472 0.2394675 0.3182802
mean(AB_test$posteriors$Probability$A > AB_test$posteriors$Probability$B)
# [1] 0.89305
quantile(AB_test$posteriors$Probability$A / AB_test$posteriors$Probability$B - 1, c(0.05, 0.95))
# 5% 95%
# -0.04278263 0.37454069
但我不确定如何计算输出最后一部分中显示的后验预期损失。
这可能吗?
您可以通过查看摘要的创建方式来弄清楚这一点,即控制台中的 运行 bayesAB:::summary.bayesTest
。我自己做这个我发现:
> lapply(
+ AB_test$posteriors,
+ function(x) do.call(bayesAB:::getPostError, unname(x))
+ )
$Probability
[1] 0.00587424
控制台中的 运行 bayesAB:::getPostError
准确显示了计算的工作原理:
> bayesAB:::getPostError
function (A_samples, B_samples, f = function(a, b) (a - b)/b)
{
BoverA <- B_samples > A_samples
loss <- f(B_samples, A_samples)
coalesce(mean(BoverA) * mean(loss[BoverA]))
}
<bytecode: 0x0000017fe9329040>
<environment: namespace:bayesAB>
当我在一些模拟数据上使用该函数时
library(bayesAB)
set.seed(255)
# simulate data
A <- rbinom(250, 1, .25)
B <- rbinom(250, 1, .2)
# apply the function for AB group comparison
AB_test <- bayesTest(A,
B,
priors = c('alpha' = 65, 'beta' = 200),
n_samples = 1e5,
distribution = 'bernoulli')
# obtain the output
summary(AB_test)
我得到以下输出
# Quantiles of posteriors for A and B:
#
# $Probability
# $Probability$A
# 0% 25% 50% 75% 100%
# 0.1775006 0.2469845 0.2598399 0.2730324 0.3506919
#
# $Probability$B
# 0% 25% 50% 75% 100%
# 0.1510354 0.2146442 0.2268472 0.2394675 0.3182802
#
#
# --------------------------------------------
#
# P(A > B) by (0)%:
#
# $Probability
# [1] 0.89305
#
# --------------------------------------------
#
# Credible Interval on (A - B) / B for interval length(s) (0.9) :
#
# $Probability
# 5% 95%
# -0.04278263 0.37454069
#
# --------------------------------------------
#
# Posterior Expected Loss for choosing A over B:
#
# $Probability
# [1] 0.00587424
我知道如何使用后验数据手动获取前 3 个部分
quantile(AB_test$posteriors$Probability$A, c(0, 0.25, 0.50, 0.75, 1))
# 0% 25% 50% 75% 100%
# 0.1775006 0.2469845 0.2598399 0.2730324 0.3506919
quantile(AB_test$posteriors$Probability$B, c(0, 0.25, 0.50, 0.75, 1))
# 0% 25% 50% 75% 100%
# 0.1510354 0.2146442 0.2268472 0.2394675 0.3182802
mean(AB_test$posteriors$Probability$A > AB_test$posteriors$Probability$B)
# [1] 0.89305
quantile(AB_test$posteriors$Probability$A / AB_test$posteriors$Probability$B - 1, c(0.05, 0.95))
# 5% 95%
# -0.04278263 0.37454069
但我不确定如何计算输出最后一部分中显示的后验预期损失。 这可能吗?
您可以通过查看摘要的创建方式来弄清楚这一点,即控制台中的 运行 bayesAB:::summary.bayesTest
。我自己做这个我发现:
> lapply(
+ AB_test$posteriors,
+ function(x) do.call(bayesAB:::getPostError, unname(x))
+ )
$Probability
[1] 0.00587424
控制台中的 运行 bayesAB:::getPostError
准确显示了计算的工作原理:
> bayesAB:::getPostError
function (A_samples, B_samples, f = function(a, b) (a - b)/b)
{
BoverA <- B_samples > A_samples
loss <- f(B_samples, A_samples)
coalesce(mean(BoverA) * mean(loss[BoverA]))
}
<bytecode: 0x0000017fe9329040>
<environment: namespace:bayesAB>