Python实现高斯似然函数:二维似然矩阵与等高线图问题
Hey there! Let's work through building your Gaussian likelihood contour plot step by step. I'll cover the key parts that might have tripped you up, especially the plt.contour setup.
Step 1: Define Core Components
First, let's lay out the basics: our dataset, the parameter grids, and the Gaussian likelihood function.
1.1 Sample Dataset
I'll use a synthetic set of 10 measurements (feel free to replace this with your actual data):
import numpy as np import matplotlib.pyplot as plt # Your 10-measurement dataset (replace with your values!) X = np.array([0.82, 0.91, 0.95, 1.02, 1.08, 1.11, 0.98, 1.05, 0.93, 1.01])
1.2 Gaussian Log-Likelihood Function
Using log-likelihood is better for numerical stability (product of small probabilities can underflow quickly). For independent samples, the log-likelihood is the sum of individual log-probabilities:
def gaussian_log_likelihood(mu, sigma, data): # Avoid division by zero (sigma >= 0.01 per your range) if sigma <= 0: return -np.inf # Log probability for each data point log_probs = -0.5 * np.log(2 * np.pi * sigma**2) - (data - mu)**2 / (2 * sigma**2) # Sum for total log-likelihood return np.sum(log_probs)
Step 2: Generate Parameter Grids
We'll create a 2D grid of mu (0.5 to 1.5, step 0.01) and sigma (0.01 to 0.3, step 0.01):
# Create 1D arrays for mu and sigma mu_vals = np.arange(0.5, 1.51, 0.01) # 0.5 to 1.5 inclusive sigma_vals = np.arange(0.01, 0.31, 0.01) # 0.01 to 0.3 inclusive # Convert to 2D grids for contour plotting mu_grid, sigma_grid = np.meshgrid(mu_vals, sigma_vals) # Initialize likelihood matrix (same shape as grids) log_likelihood_grid = np.zeros_like(mu_grid)
Step 3: Fill the Likelihood Matrix
Loop through each grid point and compute the log-likelihood:
for i in range(len(sigma_vals)): for j in range(len(mu_vals)): log_likelihood_grid[i, j] = gaussian_log_likelihood(mu_grid[i,j], sigma_grid[i,j], X)
Note: For faster computation, you can vectorize this instead of nested loops, but nested loops are easier to follow for beginners.
Step 4: Plot the Contour Map
This is where you might have run into issues. Let's break down plt.contour usage:
- The first two arguments are the 2D grids (
mu_grid,sigma_grid) - The third argument is the 2D likelihood matrix (
log_likelihood_grid) - We can add labels, a colorbar, and adjust levels for clarity
plt.figure(figsize=(10, 6)) # Create contour plot (use contourf for filled contours, contour for lines) contour = plt.contour(mu_grid, sigma_grid, log_likelihood_grid, levels=15, cmap='viridis') # Add filled contours for better visualization (optional) plt.contourf(mu_grid, sigma_grid, log_likelihood_grid, levels=15, cmap='viridis', alpha=0.7) # Add labels and title plt.xlabel('Mean ($\\mu$)', fontsize=12) plt.ylabel('Standard Deviation ($\\sigma$)', fontsize=12) plt.title('Gaussian Log-Likelihood Contour Plot', fontsize=14) # Add colorbar with label plt.colorbar(contour, label='Log-Likelihood Value') # Add contour labels (optional) plt.clabel(contour, inline=True, fontsize=10) plt.grid(alpha=0.3) plt.show()
Common Pitfalls to Check
- Grid Shape Mismatch: Make sure
mu_grid,sigma_grid, andlog_likelihood_gridall have the same shape (you can check withprint(mu_grid.shape)). - Sigma Range: Your sigma starts at 0.01, which avoids division by zero—great call!
- Likelihood vs Log-Likelihood: If you want to use raw likelihood instead, just exponentiate the log-likelihood (
likelihood_grid = np.exp(log_likelihood_grid)), but be aware of potential numerical underflow for small sigma values. - Contour Levels: Adjust the
levelsparameter to make the plot more or less detailed (e.g.,levels=20for more contours).
内容的提问来源于stack exchange,提问作者TheKingConnie

