如何解决在 HydeNet 中指定决策节点的自定义模型
我正在使用 HydeNet
包构建贝叶斯决策网络:
Market 节点和 Survey 节点分别具有自定义概率和 cpt 性质。这个想法是将Found节点定义为一个Decision节点,逻辑如下:agent根据Survey节点信息计算他的效用,然后决定是否在市场上找到一家公司。在这里,我分别计算了 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,2)),EV = as.numeric(c(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",1))
net <- setDecisionNodes(net,Found)
如何在节点中编写 ExpUtility 最大化过程?
upd:ExpValue 的公式为:
所以如果创建的 ExpUtility 大于 0(这是定义上没有创建的效用),则代理决定创建一家公司。
完整代码:
library(dplyr)
library(HydeNet)
# 1 - poor,2 - medium,3 - perspective
market_status = data.frame(Market = as.factor(c(1,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,Survey = as.factor(c(rep(1,rep(2,rep(3,prob_surv = as.numeric(c(0.6,0.1,0.4,0.5)))
# final utilities
outcomes = data.frame(Found = as.factor(c(rep(0,Market = as.factor(rep(c(1,Utility = as.factor(c(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,Found)
# Utility node
net <- setNode(net,Utility,"determ",nodeFormula = Utility ~ ifelse(Found == 0,ifelse(Market == 1,ifelse(Market == 2,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)
解决方法
我不知道 HydeNet,但是使用 pyAgrum,你可以很容易地做到这一点:
infdiag=gum.fastID("Market{poor|medium|perspective}->Survey{poor|medium|perspective}->*Found{nOK|OK}->$Utility[3]<-Market")
infdiag.cpt("Market")[:]=[0.5,0.3,0.2]
infdiag.cpt("Survey")[{"Market":"poor"}]=[0.6,0.1]
infdiag.cpt("Survey")[{"Market":"medium"}]=[0.3,0.4,0.3]
infdiag.cpt("Survey")[{"Market":"perspective"}]=[0.1,0.6]
infdiag.utility("Utility").fillWith([0,-7,5,20])
ie=gum.ShaferShenoyLIMIDInference(infdiag)
ie.makeInference()
print(ie.optimalDecision("Found"))
对于这个结果:
|| Found |
Survey||nOK |OK |
------||---------|---------|
poor || 1.0000 | 0.0000 |
medium|| 0.0000 | 1.0000 |
perspe|| 0.0000 | 1.0000 |
PS- 为了获得正确的 CPT,我更改了 P(survey|Market) 中的一些值。我可以假设实用程序中的值也不是正确的值...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。