op#mizaon,+con#nued:+more+ aboutfing+datato+atrend+line+keshet/m102/2015/lect7.2.pdf ·...
TRANSCRIPT
Op#miza#on, con#nued: More about fi6ng data to a trend line
Con#nued
UBC Math 102
Announcements
• No office hours today (due to MT grading) • Nothing due this Friday (YAY!) • We will discuss the MT in Friday’s class.
UBC Math 102
Homework Help
UBC Math 102
(Not quite the same, but similar) A piece of wire of length L is cut into two pieces. One piece is bent into a square and the other is bent into a triangle. How much of the wire should go to the square to maximize the total area enclosed by both figures?
UBC Math 102
Steps in the solu#on:
First, recall what we know about an equilateral triangle. Need to find the height in order to compute its area
UBC Math 102
Diagram:
UBC Math 102
The func#on to maximize
UBC Math 102
Finding the cri#cal point(s)
This is the cri#cal point – but is it a max???
UBC Math 102
Checking the cri#cal point (2nd deriva#ve test)
The second deriva#ve: So the CP is not a local max, and it is not what we want!!!! Hence, we must consider the endpoints of the domain.
UBC Math 102
Endpoints
We see that x can only take on values in the interval 0 ≤ x ≤ L, and we find that Hence the absolute max occurs at x=0, which means that we should use the en#re wire for the square, and none for the triangle.
Total area A vs x
And here is the graph of the total area vs x.
UBC Math 102
Abs max here
Distance between point and line
• Assignment6: Problem 7 Find the point on the line y=x+1 that is closest to the point (1,3).
UBC Math 102
Distance between point and line
• Assignment6: Problem 7 Find the point on the line y=x+1 that is closest to the point (1,3).
D2= (x-‐1)2+(y-‐3)2
UBC Math 102
(1,3)
(x,y)
Distance between point and line
• Assignment6: Problem 7 Find the point on the line y=x+1 that is closest to the point (1,3). D2= (x-‐1)2+(y-‐3)2
D2(x)= (x-‐1)2+(x+1-‐3)2 Minimize the func#on f(x)=(x-‐1)2+(x-‐2)2 find x, then find y
(1,3)
(x,y)
The boat problem (Assignment6: Problem 2)
A boat leaves a dock at 12:00 P.M. and travels north at a speed of 12 km/h. Another boat is traveling west at speed 10 km/h and arrives at the dock 15 min later. When were the boats closest together?
UBC Math 102
Steps
• Draw a diagram • Determine posi#on of each boats: x(t) and y(t) coordinates using informa#on provided
• Write down the distance formula which should depend only on #me
• [D(t)]2= (x(t))2+(y(t))2 • Minimize [D(t)]2 – easier than minimizing D.
UBC Math 102
• A boat leaves a dock at 12:00 P.M. and travels north at a speed of 12 km/h. Another boat is traveling west at speed 10 km/h and arrives at the dock 15 min later. When were the boats closest together?
UBC Math 102
12 km/h
10 km/h
Steps in the solu#on • A boat leaves a dock at 12:00 P.M. and travels north at a
speed of 12 km/h. Another boat is traveling west at speed 10 km/h and arrives at the dock 15 min later. When were the boats closest together?
• x(t)=x0-‐10t = 2.5 – 10 t • y(t)=12 t
UBC Math 102
y(t)
x(t)
Now, back to data fi6ng..
UBC Math 102
Op#miza#on applied to data fi6ng.
• We are given some data and want to describe its trend.
• How do we fit the best line through the data?
• “Least squares fi6ng” – a useful (and mathema#cally simple) procedure
UBC Math 102
Learning goals
• Understand what data fi6ng means in the simplest (linear least squares) se6ng.
• Understand the connec#on to op#miza#on • Be able to fit a line y=ax +b data points • Be able to use a spreadsheet to manipulate data and fit a line to it
Last #me: Example
• Rain fall over three days
• Can we describe this by
UBC Math 102
Day 1 2 3
Rain (cm) 2 3.3 4
Goal
• Chose line for which the residuals are as small as possible! (minimize!)
UBC Math 102
y=ax
(x1, y1) (xn, yn)
(xi, yi)
Residual
• For the line y=ax and any data point (xi, yi), the residual is (yi – a xi )
UBC Math 102
“Sum of square residuals”
• SSR= (y1-‐ax1)2 + (y2-‐ax2) 2 +...+ (yn-‐axn) 2 • Short-‐hand nota#on: SSR(a) = Minimizing SSR(a) is equivalent to finding the slope of the line for which the devia#ons of data from the line are smallest overall.
UBC Math 102
Line y=ax; best fit value of a:
• We showed that this was obtained by minimizing SSR
• The op#mal value of the slope of the line to fit the data!
Defini#ons
• A model is a func#on used to represent or fit data. For example, some common ones: f(x)=ax, f(x)=ax+b, f(x)=Ce-‐kx.
• Residuals are a measure of how far each model value is from the data value: ri=yi-‐f(xi).
• The Sum of Squared Residuals (SSR) is a measure of how well the model fits all the data: SSR = ∑ (yi-‐f(xi))2.
• Smaller SSR is beler. UBC Math 102
“Best fit”
• The best fit model is the model with parameter value(s) (a, a and b, etc) that gives the smallest SSR.
UBC Math 102
(1) Best fit line with intercept y=ax+b
• The residuals are (A) axi+b (B) (axi+b) 2 (C) yi-‐(axi+b) (D) yi2-‐(axi+b) 2 (E) yi-‐axi
Residuals for line y=ax+b
UBC Math 102
(2) For best fit line with intercept y=ax+b
• The residuals are yi-‐(axi+b) and we will minimize
(A) Σ yi-‐(axi+b) (B) Σ |yi-‐(axi+b)| (C) Σ (yi-‐(axi+b))2 (D) Σ yi2-‐(axi+b) 2 (E) None of the above
(3) For best fit line with intercept y=ax+b
• We will minimize SSR=Σ (yi-‐(axi+b))2 With respect to
(A) a (B) b (C) xi (D) xi and yi (E) both a and b
(4) How would we do that?
Find value of a and b such that
(A) d(SSR)/da=0 (B) d(SSR)/db=0 (C) Both (A) and (B) (D) none of the above
UBC Math 102
Challenge!
• For fun: Calculate values of of a and b such that
d(SSR)/da=0 AND d(SSR)/db=0 Where SSR=Σ (yi-‐(axi+b))2
UBC Math 102
Best fit line with intercept y=ax+b
• RESULT: (See Supplement on wiki, no need to memorize these!)
• Where:
UBC Math 102
Best fit line with intercept y=ax+b
• Many spreadsheets will compute such lines for you automa#cally
• Example: google sheets • First we will show how to find the best fit line y=ax using the formulae we derived for the slope a
• Then we will show how google sheets will do y=ax+b for us as a trendline.
UBC Math 102
Assignment7: Problem 14
Using a spreadsheet to fit a trend-‐line to data: y is the total volume of all chloroplasts inside a given cell with volume x Wanted: given data, fit to it
Many spreadsheets will fit data
Excel: Highlight the rows containing x an y values Insert; chart; scalerplot
UBC Math 102
Excel has automa#c “fit line” func#on
UBC Math 102
y = 0.5887x + 0.0282 R² = 0.99493
0
0.5
1
1.5
2
2.5
3
3.5
4
0 1 2 3 4 5 6 7
Axis Title
Axis Title
y
y
Linear (y)
Or, use Google sheets
UBC Math 102
Here we show how to calculate best fits from scratch
• Copy the data from the Webwork ques#on
Paste into Google sheets (do not retype!)
Pick some value of a and compute the residuals
UBC Math 102
Residuals
UBC Math 102
Square residuals
UBC Math 102
Sum of Square Residuals (SSR)
UBC Math 102
Compute slope for best fit line y=ax
UBC Math 102
• xi yi • xi2 • Σxi yi • Σ xi2
•
• (This is best slope)
UBC Math 102
Compute slope for best fit line y=ax
The SSR should be small for the best fit line
• Large SSR for arbitrary value of a:
• Much smaller SSR when we plug in the a value we found.
UBC Math 102
Plot the data
• Make a scaler plot of the data and get Google sheets to fit a best line to it.
y = ax + b • Note: we can also do this ourselves by calcula#ng the quan##es a and b from the data
UBC Math 102
Make a scaler plot
• Highlight the cells with data, including labels
• Insert chart:
UBC Math 102
Format chart
• Start
• Switch rows and columns • Use column A as headers
UBC Math 102
• Charts
• Scaler • (Select top choice)
• Insert
Data plot will appear
UBC Math 102
Add trendline • {Control click} on chart
• Advanced edit
• Customize • Scroll down menu • Select Trendline; linear
UBC Math 102
Trendline will appear
UBC Math 102
Clicking on line displays trendline
UBC Math 102
Line agrees with our own calcula#ons
• Calculated values
Calcula#ons
• Cells:
UBC Math 102
Answers
• 1 C • 2 C • 3 E • 4 C
UBC Math 102