Ch10 - Curve Fitting
Ch10 - Curve Fitting
Ch10 - Curve Fitting
Chapter 10
Curve Fitting
Introduction
What is Curve Fitting ?
General Approaches of Curve Fittings!!!
1. Least-squares Regression : derive a single curve to describe a
general trend of the data.
(data exhibit significant degree of errors)
2. Interpolation: Fitting a curve or series of curves that pass directly
through each of the points.
(Precise Data)
Curve Fitting
Introduction
-Non computer method for curve fitting, but this is dependent to
subjective viewpoint of the person sketching the curve.
• Integration
• Approximate solution of differential equations
• Derive simple functions to approximate complicated
functions
Curve Fitting
Recall: Pearson Correlation Coefficient
Linear regression
• In correlation, the two variables are treated as equals. In
regression, one variable is considered independent (=predictor)
variable (X) and the other the dependent (=outcome) variable
Y.
• The output of a regression is a function that predicts the dependent
variable based upon values of the independent variables.
β
1 slope
x
A slope of β means that every 1-unit
intercept change in X yields a β -unit change in Y.
Prediction
If you know something about X, this knowledge helps you
predict something about Y. (Sound familiar?…sound like
conditional probabilities?)
Regression equation…
E ( yi / xi ) xi
Predicted value for an individual…
yi = + *xi + random error i
Sy/x
Sy/x
Sy/x
Sy/x
Sy/x
Sy/x
Simple linear regression
Observation: y
Dependent variable
Prediction: y^
Zero
Independent variable (x)
The function will make a prediction for each observed data point.
The observation is denoted by y and the prediction is denoted by y. ^
Regression Error
Prediction error: ε
Observation: y
Prediction: y^
Zero
y = y^ + ε
Actual = Explained + Error
Sum of squares of error (SSE)
Dependent variable
Dependent variable
Population mean: y
The Sum of Squares Regression (SSR) is the sum of the squared differences
between the prediction for each observation and the population mean.
SST, SSR and SSE
Mathematically,
B
y
A
B y
C
yi
*Least squares estimation
gives us the parameters (a,
β) that minimizes C2 and SSE
x
n n n
Equality holds when least
(y
i 1
i y) 2
( yˆ
i 1
i y) 2
( yˆ
i 1
i yi ) 2
square solution is found
A2 B2 C2
SST: Total variation in y SSR: variation explained by x SSE: unexplained variance
Total squared distance of Distance from regression line to naïve Variance around the regression line
observations from naïve mean of y
mean of y
The Coefficient of Determination (aka R-squared)
The proportion of total variation (SST) that is explained by the regression (SSR) is
known as the Coefficient of Determination, and is often referred to as R 2 .
R2=B2/A2=SSR/SST= 1 – SSE/SST
The value of R can range between 0 and 1, and the higher its value the more
accurate the regression
2 model is. It is often referred to as a percentage.
Solutions for least square fit
• In general, can be solved with optimization algorithms
• E.g. gradient descent
To find the values of constants we need to differentiate Sr with respect to each coefficient
S r
2 ( yi ao a1 xi ) 0
ao
S r
2 ( yi ao a1 xi ) xi 0
a1
0 yi a 0 a1 xi
0 yi xi a 0 xi a1 xi2
Least-Squares Fit of a Straight Line
n xi yi xi yi
a1
n x xi
2 2
i Mean values
a0 y a1 x
“Goodness” of our fit
If
• Total sum of the squares around the mean for the dependent variable, y, is St ( yi y )
2
St S r
r
2 r2-coefficient of determination
St r– correlation coefficient
• For a perfect fit
Sr=0 and r=r2=1, signifying that the line explains 100
percent of the variability of the data.
• For r=r2=0, Sr=St, the fit represents no improvement.
X y
1 0.5
2 2.5
3 2
4 4
5 3.5
6 6
7 5.5
Solution x y X2 x*y
1 0.5
n xi yi xi yi 1 0.5
a1 2 2.5 4 5
n x xi
2 2
i 3 2 9 6
4 4 16 16
a0 y a1 x
5 3.5 25 17.5
6 6 36 36
7 5.5 49 38.5
28 24 140 119.5
4 3.428
St S r
r
2
St
22.71429 2.991071
r
2
0.868
22.71429
Calculate the standard error of the estimate
This is the standard deviation for the regression line called
Sr “standard error of the estimate”
Sy/x
n2 The subscript notation “y/x” designates that the error is for a predicted value
of y corresponding to a particular value of x
2.9911
Sy/x 0.7735
72
Example
use least square regression to fit a straight line to:-
X 6 15 23 30 39
Y 29 14 7 13 3
Solution
X 6 15 23 30 39 113
Y 29 14 7 13 3 66
X *Y 174 210 161 390 117 1052
X2 36 225 529 900 1521 3211
5 *1052 113 * 66
a1 0.6689
5 * 3211 113 2
(this model is very common in many engineering applications to characterize quantities that increase or
decrease at a rate that is directly proportional to their own magnitude. For example, population growth )
ln y ln a1 b1 x
Linearization of
Nonlinear Relationships
• Another example of a nonlinear
model is the simple power equation
y = a2xb2
•It can be transform
•into the linear form
1 1 b3 x 1 1 b3
1
y a3 x y a3 x
1 b3 1 1
y a3 x a3
Example
Fit the following data with 3
b) power equation 2
1.5
c) a parabola 1
0.5
0
0 1 2 3 4 5 6 7 8 9
X 0.75 2 3 4 6 8 8.5
y 1.2 1.95 2 2.4 2.4 2.7 2.6
a) Saturation growth model
• After linearization
n xi yi xi yi
a1
n x xi
2 2
i
a0 y a1 x
a) Saturation growth model
=0.369 which is the slope
1 0.369 1 1
y 1.0819 x 0.369
b) Power model
a0 y a1 x
logx -0.12493874 0.30103 0.477121 0.60206 0.778151 0.90309 0.929419 3.865933
0.311422=β
0.153296
α=100.153296
c) Parabola
n xi yi xi yi
a1
n x xi
2 2
i
a0 y a1 x
Comparison Between different Models
Example
Pollution Control: The percentage of new plant expenditures by US
public utility companies on pollution control is as shown.
x = e(y-b)/a
a0 y a1 ln x
a=-1.75945
b=9.944
y=-1.75945*lnx+ 9.944 y(12) = 5.57
Solution:
standard error for the estimates.
Sr
Sy/x
n2
0.511
Sy/x 0.412714
3
Polynomial Regression
•We need to fit a polynomial to data using polynomial regression.
•A second-order polynomial or quadratic fit is
y = a0 + a1 x + a2 x 2 +
•The sum of squares of the residues:
n
S r yi a0 a1 xi a x 2 2
2 i
i 1
•Differentiate Sr with respect to all parameters:
•Set the partials to zero and arrange
X 0.75 2 3 4 6 8 8.5
y 1.2 1.95 2 2.4 2.4 2.7 2.6
a0 0.990728
a1 = 0.449901
a2 -0.03069
1.5 Series1
Polynomial (Series1)
0.5
0
0 1 2 3 4 5 6 7 8 9
Calculate Sr and Sy/x
n
S r yi a0 a1 xi a x
2 2
2 i
i 1
= 0.09962
Sr 0.09962
Sy/x
n (m 1) 4
= 0.157813
Revision
Cramer’s Rule
“Solving system of linear equation”
G matrix
Revision
Cramer’s Rule
Revision
Cramer’s Rule
Revision
Cramer’s Rule
Example
A missile is traced while moving upward in a straight line and in a
constant acceleration. Different heights of that missile have been
recorded for the 10th, 20th, 30th, 40thsecond after elevation as in the
following table:
Where, H(t): is the height after t seconds from elevation; H0: is the
height of the elevation stage; V0: is the initial velocity and a: is the net
acceleration
a)From the data in the table above find H0,V0 and a by
using polynomial regression
Solution:- Time (s) Height (m)
10 170
20 640
30 1410
40 2480
H0
4 100 3000 4700
100 3000 100000
V0
= 156000
0.5*a
3000 100000 3540000 5510000
H0
-1.45519E-11
V0
0.5*a
= 2
1.5
H(t) = -1.45519E-11 + 2t + 1.5 t²
Example
b) If the stage height was 8m, find the values of V0 and a.
• Solution:
100*8 +3000*V0+100000*0.5a =156000
3000*8 + 100000*V0+3540000*0.5a= 5510000
Solution…continue
3000*V0+100000*0.5a = 155200
100000*V0+3540000*0.5a= 5486000
V0 = 1.3032258
0.5a = 1.5129032
Multiple Linear Regression
•The function y is a linear function of 2 or more independent variables,
such as
y = a0 + a1 x1 + a2 x2 +
The regression
Line becomes
plane
Multiple Linear Regression
•The sum of the squares of the residuals
n
log D log S log a0
log Q
log D (log D) (log D)(log S ) a1 (log D) * (log Q)
2
x
y xe
Solution
After linearization the model will be
ln y ln ln x x
We can assume that lnx as x1 and x as x2
So a0=lnα
a1 =1
a2 =β
Solution
• After linearization the model becomes
124
125
126
127
128
129
130
131
132
133
Gradient Descent (Python)
while perfGoalNotMet:
gradient = eval_gradient(X, W)
W_old = W
W = W – learningRate * gradient
perfGoalNotMet = sum(abs(W - W_old)) > goal
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
while perfGoalNotMet:
X_batch = select_random_subsample(X)
gradient = eval_gradient(@loss, X_batch, W)
...
156
157