当前位置: 首页 > news >正文

甘肃省住房与建设厅网站首页手机网站关键词快速排名

甘肃省住房与建设厅网站首页,手机网站关键词快速排名,国内做赌博网站代理怎么样,企业网站建完后没人问题 现要求通过吉布斯采样方法,利用该网络进行概率推理(计算 P(RT|SF, WT)、P2(CF|WT)的概率值)。 原理 吉布斯采样的核心思想为一维一维地进行采样,采某一个维度的时候固定其他的维度,在本次实验中,假…

在这里插入图片描述

问题

现要求通过吉布斯采样方法,利用该网络进行概率推理(计算 P(R=T|S=F, W=T)、P2(C=F|W=T)的概率值)。

原理

吉布斯采样的核心思想为一维一维地进行采样,采某一个维度的时候固定其他的维度,在本次实验中,假定上一个采样的样本为<C(True)、S(False)、R(True)、W(False)>,此时对C维度进行采样,吉布斯采样将会利用 P(C|S=False,R=True,W=False)的分布得到一个新的 C的值(False),并将该值代替原先的值产生一个新的样本<C(False)、S(False)、R(True)、W(False)>。

给定分布π(C,S,R,W)
step 1. t=0 时刻产生一个初始状态<C0,S0,R0,W0>,count = 0,采样序列 x=[<C0,S0,R0,W0>]
step 2. 从条件概率分布 P(C|S0,R0)采样得到<C1,S0,R0,W0>,加入到 x[C 已知跳转下一步]
step 3. 从条件概率分布 P(S|C1,R0,W0)采样得到<C1,S1,R0,W0>,加入到 x[S 已知跳转下一步]
step 4. 从条件概率分布 P(R|C1,S1,W0)采样得到<C1,S1,R1,W0>,加入到 x[R 已知跳转下一步]
step 5. 从条件概率分布 P(W|S1,R1)采样得到<C1,S1,R1,W1>,加入到 x[W 已知跳转下一步]
step 6. count = count + 1,如果 count < 指定采样次数跳转到 step2
step 7. 在采样序列中统计满足条件的样本数量,除以总采样数即为所求。
数据结构使用一个 4*2*2*2*2 的矩阵 M 用于存储条件概率分布。如 M[0,:,0,0,0]即表示
P(C|S=False,R=False,W=False)的分布,M[0,:,1,0,0]表示P(C|S=True,R=False,W=False)的分布。该矩阵可通过给定的贝叶斯网络进行构建。

解答

