(三)推断的逼近方法-通过加权重采样的贝叶斯定理

加权重采样

import numpy as np
import matplotlib.pyplot as plt

# Step 1: Generate 10,000 random theta values from U([0, 1])
n = 10000
theta_values = np.random.rand(n)

# Define the function to compute weights for a given theta
def compute_weight(theta):
    return ((2 + theta) ** 125) * ((1 - theta) ** 38) * (theta ** 34)

# Compute the weights for all theta values
weights = np.array([compute_weight(theta) for theta in theta_values])

# Normalize the weights to make them probabilities
weights /= np.sum(weights)

# Step 2: Resample theta values based on the computed weights
resampled_theta_values = np.random.choice(theta_values, size=n, p=weights)

# Plot a histogram of the resampled theta values
plt.hist(resampled_theta_values, bins=50, density=True, alpha=0.7)
plt.title("Histogram of Resampled Theta Values")
plt.xlabel("Theta")
plt.ylabel("Frequency")
plt.show()

(三)推断的逼近方法-通过加权重采样的贝叶斯定理_第1张图片

你可能感兴趣的:(数据科学和Python实现,python)