集成学习-数学基础

01
Metropolis-Hastings算法

import numpy as np
import random
import matplotlib.pyplot as plt

## 设置参数
mu = 0.5
sigma = 0.1
skip = 700  ## 设置收敛步长
num = 100000  ##采样点数

def Gussian(x,mu,sigma):
    return 1/(np.sqrt(2*np.pi)*sigma)*np.exp(-np.square((x-mu))/(2*np.square(sigma)))

def M_H(num):
    x_0 = 0
    samples = []
    j = 1
    while(len(samples) <= num):
        while True:
            x_1 = random.random()  # 转移函数(转移矩阵)
            q_i = Gussian(x_0,mu,sigma)
            q_j = Gussian(x_1,mu,sigma)
            alpha = min(1,q_i/q_j)
            u = random.random()
            if u <= alpha:
                x_0 = x_1
                if j >= skip:
                    samples.append(x_1)
                j = j + 1
                break
    return samples

norm_samples = M_H(num)

X = np.array(norm_samples)
px = Gussian(X,mu,sigma)
plt.scatter(X,px)
plt.show()

结果
在这里插入图片描述
02
基础知识学习

2.1概率论与数理统计

# pi的估计问题
import numpy as np 
def pi_estimate(n):
    '''
    n为投点的数量
    '''
    n_rand_X = np.random.uniform(-1.0,1.0,n)
    n_rand_Y = np.random.uniform(-1.0,1.0,n)
    ## 判断是否在圆内
    distance = np.sqrt(n_rand_X**2 + n_rand_Y**2)
    dis_n = float(len(distance[distance<=1.0]))
    return 4 * (dis_n / n)

for i in [10,50,100,500,1000,5000,10000,50000,100000,500000,10000000]:
    print("pi的估计值为",pi_estimate(i))
# 电子元件寿命问题
import numpy 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值