Professional Documents
Culture Documents
1
𝑅(ℎ) = (|90,000 − ℎ| + |94,000 − ℎ| + |96,000 − ℎ|
5
+ |120,000 − ℎ| + |160,000 − ℎ|)
Many possible predictions
▶ Of these four options, the median has the lowest MAE. But
is it the best possible prediction overall?
A general formula for the mean absolute error
▶ Suppose we collect 𝑛 salaries, 𝑦1 , 𝑦2 , … , 𝑦𝑛 .
1
𝑅(ℎ) = (|𝑦 − ℎ| + |𝑦2 − ℎ| + ... + |𝑦𝑛 − ℎ|)
𝑛 1
𝑛
1
𝑅(ℎ) = ∑ |𝑦 − ℎ|
𝑛 𝑖=1 𝑖
The best prediction
▶ We want the best prediction, ℎ∗ (i.e. 𝑅(ℎ∗ ) = minℎ>0 𝑅(ℎ)).
ℎ∗ = argminℎ>0 𝑅(ℎ)
Discussion Question
minℎ>0 𝑅(ℎ)
min 𝑅(ℎ)?
Minimizing with calculus
Given an arbitrary function 𝑅, under which conditions the
equation
𝑑
𝑅(ℎ) = 0
𝑑ℎ
return to us the solution of the optimization problem
min 𝑅(ℎ)?
𝑛
𝑑 1 𝑑
⇔ 𝑅(ℎ) = ( ∑ |𝑦 − ℎ|)
𝑑ℎ 𝑛 𝑑ℎ 𝑖=1 𝑖
Minimizing with calculus
▶ Calculus: take derivative with respect to ℎ, set equal to
zero, solve.
Given
𝑛
1
𝑅(ℎ) = ∑ |𝑦𝑖 − ℎ|
𝑛 𝑖=1
𝑑
What is 𝑑ℎ
𝑅(ℎ)?
𝑛
𝑑 𝑑 1
𝑅(ℎ) = ( ∑ |𝑦 − ℎ|)
𝑑ℎ 𝑑ℎ 𝑛 𝑖=1 𝑖
𝑛
𝑑 1 𝑑
⇔ 𝑅(ℎ) = ( ∑ |𝑦 − ℎ|)
𝑑ℎ 𝑛 𝑑ℎ 𝑖=1 𝑖
𝑛
𝑑 1 𝑑
⇔ 𝑅(ℎ) = ∑ |𝑦 − ℎ|
𝑑ℎ 𝑛 𝑖=1 𝑑ℎ 𝑖
Uh oh...
▶ 𝑅 is not differentiable.
https://www.desmos.com/calculator
Discussion Question
A) positive to negative
B) negative to positive
C) positive to zero.
D) negative to zero.
Discussion Question
A) positive to negative
B) negative to positive
C) positive to zero.
D) negative to zero.
Answer: B
What we know from Calculus
Source:
https://en.wikipedia.org/wiki/Maxima_and_minima
What we know from Calculus
First, start with 𝑓(𝑥) = |𝑥| and then shift the plot by 𝑎 units to
the right.
Absolute value functions
Recall, 𝑓(𝑥) = |𝑥 − 𝑎| is an absolute value function centered at
𝑥 = 𝑎.
𝑎=2
Sums of absolute values
▶ Let
1
𝑅(ℎ) = (|ℎ − 𝑦1 | + |ℎ − 𝑦2 | + … + |ℎ − 𝑦𝑛 |)
𝑛
The slope of the mean absolute error
𝑅(ℎ) is a sum of absolute value functions (times 𝑛1 ):
1
𝑅(ℎ) = (|ℎ − 𝑦1 | + |ℎ − 𝑦2 | + … + |ℎ − 𝑦𝑛 |)
𝑛
We have:
1
𝑅(ℎ) = ( ∑ |ℎ − 𝑦𝑖 | + ∑ |ℎ − 𝑦𝑖 |)
𝑛 𝑖∶𝑦 <ℎ 𝑖∶𝑦 >ℎ
𝑖 𝑖
The slope of the mean absolute error
𝑅(ℎ) is a sum of absolute value functions (times 𝑛1 ):
1
𝑅(ℎ) = (|ℎ − 𝑦1 | + |ℎ − 𝑦2 | + … + |ℎ − 𝑦𝑛 |)
𝑛
We have:
1
𝑅(ℎ) = ( ∑ |ℎ − 𝑦𝑖 | + ∑ |ℎ − 𝑦𝑖 |)
𝑛 𝑖∶𝑦 <ℎ 𝑖∶𝑦 >ℎ
𝑖 𝑖
1
⇔ 𝑅(ℎ) = ( ∑ (ℎ − 𝑦𝑖 ) + ∑ (𝑦𝑖 − ℎ))
𝑛 𝑖∶𝑦 <ℎ 𝑦 >ℎ
𝑖 𝑖
The slope of the mean absolute error
𝑅(ℎ) is a sum of absolute value functions (times 𝑛1 ):
1
𝑅(ℎ) = (|ℎ − 𝑦1 | + |ℎ − 𝑦2 | + … + |ℎ − 𝑦𝑛 |)
𝑛
We have:
1
𝑅(ℎ) = ( ∑ |ℎ − 𝑦𝑖 | + ∑ |ℎ − 𝑦𝑖 |)
𝑛 𝑖∶𝑦 <ℎ 𝑖∶𝑦 >ℎ
𝑖 𝑖
1
⇔ 𝑅(ℎ) = ( ∑ (ℎ − 𝑦𝑖 ) + ∑ (𝑦𝑖 − ℎ))
𝑛 𝑖∶𝑦 <ℎ 𝑦 >ℎ
𝑖 𝑖
1
⇔ 𝑅(ℎ) = ( ∑ 1 − ∑ 1)ℎ + constant
𝑛 𝑖∶𝑦 <ℎ 𝑖∶𝑦 >ℎ
𝑖 𝑖
The slope of the mean absolute error
The slope of 𝑅 at ℎ is:
1
𝑛
⋅ [(# of 𝑦𝑖 ’s < ℎ) - (# of 𝑦𝑖 ’s > ℎ)]
Where the slope’s sign changes
Discussion Question
A) ℎ = mean of 𝑦1 , … , 𝑦𝑛
B) ℎ = median of 𝑦1 , … , 𝑦𝑛
C) ℎ = mode of 𝑦1 , … , 𝑦𝑛
Where the slope’s sign changes
Discussion Question
A) ℎ = mean of 𝑦1 , … , 𝑦𝑛
B) ℎ = median of 𝑦1 , … , 𝑦𝑛
C) ℎ = mode of 𝑦1 , … , 𝑦𝑛
Answer: B
The median minimizes mean absolute error,
when 𝑛 is odd
Answer: D
Plotting the mean absolute error, with an even
number of data points
Discussion Question
|𝑦 − ℎ|2 = (𝑦 − ℎ)2
𝑑
(𝑓 ∘ 𝑔)′ = [𝑓(𝑔(𝑥))] = 𝑓 ′ (𝑔(𝑥)) ⋅ 𝑔′ (𝑥)
𝑑𝑥
Therefore:
The squared error
Reminder that:
𝑑 𝑛
𝑥 = 𝑛 ⋅ 𝑥 𝑛−1
𝑑𝑥
Thus:
𝑑 2
𝑥 =2⋅𝑥
𝑑𝑥
Reminder about the derivative of composite function:
𝑑
(𝑓 ∘ 𝑔)′ = [𝑓(𝑔(𝑥))] = 𝑓 ′ (𝑔(𝑥)) ⋅ 𝑔′ (𝑥)
𝑑𝑥
Therefore:
𝑑 𝑑
(𝑦 − ℎ)2 = 2 ⋅ (𝑦 − ℎ) ⋅ (𝑦 − ℎ) =
𝑑ℎ 𝑑ℎ
𝑑𝑦 𝑑ℎ
= 2 ⋅ (𝑦 − ℎ) ⋅ ( − ) = 2 ⋅ (𝑦 − ℎ) ⋅ (−1) = 2(ℎ − 𝑦)
𝑑ℎ 𝑑ℎ
The mean squared error
▶ Suppose we predicted a future salary of ℎ1 = 150,000
before collecting data.
𝑛
1 2
𝑅sq (ℎ) = ∑(𝑦 − ℎ)
𝑛 𝑖=1 𝑖
Discussion Question
Discussion Question
Answer: D
The new idea
▶ Make prediction by minimizing the mean squared error:
𝑛
1 2
𝑅sq (ℎ) = ∑(𝑦 − ℎ)
𝑛 𝑖=1 𝑖