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

马尔科夫蒙特卡洛_吉布斯抽样算法(Markov Chain Monte Carlo(MCMC)_Gibbs Sampling)

定义

输入:目标概率分布的密度函数 p ( x ) p(x) p(x),函数 f ( x ) f(x) f(x)

输出: p ( x ) p(x) p(x)的随机样本 x m + 1 , x m + 2 , ⋯   , x n x_{m+1},x_{m+2},\cdots,x_n xm+1,xm+2,,xn,函数样本均值 f m n f_{mn} fmn;

参数:收敛步数 m m m,迭代步数 n n n

(1)初始化。给出初始样本 x 0 = ( x 1 ( 0 ) , x 2 ( 0 ) , ⋯   , x k ( 0 ) ) T x^{0} = ( x_1^{(0)},x_2^{(0)},\cdots,x_k^{(0)} )^T x0=(x1(0),x2(0),,xk(0))T

(2)对i循环执行

设第 ( i − 1 ) (i-1) (i1)次迭代结束时的样本为 x ( i − 1 ) = ( x 1 ( i − 1 ) , x 2 ( i − 1 ) , ⋯   , x k ( i − 1 ) ) T x^{(i-1)} = (x_1^{(i-1)},x_2^{(i-1)},\cdots,x_k^{(i-1)})^T x(i1)=(x1(i1),x2(i1),,xk(i1))T,则第 i i i次迭代进行如下几步操作:

