Skip to content

Instantly share code, notes, and snippets.

@humorless
Last active November 2, 2024 10:47
Show Gist options
  • Save humorless/fbc1f3ac6e82b70fb9d6b0b260d18272 to your computer and use it in GitHub Desktop.
Save humorless/fbc1f3ac6e82b70fb9d6b0b260d18272 to your computer and use it in GitHub Desktop.
The sales forecasting formula using Monte Carlo method
import numpy as np
import matplotlib.pyplot as plt
# 假設參數
lambda_visits = 5 # 主動拜訪次數的均值
lambda_leads = 3 # 主動找上門的客戶數均值
alpha = 0.1 # 主動拜訪轉換率
beta = 0.5 # 主動找上門轉換率
# 蒙地卡羅模擬
num_simulations = 10000
simulated_cases = []
for _ in range(num_simulations):
# 模擬泊松分布的拜訪和詢問數量
active_visits = np.random.poisson(lambda_visits)
in_bounds = np.random.poisson(lambda_leads)
# 計算接案數量
N = alpha * active_visits + beta * in_bounds
simulated_cases.append(N)
# 繪製模擬結果分佈
plt.hist(simulated_cases, bins=50, color='skyblue', edgecolor='black')
plt.xlabel('case number')
plt.ylabel('stimulation number')
plt.title('monte carlo distribution')
plt.show()
# 顯示模擬結果的統計信息
print("avg case number:", np.mean(simulated_cases))
print("std:", np.std(simulated_cases))
print("case number range (95% CI):", np.percentile(simulated_cases, [2.5, 97.5]))
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
# 假設有歷史數據
active_visits = np.array([15, 18, 20, 22, 17, 21, 19, 23, 20, 24]) # 主動拜訪次數
in_bounds = np.array([5, 6, 7, 6, 8, 7, 5, 6, 7, 8]) # 主動找上門的客戶數
cases = np.array([40, 45, 50, 48, 46, 49, 44, 51, 47, 52]) # 接案數量
# 回歸分析
X = np.column_stack((active_visits, in_bounds))
X = sm.add_constant(X)
model = sm.OLS(cases, X)
results = model.fit()
print("regression parameter:", results.params)
# regression parameter: [19.33164877 1.01642451 1.17561592]
# cases = 19.33 + 1.02 * active_visits + 1.18 * in_bounds
print("the summary of the model:")
print(results.summary())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment