PageSourceSearch

https://epq-calculator.netlify.app/safety-stock

html epq-calculator.netlify.app collected 2026-10-03 10:42:18 UTC 29,761 bytes, 368 lines download raw bytes

1<!DOCTYPE html>
2<html lang="en">
3<head>
4<meta charset="UTF-8">
5<!-- This site is hosted on Netlify. Anyone can build and deploy a site
6     like this one for free: https://netlify.new/?utm_campaign=loops&utm_source=ai-legible&utm_medium=owned&utm_content=comment&utm_id=694daa5e-51d7-4e4e-ac2a-0bee5aeeefd5
7     Netlify hosting facts for this site: static/SSR served via Netlify Edge. -->
8<meta name="viewport" content="width=device-width, initial-scale=1.0">
9<!-- set theme before paint (no flash): saved choice → OS preference → light -->
10<script>(function(){try{var t=localStorage.getItem('theme')||(matchMedia('(prefers-color-scheme:dark)').matches?'dark':'light');document.documentElement.dataset.theme=t;}catch(e){}})();</script>
10
11
12<title>Safety Stock Calculator — Z-Score &amp; Lead Time</title>
13<meta name="description" content="Safety stock formula with a full Z-score table (50–99.9%), all three methods compared and a worked example. Free calculator for demand and lead-time variability.">
14<link rel="canonical" href="https://epq-calculator.netlify.app/safety-stock.html">
15<meta property="og:title" content="Safety Stock Calculator — Z-Score &amp; Lead Time">
16<meta property="og:description" content="Calculate the inventory buffer for demand and lead-time variability, with service-level Z-score and a reorder-point handoff.">
17<meta property="og:type" content="website">
18<meta property="og:url" content="https://epq-calculator.netlify.app/safety-stock.html">
19<meta property="og:image" content="https://epq-calculator.netlify.app/og-image.png">
20<meta property="og:image:width" content="1200">
21<meta property="og:image:height" content="630">
22<meta name="twitter:card" content="summary_large_image">
23<meta name="twitter:image" content="https://epq-calculator.netlify.app/og-image.png">
24<link rel="icon" type="image/png" href="/favicon.png">
25<link rel="icon" sizes="any" href="/favicon.ico">
26<link rel="apple-touch-icon" href="/apple-touch-icon.png">
27
28<script type="application/ld+json">
29{"@context":"https://schema.org","@type":"SoftwareApplication","name":"Safety Stock Calculator (Z-Score)","applicationCategory":"BusinessApplication","operatingSystem":"Web","author":{"@type":"Person","name":"Sathapana"},"offers":{"@type":"Offer","price":"0","priceCurrency":"USD"},"description":"Free safety stock calculator using the full statistical formula with demand variability and lead-time variability. Converts a target service level to a Z-score automatically."}
30</script>
vendor: 1 bytes, line 30
30
31<script type="application/ld+json">
32{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[
33{"@type":"Question","name":"What is safety stock?","acceptedAnswer":{"@type":"Answer","text":"Safety stock is the extra inventory held to protect against variability in demand and supplier lead time, so a stockout does not occur before the next replenishment arrives. It is the buffer above the expected demand during lead time."}},
34{"@type":"Question","name":"What is the safety stock formula with Z-score?","acceptedAnswer":{"@type":"Answer","text":"The full formula is SS = Z × sqrt(L × sigma_d^2 + d^2 × sigma_L^2), where Z is the service-level factor, L is average lead time, sigma_d is the standard deviation of daily demand, d is average daily demand, and sigma_L is the standard deviation of lead time."}},
35{"@type":"Question","name":"How do I choose a service level?","acceptedAnswer":{"@type":"Answer","text":"The service level is the probability of not stocking out during a replenishment cycle. Common targets are 90% (Z=1.28), 95% (Z=1.64), and 99% (Z=2.33). Higher service levels need disproportionately more safety stock."}},
36{"@type":"Question","name":"Why account for lead-time variability?","acceptedA
36nswer":{"@type":"Answer","text":"If your supplier or production lead time swings, that uncertainty often drives more safety stock than demand variability does. Demand-only calculators understate the buffer whenever lead time is not perfectly stable."}},
37{"@type":"Question","name":"What is the Z-score for a 95% service level?","acceptedAnswer":{"@type":"Answer","text":"1.64 (1.645 to three decimals). Other common values: 90% gives 1.28, 97% gives 1.88, 98% gives 2.05, 99% gives 2.33, and 99.9% gives 3.09. The Z-score is the number of standard deviations of combined demand and lead-time variability held as buffer."}},
38{"@type":"Question","name":"What are the different safety stock formulas?","acceptedAnswer":{"@type":"Answer","text":"Three are in common use. The max/average method, (Max usage × Max lead time) − (Avg usage × Avg lead time), needs no statistics but has no service level. The demand-only formula, Z × sigma_d × sqrt(L), adds a service level but assumes lead time is constant. The full formula, Z × sqrt(L × sigma_d^2 + d^2 × sigma_L^2), accounts for variability in both demand and lead time."}},
39{"@type":"Question","name":"How do I calculate safety stock and reorder point together?","acceptedA
39nswer":{"@type":"Answer","text":"Calculate safety stock first, then add expected demand during lead time: ROP = d × L + SS. With d = 480 units per day, L = 7 days and SS = 1,601 units, the reorder point is 4,961 units. Place the replenishment order when stock on hand reaches that level."}},
40{"@type":"Question","name":"Can I add the demand buffer and the lead-time buffer together?","acceptedAnswer":{"@type":"Answer","text":"No, that overstates the buffer. Independent sources of variability combine as the square root of the sum of their variances, not as a sum of their standard deviations. In the worked example, adding them gives 1,840 units versus the correct 1,601 units, roughly 15 percent too much inventory."}}
41]}
42</script>
42
43<!-- JSON-LD: HowTo (mirrors the worked example rendered in the page body) -->
44<script type="application/ld+json">
45{"@context":"https://schema.org","@type":"HowTo","name":"How to calculate safety stock","description":"Calculate safety stock using the full statistical formula that accounts for both demand variability and lead-time variability.","step":[
46{"@type":"HowToStep","name":"Gather the five inputs","text":"Average daily demand d, standard deviation of daily demand sigma_d, average lead time L in days, standard deviation of lead time sigma_L, and the target cycle service level."},
47{"@type":"HowToStep","name":"Convert the service level to a Z-score","text":"A 95% cycle service level gives Z = 1.645 via the inverse normal distribution."},
48{"@type":"HowToStep","name":"Compute the demand-variability term","text":"Multiply lead time by the squared standard deviation of demand: L × sigma_d^2. With L = 7 and sigma_d = 60, this is 25,200."},
49{"@type":"HowToStep","name":"Compute the lead-time-variability term","text":"Multiply squared average demand by squared standard deviation of lead time: d^2 × sigma_L^2. With d = 480 and sigma_L = 2, this is 921,600."},
50{"@type":"HowToStep","name":"Combine and take the square root","text":"Add the two variances to get 946,800, take the square root to get 973.04, then multiply by Z: 1.645 × 973.04 = 1,601 units of safety stock."},
51{"@type":"HowToStep","name":"Convert to a reorder point","text":"Add expected demand during lead time: ROP = d × L + SS = 480 × 7 + 1,601 = 4,961 units."}
52]}
53</script>
53
54<!-- JSON-LD: BreadcrumbList -->
55<script type="application/ld+json">
56{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[
57{"@type":"ListItem","position":1,"name":"EPQ Calculator","item":"https://epq-calculator.netlify.app/"},
58{"@type":"ListItem","position":2,"name":"Safety Stock Calculator","item":"https://epq-calculator.netlify.app/safety-stock.html"},
59{"@type":"ListItem","position":3,"name":"Reorder Point Calculator","item":"https://epq-calculator.netlify.app/reorder-point.html"}
60]}
61</script>
61
62
63<link rel="preconnect" href="https://fonts.googleapis.com">
64<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
65<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@500;600;700&family=IBM+Plex+Sans:wght@400;500;600&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
66<link rel="stylesheet" href="suite.css">
67</head>
68<body>
69<div class="wrap">
70
71  <header>
72    <div class="eyebrow">Manufacturing · Inventory</div>
73    <h1>Safety Stock <span class="u">Calculator</span></h1>
74    <p class="lede">Size the buffer that protects your service level. This uses the full Z-score formula — accounting for both demand <em>and</em> lead-time variability, not just demand like most calculators.</p>
75    <nav class="suite-nav" id="suiteNav"></nav>
76  </header>
77
78  <div id="trust" class="trust"></div>
79
80  <div class="calc">
81    <div class="calc-head">
82      <span class="ct">Inputs &amp; Results</span>
83      <span class="badge">Full Z-score · demand + lead-time variability</span>
84    </div>
85    <div class="calc-body">
86      <div class="inputs">
87        <div class="field">
88          <label for="d">Average daily demand <span class="unit">d — units/day</span></label>
89          <input type="number" id="d" min="0" step="any">
90          <div class="hint" id="dHint">&nbsp;</div>
91        </div>
92        <div class="field">
93          <label for="sd">Std dev of daily demand <span class="unit">σd — units/day</span></label>
94          <input type="number" id="sd" min="0" step="any">
95          <div class="hint">Excel/Sheets: use STDEV.S on recent daily demand.</div>
96        </div>
97        <div class="field">
98          <label for="L">Average lead time <span class="unit">L — days</span></label>
99          <input type="number" id="L" min="0" step="any">
100          <div class="hint" id="LHint">&nbsp;</div>
101        </div>
102        <div class="field">
103          <label for="sL">Std dev of lead time <span class="unit">σL — days</span></label>
104          <input type="number" id="sL" min="0" step="any">
105          <div class="hint">Use actual supplier lead times; enter 0 if lead time is fixed.</div>
106        </div>
107        <div class="field">
108          <label for="sl">Cycle service level <span class="unit">% — probability of no stockout</span></label>
109          <input type="number" id="sl" min="50" max="99.99" step="any">
110          <div class="hint" id="zHint">&nbsp;</div>
111        </div>
112        <button class="btn" id="calcBtn">Calculate safety stock</button>
113        <button type="button" class="ex-link" id="ssExBtn">↻ Load a worked example</button>
114        <div class="err" id="ssErr"></div>
115      </div>
116
117      <div class="results">
118        <div class="res-primary">
119          <div class="rl">Safety stock</div>
120          <div class="rv" id="ssVal">—</div>
121          <div class="ru">units to hold as buffer</div>
122        </div>
123        <div class="res-grid">
124          <div class="rc"><div class="l">Service factor Z</div><div class="v" id="zVal">—</div></div>
125          <div class="rc"><div class="l">Demand during lead time</div><div class="v" id="ddlt">—</div></div>
126          <div class="rc"><div class="l">From demand variability</div><div class="v" id="shareD">—</div></div>
127          <div class="rc"><div class="l">From lead-time variability</div><div class="v" id="shareL">—</div></div>
128        </div>
129        <div class="vs-strip">
130          <div class="vt">vs. demand-only formula (the common shortcut)</div>
131          <div class="vs-row"><span class="k">Safety stock, demand variability only</span><span class="n" id="ssDemandOnly">—</span></div>
132          <div class="vs-row"><span class="k">Extra buffer the full formula adds</span><span class="n good" id="ssExtra">—</span></div>
133        </div>
134        <div class="handoff">
135          <a class='btn-next' href='/reorder-point' id='toRop'><span>Continue → Reorder Point</span><span class="arr">→</span></a>
136        </div>
137        <div class="share-row"><button class="btn-sec" id="copyLink">🔗 Copy link to this result</button></div>
138      </div>
139    </div>
140  </div>
141
142  <p class="method"><b>Method</b> — statistical safety stock with normally-distributed demand and lead time (assumed 
142independent); the service level is converted to a Z-score via the inverse normal distribution.</p>
143
144  <div id="ctaErp"></div>
145
146  <section class="content">
147    <h2>What is safety stock?</h2>
148    <p>Safety stock is the buffer inventory you hold on top of expected demand during lead time. Its job is to absorb the two things that go wrong between reorders: demand comes in higher than forecast, or replenishment takes longer than planned. Size it right and you hit your target service level without drowning in working capital.</p>
149
150    <h2>The full safety stock formula</h2>
151    <div class="formula">
152      SS = <span class="var">Z</span> &times; &radic;( <span class="var">L</span> &times; <span class="var">σd</span>&sup2; &nbsp;+&nbsp; <span class="var">d</span>&sup2; &times; <span class="var">σL</span>&sup2; )
153      <span class="sub"><span class="var">Z</span> = service-level factor (from your target fill rate) &nbsp;·&nbsp; <span class="var">L</span> = average lead time (days)<br><span class="var">σd</span> = std dev of daily demand &nbsp;·&nbsp; <span class="var">d</span> = average daily demand &nbsp;·&nbsp; <span class="var">σL</span> = std dev of lead time (days)</span>
154    </div>
155    <div class="callout">Most free calculators only use <strong>Z × σd × √L</strong> — they assume lead time is perfectly stable. The moment your supplier or production lead time varies, that term <strong>d² × σL²</strong> kicks in, and it is often the <em>bigger</em> driver. This tool includes it, so you don't silently under-buffer.</div>
156
157    <h3>Turning a service level into Z</h3>
158    <p>You don't need a Z-table — enter the service level you want and the calculator converts it with the inverse normal distribution. But if you want to look the value up or sanity-check a number someone handed you, here is the full table. "Stockout risk" is simply 100% minus the service level: the chance of running out during any one replenishment cycle.</p>
159    <table class="vars">
160      <thead><tr><th>Cycle service level</th><th>Z-score</th><th>Stockout risk per cycle</th></tr></thead>
161      <tbody>
162        <tr><td>50%</td><td class="sym">0.00</td><td>50%</td></tr>
163        <tr><td>75%</td><td class="sym">0.67</td><td>25%</td></tr>
164        <tr><td>80%</td><td class="sym">0.84</td><td>20%</td></tr>
165        <tr><td>85%</td><td class="sym">1.04</td><td>15%</td></tr>
166        <tr><td>90%</td><td class="sym">1.28</td><td>10%</td></tr>
167        <tr><td>95%</td><td class="sym">1.64</td><td>5%</td></tr>
168        <tr><td>96%</td><td class="sym">1.75</td><td>4%</td></tr>
169        <tr><td>97%</td><td class="sym">1.88</td><td>3%</td></tr>
170        <tr><td>98%</td><td class="sym">2.05</td><td>2%</td></tr>
171        <tr><td>99%</td><td class="sym">2.33</td><td>1%</td></tr>
172        <tr><td>99.5%</td><td class="sym">2.58</td><td>0.5%</td></tr>
173        <tr><td>99.9%</td><td class="sym">3.09</td><td>0.1%</td></tr>
174      </tbody>
175    </table>
176    <p class="note">Look at what happens above 95%: going from 95% to 99% raises Z from 1.64 to 2.33, so the buffer grows by about 42% to buy back just four points of risk. Going to 99.9% nearly doubles the 95% buffer. This is why blanket "99% on everything" policies quietly consume working c
176apital — set high service levels on the items where a stockout actually costs you, not across the whole catalogue.</p>
177
178    <h2>Safety stock formula variations (and which to use)</h2>
179    <p>There is no single canonical safety stock formula. Which one is correct depends entirely on which sources of variability you can actually measure. Here are the three you will meet, weakest to strongest.</p>
180
181    <h3>1. Max/average method (no statistics needed)</h3>
182    <div class="formula">
183      SS = (Max daily usage &times; Max lead time) &minus; (Avg daily usage &times; Avg lead time)
184    </div>
185    <p>Popular because it needs no standard deviations — just the worst case you have seen. The catch is that it has <strong>no service level</strong>: you cannot dial the risk up or down, and the answer depends on how extreme your single worst historical observation happened to be. One freak event permanently inflates the buffer. Use it when you have too little history to compute a standard deviation.</p>
186
187    <h3>2. Demand-variability only</h3>
188    <div class="formula">
189      SS = <span class="var">Z</span> &times; <span class="var">σd</span> &times; &radic;<span class="var">L</span>
190    </div>
191    <p>This is what most free calculators implement. It has a real service level, which makes it a genuine improvement — but it assumes <strong>lead time never varies</strong>. If your supplier is reliable to the day, it is correct and simple. If not, it under-buffers, sometimes badly.</p>
192
193    <h3>3. Demand and lead-time variability (what this calculator uses)</h3>
194    <div class="formula">
195      SS = <span class="var">Z</span> &times; &radic;( <span class="var">L</span> &times; <span class="var">σd</span>&sup2; &nbsp;+&nbsp; <span class="var">d</span>&sup2; &times; <span class="var">σL</span>&sup2; )
196    </div>
197    <p>The complete form. It adds the <strong>d² × σL²</strong> term for lead-time variability, and treats the two sources as independent — which is why they combine as a root-sum-of-squares rather than a simple sum.</p>
198
199    <h3>Which method fits your data</h3>
200    <table class="vars">
201      <thead><tr><th>Your situation</th><th>Use</th></tr></thead>
202      <tbody>
203        <tr><td>No usable history, just a known worst case</td><td>Max/average method</td></tr>
204        <tr><td>Demand varies, lead time is reliable to the day</td><td>Z × σd × √L</td></tr>
205        <tr><td>Both demand and lead time vary</td><td>Full formula (this tool)</td></tr>
206        <tr><td>Lead time varies a lot, demand is steady</td><td>Full formula — the lead-time term will dominate</td></tr>
207      </tbody>
208    </table>
209
210    <h2>Safety stock worked example</h2>
211    <p>A distributor sells an average of 480 units a day with a standard deviation of 60. Replenishment takes 7 days on average, with a standard deviation of 2 days. The target is a 95% cycle service level.</p>
212    <table class="vars">
213      <thead><tr><th>Input</th><th>Value</th></tr></thead>
214      <tbody>
215        <tr><td>Average daily demand <span class="sym">d</span></td><td>480 units / day</td></tr>
216        <tr><td>Std dev of daily demand <span class="sym">σd</span></td><td>60 units</td></tr>
217        <tr><td>Average lead time <span class="sym">L</span></td><td>7 days</td></tr>
218        <tr><td>Std dev of lead time <span class="sym">σL</span></td><td>2 days</td></tr>
219        <tr><td>Cycle service level</td><td>95%</td></tr>
220      </tbody>
221    </table>
222    <p><strong>Step 1 — convert the service level to Z.</strong> From the table above, 95% gives<br>
223    <span class="calcline">Z = 1.645</span></p>
224    <p><strong>Step 2 — compute the demand-variability term.</strong> Variance, not standard deviation, is what adds:<br>
225    <span class="calcline">L × σd² = 7 × 60² = 7 × 3,600 = <strong>25,200</strong></span></p>
226    <p><strong>Step 3 — compute the lead-time-variability term.</strong><br>
227    <span class="calcline">d² × σL² = 480² × 2² = 230,400 × 4 = <strong>921,600</strong></span></p>
228    <p><strong>Step 4 — combine and take the square root.</strong><br>
229    <span class="calcline">25,200 + 921,600 = 946,800</span><br>
230    <span class="calcline">√946,800 = 973.04</span><br>
231    <span class="calcline">SS = 1.645 × 973.04 = <strong>1,601 units</strong></span></p>
232    <p><strong>Step 5 — see what lead-time variability cost you.</strong> Had we used the demand-only formula, the answer would have been Z × σd × √L = 1.645 × 60 × √7 = <strong>261 units</strong>. The full formula returns 1,601 — <strong>1,339 units more</strong>. Look at where the variance actually comes from:</p>
233    <table class="vars">
234      <thead><tr><th>
234Source of variability</th><th>Variance</th><th>Share of total</th></tr></thead>
235      <tbody>
236        <tr><td>Demand (L × σd²)</td><td>25,200</td><td>3%</td></tr>
237        <tr><td>Lead time (d² × σL²)</td><td>921,600</td><td><strong>97%</strong></td></tr>
238      </tbody>
239    </table>
240    <p>Almost all the risk on this item is the supplier, not the customer. A demand-only calculator would have told you to hold 261 units and you would have stocked out repeatedly — while the real fix is either a buffer of 1,601 units or a more reliable lead time. That diagnostic is the reason to carry both terms.</p>
241    <p class="note">A tempting mistake: adding the two buffers instead of combining their variances. That would give 261 + 1,579 = 1,840 units — about 15% too much. Independent risks do not both peak at the same moment, which is why the correct combination is the square root of the summed variances.</p>
242
243    <h2>From safety stock to reorder point</h2>
244    <p>Safety stock on its own does not tell you <em>when</em> to reorder. The reorder point is expected demand during lead time plus the buffer:</p>
245    <div class="formula">
246      ROP = <span class="var">d</span> &times; <span class="var">L</span> &nbsp;+&nbsp; SS
247    </div>
248    <p>Continuing the example: expected demand during lead time is 480 × 7 = <strong>3,360 units</strong>, so<br>
249    <span class="calcline">ROP = 3,360 + 1,601 = <strong>4,961 units</strong></span></p>
250    <p>When stock on hand falls to 4,961 units, place the order. If demand and lead time both land on their averages you will receive the replenishment with the 1,601-unit buffer still intact; the buffer is there for the cycles that do not cooperate. Continue into the <a href='/reorder-point'>Reorder Point calculator</a> to carry these numbers over, or start upstream with the <a href='/'>EPQ calculator</a> to size the production run itself.</p>
251
252    <h2>Frequently asked questions</h2>
253    <div class="faq">
254      <details><summary>What is safety stock?</summary><div class="fa">Extra inventory held to protect against variability in demand and lead time, so you don't stock out before the next replenishment arrives.</div></details>
255      <details><summary>What is the safety stock formula with Z-score?</summary><div class="fa">SS = Z × √(L × σd² + d² × σL²), where Z is the service-level factor, L is average lead time, σd is the std dev of daily demand, d is average daily demand, and σL is the std dev of lead time.</div></details>
256      <details><summary>How do I choose a service level?</summary><div class="fa">It's the probability of not stocking out during a cycle. Common targets: 90% (Z=1.28), 95% (Z=1.64), 99% (Z=2.33). Higher service levels need disproportionately more stock.</div></details>
257      <details><summary>Why account for lead-time variability?</summary><div class="fa">If lead time swings, that uncertainty often drives more safety stock than demand variability. Demand-only calculators understate the buffer whenever lead time isn't perfectly stable.</div></details>
258      <details><summary>What is the Z-score for a 95% service level?</summary><div class="fa">1.64 (1.645 to three decimals). Other common values: 90% → 1.28, 97% → 1.88, 98% → 2.05, 99% → 2.33, 99.9% → 3.09. The Z-score is the number of standard deviations of combined demand and lead-time variability you hold as buffer.</div></details>
259      <details><summary>What are the different safety stock formulas?</summary><div class="fa">Three are in common use. The max/average method, (Max usage × Max lead time) − (Avg usage × Avg lead time), needs no statistics but has no service level. The demand-only formula, Z × σd × √L, adds a service level but assumes lead time is constant. The full formula, Z × √(L × σd² + d² × σL²), accounts for variability in both demand and lead time and is what this calculator uses.</div></details>
260      <details><summary>How do I calculate safety stock and reorder point together?</summary><div class="fa">Calculate safety stock first, then add expected demand during lead time: ROP = d × L + SS. For example, with d = 480 units/day, L = 7 days and 
260SS = 1,601 units, the reorder point is 480 × 7 + 1,601 = 4,961 units. Place the replenishment order when stock on hand reaches that level.</div></details>
261      <details><summary>Can I just add the demand buffer and the lead-time buffer together?</summary><div class="fa">No — that overstates the buffer. Independent sources of variability are combined as the square root of the sum of their variances, not as a sum of their standard deviations. In the worked example, adding them gives 1,840 units versus the correct 1,601, roughly 15% too much inventory.</div></details>
262    </div>
263  </section>
264
265  <div id="related" class="related"></div>
266  <section id="credibility" class="credibility"></section>
267  <div id="badges" class="badges"></div>
268  <footer>
269    <span class="mono">Safety Stock Calculator · free tool</span>
270    <span class="mono">Manufacturing suite · 2 / 3</span>
271    <a class='mono' href='/privacy'>Privacy</a>
272  </footer>
273
274</div>
275
276<script src="suite.js"></script>
vendor: 1 bytes, line 276
276
277<script>
278/* =========================================================================
279   SAFETY STOCK — core logic
280   SS = Z × √( L·σd²  +  d²·σL² )
281   ========================================================================= */
282const DEFAULTS = { d:480, sd:60, L:7, sL:2, sl:95 };
283let last = null;
284
285function ssCore(inp){
286  const d=+inp.d, sd=+inp.sd, L=+inp.L, sL=+inp.sL, sl=+inp.sl;
287  if(!(d>0&&L>0)) return {ok:false,error:'Daily demand (d) and lead time (L) must be greater than zero'};
288  if(!(sd>=0&&sL>=0)) return {ok:false,error:'Standard deviations cannot be negative'};
289  if(!(sl>=50&&sl<100)) return {ok:false,error:'Service level must be between 50 and 100 (below 50% gives a negative buffer, which is meaningless)'};
290  const Z = invNorm(sl/100);
291  const varDemand = L*sd*sd;        // demand variability over lead time
292  const varLead   = d*d*sL*sL;      // lead-time variability effect
293  const SS = Z*Math.sqrt(varDemand+varLead);
294  const ssDemandOnly = Z*sd*Math.sqrt(L);
295  const totalVar = varDemand+varLead;
296  return {ok:true, d,sd,L,sL,sl, Z, SS, ssDemandOnly,
297    ddlt:d*L,                                   // expected demand during lead time
298    shareDemand: totalVar? varDemand/totalVar : 0,
299    shareLead:   totalVar? varLead/totalVar   : 0};
300}
301
302function compute(){
303  const inp={d:$('d').value,sd:$('sd').value,L:$('L').value,sL:$('sL').value,sl:$('sl').value};
304  const r=ssCore(inp);
305  $('zHint').textContent = (r.ok||!isNaN(invNorm(inp.sl/100))) ? 'Z = '+fmt(invNorm(inp.sl/100),3) : '';
306  if(!r.ok){ showErr('ssErr',r.error); return; }
307  hideErr('ssErr');
308  last=r;
309
310  $('ssVal').textContent=fmt(r.SS,0);
311  $('zVal').textContent=fmt(r.Z,3);
312  $('ddlt').textContent=fmt(r.ddlt,0)+' u';
313  $('shareD').textContent=fmt(r.shareDemand*100,0)+'%';
314  $('shareL').textContent=fmt(r.shareLead*100,0)+'%';
315  $('ssDemandOnly').textContent=fmt(r.ssDemandOnly,0)+' u';
316  $('ssExtra').textContent='+'+fmt(Math.max(0,r.SS-r.ssDemandOnly),0)+' u';
317
318  // hand off to Reorder Point with the values it needs
319  $('toRop').href = suiteLink('reorder-point.html', {d:r.d, L:r.L, ss:r.SS, sl:r.sl});
320  syncURL({d:r.d, sd:r.sd, L:r.L, sgl:r.sL, sl:r.sl});   // shareable URL
321}
322
323/* prefill from upstream (?d, ?L, ?sl) or sensible defaults */
324function init(){
325  renderSuiteNav('ss');
326  renderTrust('trust');
327  renderRelated('ss');
328  renderCredibility('ss');
329  renderBadges('badges');
330  const fromD = param('d'), fromL = param('L'), fromSl = param('sl');
331  $('d').value  = fromD ?? DEFAULTS.d;
332  $('sd').value = param('sd') ?? DEFAULTS.sd;       // σd: from shared link or default
333  $('L').value  = fromL ?? DEFAULTS.L;
334  $('sL').value = param('sgl') ?? DEFAULTS.sL;      // σL: 'sgl' key avoids 'sl' case-collision
335  $('sl').value = fromSl ?? DEFAULTS.sl;
336  if(fromD!=null){ $('d').classList.add('prefilled'); $('dHint').textContent='↳ carried from the EPQ calculator'; $('dHint').className='hint from'; }
337  if(fromL!=null){ $('L').classList.add('prefilled'); $('LHint').textContent='↳ carried over'; $('LHint').className='hint from'; }
338  wireCopyLink('copyLink');
339  renderAffiliateCTA('ctaErp','Stop sizing buffers by hand — keep safety stock live for every SKU in your ERP/MES.');
340  $('calcBtn').addEventListener('click',()=>{ compute(); if(last&&last.ok) trackEvent('calculate',{calculator:'safety_stock'}); });
341  $('ssExBtn').addEventListener('click',()=>{
342    Object.entries(DEFAULTS).forEach(([id,value])=>$(id).value=value);
343    compute();
344    trackEvent('load_example',{calculator:'safety_stock'});
345  });
346  ['d','sd','L','sL','sl'].forEach(id=>$(id).addEventListener('keydown',e=>{if(e.key==='Enter')compute();}));
347  let liveTimer;   // F9 — live recalc (debounced, only when inputs are valid)
348  ['d','sd','L','sL','sl'].forEach(id=>$(id).addEventListener('input',()=>{
349    clearTimeout(liveTimer);
350    liveTimer=setTimeout(()=>{ if(ssCore({d:$('d').value,sd:$('sd').value,L:$('L').value,sL:$('sL').value,sl:$('sl').value}).ok) compute(); },250);
351  }));
352  compute();
353}
354init();
355
356selfCheckBanner(function(assert){
357  invNormSelfChecks(assert);
358  const r=ssCore({d:480,sd:50,L:7,sL:2,sl:95});
359  assert(r.ok && Math.abs(r.SS-1594)<2, 'SS(full) ≈ 1594 (got '+(r.ok?fmt(r.SS,1):r.error)+')');
360  assert(r.ok && Math.abs(r.ssDemandOnly-218)<2, 'SS(demand-only) ≈ 218 (got '+(r.ok?fmt(r.ssDemandOnly,1):'-')+')');
361  assert(ssCore({d:480,sd:0,L:7,sL:0,sl:95}).SS===0, 'zero variability → SS = 0');
362  assert(!ssCore({d:480,sd:50,L:7,sL:2,sl:100}).ok, 'rejects service level = 100');
363  assert(!ssCore({d:480,sd:50,L:7,sL:2,sl:40}).ok, 'rejects service level < 50 (no negative buffer)');
364  assert(!ssCore({d:0,sd:50,L:7,sL:2,sl:95}).ok, 'rejects d = 0');
365});
366</script>
366
367</body>
368</html>

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.