{ ( 1 ) 由满足条件分布 p ( x 1 ∣ x 2 ( i ) , ⋯   , x k ( i − 1 ) ) 抽取 x 1 ( i ) ⋮ ( j ) 由满足条件分布 p ( x j ∣ x 1 ( i ) , ⋯   , x j − 1 ( i ) , x j + 1 ( i ) , ⋯   , x k ( i − 1 ) ) 抽取 x j ( i ) ⋮ ( k ) 由满足条件分布 p ( x k ∣ x 1 ( i ) , ⋯   , x ( k − 1 ) ( i ) 抽取 x k ( i ) \begin{cases} (1)由满足条件分布p(x_1|x_2^{(i)},\cdots,x_k^{(i-1)})抽取x_1^{(i)}\\ \vdots \\ (j)由满足条件分布p(x_j|x_1^{(i)},\cdots,x_{j-1}^{(i)},x_{j+1}^{(i)},\cdots,x_k^{(i-1)})抽取x_j^{(i)} \\ \vdots \\ (k)由满足条件分布p(x_k|x_1^{(i)},\cdots,x_{(k-1)}^(i)抽取x_k^{(i)} \end{cases} (1)由满足条件分布p(x1x2(i),,xk(i1))抽取x1(i)(j)由满足条件分布p(xjx1(i),,xj1(i),xj+1(i),,xk(i1))抽取xj(i)(k)由满足条件分布p(xkx1(i),,x(k1)(i)抽取xk(i)

得到第 i i i次迭代值 x ( i ) = ( x 1 ( i ) , x 2 ( i ) , ⋯   , x k ( i ) ) T x^{(i)} = (x_1^{(i)},x_2^{(i)},\cdots,x_k^{(i)})^T x(i)=(x1(i),x2(i),,xk(i))T
(3)得到样本集合
{ x ( m + 1 ) , x ( m + 2 ) , ⋯   , x ( n ) } \{ x^{(m+1)},x^{(m+2)},\cdots,x^{(n)} \} {x(m+1),x(m+2),,x(n)}
(4)计算
f m n = 1 n − m ∑ i = m + 1 n f ( x ( i ) ) f_{mn} = \frac{1}{n-m}\sum_{i=m+1}^n f(x^{(i)}) fmn=nm1i=m+1nf(x(i))

import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import gaussian_kde


def gibbs_sampling(dim, conditional_sampler, x0=None, burning_steps=1000, max_steps=10000, epsilon=1e-8, verbose=False):
    """
    给定一个从p(x_j|x_1,x_2,…x_n)采样的条件采样器
    返回样本x~p的列表,其中p是条件分布的原始分布。
    x0是x的初始值。如果未指定,则将其设置为零向量。
    条件采样器将(x,j)作为参数
    """
    x = np.zeros(dim) if x0 is None else x0
    samples = np.zeros([max_steps - burning_steps, dim])
    for i in range(max_steps):
        for j in range(dim):
            x[j]  = conditional_sampler(x, j)
            if verbose:
                print("New value of x is", x_new)
        if i >= burning_steps:
            samples[i - burning_steps] = x
    return samples


if __name__ == '__main__':
    i = 0
    def demonstrate(dim, p, desc, **args):
        samples = gibbs_sampling(dim, p, **args)
        z = gaussian_kde(samples.T)(samples.T)
        plt.scatter(samples[:, 0], samples[:, 1], c=z, marker='.')
        plt.plot(samples[: 100, 0], samples[: 100, 1], 'r-')
        plt.title(desc)
        plt.savefig(str(i)+".png")
        plt.show()

    # example 1:
    mean = np.array([2, 3])
    covariance = np.array([[1, 0],
                           [0, 1]])
    covariance_inv = np.linalg.inv(covariance)
    det_convariance = 1
    def gaussian_sampler1(x, j):
        return np.random.normal()
    demonstrate(2, gaussian_sampler1, "Gaussian distribution with mean of 2 and 3")

    # example 2:
    mean = np.array([2, 3])
    covariance = np.array([[1, 0],
                           [0, 1]])
    covariance_inv = np.linalg.inv(covariance)
    det_convariance = 1
    def gaussian_sampler2(x, j):
        if j == 0:
            return np.random.normal(2)
        else:
            return np.random.normal(3)
    demonstrate(2, gaussian_sampler2, "Gaussian distribution with mean of 2 and 3")

    # example 3:
    def blocks_sampler(x, j):
        sample = np.random.random()
        if sample > .5:
            sample += 1.
        return sample
    demonstrate(2, blocks_sampler, "Four blocks")

    # example 4:
    def blocks_sampler(x, j):
        sample = np.random.random()
        if sample > .5:
            sample += 100.
        return sample
    demonstrate(2, blocks_sampler, "Four blocks with large gap.")

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


http://www.kler.cn/news/316833.html

相关文章:

  • 小程序服务零工市场
  • 基于51单片机的矿井安全检测系统
  • SpringBoot 整合 apache fileupload 轻松实现文件上传与下载(通用版)
  • LeetCode 260. 只出现一次的数字 III
  • 用Python提取PowerPoint演示文稿中的音频和视频
  • CVE-2024-46101
  • 0.初始化项目(Vue2升级到Vue3)
  • 物联网新闻2024.09.16-2024.09.22
  • flume系列之:出现数据堆积时临时增大sink端消费能力
  • LAMP环境搭建记录:基于VM的Ubuntu虚拟机
  • 编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MSVC版)
  • (学习记录)使用 STM32CubeMX——GPIO引脚输入配置
  • 实时数据的处理一致性
  • JavaScript(JS)学习笔记 3(DOM简介 事件简介 元素修改 节点操作 事件操作)
  • MySQL:事务隔离级别
  • Kubernets基础-包管理工具Helm详解
  • 计算机组成原理==初识二进制运算
  • Redisson分布式锁主从一致性问题
  • CentOS修改主机名
  • 【已解决】如何使用JAVA 语言实现二分查找-二分搜索折半查找【算法】手把手学会二分查找【数据结构与算法】
  • 用Qt 对接‌百度AI平台
  • 响应式流规范
  • 胤娲科技:谷歌DeepMind祭出蛋白质设计新AI——癌症治疗迎来曙光
  • DoppelGanger++:面向数据库重放的快速依赖关系图生成
  • JavaScript语法特点
  • linux 使用mdadm 创建raid0 nvme 磁盘
  • 深入理解SpringBoot(一)----SpringBoot的启动流程分析
  • 邮储银行:面向金融行业的移动应用安全风险监测案例
  • 【docker】命令之容器操作
  • C++:布尔类型,引用,堆区空间