But where do the \(\alpha\) and \(\beta\) values come from? How do we estimate the “line of best fit”?
An Optimization Problem
We want to find values for \(\alpha\) and \(\beta\) that minimize the sum of squared error.
. . .
sse <-function(a,b){ y <- d$final # outcome x <- d$midterm # explanatory variable predicted_y <- a + b*x error <- y - predicted_yreturn( sum(error^2) )}
An Optimization Problem
plot(d$midterm, d$final,xlab ='Midterm Grade', ylab ='Final Grade')abline(a =10, b =0.5) # too shallow
sse(a =10, b =0.5)
[1] 54632.59
An Optimization Problem
plot(d$midterm, d$final, xlab ='Midterm Grade', ylab ='Final Grade') abline(a =0, b =1.2) # too steep!
sse(a =0, b =1.2)
[1] 61043.95
An Optimization Problem
We could keep hunting blindly for values \(\alpha\) and \(\beta\) that minimize the sum of squared errors, or we could take a more systematic approach…
Nonlinear functions are confusing and scary. Sometimes the slope is positive. Sometimes it’s negative. Sometimes it’s zero. And unlike with linear functions, just looking at the formula gives you no indication what the slope is at any point.
Newton & Leibniz
Developed/Discovered: - The theory of universal gravitation - Three Laws of Motion - The Nature of Light - And, as a side project so he’d have mathematical notation for those other projects, he created calculus
(NB Fall 2020: Newton did some of his best work while stuck at home during an epidemic. So, you know, get to it.)
Also invented calculus, but with better notation. A philosophical optimist who believed we lived in the “best of all possible worlds”, a sentiment parodied by Voltaire, and perhaps belied by the fact that Newton took all the credit for inventing calculus.
The Key Insight
Any curve becomes a straight line if you “zoom in” far enough.
. . .
The Key Insight
Any curve becomes a straight line if you “zoom in” far enough.
The Key Insight
Any curve becomes a straight line if you “zoom in” far enough.
The Key Insight
Any curve becomes a straight line if you “zoom in” far enough.
The Key Insight
Any curve becomes a straight line if you “zoom in” far enough.
The Key Insight
Any curve becomes a straight line if you “zoom in” far enough.
The Key Insight
Any curve becomes a straight line if you “zoom in” far enough.
The point is that, in the limit, as you shrink the interval smaller and smaller (infinitesimally small), the function is better and better approximated by a straight line. And we already know the slope of a straight line, so the problem is solved! (That line is called the tangent line FYI.)
The function \(f'(x)\), outputs the slope of \(f(x)\) at every point.
Derivative Shortcuts
Good news! You don’t have to go through that process every time. Mathematicians have done it for you, and have discovered a whole bunch of useful shortcuts.
Shortcut 1: The Power Rule
If \(f(x) = ax^k\), then \(f'(x) = kax^{k-1}\)
. . .
Example: If \(f(x) = 5x^4\), then \(f'(x) = 20x^3\).
. . .
Practice Problem: Let \(f(x) = 2x^3\). What is \(f'(x)\)?
. . .
\[f'(x) = 6x^2\]
Shortcut 2: The Sum Rule
The derivative of a sum is equal to the sum of derivatives.
If \(f(x) = g(x) + h(x)\), then \(f'(x) = g'(x) + h'(x)\)
. . .
Example: If \(f(x) = x^3 + x^2\), then \(f'(x) = 3x^2 + 2x\)
. . .
Practice Problem: If \(f(x) = 2x^3 + x^2\), what is \(f'(x)\)?
. . .
\[f'(x) = 6x^2 + 2x\]
Shortcut 3: The Constant Rule
The derivative of a constant is zero.
If \(f(x) = c\), then \(f'(x) = 0\)
. . .
Example: If \(f(x) = 5\), then \(f'(x) = 0\).
. . .
Practice Problem: If \(f(x) = 4x^2 + 3x + 5\), what is \(f'(x)\)?
. . .
\[
f'(x) = 8x + 3
\]
Shortcut 4: The Product Rule
The derivative of a product is a bit trickier…
If \(f(x) = g(x) \cdot h(x)\), then \(f'(x) = g'(x) \cdot h(x) + g(x) \cdot h'(x)\)
. . .
Example: If \(f(x) = (2x)(x + 2)\), then \(f'(x) = 2x + 2(x+2) = 4x + 4\)
. . .
Practice Problem:\(f(x) = (3x^2 + 6x)(x+2)\), what is \(f'(x)\)?
. . .
\[f'(x) = (3x^2 + 6x)(1) + (6x + 6)(x+2)\]
\[f'(x) = 3x^2 + 6x + 6x^2 + 6x + 12x + 12\]
\[f'(x) = 9x^2 + 24x + 12\]
Shortcut 5: The Chain Rule
If \(f(x) = g(h(x))\), then \(f'(x) = g'(h(x)) \cdot h'(x)\)
. . .
“The derivative of the outside times the derivative of the inside.”
. . .
Example: If \(f(x) = (2x^2 - x + 1)^3\), then \(f'(x) = 3(2x^2 - x + 1)^2 (4x - 1)\)
. . .
Practice Problem:\(f(x) = \sqrt{x + 3} = (x+3)^{\frac{1}{2}}\), what is \(f'(x)\)?
Let \(f(x) = 3(x^2 + x + 42)\). What is \(f'(x)\)?
Let \(f(x) = (x^2 + 1)(x+3)\). What is \(f'(x)\)?
Now We Can Do Optimization!
Let \(f(x) = 2x^2 + 8x - 32\). At what value of \(x\) is the function minimized?
Key Insight: Function is minimized when the slope “switches” from decreasing to increasing. Exactly at the point where the slope equals zero.
Optimization in Three Steps
. . .
1. Take the derivative of the function.
. . .
2. Set it equal to zero.
. . .
3. Solve for\(x\).
Optimization in Three Steps
1. Take the derivative of the function.
\[
f(x) = 2x^2 + 8x - 32
\]
. . .
\[
f'(x) = 4x + 8
\]
. . .
2. Set it equal to zero
. . .
\[
4x + 8 = 0
\]
. . .
3. Solve for\(x\).
. . .
\[
x = -2
\]
That second step is called the “First Order Condition”, or FOC.
Optimization in Three Steps
Now You Try It!
Suppose that happiness as a function of jellybeans consumed is \(h(j) = -\frac{1}{3}j^3 + 81j + 2\). How many jellybeans should you eat? (Assume you can only eat a positive number of jellybeans).
Now You Try It!
Suppose that happiness as a function of jellybeans consumed is \(h(j) = -\frac{1}{3}j^3 + 81j + 2\). How many jellybeans should you eat? (Assume you can only eat a positive number of jellybeans).
It’s a maximum when the slope is decreasing, and a minimum when then slope is increasing. How do you figure out if the slope is increasing or decreasing?
. . .
That’s right. You find the slope of the slope (the derivative of the derivative, aka the second derivative).