theme = Generators.observe(notify => {const read = () => {const css =getComputedStyle(document.body);const v = (name, fallback) => (css.getPropertyValue(name).trim() || fallback);return {dark:document.body.classList.contains("quarto-dark"),fg:v("--bs-body-color","#212529"),bg:v("--bs-body-bg","#ffffff"),muted:v("--bs-secondary-color","#6c757d"),grid:v("--bs-border-color","#dee2e6"),primary:v("--bs-link-color","#0d6efd"),accent:v("--bs-danger","#dc3545"),good:v("--bs-success","#198754") }; };// Quarto swaps stylesheets when the theme changes, and the new variables apply only once// the stylesheet has loaded; read them again shortly after, and keep the last reading.let last ="";const emit = () => {const t =read();const key =JSON.stringify(t);if (key !== last) { last = key;notify(t); } };const update = () => { emit();setTimeout(emit,100);setTimeout(emit,400); };update();const observer =newMutationObserver(update); observer.observe(document.body, {attributes:true,attributeFilter: ["class"]});return () => observer.disconnect();})
plotStyle = ({// "inherit" takes the page's text color directly, so axes and labels follow a theme// switch even before the plot redraws.color:"inherit",background:"transparent",fontSize:"13px",overflow:"visible"})
themedButton = (label, counterLabel =null) => {const button =html`<button type="button" class="btn btn-sm btn-outline-primary bisc-explorer-button">${label}</button>`;// The counter shows which run is on screen, so every press visibly does something and a// reader can tell runs apart. It reads 1 at page load and after Reset.const counter = counterLabel?html`<span class="bisc-run-counter" aria-live="polite"></span>`:null;const form =html`<form class="bisc-explorer-control">${button}${counter ||""}</form>`;const show = () => { if (counter) counter.textContent=`${counterLabel}${form.value+1}`; }; form.value=0;show(); button.onclick= () => { form.value+=1; form.dispatchEvent(newEvent("input", {bubbles:true})); };// Reset sets the value back to 0 and dispatches input; refresh the counter either way. form.addEventListener("input", show);return form;}
resetButton = (controls, label ="Reset to the starting values") => {// This runs once per page, because the cell that calls it depends on the controls// themselves (viewof), not on their values; so these are the values at page load.const start = controls.map(c => c.value);const button =html`<button type="button" class="btn btn-sm btn-outline-primary bisc-explorer-button">${label}</button>`; button.onclick= () => { controls.forEach((c, k) => { c.value= start[k]; c.dispatchEvent(newEvent("input", {bubbles:true})); }); };returnhtml`<div style="margin: 0.5rem 0">${button}</div>`;}
// A small seeded generator (mulberry32), so a run can be repeated.makeRandom = (seed) => {let a = seed >>>0;return () => { a = (a +0x6D2B79F5) >>>0;let t = a; t =Math.imul(t ^ (t >>>15), t |1); t ^= t +Math.imul(t ^ (t >>>7), t |61);return ((t ^ (t >>>14)) >>>0) /4294967296; };}
This page sets the order-up-to level of an \((R, S)\) policy for an item you describe, by the exact method of Chapter 9, and evaluates any level beside the optimal one. It also scans the review interval, with the level set to its best value at each interval. It starts from the transformer reviewed monthly; Reset returns to it. The page runs in your browser and records nothing. Its formulas are a restatement in JavaScript of the book’s code, and they are tested against that code.
What This Page Assumes
The policy. Every \(R\) the inventory position is reviewed and raised to \(S\); the order arrives a constant lead time \(L\) later, and \(L\) must be positive. Unmet demand is backordered.
The demand. Stationary, given by its rate \(\lambda\) and its variance per year. A variance equal to the rate is Poisson, one unit at a time. A larger variance is a negative binomial over every interval, with both moments scaling with the interval’s length. A variance below the rate is refused. Demand over \(R + L\) must average at most 5,000 units.
The costs. A fixed cost \(k\) per order, a holding cost \(h\) per unit per year, and a backorder cost \(b\) per unit per year owed. An order is placed at a review only if some demand has arrived since the last one.
The measures. Averages over the review cycle, computed as the code does by Simpson’s rule on 128 intervals. The fill rate equals the ready rate only when demand is Poisson.
NoteFor anything else, use the software
Demand in lots, the \((s, S)\) and \((R, s, S)\) policies, a gamma for large means, and many items reviewed together are in the code the chapter teaches, in the chapter’s Using the Software section, and the chapter’s workbook covers the same model.
Your Item
viewof rate = Inputs.number({value:45,label:"Demand rate, λ (units a year)"})viewof varRate = Inputs.number({value:45,label:"Variance of demand per year"})viewof k = Inputs.number({value:220,label:"Ordering cost, k ($ an order)"})viewof h = Inputs.number({value:950,label:"Holding cost, h ($ a unit a year)"})viewof b = Inputs.number({value:8550,label:"Backorder cost, b ($ a unit a year)"})viewof unit = Inputs.select(newMap([["days",365], ["weeks",52], ["months",12], ["years",1]]), {value:12,label:"Time unit for R and L"})viewof L = Inputs.number({value:2,label:"Lead time, L"})viewof R = Inputs.number({value:1,label:"Review interval, R"})viewof S = Inputs.number({value:13,label:"Order-up-to level, S"})useOptimum = {const button =html`<button type="button" class="btn btn-sm btn-outline-primary bisc-explorer-button">Use the optimal level</button>`; button.onclick= () => {if (!result.ok) return; viewof S.value= result.best; viewof S.dispatchEvent(newEvent("input", {bubbles:true})); };returnhtml`<div style="margin: 0.5rem 0">${button}</div>`;}resetButton([viewof rate, viewof varRate, viewof k, viewof h, viewof b, viewof unit, viewof L, viewof R, viewof S])
import {rsModel, economicInterval} from"./explorers/lib/periodic.js"result = {const problems = [];if (!(rate >0)) problems.push("The demand rate must be positive.");if (!(varRate >= rate)) problems.push("The variance per year must be at least the demand rate; a smaller variance fits neither family offered here.");if (!(k >=0)) problems.push("The ordering cost cannot be negative.");if (!(h >0)) problems.push("The holding cost must be positive.");if (!(b >0)) problems.push("The backorder cost must be positive.");if (!(L >0)) problems.push("The lead time must be positive.");if (!(R >0)) problems.push("The review interval must be positive.");if (!Number.isInteger(S)) problems.push("The order-up-to level must be a whole number.");if (rate * (R + L) / unit >5000) problems.push("Demand over R + L averages more than 5,000 units; this page is limited to 5,000.");if (problems.length) return {ok:false, problems};try {const item = {rate,variance: varRate, k, h, b,L: L / unit};const model =rsModel({...item,R: R / unit});const best = model.optimalLevel();return {ok:true, item, model, best,here: model.evaluate(S),opt: model.evaluate(best)}; } catch (e) {return {ok:false,problems: [e.message.charAt(0).toUpperCase() + e.message.slice(1) +"."]}; }}
{if (!result.ok) {returnhtml`<div class="callout callout-style-simple callout-warning"><div class="callout-body"> <p><strong>The page cannot evaluate this item.</strong></p> <ul>${result.problems.map(p =>html`<li>${p}</li>`)}</ul></div></div>`; }const {model, here, opt} = result;const n = (x, d =4) => x.toFixed(d);const dollars = x =>"$"+ x.toLocaleString("en-US", {minimumFractionDigits:2,maximumFractionDigits:2});const rows = [ ["Order-up-to level, S",String(here.S),String(opt.S)], ["Expected backorders, B̄",n(here.B),n(opt.B)], ["Expected on hand, Ī",n(here.I),n(opt.I)], ["Fill rate",n(here.fillRate),n(opt.fillRate)], ["Ready rate",n(here.readyRate),n(opt.readyRate)], ["Safety stock, S − λ(R + L)",n(here.safetyStock,2),n(opt.safetyStock,2)], ["Orders a year",n(here.orderFrequency,4),n(opt.orderFrequency,4)], ["Total cost a year",dollars(here.total),dollars(opt.total)] ];returnhtml`<p>Demand over the protection interval R + L averages ${(rate * (R + L) / unit).toFixed(2)} units and is${model.family==="Poisson"?"Poisson":"negative binomial"}. The critical ratio is ${n(model.w)}.</p> <table class="table table-sm" style="max-width: 42rem"> <thead><tr><th>Measure</th><th>Your level</th><th>The optimal level</th></tr></thead> <tbody>${rows.map(r =>html`<tr><td>${r[0]}</td><td>${r[1]}</td><td>${r[2]}</td></tr>`)}</tbody> </table>`;}
The first plot is the cost against the order-up-to level, with your level as the red dot.
result.ok? (() => {const {model, best, here, opt} = result;const lo =Math.max(0,Math.min(S, best) -Math.max(6,Math.ceil(0.4* best))), hi =Math.max(S, best) +Math.max(6,Math.ceil(0.4* best));const step =Math.max(1,Math.ceil((hi - lo) /120));const pts = d3.range(lo, hi +1, step).map(s => ({S: s,total: model.evaluate(s).total}));return Plot.plot({style: plotStyle,height:240,marginLeft:80,marginBottom:45,x: {label:"Order-up-to level, S",labelAnchor:"center",labelOffset:38,grid:true},y: {label:"Cost a year ($)",grid:true,domain: [0,2.5* opt.total]},marks: [ Plot.line(pts, {x:"S",y:"total",stroke: theme.primary,strokeWidth:1.5,clip:true}), Plot.dot([{S: here.S,total: here.total}], {x:"S",y:"total",r:6,fill: theme.accent,clip:true}) ],ariaLabel:"Cost against the order-up-to level",ariaDescription:`The least cost is $${opt.total.toFixed(2)} at S = ${best}.` });})() :html``
The second plot is the least cost against the review interval, from a quarter of yours to three times it, with \(S\) at its best at each interval. The red dot is your interval, and the gray square marks the interval \(\sqrt{2k/(h\lambda)}\) that balances the fixed charge against the cycle stock.
result.ok? (() => {const {item} = result;const pts = d3.range(0,25).map(j => R * (0.25+2.75* j /24)).map(r => {const m =rsModel({...item,R: r / unit}), s = m.optimalLevel();return {R: r,total: m.evaluate(s).total}; });const econR = k >0?economicInterval({k, h, rate}) *unit:null;const marks = [Plot.line(pts, {x:"R",y:"total",stroke: theme.primary,strokeWidth:1.5}), Plot.dot([{R,total: result.opt.total}], {x:"R",y:"total",r:6,fill: theme.accent})];if (econR && econR >0.25* R && econR <3* R) {const m =rsModel({...item,R: econR / unit}); marks.push(Plot.dot([{R: econR,total: m.evaluate(m.optimalLevel()).total}], {x:"R",y:"total",r:6,symbol:"square",fill: theme.muted})); }return Plot.plot({style: plotStyle,height:240,marginLeft:80,marginBottom:45,x: {label:`Review interval, R (${({365:"days",52:"weeks",12:"months",1:"years"})[unit]})`,labelAnchor:"center",labelOffset:38,grid:true},y: {label:"Least cost a year ($)",grid:true,domain: [0,1.5* d3.max(pts, d => d.total)]}, marks,ariaLabel:"Least cost against the review interval",ariaDescription:`The least cost over the intervals shown is $${d3.min(pts, d => d.total).toFixed(2)}.` });})() :html``
Things to Try
Press Use the optimal level: the transformer reviewed monthly takes \(S^{*} = 14\) at $8,397.22 a year, the chapter’s example.
Raise the variance per year from 45 to 135, three times the rate. Demand becomes negative binomial, the best level rises to 17, and the cost rises to $12,946.43: the same mean with more spread needs more protection.
Change the lead time from 2 months to 1. The protection interval shrinks from 3 months to 2, and the best level falls to 9, at $7,330.80.