Practical Tools for OR, ML and DS

Lecture 8: Inventory #2 Newsvendor

February 13th, 2020 Recall the assumptions in EOQ

• Known and constant demand rate • Known and constant lead time • Instantaneous receipt of material • No quantity discounts • No stock outs permitted • No penalty costs (only order & holding costs) These assumptions don’t fit in some problems This set of assumptions is called the “Newsvendor” model

Let’s consider a totally different set of assumptions

• We plan for only a single period • Demand is random • Deliveries are made before the demand • Stockouts are allowed • No holding costs • Penalty costs are proportional to the underage and overage amounts • No ordering costs: the cost of buying an item is accounted for by the overage penalty Let’s consider a totally different set of assumptions

• We plan for only a single period • Demand is random • Deliveries are made before the demand • Stockouts are allowed • No holding costs • Penalty costs are proportional to the underage and overage amounts • No ordering costs: the cost of buying an item is accounted for by the overage penalty

This set of assumptions is called the “Newsvendor” model Newsvendor model

Notation

D = demand, a F (x) = cumulative distribution function of demand

co = penalty cost per unit of inventory remaining at the end of the period, “overage” cost

cu = penalty cost per unit of unsatisfied demand, “underage” cost G(Q, D) = total cost when Q units are ordered and D is the demand Overage

• We ordered too many! • # of units of overage is Q − D if Q is bigger than D, otherwise zero.

 Q − D for Q ≥ D # of units, overage = 0 for Q < D = max{Q − D, 0} = (Q − D)+. Underage

• We ordered too few! • # of units of overage is D − Q if D is bigger than Q, otherwise zero.

 D − Q for D ≥ Q # of units, underage = 0 for D < Q = max{D − Q, 0} = (D − Q)+. Put this together to get the cost

G(Q, D) = co(units of overage) + cu(units of underage) + + = co(Q − D) + cu(D − Q)

Note: Because D is random, G(Q, D) is random Example Q = 10 gives an average total cost of 6.014 Q = 1 gives an average total cost of 6.95 Q = 5 gives an average total cost of 2.952 Average total cost vs. Q We’ll minimize the expected cost

• We don’t know D when we choose Q, so we can’t choose Q to minimize G(Q, D). • Instead, we’ll choose Q to minimize E[G(Q, D)] • The expected cost is:

+ + E[G(Q, D)] = E[co(Q − D) + cu(D − Q) ]

• We will again find the optimum Q by setting derivative d equal to zero, dQ E[G(Q, D)] = 0, and solving for Q. d What is dQ E[G(Q, D)]?

Let’s rewrite E[G(Q, D)]. The random variable D can be discrete or continuous. Recall from ENGRD 2700:

• If X is a discrete random variable with probability mass

function P(X = i) = pi for i = 0, 1, 2,..., then ∞ X Eg(X ) = g(i)pi . i=0 • If X is a continuous random variable with probability density function f (x), then Z ∞ Eg(X ) = g(x)f (x)dx. −∞ d What is dQ E[G(Q, D)]?

If D is discrete then

+ + E[G(Q, D)] = E[co(Q − D) + cu(D − Q) ] ∞ X + + = (co(Q − i) + cu(i − Q) )pi i=0 bQc ∞ X X = co(Q − i)pi + cu(i − Q)pi . i=0 i=bQc+1 So, Q is not an integer, the derivative is

bQc ∞ d X X dQ E[G(Q, D)] = copi − cupi i=0 i=bQc+1

= coP(D < Q) − cuP(D > Q) d What is dQ E[G(Q, D)]?

In general,

d d dQ E[G(Q, D)] = E[ dQ G(Q, D)] d + + = E[ dQ (co(Q − D) + cu(D − Q) )] d + d + = E[ dQ co(Q − D) + dQ cu(D − Q) ] d + d + = E[ dQ co(Q − D) ] + E[ dQ cu(D − Q) ],

where the first equality assumes that D is discrete and Q is not an integer, or D is a continuous random variable (ensuring G(Q, D) is differentiable at Q). d What is dQ E[G(Q, D)]?

d + dQ co(Q − D) =?

1. −co when Q > D, 0 when Q < D, undefined when Q = D

2. 0 when Q > D, −co when Q < D, undefined when Q = D

3. co when Q > D, 0 when Q < D, undefined when Q = D

4. 0 when Q > D, co when Q < D, undefined when Q = D 5.0 d What is dQ E[G(Q, D)]?