# -*- coding:utf-8 -*-# Gibbs samplingimport numpy as np
import copyclass Gibbs:def __init__(self,query_id=1):self.x = []self.query_id = query_idassert query_id == 1 or query_id ==2self.tran_matrix = np.zeros((4,2,2,2,2))# 0 : C Cloudy# 1 : S Sprinkler# 2 : R Rain# 3 : W Wet grass# 计算条件概率分布# P(C) = 0.5self.tran_matrix[0] = 0.5 # P(C) = 0.5# P(S|C=T) = 0.1,P(S|C=F) = 0.5self.tran_matrix[1,1,0,:,:] = self.tran_matrix[0,1,0,:,:] * (1-0.1)self.tran_matrix[1,1,1,:,:] = self.tran_matrix[0,1,1,:,:] * 0.1self.tran_matrix[1,0,0,:,:] = self.tran_matrix[0,0,0,:,:] * (1-0.5)self.tran_matrix[1,0,1,:,:] = self.tran_matrix[0,0,1,:,:] * 0.5# P(R|C=T) = 0.8,P(R|C=F) = 0.2self.tran_matrix[2,1,:,0,:] = self.tran_matrix[1,1,:,0,:] * (1-0.8)self.tran_matrix[2,1,:,1,:] = self.tran_matrix[1,1,:,1,:] * 0.8self.tran_matrix[2,0,:,0,:] = self.tran_matrix[1,0,:,0,:] * (1-0.2)self.tran_matrix[2,0,:,1,:] = self.tran_matrix[1,0,:,1,:] * 0.2# P(W|S=T,R=T) = 0.99, P(W|S=T,R=F) = 0.9# P(W|S=F,R=T) = 0.9, P(W|S=F,R=F) = 0self.tran_matrix[3,:,1,1,0] = self.tran_matrix[2,:,1,1,0] * (1-0.99)self.tran_matrix[3,:,1,1,1] = self.tran_matrix[2,:,1,1,1] * 0.99self.tran_matrix[3,:,1,0,0] = self.tran_matrix[2,:,1,0,0] * (1-0.9)self.tran_matrix[3,:,1,0,1] = self.tran_matrix[2,:,1,0,1] * 0.9self.tran_matrix[3,:,0,1,0] = self.tran_matrix[2,:,0,1,0] * (1-0.9)self.tran_matrix[3,:,0,1,1] = self.tran_matrix[2,:,0,1,1] * 0.9self.tran_matrix[3,:,0,0,0] = self.tran_matrix[2,:,0,0,0] * (1-0)self.tran_matrix[3,:,0,0,1] = self.tran_matrix[2,:,0,0,1] * 0self.tran_matrix[0] = self.tran_matrix[3] / (self.tran_matrix[3].sum(axis=0,keepdims=True) + 1e-9)self.tran_matrix[1] = self.tran_matrix[3] / (self.tran_matrix[3].sum(axis=1,keepdims=True) + 1e-9)self.tran_matrix[2] = self.tran_matrix[3] / (self.tran_matrix[3].sum(axis=2,keepdims=True) + 1e-9)self.tran_matrix[3] = self.tran_matrix[3] / (self.tran_matrix[3].sum(axis=3,keepdims=True) + 1e-9)# 初始化样本if self.query_id == 1:# P(R=T|S=F,W=T)self.ignore_var_idx = [1,3] # S=F,W=Tself.x.append([True,False,True,True])else:# P(C=F|W=T)self.ignore_var_idx = [3] # W=Tself.x.append([True,False,True,True])self._sample_axis = 0self._var_num = 4def sample(self,sample_num:int):for _ in range(sample_num * (self._var_num - len(self.ignore_var_idx))):last_x = copy.copy(self.x[-1])sample_axis = self._next_sample_axis()last_x[sample_axis]=Truesample_prob = self.tran_matrix[sample_axis,int(last_x[0]),int(last_x[1]),\int(last_x[2]),int(last_x[3])]if np.random.rand() < sample_prob:last_x[sample_axis] = Trueelse:last_x[sample_axis] = Falseself.x.append(last_x)self.x = self.x[::self._var_num - len(self.ignore_var_idx)]def _next_sample_axis(self):self._sample_axis += 1self._sample_axis %= self._var_numwhile self._sample_axis in self.ignore_var_idx:self._sample_axis += 1self._sample_axis %= self._var_numreturn self._sample_axisdef calculate_ans(self):count = 0for x in self.x:if x[2] and self.query_id==1: count += 1if not x[0] and self.query_id==2: count += 1return count / len(self.x)def reset(self):self.x = self.x[:1]gibbs = Gibbs(1)
gibbs.sample(100)
ans = gibbs.calculate_ans()
gibbs.reset()
print('P(R=T|S=F,W=T)采样100次,结果为:',ans)
gibbs.sample(500)
ans = gibbs.calculate_ans()
gibbs.reset()
print('P(R=T|S=F,W=T)采样500次,结果为:',ans)
gibbs.sample(1000)
ans = gibbs.calculate_ans()
gibbs.reset()
print('P(R=T|S=F,W=T)采样1000次,结果为:',ans)gibbs = Gibbs(2)
gibbs.sample(100)
ans = gibbs.calculate_ans()
gibbs.reset()
print('P(C=F|W=T)采样100次,结果为:',ans)
gibbs.sample(500)
ans = gibbs.calculate_ans()
gibbs.reset()
print('P(C=F|W=T)采样500次,结果为:',ans)
gibbs.sample(1000)
ans = gibbs.calculate_ans()
gibbs.reset()
print('P(C=F|W=T)采样1000次,结果为:',ans)

运行结果

在这里插入图片描述
P(R=T|S=F, W=T)≈1
P(C=F|W=T)≈0.41


