R 在HydeNet中指定决策节点的自定义模型
我正在使用R 在HydeNet中指定决策节点的自定义模型,r,jags,bayesian-networks,R,Jags,Bayesian Networks,我正在使用HydeNet软件包构建贝叶斯决策网络: 市场节点和调查节点分别具有自定义概率和cpt性质。其思想是将发现节点定义为具有以下逻辑的决策节点:代理根据调查节点信息计算其效用,然后做出是否在市场上发现公司的决策。在这里,我分别计算了ExpUtility值,然后将它们粘贴到determ节点: # Found node (how to put down an optimization task on it?) founding <- data.frame(Found = as.fact
HydeNet
软件包构建贝叶斯决策网络:
市场节点和调查节点分别具有自定义概率和cpt性质。其思想是将发现节点定义为具有以下逻辑的决策节点:代理根据调查节点信息计算其效用,然后做出是否在市场上发现公司的决策。在这里,我分别计算了ExpUtility值,然后将它们粘贴到determ节点:
# Found node (how to put down an optimization task on it?)
founding <- data.frame(Found = as.factor(c(rep(0,3), rep(1,3))),
Survey = as.factor(rep(c(1,2,3),2)),
EV = as.numeric(c(NA,NA,NA,NA,NA,NA)))
expvalue = function(founding_option, survey_option){
df = outcomes %>%
left_join(market_status, by = "Market") %>%
inner_join(survey_cond, by = "Market") %>%
filter(Found == founding_option) %>%
filter(Survey == survey_option) %>%
mutate(expexted = as.numeric(as.character(Utility))*prob_real*prob_surv)
ev = sum(df$expexted)
return(ev)
}
for (i in 1:nrow(founding)) {
founding$EV[i] = expvalue(founding$Found[i], founding$Survey[i])
}
net <- setNode(net, Found, nodeType = "determ", define=fromFormula(),
nodeFormula = Found~ifelse(Survey == "1", 0, 1))
net <- setDecisionNodes(net, Found)
#找到节点(如何在其上放置优化任务?)
创始%
左加入(市场状态,由=“市场”)%>%
内部加入(调查条件,按=“市场”)%>%
过滤器(已找到==U选项)%>%
过滤器(调查==调查选项)%>%
mutate(expexted=as.numeric(as.character(Utility))*prob_real*prob_surv)
ev=总额(df$expexted)
返回(ev)
}
适用于(i/1:nrow(创始)){
创始$EV[i]=expvalue(创始$Found[i],创始$Survey[i])
}
net我不知道HydeNet,但使用Pyagram,您可以很容易地做到这一点:
infdiag=gum.fastd(“市场{poor | medium | perspective}->Survey{poor | medium | perspective}->*发现{nOK | OK}->$Utility[3]我真的不明白为什么P(Survey | Market)中的值是错误的:这个参数向我们展示了某些调查值的概率(s1、s2、s3)在每个市场状态下。每个市场状态的总和是1,所以它是正确的,不是吗?此外,感谢您的解决方案,但由于大量的软件包和分析,我更喜欢使用R。当然没问题,我只是想展示一个看起来更简单的解决方案(对我来说).关于你的参数,你是对的..我的错:我直接把这个列表0.6,0.3,0.1,0.3,0.4,0.4,0.1,0.1,0.3,0.5作为CPT…但是对于Pyagram,它应该被转置:0.6,0.3,0.1,0.3,0.4,0.3,0.1,0.5(这样更容易检查总和;-)。
library(dplyr)
library(HydeNet)
# 1 - poor, 2 - medium, 3 - perspective
market_status = data.frame(Market = as.factor(c(1,2,3)),
prob_real = as.numeric(c(0.5, 0.3, 0.2)))
# the probability of defining the market status
survey_cond = data.frame(Market = as.factor(rep(c(1,2,3),3)),
Survey = as.factor(c(rep(1,3), rep(2,3), rep(3,3))),
prob_surv = as.numeric(c(0.6, 0.3, 0.1, 0.3, 0.4, 0.4, 0.1, 0.3, 0.5)))
# final utilities
outcomes = data.frame(Found = as.factor(c(rep(0,3), rep(1,3))),
Market = as.factor(rep(c(1,2,3),2)),
Utility = as.factor(c(0,0,0,-7,5,20)))
net <- HydeNetwork(~ Utility | Found*Market
+ Found | Survey
+ Survey | Market)
plot(net)
# Market node
net <- setNode(net, Market, nodeType="dcat", pi=vectorProbs(p=market_status$prob_real, Market),
factorLevels = c("1","2","3"))
# Survey node
survey.cpt <- cpt(Survey ~ Market,
data = survey_cond,
wt = survey_cond$prob_surv)
net <- setNodeModels(net, survey.cpt)
# Found node (how to put down an optimization task on it?)
founding <- data.frame(Found = as.factor(c(rep(0,3), rep(1,3))),
Survey = as.factor(rep(c(1,2,3),2)),
EV = as.numeric(c(NA,NA,NA,NA,NA,NA)))
expvalue = function(founding_option, survey_option){
df = outcomes %>%
left_join(market_status, by = "Market") %>%
inner_join(survey_cond, by = "Market") %>%
filter(Found == founding_option) %>%
filter(Survey == survey_option) %>%
mutate(expexted = as.numeric(as.character(Utility))*prob_real*prob_surv)
ev = sum(df$expexted)
return(ev)
}
for (i in 1:nrow(founding)) {
founding$EV[i] = expvalue(founding$Found[i], founding$Survey[i])
}
net <- setNode(net, Found, nodeType = "determ", define=fromFormula(),
nodeFormula = Found~ifelse(Survey == "1", 0, 1))
net <- setDecisionNodes(net, Found)
# Utility node
net <- setNode(net, Utility, "determ", define=fromFormula(),
nodeFormula = Utility ~ ifelse(Found == 0, 0,
ifelse(Market == 1, -7,
ifelse(Market == 2, 5, 20))))
net <- setUtilityNodes(net, Utility)
plot(net)
trackedVars <- c("Market","Survey","Found","Utility")
evidence <- NULL
compiledNet <- compileJagsModel(net, data = evidence,
n.chains = 3,
n.adapt = 5000)
post <- HydeSim(compiledNet,
variable.names = trackedVars,
n.iter=10000)
dplyr::sample_n(post, 20)
table(post$Utility)
mean(post$Utility)
|| Found |
Survey||nOK |OK |
------||---------|---------|
poor || 1.0000 | 0.0000 |
medium|| 0.0000 | 1.0000 |
perspe|| 0.0000 | 1.0000 |