MCMC샘플링으로 배달시간 예측하기

MCMC샘플링으로 배달시간 예측하기
Photo by Egor Myznik / Unsplash
import numpy as np  
import matplotlib.pyplot as plt  
  
# 데이터 생성 파라미터  
n_samples = 1000  
np.random.seed(42)  
  
# 시간대에 따른 배달 시간 설정  
def generate_delivery_time(hour, rider_acceptance_rate, num_riders):  
    base_time = 25  # 전체 배달 시간 평균 25분  
    # 점심, 저녁 시간대 배달 시간 증가  
    if 11 <= hour <= 13 or 18 <= hour <= 20:    
        variance = np.random.normal(loc=8, scale=2)  # 배달시간이 늘어짐  
    else:    
        variance = np.random.normal(loc=4, scale=1)  # 기본 배달시간  
    # 라이더 배차 수락율과 규모에 따른 배달시간 조정  
    rider_effect = 1 / (rider_acceptance_rate * num_riders + 1)  
      
    return base_time + variance * rider_effect  
  
# 가상의 데이터 생성  
hours = np.random.randint(0, 24, n_samples)  
rider_acceptance_rates = np.random.uniform(0.5, 1.0, n_samples)  
num_riders = np.random.randint(10, 50, n_samples)  
  
delivery_times = np.array([generate_delivery_time(hour, rate, riders)   
                           for hour, rate, riders in zip(hours, rider_acceptance_rates, num_riders)])  
  
# Metropolis-Hastings MCMC 구현  
def metropolis_hastings(delivery_times, iterations=10000, prior_mu=25, prior_sigma=5):  
    # 초기 상태 설정  
    current_state = np.mean(delivery_times)  
    states = [current_state]  
      
    for _ in range(iterations):  
        # 새로운 제안 상태 생성  
        proposed_state = np.random.normal(current_state, 1.0)  
          
        # 수락 확률 계산 (사전분포 반영)  
        current_likelihood = np.sum(-0.5 * (delivery_times - current_state) ** 2)  
        proposed_likelihood = np.sum(-0.5 * (delivery_times - proposed_state) ** 2)  
          
        # 사전분포 적용  
        current_prior = -0.5 * ((current_state - prior_mu) ** 2) / (prior_sigma ** 2)  
        proposed_prior = -0.5 * ((proposed_state - prior_mu) ** 2) / (prior_sigma ** 2)  
          
        # 수락 비율 계산  
        acceptance_ratio = np.exp(proposed_likelihood + proposed_prior - current_likelihood - current_prior)  
          
        # 새로운 상태를 수락할지 결정  
        if np.random.rand() < acceptance_ratio:  
            current_state = proposed_state  
              
        states.append(current_state)  
      
    return np.array(states)  
  
# MCMC 샘플링 수행 (사전분포 적용)  
prior_mu = 25  # 사전분포의 평균  
prior_sigma = 5  # 사전분포의 표준편차  
mcmc_samples = metropolis_hastings(delivery_times, iterations=10000, prior_mu=prior_mu, prior_sigma=prior_sigma)  
  
# 결과 시각화  
plt.hist(mcmc_samples, bins=50, density=True, alpha=0.7)  
plt.title('Posterior Distribution of Delivery Times (MCMC with Prior)')  
plt.xlabel('Delivery Time (minutes)')  
plt.ylabel('Density')  
plt.show()  
  
# 95% 신뢰 구간 계산  
lower_bound = np.percentile(mcmc_samples, 2.5)  
upper_bound = np.percentile(mcmc_samples, 97.5)  
mean_estimate = np.mean(mcmc_samples)  
  
print(f"Estimated Mean Delivery Time: {mean_estimate:.2f} minutes")  
print(f"95% Confidence Interval: [{lower_bound:.2f}, {upper_bound:.2f}] minutes")

Read more

[책]Reshuffle: Who wins when AI restacks the knowledge economy

[책]Reshuffle: Who wins when AI restacks the knowledge economy

원래는 Amazon에 가서 Personal Knowledge Managment에 관한 책을 사려고 했다. Sketch Your Mind라는 책이었는데, 그 때 이 책 “Reshuffle”을 발견하였다. AI가 어떻게 Knowledge Economy를 흔들 것가? 라는 부제를 훑어보면서 저자가 쓴 다른 책을 보게 되었는데 거기에 내가 좋아했던 책을쓴 저자라는 것을 알게 되었다. 그래서 크게 고민하지 않고 구매를 하고

By Bongho, Lee
[책]올라운드투자, 누군가의 투자일기

[책]올라운드투자, 누군가의 투자일기

“올라운드 투자”라는 제목을 보았을 때는, “올라운드 플레이어”가 생각이 났다. “올라운드”라는 표현을 오랜만에 들어본 까닭이었다. 그럼에도 불구하고 이 책을 고른 것은 저자가 그간 보여준 컨텐츠에 대한 신뢰가 있던 까닭이었다. 컨텐츠를 다양하게 보는 편이지만 깊이가 아주 있지는 않았다. 여기서 깊이라 함은 기존 전문적인 정량적 분석의 내용의 수준을 말하는 것이다.

By Bongho, Lee
내가 놓치고 있던 미래, 먼저 온 미래를 읽고

내가 놓치고 있던 미래, 먼저 온 미래를 읽고

장강명 작가의 책은, 유학시절 읽고 처음이었다. 유학시절 "한국이 싫어서"라는 책은 동기부여가 상당히 되는 책이었다. 한국을 떠나 새로운 정채성을 학생으로서 Build up 해나가고 있던 상황에서 이 책은 제목부터 꽤 솔깃하였다. 물론 결말이 기억날 정도로 인상깊은 책은 아니었지만 말이다. 그렇게 시간이 흘러 장강명 작가의 책은 더 이상 읽지 않던

By Bongho, Lee