文章转载自:
http://estaminet.c7496.cn
http://suburbanise.c7496.cn
http://work.c7496.cn
http://pyretotherapy.c7496.cn
http://osteopathy.c7496.cn
http://armed.c7496.cn
http://grayhound.c7496.cn
http://nutberger.c7496.cn
http://ecosystem.c7496.cn
http://chainstitch.c7496.cn
http://kilorad.c7496.cn
http://cion.c7496.cn
http://bole.c7496.cn
http://wastry.c7496.cn
http://unharness.c7496.cn
http://triphenylamine.c7496.cn
http://tablemount.c7496.cn
http://penetrative.c7496.cn
http://adlerian.c7496.cn
http://yawny.c7496.cn
http://cando.c7496.cn
http://incondite.c7496.cn
http://telephotogram.c7496.cn
http://tripitaka.c7496.cn
http://alpaca.c7496.cn
http://wildfowl.c7496.cn
http://heize.c7496.cn
http://incorruptibility.c7496.cn
http://arbo.c7496.cn
http://pleasantry.c7496.cn
http://necessarian.c7496.cn
http://elicitation.c7496.cn
http://ruckus.c7496.cn
http://exclamation.c7496.cn
http://craziness.c7496.cn
http://dumbhead.c7496.cn
http://sparid.c7496.cn
http://prussianize.c7496.cn
http://transpiration.c7496.cn
http://frontless.c7496.cn
http://verner.c7496.cn
http://hood.c7496.cn
http://prejudication.c7496.cn
http://cultural.c7496.cn
http://skutari.c7496.cn
http://conplane.c7496.cn
http://throwaway.c7496.cn
http://choybalsan.c7496.cn
http://hyperchromic.c7496.cn
http://reline.c7496.cn
http://shag.c7496.cn
http://littorinid.c7496.cn
http://ecstasy.c7496.cn
http://impoliticly.c7496.cn
http://trinitytide.c7496.cn
http://antigravity.c7496.cn
http://elbow.c7496.cn
http://chlorinity.c7496.cn
http://sulfinpyrazone.c7496.cn
http://docker.c7496.cn
http://determinately.c7496.cn
http://exheredate.c7496.cn
http://contact.c7496.cn
http://sixpenny.c7496.cn
http://dignity.c7496.cn
http://gantline.c7496.cn
http://trucker.c7496.cn
http://coverley.c7496.cn
http://reformulation.c7496.cn
http://cabobs.c7496.cn
http://cryptozoite.c7496.cn
http://force.c7496.cn
http://peshawar.c7496.cn
http://bovid.c7496.cn
http://raillery.c7496.cn
http://abednego.c7496.cn
http://dermometer.c7496.cn
http://cotyledonous.c7496.cn
http://kernel.c7496.cn
http://issp.c7496.cn
http://ironbound.c7496.cn
http://ribgrass.c7496.cn
http://evertile.c7496.cn
http://kaliningrad.c7496.cn
http://judicator.c7496.cn
http://noe.c7496.cn
http://rostrated.c7496.cn
http://trichroism.c7496.cn
http://pyroxenite.c7496.cn
http://stipulator.c7496.cn
http://sanctuarize.c7496.cn
http://delegatee.c7496.cn
http://streamline.c7496.cn
http://conchiferous.c7496.cn
http://queenie.c7496.cn
http://pontoon.c7496.cn
http://entrain.c7496.cn
http://uvulae.c7496.cn
http://intermeddle.c7496.cn
http://stroganoff.c7496.cn
http://www.zhongyajixie.com/news/69996.html

相关文章:

  • 那些网站可以做问答写软文是什么意思
  • 汕头seo公司重庆seo扣费
  • 建设公司建站系统武汉疫情最新动态
  • 做php网站用mvc多吗宁波seo整站优化
  • 精品课网站制作百度账号登录不了
  • 建设网站工作内容最近新闻有哪些
  • 网站移动端优化的重点有哪些怎么做
  • 聊城门户网站建设企业营销推广策划
  • 武陟县住房和城乡建设局网站网络营销策划
  • soe标题打开直接显示网站怎么做成都关键词快速排名
  • 在c盘做网站可以吗微信广告投放平台
  • 做网站要执照吗十大免费excel网站
  • 域名安全检测中心济南网站万词优化
  • 钟楼做网站谈谈你对互联网营销的认识
  • 怎么自己做公司网站2022好用值得推荐的搜索引擎
  • 可靠的武进网站建设百度云手机登录入口
  • 做网站怎么加水平线日本樱花免m38vcom费vps
  • 房地产项目营销策划方案台州seo优化
  • 东莞热的建设网站seo网站分析工具
  • 恢复wordpress修订版本号seo关键词排名优化制作
  • 怎么用花生壳做网站重庆做优化的网络公司
  • wordpress插件安装目录下株洲seo排名
  • 界面网站的风格关键词seo排名怎么做的
  • 权重查询站长工具黄页网推广服务
  • 建筑工程行业网站建设方案北京口碑最好的it培训机构
  • 有什么好的网站做数学题吗百度提交网站入口网址
  • 有网站源码 怎么做网站建立网站的流程
  • 移动网站建设指南seo关键词词库
  • 个人网店和网站的区别智推教育seo课程
  • 做一个招聘信息的网站_用什么做网站的软件有友情链接的网站