如何用Python生成以指定时刻为中心的全天高斯分布函数
Got it, let's break down how to build this exactly as you need: a Gaussian (normal) distribution centered on a specific time like 14:00, spanning an entire day, with a function that takes a timestamp and returns the corresponding Gaussian value.
Core Approach
First, we need to map time values to a numerical axis—since we're dealing with a day, converting timestamps to minutes since midnight gives us a clean 0-to-1440 range (24*60). Then we can apply the standard Gaussian formula using this numerical axis, centering it on your target time.
To ensure the distribution covers the full day, we'll set the standard deviation (σ) so that ~99.7% of the distribution (the 3σ range) falls within the 0-1440 minute window. This means most of the probability mass stays within the day, with negligible values at the edges.
Full Implementation
Here's a ready-to-use function with comments explaining each step:
import math from datetime import datetime def gaussian_value_at_timestamp(timestamp: datetime, center_time: str = "14:00", sigma_minutes: float = 240) -> float: """ Calculate the Gaussian distribution value for a given timestamp, centered at a specific time of day. Args: timestamp: A datetime object representing the time to evaluate. center_time: Target center time in "HH:MM" format (default: "14:00"). sigma_minutes: Standard deviation in minutes (default: 240, which makes 3σ = 720 minutes, covering most of the day). Returns: The Gaussian probability density at the given timestamp. """ # Convert input timestamp to minutes since midnight minutes_since_midnight = timestamp.hour * 60 + timestamp.minute # Parse the center time to minutes since midnight center_hour, center_minute = map(int, center_time.split(":")) center_minutes = center_hour * 60 + center_minute # Standard Gaussian probability density formula coefficient = 1 / (sigma_minutes * math.sqrt(2 * math.pi)) exponent = -((minutes_since_midnight - center_minutes) ** 2) / (2 * sigma_minutes ** 2) return coefficient * math.exp(exponent)
How to Use It
Test the function with different timestamps to see how it behaves:
# Test peak value (exactly at center time) peak_time = datetime(2024, 5, 20, 14, 0) print(f"Peak value at 14:00: {gaussian_value_at_timestamp(peak_time):.6f}") # Output: ~0.001329 # Test 1σ away (4 hours before center) one_sigma_time = datetime(2024, 5, 20, 10, 0) print(f"Value at 10:00 (1σ away): {gaussian_value_at_timestamp(one_sigma_time):.6f}") # Output: ~0.000786 # Test 3σ away (12 hours before center) three_sigma_time = datetime(2024, 5, 20, 2, 0) print(f"Value at 02:00 (3σ away): {gaussian_value_at_timestamp(three_sigma_time):.10f}") # Output: ~0.0000043981 (almost zero)
Customization Tips
- Adjust the spread: Change
sigma_minutesto make the distribution wider or narrower. For example:sigma_minutes=180(3 hours): 3σ = 9 hours, so the distribution is tighter around the center.sigma_minutes=300(5 hours): 3σ = 15 hours, so the spread covers almost the entire day.
- Center on a different time: Pass any "HH:MM" string to
center_time(e.g.,"08:30"for 8:30 AM). - Handle midnight wrap-around: If you need the distribution to smoothly wrap from 23:59 to 00:00 (instead of dropping to zero), you can modify the function to use the circular distance between times (e.g., treat 23:00 and 01:00 as 2 hours apart, not 22).
内容的提问来源于stack exchange,提问作者may

