-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.typ
More file actions
1703 lines (1309 loc) · 41.9 KB
/
notes.typ
File metadata and controls
1703 lines (1309 loc) · 41.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "/lib/imports.typ": *
#import "@local/tystats:0.1.0": norm
- If you don't understand the continuous, try the discrete
- If you don't understand the multivariate, try the univariate
- If you don't understand the calculus / linear algebra / etc., try the geometric interpretation
= Lead Time
Why $sqrt(L)$?
Setup
Daily demand has mean $mu$ and standard deviation $sigma$
Say:
- $mu=100$
- $sigma=20$
One day:
- Mean is $mu = 100$
- Spread is $sigma = 20$
Two days:
- Demand: $D_1 + D_2$ (each day is independent)
Mean add:
$
EE[D_1 + D_2] = mu_1 + mu_2 = 2mu = 200
$
But standard deviations do *NOT* add, Variances add:
$
"Var"(D_1 + D_2) = "Var"(D_1) + "Var"(D_2) = sigma_1^2 + sigma_2^2 = 2 sigma^2
$
So:
$
"Std"(D_1 + D_2) = sqrt(2 sigma^2) = sigma sqrt(2)
$
Generalize to $L$ days:
$
"Var"(sum_(t=1)^L D_t) = L dot sigma^2
$
$
"Std"(sum_(t=1)^L D_t) = sigma sqrt(L)
$
- Mean scales with $L$
- Std scales with $sqrt(L)$
= Multi-Echelon Inventory
Setup:
Consider a serial system with $N$ stages, where stage $N$ faces stochastic customer demand and each replenished from stage $j + 1$ with deterministic lead time $L_j$:
#let start-x = 0
#let start-y = 0
#let spacing = 1
#std.layout(size => context {
let diagram-content = fletcher.diagram(
node-corner-radius: 4pt,
node-fill: luma(97%),
node-outset: 1em,
node-inset: 1em,
spacing: (5.5em, 2.8em),
// --- Stages (right = downstream, faces demand) ---
node((start-x + 0*spacing, start-y), [Vendor], name: <vendor>),
node((start-x + 1*spacing, start-y), [$S_n$ \ $h_n$], name: <sn>),
node((start-x + 2*spacing, start-y), $dots$, name: <dots>),
node((start-x + 3*spacing, start-y), [$S_2$ \ $h_2$], name: <s2>),
node((start-x + 4*spacing, start-y), [$S_1$ \ $h_1$], name: <s1>),
node((start-x + 5*spacing, start-y), [Demand \ $D, p$], name: <demand>),
// --- Material flow (downstream); L_j = lead time INTO stage j ---
edge(<vendor>, <sn>, "-|>", label: $L_n$),
edge(<sn>, <dots>, "-|>", label: $L_(n-1)$),
edge(<dots>, <s2>, "-|>", label: $L_2$),
edge(<s2>, <s1>, "-|>", label: $L_1$),
edge(<s1>, <demand>, "-|>"),
// --- Induced penalty flow (upstream) ---
edge(<s1>, <s2>, "-|>", label: $G_1$, dash: "dashed", bend: 30deg, stroke: rgb("#c0613a")),
edge(<s2>, <dots>, "-|>", label: $G_2$, dash: "dashed", bend: 30deg, stroke: rgb("#c0613a")),
edge(<dots>, <sn>, "-|>", label: $G_(n-1)$, dash: "dashed", bend: 30deg, stroke: rgb("#c0613a")),
// --- Echelon labels ---
node((start-x + 4*spacing, start-y + 0.5), [Echelon 1], stroke: none, fill: none, name: <lbl1>),
node((start-x + 3*spacing, start-y + 0.9), [Echelon 2], stroke: none, fill: none, name: <lbl2>),
node((start-x + 1*spacing, start-y + 1.3), [Echelon $n$], stroke: none, fill: none, name: <lbln>),
// --- Echelon nesting boxes ---
node(
enclose: (<s1>, <lbl1>),
stroke: gray + 0.5pt, fill: rgb(0, 0, 0, 4%),
outset: 0.3em, name: <ech1>
),
node(
enclose: (<s2>, <s1>, <lbl1>, <lbl2>),
stroke: gray + 0.5pt, fill: rgb(0, 0, 0, 4%),
outset: 0.7em, name: <ech2>
),
node(
enclose: (<sn>, <dots>, <s2>, <s1>, <lbl1>, <lbl2>, <lbln>),
stroke: gray + 0.5pt, fill: rgb(0, 0, 0, 4%),
outset: 1.1em, name: <echn>
),
)
let diagram-width = measure(diagram-content).width
let available-width = size.width
if diagram-width > available-width {
let factor = (available-width / diagram-width) * 100%
scale(x: factor, y: factor, reflow: true, diagram-content)
} else {
diagram-content
}
})
Solve stages $1 arrow 2 arrow n$. Each stage $j$ does three things:
- receives the penalty function from below
- solves its own newsvendor balance
- produces a new penalty function to pass up
*Base case (stage 1)*. No stage bellow it, so $G_0 = 0$. Stage 1's expected cost as a function of its echelon level $y$:
$
C_1 (y) = h_1 EE[(y - D_L_1)^+] + p EE[(D_L_1 - y)^+]
$
Where $D_L_1$ is demand over the lead-time window. This is convex; its minimizer is the fractile:
$
S_1 = F_(L_1)^(-1) (p / (p + h_1))
$
*Inductive step (stage $j$)*. Stage $j$ has received $G_(j-1)$ from bellow. It's cost combines its own echelon holding with the penalty it inflicts downstream when it can't fully supply:
$
C_j (y) = h_j EE[(y - D_L_j)^+] + EE[G_(j-1) (y - D_L_j)]
$
Minimize over $y$ to get $S_j = arg min_y C_j (y)$.
*Produce the penalty to pass up*. The clamp and floor operation:
$
G_j (x) = C_j (min(x, S_j)) - C_j (S_j)
$
Zero when $x gt.eq S_j$ (stage $j$ isn't constrained), positive and rising as x drops below $S_j$. Hand $G_j$ to stage $j + 1$.
#example([])[
#std.layout(size => context {
let diagram-content = fletcher.diagram(
node-corner-radius: 4pt,
node-fill: luma(97%),
node-outset: 1em,
node-inset: 1em,
spacing: (6.5em, 2.8em),
// --- Stages (right = downstream) ---
node((start-x + 0*spacing, start-y), [Vendor], name: <vendor>),
node((start-x + 1*spacing, start-y), [$S_3 = 33.15$ \ $h_3 = 1$], name: <s3>),
node((start-x + 2*spacing, start-y), [$S_2 = 22.86$ \ $h_2 = 2$], name: <s2>),
node((start-x + 3*spacing, start-y), [$S_1 = 23.63$ \ $h_1 = 3$], name: <s1>),
node((start-x + 4*spacing, start-y), [Demand \ $D ~ cal(N)(10, 2)$ \ $p = 27$], name: <demand>),
// --- Material flow (downstream); L_j = lead time INTO stage j ---
edge(<vendor>, <s3>, "-|>", label: $L_3 = 2$),
edge(<s3>, <s2>, "-|>", label: $L_2 = 1$),
edge(<s2>, <s1>, "-|>", label: $L_1 = 1$),
edge(<s1>, <demand>, "-|>"),
// --- Induced penalty flow (upstream) ---
edge(<s1>, <s2>, "-|>", label: $G_1$, dash: "dashed", bend: 30deg, stroke: rgb("#c0613a")),
edge(<s2>, <s3>, "-|>", label: $G_2$, dash: "dashed", bend: 30deg, stroke: rgb("#c0613a")),
// --- Echelon labels ---
node((start-x + 3*spacing, start-y + 0.5), [Echelon 1], stroke: none, fill: none, name: <lbl1>),
node((start-x + 2*spacing, start-y + 0.9), [Echelon 2], stroke: none, fill: none, name: <lbl2>),
node((start-x + 1*spacing, start-y + 1.3), [Echelon 3], stroke: none, fill: none, name: <lbl3>),
// --- Echelon nesting boxes ---
node(
enclose: (<s1>, <lbl1>),
stroke: gray + 0.5pt, fill: rgb(0, 0, 0, 4%),
outset: 0.3em, name: <ech1>
),
node(
enclose: (<s2>, <s1>, <lbl1>, <lbl2>),
stroke: gray + 0.5pt, fill: rgb(0, 0, 0, 4%),
outset: 0.7em, name: <ech2>
),
node(
enclose: (<s3>, <s2>, <s1>, <lbl1>, <lbl2>, <lbl3>),
stroke: gray + 0.5pt, fill: rgb(0, 0, 0, 4%),
outset: 1.1em, name: <ech3>
),
)
let diagram-width = measure(diagram-content).width
let available-width = size.width
if diagram-width > available-width {
let factor = (available-width / diagram-width) * 100%
scale(x: factor, y: factor, reflow: true, diagram-content)
} else {
diagram-content
}
})
*Stage 1.* Window $L_1 + 1 = 2$, fractile $27 / (27 + 3) = 0.9$, $z = 1.282$
$
S_1 = 10 (2) + 1.282 dot 2 sqrt(2) = 20 + 2.63 approx 23.63
$
*Stage 2.* Window $L_2 + 1 = 2$, fractile $27 / (27 + 3 + 2) = 27 / 32 = 0.844$, $z = 1.011$
$
S_2 = 20 + 1.011 dot 2 sqrt(2) = 20 + 2.86 approx 22.86
$
*Stage 3.* Window $L_3 + 1 = 3$, fractile $27 / (27 + 6) = 27 / 33 = 0.818$, $z = 0.908$
$
S_3 = 10 (3) + 0.908 dot 2 sqrt(3) = 30 + 3.25 approx 33.15
$
- $S_3 - S_2$at stage 3
- $S_2 − S_1$ at stage 2
- $S_1$ at stage 1
Note:
The fractile formula $p / (p + sum_(i lt j) h_i)$ with window $L_j + 1$ is the exact stage-1 newsvendor and a standard approximation for upstream stages. The *exact* Clark-Scarf $S_2$, $S_3$ come from minimizing $C_j (Y) = h_j EE[(y - D_L_j)^+] + EE[G_(j-1)(y - D_L_j)]$ numerically, since $G_(j-1)$ isn't a clean newsvendor cost.
]
#code([])[
```py
# pip install stockpyl
from stockpyl.ssm_serial import optimize_base_stock_levels
# Nodes indexed 1=downstream(demand-facing) ... 3=upstream(vendor-facing)
S_star, C_star = optimize_base_stock_levels(
num_nodes=3,
echelon_holding_cost=[3, 2, 1], # h_1, h_2, h_3
stockout_cost=27, # p, charged at downstream node
lead_time=[1, 1, 2], # L_1, L_2, L_3 (into each stage)
demand_mean=10,
demand_standard_deviation=2,
)
print(S_star) # exact echelon base-stock levels
print(C_star) # optimal expected cost
```
]
= Newsvendor Model
Critical Ratio
Probability you *don't stock out*:
- Type-1 service level
- In-stock probability
$F(Q) = P(D lt.eq Q)$: the probability that demand is *at most* $Q$
The left side is the in-stock probability at the optimum:
$
F(Q^*) = c_u / (c_u + c_o) \
Q^* = F^(-1)(c_u / (c_u + c_o))
$
$
c_u = p - c \
c_o = c - s \
$
Where:
- $c$: unit cost cost per unit
- $p$: sale price per unit
- $s$: salvage price per unit
You order $Q$ units. Then demand $D$ shows up. One of two things happens:
- Case 1: You ordered too much ($Q > D$)
You sold $D$ units \
You have $Q - D$ leftover units sitting there, useless
- Case 2: You ordered too little ($Q < D$)
You sold $Q$ units (all you had)\
You missed out on $D − Q$ sales you could have made.
Notice these cases are mutually exclusive. You're either over or under — never both at the same time.
Attach a Cost to Each Mistake
- Each leftover unit costs you $c_o$ (overage cost)
- Each missed sale costs you $c_u$ (underage cost)
So the actual cost for a given realization of demand $D$ is:
$
C = cases(
c_o dot (Q - D) "if" Q > D quad "(overage)",
c_u dot (D - Q) "if" D > Q quad "(underage)",
)
$
The $(⋅)^+$ Notation is a Trick to Combine Both Cases
Writing piecewise functions is clunky. The notation $(x)^+ = max(x, 0)$ lets us collapse both cases into one expression:
$
(Q - D)^+ = cases(
Q - D "if" Q > D,
0 "otherwise",
)
$
$
(D - Q)^+ = cases(
D - Q "if" D > Q,
0 "otherwise",
)
$
So the total cost for a given $D$ becomes:
$
C(Q, D) = c_o dot (Q − D)^+ + c_u dot (D − Q)^+
$
Why does this work? Because at most one of these terms is nonzero for any given $D$. If:
- $D < Q$: the first term is positive and the second is zero.
- $D > Q$: the first term is zero and the second is positive.
- $D = Q$: both are zero
We can't minimize a random quantity directly. We minimize its average over the distribution of demand. That's why we take the expectation:
$
EE[C(Q, D)] = EE[c_o dot (Q − D)^+ + c_u dot (D − Q)^+]
$
By linearity of expectation (constants pull out, sums split):
$
C(Q) = c_o dot EE[(Q − D)^+] + c_u dot EE[(D − Q)^+]
$
We want to find the $Q$ that minimizes:
$
C(Q) &= c_o dot EE[(Q − D)^+] + c_u dot EE[(D − Q)^+] \
Q^∗ &= arg min_QC(Q)
$
== Rewrite the Expectations as Integrals:
$
EE[(Q - D)^+] = integral_0^Q (Q - x) f(x) dif x
$
$
EE[(D - Q)^+] = integral_Q^infinity (x - Q) f(x) dif x
$
- We're averaging $(Q − x)$ over all values of $x$, weighted by how likely each $x$ is. But $(Q - x)^+$ is zero whenever $x > Q$, so we only integrate from 0 to $Q$. Above $Q$, the integrand vanishes.
- We're averaging $(x − Q)$ over all values of $x$, weighted by how likely each $x$ is. But $(x - Q)^+$ is zero whenever $x < Q$, so we only integrate from $Q$ to $infinity$. Below $Q$, the integrand vanishes.
$
C(Q) = c_o dot integral_0^Q (Q - x) f(x) dif x + c_u dot integral_Q^infinity (x - Q) f(x) dif x
$
== Differentiate with Respect to $Q$
We need $(dif C) / (dif Q)$, the value of $Q$ that minimzes $C(Q)$.
1. First Term (upper limit depends on $Q$)
$
dif / (dif Q) [integral_0^Q (Q - x) f(x) dif x]
$
$
&= underbrace(integral_0^Q partial / (partial Q) (Q - x) f(x) dif x, "differentiate inside") + underbrace((Q - x) bar_(x=Q) dot f(Q) dot (dif Q) / (dif Q), "boundary term (upper limit)") \
&= integral_0^Q 1 dot f(x) dif x + (Q - Q) dot f(Q) dot 1 \
&= integral_0^Q f(x) dif x + 0 \
&= F(Q)
$
2. Second term
$
dif / (dif Q) [integral_Q^infinity (x - Q) f(x) dif x]
$
$
&= underbrace(integral_Q^infinity partial / (partial Q) (x - Q) f(x) dif x, "differentiate inside") - underbrace((x - Q) bar_(x=Q) dot f(Q) dot (dif Q) / (dif Q), "boundary term (lower limit)") \
&= integral_Q^infinity (-1) dot f(x) dif x - (Q - Q) dot f(Q) dot 1 \
&= - integral_Q^infinity f(x) dif x - 0 \
&= -(1 - F(Q))
$
Putting it all together
$
(dif C) / (dif Q) = c_o dot F(Q) - c_u dot (1 - F(Q))
$
== Set the Derivative to Zero
$
c_odot F(Q^∗) − c_udot (1 − F(Q^∗)) = 0
$
Solve:
$
c_odot F(Q^∗) = c_u- c_udot F(Q^∗) \
c_odot F(Q^∗) + c_udot F(Q^∗) = c_u− c_udot F(Q^∗) + c_udot F(Q^∗) \
c_odot F(Q^∗) + c_udot F(Q^∗) = c_u \
F(Q^*) (c_o + c_u) = c_u \
F(Q^*) = c_u / (c_u + c_o)
$
This is the critical ratio (or critical fractile). The optimal order quantity is the one where the CDF of demand equals this ratio.
In other words:
$
Q^* = F^(-1) (c_u / (c_u + c_o))
$
== Check It's a Minimum (Not a Maximum)
Take the second derivative of:
$
(dif C) / (dif Q) = c_0 dot F(Q) - c_u dot (1 - F(Q))
$
Using:
$
dif / (dif Q) F(Q) = f(Q) \
dif / (dif Q) [1 - F(Q)] = -f(Q) \
$
We get:
$
(dif^2 C) / (dif Q^2) = c_o dot f(Q) + c_u dot f(Q) = (c_o + c_u) f(Q) gt.eq 0
$
Since this is non-negative (assuming $f(Q) gt 0$), $C(Q)$ is convex and the critical ratio is the global minimum.
#code([$Q^* = F^(-1)("cr")$])[
```py
from scipy.stats import norm
mu, sigma = 50, 12
c_u, c_o = 5, 3
cr = c_u / (c_u + c_o)
z = norm.ppf(cr)
# Q_star = norm.ppf(cr, loc=mu, scale=sigma)
Q_star = mu + sigma * z
```
]
#code([$Q^∗=F^(−1)(0.95)$])[
```py
from scipy.stats import norm
serive_level = 0.95
z = norm.ppf(service_level)
# Q_star = norm.ppf(service_level, loc=mu, scale=sigma)
Q_star = mu + sigma * z
```
]
#table(
columns: 2,
inset: 1em,
[Question], [Integral],
[How often do I stock out?], [
$
integral_Q^infinity f(x) dif x
$
],
[How badly do I stock out on average?], [
$
integral_Q^infinity (x - Q) f(x) dif x
$
],
[How often am I overstocked?], [
$
integral_0^Q f(x) dif x
$
],
[How badly am I overstocked on average?], [
$
integral_0^Q (Q - x) f(x) dif x
$
],
)
== Profit Maximization
The same $Q^*$ can be obtained by maximizing profit rather than minimizing costs:
$
Pi(Q) = p dot EE[min(Q, D)] + s dot EE[(Q−D)^+] − c dot Q \
Q^* = arg max_Q Pi(Q)
$
== Discrete
$
C(Q) = c_o sum_(k=0)^Q (Q - k) p(k) + c_u sum_(k=Q+1)^infinity (k - Q) p(k)
$
Finite differences:
$
Delta C(Q) = C(Q + 1) - C(Q)
$
$
Delta C(Q) = c_o dot F(Q) - c_u dot (1 - F(Q))
$
$
Q^* = min { Q: F(Q) gt.eq c_u / (c_u + c_o) }
$
#code([$$])[
```py
from scipy.stats import poisson
lam = 4
cr = 5 / (5 + 3)
Q = 0
while poisson.cdf(Q, lam) < cr:
Q += 1
Q_star = Q
```
]
= Standard Normal Loss Function
$
L(k) = EE[(Z - k)^+]
$
$L(k)$ is the expected amount by which $Z$ exceeds the threshold $k$:
- $Z < k$: excess is 0
- $Z > k$: excess is $Z - k$. Average this across all the ways $Z$ could come in above $k$, weighted by probability
#result[
$
L(k) = integral_k^infinity (z - k) phi.alt (z) dif z
$
]
#result[
$
L(k) = phi.alt(k) - k dot (1 - Phi(k))
$
]
We want $EE[max(X - k, 0)]$ - the average excess of $X$ above the threshold $k$, where the excess is *zero* when $X lt.eq k$.
Imaging 2 related quantities:
*Quantity A*: Tail Contribution
Sum up the value of $X$ at each tail outcome, weighted by probability:
$
A = integral_k^infinity u dot phi.alt(u) dif u = phi.alt(k)
$
This is "if $X$ is in the tail, what value does it take, on average - weighted by likelihood?" Note that this counts the *full* value of $u$, not the excess $u - k$.
*Quantity B*: Threshold Contribution
$
B = integral_k^infinity k dot phi.alt(u) dif u = k dot [1 - Phi(k)] = k dot "sf"(k)
$
This is "for every tail outcome, count $k$ once, weighted by probability of being there"
*Subtraction*
Each tail outcome at value $u$ contributes $u$ to A and $k$ to B. So the difference A - B contributes (u - k) per tail outcome - *exactly the excess we want*
$
L(k) = A - B = underbrace(integral_k^infinity u phi.alt(u) dif u, "total tail values") - underbrace(integral_k^infinity k phi.alt(u) dif u, "treshold counted across tail")
$
The subtraction strips off the "baseline up to $k$" porton of each tail outcome, leaving only the part *above* $k$. Without that subtraction, we'd be over counting by $k$ for every tail occurrence.
// #import "@preview/cetz:0.4.2"
// #import "@preview/cetz-plot:0.1.3"
#let L(k) = norm.pdf(k) - k * (1.0 - norm.cdf(k))
#cetz.canvas({
import cetz.draw: *
cetz-plot.plot.plot(
size: (10, 6),
x-label: $k$,
y-label: $L(k)$,
x-tick-step: 1,
y-tick-step: 0.5,
x-min: -3, x-max: 3,
y-min: 0, y-max: 3.2,
{
cetz-plot.plot.add(
domain: (-3, 3),
samples: 200,
style: (stroke: rgb("#534AB7") + 2pt),
k => L(k),
)
// Mark L(0) ≈ 0.399
cetz-plot.plot.add(
((0, L(0)),),
mark: "o",
mark-size: 0.15,
mark-style: (fill: rgb("#534AB7"), stroke: none),
style: (stroke: none),
)
}
)
})
#code([$L(k) = phi.alt(k) - k dot (1 - Phi(k))$])[
```py
from scipy.stats import norm
import numpy as np
mu, sigma = 50, 12
c_u, c_o = 5, 3
# Optimal Q from critical ratio
cr = c_u / (c_u + c_o)
k_star = norm.ppf(cr)
Q_star = mu + sigma * k_star
# Standard normal loss function
def L(k):
return norm.pdf(k) - k * (1 - norm.cdf(k))
expected_lost_sales = sigma * L(k_star)
expected_sales = mu - expected_lost_sales
expected_leftover = Q_star - mu + sigma * L(k_star)
```
]
#table(
columns: 2,
inset: 1em,
[$ phi.alt(u) $], [PDF],
[$ phi.alt'(u) = - u phi.alt(u) $], [Derivative of PDF],
[$ u phi.alt(u) = -phi.alt'(u) $], [Negative derivative of PDF],
)
#let norm_pdf(x, mu, sigma) = {
let var = calc.pow(sigma, 2)
1.0 / calc.sqrt(2.0 * calc.pi * var) * calc.exp(-calc.pow(x - mu, 2) / (2.0 * var))
}
#let _phi(x) = norm_pdf(x, 0.0, 1.0)
#let u_phi(x) = x * _phi(x)
#let zero(x) = 0.0
#let k = -1.0
// Plot 1: the bell curve ϕ(u) with k marked
#cetz.canvas({
import cetz.draw: *
cetz-plot.plot.plot(
size: (10, 6),
x-label: $u$,
y-label: $phi.alt(u)$,
x-tick-step: 5,
y-tick-step: 1,
x-min: -3, x-max: 3,
y-min: 0, y-max: 0.45,
{
cetz-plot.plot.add(
domain: (-3, 3),
samples: 200,
style: (stroke: rgb("#534AB7") + 2pt),
x => _phi(x),
)
cetz-plot.plot.add-vline(k, style: (stroke: red + 1pt))
}
)
})
// Plot 2: u·ϕ(u) with tail area from k to ∞ shaded
#cetz.canvas({
import cetz.draw: *
cetz-plot.plot.plot(
size: (10, 6),
x-label: $u$,
y-label: $- u dot phi.alt(u)$,
x-tick-step: 5,
y-tick-step: 1,
x-min: -3, x-max: 3,
y-min: -0.3, y-max: 0.3,
{
// Shade tail area between u·ϕ(u) and the x-axis from k to 3
cetz-plot.plot.add-fill-between(
domain: (k, 3),
samples: 100,
style: (fill: rgb("#EF9F2799"), stroke: none),
x => - u_phi(x),
zero,
)
cetz-plot.plot.add(
domain: (-3, 3),
samples: 200,
style: (stroke: rgb("#534AB7") + 2pt),
x => - u_phi(x),
)
cetz-plot.plot.add-hline(0, style: (stroke: black + 0.5pt))
cetz-plot.plot.add-vline(k, style: (stroke: red + 1pt))
}
)
})
#cetz.canvas({
import cetz.draw: *
cetz-plot.plot.plot(
size: (10, 6),
x-label: $u$,
y-label: $u dot phi.alt(u)$,
x-tick-step: 5,
y-tick-step: 1,
x-min: -3, x-max: 3,
y-min: -0.3, y-max: 0.3,
{
// Shade tail area between u·ϕ(u) and the x-axis from k to 3
cetz-plot.plot.add-fill-between(
domain: (k, 3),
samples: 100,
style: (fill: rgb("#EF9F2799"), stroke: none),
u_phi,
zero,
)
cetz-plot.plot.add(
domain: (-3, 3),
samples: 200,
style: (stroke: rgb("#534AB7") + 2pt),
u_phi,
)
cetz-plot.plot.add-hline(0, style: (stroke: black + 0.5pt))
cetz-plot.plot.add-vline(k, style: (stroke: red + 1pt))
}
)
})
= Differentiating Normal Distribution
$
f(x) = 1 / sqrt(2 pi sigma^2) exp(- (x - mu)^2 / (2 sigma^2))
$
We want $f'(x) = dif / (dif x) f(x)$
- $mu$, $sigma$: constants
- $x$: variable
Step 1. Structure
Two factors:
$
f(x) = underbrace(1 / sqrt(2 pi sigma^2), "constant") dot underbrace(exp(- (x - mu)^2 / (2 sigma^2)), "function of" x)
$
First factor is a constant (does not depend on $x$):
$
f'(x) = 1 / sqrt(2 pi sigma^2) dot dif / (dif x) exp(- (x - mu)^2 / (2 sigma^2))
$
Step 2: Set up the chain rule
$
u(x) = - (x - mu)^2 / (2 sigma^2)
$
Then
$
exp(- (x - mu)^2 / (2 sigma^2)) = exp(u(x))
$
The chain rule:
$
dif / (dif x) exp(u(x)) = exp(u(x)) dot u'(x)
$
We need 2 things:
- $exp(u(x)) = exp(u(x))$
- $u'(x)$
Step 3. Differentiate $u(x)$
$
u(x) = - (x - mu)^2 / (2 sigma^2)
$
3.a. Pull out constant
$
u(x) = - 1 / (2 sigma^2) dot (x - mu)^2
$
The factor $- 1 / 2 sigma^2$ depends only on $sigma$, not $x$. Constants pull out of derivatives:
$
u'(x) = - 1 / (2 sigma^2) dot dif / (dif x) (x - mu)^2
$
Now we need $dif / (dif x) (x - mu)^2$
3.b. Apply chain rule again to $(x - mu)^2$
This is itself a composition:
- Outer function: $v^2$ where $v = x - mu$
- Inner function: $v(x) = x - mu$
Chain rule:
$
dif / (dif x) (x - mu)^2 = dif / (dif v) (v^2) dot (dif v) / (dif x)
$
Outer derivative:
$
dif / (dif v) (v^2) = 2 v = 2 (x - mu)
$
Inner derivative: $v(x) = x - mu$
Since $mu$ is a constant, $(dif u) / (dif x) = 0$. So:
$
(dif v) / (dif x) = dif / (dif x) (x - mu) = dif / (dif x) (x) - dif / (dif x) (mu) = 1 - 0 = 1
$
Combine:
$
dif / (dif x) (x - mu)^2 = 2 (x - mu) dot 1 = 2 (x - mu)
$
3.c. Put Step 3 together
Substitute back into expression for $u'(x)$
$
u'(x) = - 1 / (2 sigma^2) dot (x - mu)
$
Simplify be canceling the 2's:
$
u'(x) = - (2 (x - mu) / (2 sigma^2)) = - (x - mu) / sigma^2
$
So:
$
u'(x) = - (x - mu) / sigma^2
$
Step 4. Apply chain tule from Step 2
We had:
$
dif / (dif x) exp(u(x)) = exp(u(x)) dot u'(x)
$
Plug in:
- $exp(u(x)) = exp(- (x - mu)^2 / (2 sigma^2))$
- $u'(x) = - (x - mu) / sigma^2$
So:
$
dif / (dif x) exp(- (x - mu)^2 / (2 sigma^2)) = exp(- (x - mu)^2 / (2 sigma^2)) dot (- (x - mu) / sigma^2)
$
Move the factor to the front for clarity:
$
- (x - mu) / sigma^2 dot exp(- (x - mu)^2 / (2 sigma^2))
$
Step 5. Putting everything together
Recall from Step 1:
$
f'(x) = 1 / sqrt(2 pi sigma^2) dot dif / (dif x) exp(- (x - mu)^2 / (2 sigma^2))
$
Substitute what we found:
$
f'(x) = 1 / sqrt(2 pi sigma^2) dot [- (x - mu) / sigma^2 dot exp(- (x - mu)^2 / (2 sigma^2))]
$
Pull the $- (x - mu) / sigma^2$ out front:
$
f'(x) = - (x - mu) / sigma^2 dot 1 / sqrt(2 pi sigma^2) dot exp(- (x - mu)^2 / (2 sigma^2))
$
Step 6. Recognize $f(x)$ on the right side
Look at the last two factors
$
1 / sqrt(2 pi sigma^2) dot exp(- (x - mu)^2 / (2 sigma^2)) = f(x)
$
That's exactly the original PDF. So:
$
f'(x) = - (x - mu) / sigma^2 dot f(x)
$
Step 7. Standard Normal
Does this collapse to $phi.alt'(z)$ when $mu = 0$ and $sigma = 1$?
Plug in $mu = 0$, $sigma = 1$:
$
f'(x) = - (x - 0) / 1^2 dot f(x) = - x dot f(x)
$
With $mu = 0$ and $sigma = 1$, $f(x)$ becomes $phi.alt(x)$, so:
$
phi.alt(x) = -x dot phi.alt(x)
$
#table(
columns: 2,
inset: 1em,
[Distribution], [Derivative],
[General Normal $f(x)$], [
$
f'(x) = - (x - mu) / sigma^2 dot f(x)
$
],
[Standard Normal $phi.alt(x)$], [
$
phi.alt'(z) = -z dot phi.alt(z)
$
],
)
= Clark-Scarf view (stochastic service)
Echelon inventory position at stage $j$:
// #line(width: 100%)
Demand
- $mu$: 100 units
- $sigma$: 20 units
Lead time:
- $L_(f, w)$: 4 days
- $L_(f)$: 5 days
The question: *where do we keep safety stock?*