algo-sc-newsvendor
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNewsvendor Model
报童模型
Overview
概述
The newsvendor model determines optimal order quantity for a single selling period with uncertain demand. Balances overage cost (Co = cost - salvage) against underage cost (Cu = price - cost). Optimal Q* satisfies: P(D ≤ Q*) = Cu / (Cu + Co). Known as the critical ratio solution.
报童模型用于确定单个销售周期内、需求不确定情况下的最优订购量。平衡超储成本(Co = 成本 - 残值)与欠储成本(Cu = 售价 - 成本)。最优订购量Q满足:P(D ≤ Q) = Cu / (Cu + Co),这被称为临界比率解法。
When to Use
适用场景
Trigger conditions:
- One-time or seasonal purchasing decisions (fashion, holiday goods, event tickets)
- Perishable products with no restocking opportunity
- Setting initial stocking levels before demand is observed
When NOT to use:
- For continuous replenishment with stable demand (use EOQ)
- When backorders are acceptable and demand carries over (multi-period models)
触发条件:
- 一次性或季节性采购决策(如时尚产品、节日商品、活动门票)
- 无补货机会的易腐产品
- 需求未明确前设定初始库存水平
不适用场景:
- 需求稳定的连续补货(应使用EOQ模型)
- 允许缺货且需求可结转的情况(应使用多周期模型)
Algorithm
算法
IRON LAW: The Critical Ratio Determines Optimal Service Level
Q* = F⁻¹(Cu / (Cu + Co)) where F⁻¹ is the inverse demand CDF.
If margin is high relative to cost (Cu >> Co), order MORE (high service level).
If margin is low relative to excess cost (Co >> Cu), order LESS (low service level).
The optimal solution almost NEVER equals expected demand.铁律:临界比率决定最优服务水平
Q* = F⁻¹(Cu / (Cu + Co)),其中F⁻¹是需求累积分布函数的逆函数。
若边际利润远高于成本(Cu >> Co),则应多订购(高服务水平)。
若超储成本远高于边际利润(Co >> Cu),则应少订购(低服务水平)。
最优解几乎永远不等于预期需求。Phase 1: Input Validation
阶段1:输入验证
Define: unit cost (c), selling price (p), salvage value (v), demand distribution (mean μ, std σ). Compute: Cu = p - c, Co = c - v.
Gate: p > c > v (profitable with positive overage cost), demand distribution estimated.
定义:单位成本(c)、售价(p)、残值(v)、需求分布(均值μ,标准差σ)。计算:Cu = p - c,Co = c - v。
准入条件: p > c > v(盈利且超储成本为正),已估算需求分布。
Phase 2: Core Algorithm
阶段2:核心算法
- Critical ratio: CR = Cu / (Cu + Co) = (p - c) / (p - v)
- If demand ~ Normal(μ, σ): Q* = μ + z(CR) × σ where z(CR) = inverse normal CDF at CR
- Expected profit = Cu × E[min(Q,D)] - Co × E[max(Q-D, 0)]
- Expected units sold = μ - σ × L(z) where L(z) is the standard loss function
- 临界比率:CR = Cu / (Cu + Co) = (p - c) / (p - v)
- 若需求服从正态分布Normal(μ, σ):Q* = μ + z(CR) × σ,其中z(CR)是临界比率对应的正态分布逆累积分布函数值
- 预期利润 = Cu × E[min(Q,D)] - Co × E[max(Q-D, 0)]
- 预期销量 = μ - σ × L(z),其中L(z)是标准损失函数
Phase 3: Verification
阶段3:验证
Check: Q* > 0, CR between 0 and 1, Q* is above or below μ depending on whether CR > or < 0.5.
Gate: Q* directionally correct relative to mean demand.
检查:Q* > 0,CR介于0和1之间,Q高于或低于均值μ取决于CR是否大于或小于0.5。
准入条件: Q相对于预期需求的方向正确。
Phase 4: Output
阶段4:输出
Return optimal order quantity with profit analysis.
返回最优订购量及利润分析。
Output Format
输出格式
json
{
"optimal_quantity": 130,
"critical_ratio": 0.71,
"expected_profit": 2800,
"expected_leftover": 15,
"expected_stockout_probability": 0.29,
"metadata": {"price": 50, "cost": 20, "salvage": 5, "demand_mean": 100, "demand_std": 30}
}json
{
"optimal_quantity": 130,
"critical_ratio": 0.71,
"expected_profit": 2800,
"expected_leftover": 15,
"expected_stockout_probability": 0.29,
"metadata": {"price": 50, "cost": 20, "salvage": 5, "demand_mean": 100, "demand_std": 30}
}Examples
示例
Sample I/O
输入输出样例
Input: p=$50, c=$20, v=$5, D~Normal(100, 30)
Expected: Cu=30, Co=15, CR=30/45=0.667, z=0.43, Q*=100+0.43×30=113 units.
输入: p=$50, c=$20, v=$5, D~Normal(100, 30)
预期结果: Cu=30, Co=15, CR=30/45=0.667, z=0.43, Q*=100+0.43×30=113单位。
Edge Cases
边缘案例
| Input | Expected | Why |
|---|---|---|
| v = 0 (total loss) | Lower Q*, conservative | High overage cost pushes order down |
| p >> c (high margin) | Q* well above mean | Worth risking excess to avoid lost sales |
| σ = 0 (certain demand) | Q* = μ exactly | No uncertainty, order exactly demand |
| 输入 | 预期结果 | 原因 |
|---|---|---|
| v = 0(完全损失) | 订购量Q*更低,偏保守 | 高超储成本促使减少订购 |
| p >> c(高边际利润) | Q*远高于均值 | 值得承担超储风险以避免销售损失 |
| σ = 0(需求确定) | Q* = μ 完全等于预期需求 | 无不确定性,按需订购即可 |
Gotchas
注意事项
- Distribution choice matters: Normal allows negative demand. For low-mean items, use Poisson or truncated normal. For high CV, use lognormal.
- Demand estimation: The hardest part is estimating μ and σ. Use historical data, expert judgment, or Bayesian updating from early sales signals.
- Risk aversion: The newsvendor model is risk-neutral. Risk-averse decision makers systematically under-order relative to Q*. Adjust for behavioral bias.
- Multi-product constraints: With a shared budget constraint across products, solve the constrained newsvendor (Lagrangian relaxation).
- Salvage value assumption: Assumes all excess can be salvaged at v. If disposal has a cost (v < 0), the model still works but Q* drops further.
- 分布选择至关重要:正态分布允许负需求。对于低均值物品,使用泊松分布或截断正态分布。对于高变异系数(CV)的情况,使用对数正态分布。
- 需求估算:最困难的部分是估算μ和σ。可使用历史数据、专家判断或基于早期销售信号的贝叶斯更新。
- 风险厌恶:报童模型是风险中性的。风险厌恶型决策者会系统性地订购少于Q*的数量。需针对行为偏差进行调整。
- 多产品约束:若存在跨产品的共享预算约束,需求解约束性报童问题(拉格朗日松弛法)。
- 残值假设:假设所有过剩库存都能以v的价格残值处理。若处置存在成本(v < 0),模型仍然适用,但Q*会进一步降低。
Scripts
脚本
| Script | Description | Usage |
|---|---|---|
| Compute newsvendor optimal quantity, expected profit, and fill rate | |
Run to execute built-in sanity tests.
python scripts/newsvendor.py --verify| 脚本 | 描述 | 使用方法 |
|---|---|---|
| 计算报童模型的最优订购量、预期利润及填充率 | |
运行可执行内置的完整性测试。
python scripts/newsvendor.py --verifyReferences
参考文献
- For multi-product constrained newsvendor, see
references/constrained-newsvendor.md - For demand distribution fitting, see
references/demand-fitting.md
- 关于多产品约束性报童模型,参见
references/constrained-newsvendor.md - 关于需求分布拟合,参见
references/demand-fitting.md