algo-sc-newsvendor
Solve the newsvendor problem for single-period ordering decisions under uncertain demand. Use this skill when the user needs to determine optimal order quantity for perishable goods, seasonal products, or one-time purchase decisions — even if they say 'how much to order for this season', 'perishable inventory', or 'single-period ordering'.
git clone --depth 1 https://github.com/asgard-ai-platform/skills /tmp/algo-sc-newsvendor && cp -r /tmp/algo-sc-newsvendor/algo-sc-newsvendor ~/.claude/skills/algo-sc-newsvendorSKILL.md
# Newsvendor 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.
## 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)
## 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.
```
### Phase 1: Input Validation
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.
### Phase 2: Core Algorithm
1. Critical ratio: CR = Cu / (Cu + Co) = (p - c) / (p - v)
2. If demand ~ Normal(μ, σ): Q* = μ + z(CR) × σ where z(CR) = inverse normal CDF at CR
3. Expected profit = Cu × E[min(Q,D)] - Co × E[max(Q-D, 0)]
4. Expected units sold = μ - σ × L(z) where L(z) is the standard loss function
### Phase 3: Verification
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.
### Phase 4: Output
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}
}
```
## 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.
### 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 |
## 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.
## Scripts
| Script | Description | Usage |
|--------|-------------|-------|
| `scripts/newsvendor.py` | Compute newsvendor optimal quantity, expected profit, and fill rate | `python scripts/newsvendor.py --help` |
Run `python scripts/newsvendor.py --verify` to execute built-in sanity tests.
## References
- For multi-product constrained newsvendor, see `references/constrained-newsvendor.md`
- For demand distribution fitting, see `references/demand-fitting.md`Implement and select ad bidding strategies from manual CPC to automated target-CPA and target-ROAS. Use this skill when the user needs to choose a bidding strategy, set up automated bidding, or optimize bid parameters — even if they say 'what bidding strategy should I use', 'target CPA setup', or 'smart bidding configuration'.
Optimize advertising budget allocation across campaigns using marginal returns analysis. Use this skill when the user needs to distribute budget across multiple campaigns, optimize spend pacing, or maximize overall ROAS under budget constraints — even if they say 'how to split my ad budget', 'campaign budget optimization', or 'diminishing returns on ad spend'.
Build CTR prediction models for estimating ad click-through rates from features. Use this skill when the user needs to predict click probability, build an ad ranking model, or evaluate ad creative performance — even if they say 'predict click rate', 'ad relevance scoring', or 'which ad will get more clicks'.
Implement Generalized Second Price auction for ad slot allocation and pricing. Use this skill when the user needs to understand search ad auctions, compute ad positions and costs-per-click, or analyze bidding dynamics — even if they say 'how does Google Ads auction work', 'ad rank calculation', or 'second price auction for ads'.
Implement VCG mechanism for incentive-compatible ad slot allocation with truthful bidding. Use this skill when the user needs to design a truthful auction mechanism, compute externality-based payments, or understand why platforms may prefer GSP over VCG — even if they say 'truthful auction design', 'VCG payments', or 'incentive-compatible mechanism'.
Explain blockchain fundamentals including distributed ledger architecture, consensus mechanisms, and block structure. Use this skill when the user needs to understand blockchain concepts, evaluate whether blockchain fits a use case, or design a blockchain-based solution — even if they say 'how does blockchain work', 'do I need blockchain', or 'distributed ledger'.
Design and implement smart contracts as self-executing programmatic agreements on blockchain. Use this skill when the user needs to build automated on-chain logic, evaluate smart contract security, or design tokenized business rules — even if they say 'smart contract development', 'automated agreement', or 'on-chain logic'.
Implement BM25 ranking function for e-commerce product search relevance scoring. Use this skill when the user needs to build a text-based product search engine, improve search result relevance, or replace basic TF-IDF with a more robust ranking function — even if they say 'product search ranking', 'search relevance', or 'BM25 implementation'.