d + dQ co(Q − D) =?

1. −co when Q > D, 0 when Q < D, undefined when Q = D

2. 0 when Q > D, −co when Q < D, undefined when Q = D

3. co when Q > D, 0 when Q < D, undefined when Q = D

4. 0 when Q > D, co when Q < D, undefined when Q = D 5.0 d What is dQ E[G(Q, D)]?

d + dQ cu(D − Q) =?

1. −cu when Q > D, 0 when Q < D, undefined when Q = D

2. 0 when Q > D, −cu when Q < D, undefined when Q = D

3. cu when Q > D, 0 when Q < D, undefined when Q = D

4. 0 when Q > D, cu when Q < D, undefined when Q = D 5.0 d What is dQ E[G(Q, D)]?

d + dQ cu(D − Q) =?

1. −cu when Q > D, 0 when Q < D, undefined when Q = D

2.0 when Q > D, −cu when Q < D, undefined when Q = D

3. cu when Q > D, 0 when Q < D, undefined when Q = D

4. 0 when Q > D, cu when Q < D, undefined when Q = D 5.0 d What is dQ E[G(Q, D)]?

d d + d + dQ E[G(Q, D)] = E[ dQ co(Q − D) ] + E[ dQ cu(D − Q) ] 1 1 = E[co {Q>D}] + E[−cu {QD}] − cuE[ {Q

Note: 1{Q>D} means “1 when Q > D, and 0 otherwise”. 1 What is E[ {Q>D}]?

1. P(Q < D) 2. P(Q > D) 3. E[Q] 4. E[Q − D] 5. None of the above

Note: 1{Q>D} means “1 when Q > D, and 0 otherwise”. = coP(Q > D) − cuP(Q < D)

= coP(D < Q) − cu(1 − P(D ≤ Q))

= coP(D ≤ Q) − cu(1 − P(D ≤ Q))

= −cu + (cu + co)P(D ≤ Q)

d What is dQ E[G(Q, D)]?

In general,

d 1 1 dQ E[G(Q, D)] = coE[ {Q>D}] − cuE[ {Q

= coP(D ≤ Q) − cu(1 − P(D ≤ Q))

= −cu + (cu + co)P(D ≤ Q)

d What is dQ E[G(Q, D)]?

In general,

d 1 1 dQ E[G(Q, D)] = coE[ {Q>D}] − cuE[ {Q

= coP(Q > D) − cuP(Q < D) d What is dQ E[G(Q, D)]?

In general,

d 1 1 dQ E[G(Q, D)] = coE[ {Q>D}] − cuE[ {Q

= coP(Q > D) − cuP(Q < D)

= coP(D < Q) − cu(1 − P(D ≤ Q))

= coP(D ≤ Q) − cu(1 − P(D ≤ Q))

= −cu + (cu + co)P(D ≤ Q)

where the penultimate inequality assumes D is a continuous random variable or D is a discrete random variable taking integer values and Q is not an integer. Does this Q∗ give us minimum cost?

d Setting dQ E[G(Q, D)] = 0

d dQ E[G(Q, D)] = −cu + (cu + co)P(D ≤ Q) = 0

means we want Q∗ so that

∗ cu P(D ≤ Q ) = . cu + co d Setting dQ E[G(Q, D)] = 0

d dQ E[G(Q, D)] = −cu + (cu + co)P(D ≤ Q) = 0

means we want Q∗ so that

∗ cu P(D ≤ Q ) = . cu + co

Does this Q∗ give us minimum cost? d Setting dQ E[G(Q, D)] = 0

For Q < Q∗:

d dQ E[G(Q, D)] = −cu + (cu + co)P(D ≤ Q) ∗ < −cu + (cu + co)P(D ≤ Q ) = 0

So costs are decreasing in Q until Q∗! d Setting dQ E[G(Q, D)] = 0

For Q > Q∗:

d dQ E[G(Q, D)] = −cu + (cu + co)P(D ≤ Q) ∗ > −cu + (cu + co)P(D ≤ Q ) = 0

So costs are increasing in Q after Q∗! Pictorially How to find Q∗?

∗ cu P(D ≤ Q ) = . cu + co In words: We want Q∗ so that the probability that the demand c is Q∗ or less is u . cu + co

If F (·) is cdf of D, then in terms of cdf: c F (Q∗) = u . cu + co Continuous random variables have cdfs that are invertible!

How to find Q∗?

If F (·) has an inverse F −1(·) then c Q∗ = F −1( u ). cu + co How to find Q∗?

If F (·) has an inverse F −1(·) then c Q∗ = F −1( u ). cu + co

Continuous random variables have cdfs that are invertible! F −1(·) and quantiles

• F −1(q) is called the “q-quantile” (of the random variable that has cdf F (·)) • Excel, Python, and R implement the inverse cumulative distribution function for many common distributions • Here are some useful functions in Excel: • NORMAL.INV(probability, m, v) • LOGNORM.INV(probability, m, s) • GAMMA.INV(probability, alpha, beta) • While U − L > ε: L + U • Choose M = . 2 cu • If P(D ≤ M) ≥ , set U = M cu + co cu • If P(D ≤ M) < , set L = M. cu + co

How to find Q∗ without an inverse?

∗ cu P(D ≤ Q ) = . cu + co You can use bisection search to find Q∗ (upto some ε):

• Set L = 0 and find an integer U large enough that

cu P(D ≤ U) ≤ . cu + co • While U − L > ε: L + U • Choose M = . 2 cu • If P(D ≤ M) ≥ , set U = M cu + co cu • If P(D ≤ M) < , set L = M. cu + co

How to find Q∗ without an inverse?

∗ cu P(D ≤ Q ) = . cu + co You can use bisection search to find Q∗ (upto some ε):

• Set L = 0 and find an integer U large enough that

cu P(D ≤ U) ≤ . cu + co

To do this, guess U = 1, check P(D ≤ U), and keep cu doubling U until P(D ≤ U) ≥ . cu + co How to find Q∗ without an inverse?

∗ cu P(D ≤ Q ) = . cu + co You can use bisection search to find Q∗ (upto some ε):

• Set L = 0 and find an integer U large enough that

cu P(D ≤ U) ≤ . cu + co • While U − L > ε: L + U • Choose M = . 2 cu • If P(D ≤ M) ≥ , set U = M cu + co cu • If P(D ≤ M) < , set L = M. cu + co Example

• Lowe’s sells holiday lights for the winter holiday season. During the holiday season, the lights sell for $2.00 each. • Since the product is seasonal, the store decides to sell all unsold lights during the January clearance for $0.50 each. • Each string of lights costs the store $1. • Past demand has followed a log-normal(7,3) distribution, which means that the natural log of demand is normal with mean 7 and 3. • Find the optimal order quantity for the season. Example

We see that if Lowe’s orders too many, the cost is $0.50 each. If they order too few, each lost sale represents $1 of unrealized

profit. Thus, co = $0.50, and cu = $1. Hence we want Q so that: c 1 F (Q) = u = = 0.6667 cu + co 1 + .50 where F is the cdf of the log-normal(7,3) distribution. Example

• Excel’s LOGNORM.INV function tells us:

• The syntax of LOGNORM.INV is:

• So, we should stock 3992 holiday lights Newsvendor with the

If you have a computer:

• Suppose demand is D ∼Normal(µ, σ2) ∗ −1 • The optimal order quantity Q is F (cu/(cu + co)) • In Excel, we can calculate this via NORM.INV Newsvendor with the Normal Distribution

If you don’t have a computer: We’ll now show you how to compute Q∗ and the expected 2 cost E[G(Q, D)] when D ∼ Normal(µ, σ ) without a computer using pencil/paper and normal distribution tables. Normal Distribution Tables Computing Q∗

∗ −1 • Q = F (cu/(cu + co))

∗ cu/(cu + co) = P(D ≤ Q ) ∗ = P(µ + σZ ≤ Q ) where Z ∼ N(0, 1) ∗ = P(Z ≤ (Q − µ)/σ)

∗ ∗ • Step 1: Find z such that cu/(cu + co) = P(Z ≤ z ) from the normal distribution tables Step 2: Q∗ = µ + σz ∗ Computing the expected cost if D is normal

+ + E[G(Q, D)] = E[co(Q − D) + cu(D − Q) ] Z Q Z ∞ = co (Q − x)f (x)dx + cu (x − Q)f (x)dx. −∞ Q

Z Q Let’s try and calculate (Q − x)f (x)dx. −∞ Computing the expected cost if D is normal

Transform to standard normal: y = (x − µ)/σ

Z Q Z (Q−µ)/σ (Q − x)f (x)dx = (Q − (σy + µ))ϕ(y)dy −∞ −∞ Z (Q−µ)/σ Z (Q−µ)/σ = (Q − µ)ϕ(y)dy −σ yϕ(y)dy. −∞ −∞ | {z } (Q−µ)P(D≤Q) Computing the expected cost if D is normal

Z t • What about xϕ(x)dx? −∞ 1 2 • ϕ(x) = √ e−x /2 2π • Here is a weird little fact: 1 2 d ϕ(x) = −x √ e−x /2 = −xϕ(x)! dx 2π

Z t Z t d So: xϕ(x)dx = − ( dx ϕ(x))dx −∞ −∞ = −(ϕ(t) − ϕ(−∞)) = −ϕ(t)! Computing the expected cost if D is normal

Z Q Z (Q−µ)/σ (Q − x)f (x)dx = (Q − (σx + µ))ϕ(x)dx −∞ −∞ Z (Q−µ)/σ Z (Q−µ)/σ = (Q − µ)ϕ(x)dx −σ xϕ(x)dx . −∞ −∞ | {z } | {z } (Q−µ)P(D≤Q) −ϕ((Q−µ)/σ) Computing the expected cost if D is normal

Z Q (Q − x)f (x)dx = (Q − µ)P(D ≤ Q) + σϕ((Q − µ)/σ) −∞ = σ(zP(D ≤ Q) + ϕ(z)) = σ(zΦ(z) + ϕ(z))

where z = (Q − µ)/σ. Computing the expected cost if D is normal

Z Q Z ∞ E[G(Q, D)] = co (Q − x)f (x)dx + cu (x − Q)f (x)dx −∞ Q Z ∞ = coσ(zΦ(z) + ϕ(z)) + cu (x − Q)f (x)dx. Q Z ∞ What about (x − Q)f (x)dx? Q Computing the expected cost if D is normal

Let D˜ = −D, a normally distributed random variable with meanµ ˜ = −µ and variance σ2. Now Z ∞ Z −Q (x − Q)f (x)dx = (−x − Q)f˜(x)dx Q −∞ Z Q˜ = (Q˜ − x)f˜(x). −∞

Can use previous result:

Z Q˜ (Q˜ − x)f˜(x)dx = σ(˜zΦ(˜z) + ϕ(˜z)). −∞ Note:z ˜ = (Q˜ − µ˜)/σ = (−Q − (−µ))/σ = (µ − Q)/σ = −z.

Computing the expected cost if D is normal

Z Q Z ∞ E[G(Q, D)] = co (Q − x)f (x)dx + cu (x − Q)f (x)dx −∞ Q

= coσ(zΦ(z) + ϕ(z)) + cuσ(˜zΦ(˜z) + ϕ(˜z)).

where z = (Q − µ)/σ andz ˜ = (Q˜ − µ˜)/σ. Computing the expected cost if D is normal

Z Q Z ∞ E[G(Q, D)] = co (Q − x)f (x)dx + cu (x − Q)f (x)dx −∞ Q

= coσ(zΦ(z) + ϕ(z)) + cuσ(˜zΦ(˜z) + ϕ(˜z)).

where z = (Q − µ)/σ andz ˜ = (Q˜ − µ˜)/σ. Note:z ˜ = (Q˜ − µ˜)/σ = (−Q − (−µ))/σ = (µ − Q)/σ = −z. Finally note that ϕ(˜z) = ϕ(−z) = ϕ(z) because ϕ is symmetric about 0.

Computing the expected cost if D is normal

E[G(Q, D)] = coσ(zΦ(z) + ϕ(z)) + cuσ(˜zΦ(˜z) + ϕ(˜z))

= coσ(zΦ(z) + ϕ(z)) + cuσ(−zΦ(−z) + ϕ(−z)).

where z = (Q − µ)/σ. Computing the expected cost if D is normal

E[G(Q, D)] = coσ(zΦ(z) + ϕ(z)) + cuσ(˜zΦ(˜z) + ϕ(˜z))

= coσ(zΦ(z) + ϕ(z)) + cuσ(−zΦ(−z) + ϕ(−z)).

where z = (Q − µ)/σ. Finally note that ϕ(˜z) = ϕ(−z) = ϕ(z) because ϕ is symmetric about 0. Computing the expected cost if D is normal

E[G(Q, D)] = coσ(zΦ(z) + ϕ(z)) + cuσ(−zΦ(−z) + ϕ(−z))

= coσ(zΦ(z) + ϕ(z)) − cuσ(zΦ(−z) − ϕ(z))

where z = (Q − µ)/σ.