Professional Documents
Culture Documents
Purpose: finding an equation linear with respect to a function (with only one
coefficient).
Problem: Find the values of β0 and β1 so that y(x) = β0 + β1x best approximate the
experimental observations.
-
Our observations should thought of as a sample drawn from a normal distribution at
each xi. Moreover, we must think of our parameters β0 and β1 as estimates of their
“true” (unknown) values µβ0 and µβ1.
After computing the t statistic, proceed as per normal hypothesis testing: compare with
tcrit which is based on the desired confidence and m − 2 degrees of freedom. p value
is computed the same way.
Extreme Cases:
1. SSE=0 (perfect linear fit)
2. SSE=SST
Hypothesis test
#Hypothesis testing H0: slope = 0 versus H1: slope is not zero to check
whether there is a trend or not (point 5)
t_stat = (beta_1-0)/s_beta_1;
if (np.abs(t_stat)>t_crit):
print('Reject the NULL hypothesis\ Data support a change in UPDRS
score\ associated with a change in age');
#After looking at the slope which is negative I can also
conclude that data support a decrease in UPDRS improvement with age
else:
print('Unable to reject the NULL hypothesis\ Data failed to support
any association\ between age and UPDRS improvement');
p_value = 2.0*(1.0-stats.t.cdf(abs(t_stat),m-2));
Compute confidence bands for mean scores within age range of data + PLOT!!
upp_conf_band = np.zeros(m);
low_conf_band = np.zeros(m);
x_axis = np.sort(ages);#x axis is a sorted version of ages
for i in range (0,m):
x_h = x_axis[i];
y_h = beta_0 + beta_1*x_h;
s_y_h = s_y*np.sqrt(1/m + (x_h-x_bar)**2/ssx);
upp_conf_band[i] = y_h + t_crit*s_y_h;
low_conf_band[i] = y_h - t_crit*s_y_h;
#Plot the regression line and the data points in one plot
plt.plot(ages,scores,'k.',ages,regr_line,'r-',\
x_axis,upp_conf_band,'g-',x_axis,low_conf_band,'g-');
8. Linear Regression General Case
Assumption: y(x) is linear WITH RESPECT TO THE PARAMETERS β0, β1, β2, ...,
βn−1.
SSE (minimize)
Minimising SSE
- Using original method is unideal as need to compute derivatives manually every
time.
- Instead, use a general approach = matrix-based approach
Matrix-based Approach
- The parameters we calculated (β0, β1, β2, etc) are estimates of the “true”
values µβ0, µβ1, etc.
- at each xi, the distribution of possible values y is normally distributed, with the
mean on the µy line and the same standard deviation for all xi
- Use residual variance (sy2) to estimate σ.
Hypothesis testing on the mean (same as normal linear regression)
- DOF = m - n
The straight line is a particular case of the matrix-based approach we saw in this
lecture (f0(x) = 1,f1(x) = x).
QUESTION
what are the assumptions made for calculating confidence intervals in linear
regression?
PYTHON
Problem 1
Determine the #Assemble matrix A
A = np.zeros((m,3));
best-fit
for i in range(0,m):
polynomial of A[i,0] = 1.0; #Coefficient of beta_0 in my model equation
order 2 using A[i,1] = length[i]; #coefficient of beta_1 in my model
the general equation
A[i,2] = length[i]**2; #coeffeicient of beta_2 in my model
matrix-based
equation
approach.
#Compute matrix K (A^T*A)^-1 * A^T
K_matrix =
np.linalg.inv(np.transpose(A).dot(A)).dot(np.transpose(A));
betas = K_matrix.dot(force);
Optimal length print(-betas[1]/(2*betas[2]));
Confidence t_crit = stats.t.ppf(0.975,m-3);#Note m-3 dof
intervals @ 95%
#Compute the SSE
sse = 0;
for i in range(0,m):
sse = sse + (force[i] -
(betas[0]+betas[1]*length[i]+betas[2]*length[i]**2) )**2
#Compute sy^2
s_y_sq = sse/(m-3);
#Confidence intervals
upp_beta_0 = betas[0] + t_crit*np.sqrt(s_beta_sq_0);
low_beta_0 = betas[0] - t_crit*np.sqrt(s_beta_sq_0);
upp_beta_1 = betas[1] + t_crit*np.sqrt(s_beta_sq_1);
low_beta_1 = betas[1] - t_crit*np.sqrt(s_beta_sq_1);
upp_beta_2 = betas[2] + t_crit*np.sqrt(s_beta_sq_2);
low_beta_2 = betas[2] - t_crit*np.sqrt(s_beta_sq_2);
Plot regression plt.plot(length,force,'k.',x_axis,regr_curve,'r-')
curve
A = np.zeros((m,3));
for i in range(0,m):
A[i,0]=s_0[i];
A[i,1]=s_1[i];
A[i,2]=s_2[i];
else:
print('Unable to reject the NULL hypothesis. Data failed to
support that a change in S0 is associated with a change in
force in the presence of S1 and S2');
9. Non-linear regression (single variable)
Iterative procedures
Bracketing Methods:
Main Idea: start from an initial bracket [a b] that contains the minimum and reduce
the size of the bracket towards the minimum.
r = 0.618
Golden Ratio = 1/r = 1.618
We conduct the golden ratio from the left and right to determine x1 and x2.
- Stopping/termination criteria
The calculated minimum will be accurate as of +/- tolerance value (the smaller, the
better). How many decimal figures the computer can handle.
Risks
- Local minimum instead of global minimum
- We always start with a guess when choosing [a,b]
PYTHON
Function to def obj_fun(beta):
m = len(exp_data[:,0]);#number of experimental data points
calculate SSE
sse = 0;
from the given for i in range(0,m):
equation x = exp_data[i,0];
y_model = (100.0/(1.0+(beta/x)**2));
y_experimental = exp_data[i,1];
sse = sse + (y_model - y_experimental)**2;
return sse;
Implement #initial bracket
a = 0;
golden sections
b = 100.0;
method
r = (np.sqrt(5.0)-1)/2.0;#key number for golden sections
#The first two internal points
x1 = (1-r)*(b-a) + a;
x2 = r*(b-a) + a;
#...the function evaluations at these points
f_x1 = obj_fun(x1);
f_x2 = obj_fun(x2);
Initialising the tolerance = 1e-9;
max_iterations = 10000000;#A large number. I will not wait for
iterations to find
longer than this amount of iterations
minimum
for i in range(0,max_iterations):
if (f_x1 >= f_x2):#we discard the interval [a x1]
a = x1; #a is where x1 used to be
x1 = x2; #x1 is where x2 used to be
x2 = r*(b-a)+a #new point
f_x1 = f_x2;#Recycling the value of f_x2
f_x2 = obj_fun(x2);
else:#we discard the interval [x2 b]
b = x2;#b is where x2 used to be
x2 = x1;#x2 is where x1 used to be
x1 = (1-r)*(b-a)+a;
f_x2 = f_x1;#Recycle the value at previous iteration
f_x1 = obj_fun(x1);
if (np.abs(b-a)<tolerance):
break;#Bracket is small enough, exit the loop
beta_min = (a+b)/2.0;
Plotting results x_axis = np.arange(0.01,30.0,0.01);
regr_curve = np.zeros(len(x_axis));
for i in range(0,len(x_axis)):
regr_curve[i] = 100.0/(1.0+(beta_min/x_axis[i])**2);
if (np.abs(b-a)<tolerance):
break;#Bracket is small enough, exit the loop
root = (a+b)/2.0;
10. Non-linear regression (multiple variables)
Iterative procedures
Instead of using the golden ratio method (bracketing method), we use the Simplex
method for multiple variables.
Termination criteria
- Simplex is too small
- F(G) has not improved for the last few tries
- Maximum number of iterations n
Risks
- Initial guess: highly affects
- Local minimum instead of global minimum
PYTHON
Problem 1: running simplex algorithm with SciPy
import numpy as np Import scipy library to help
import matplotlib.pyplot as plt run simplex
from scipy.optimize import minimize
def eval_model_equation(x,params):
a1 = params[0];
a2 = params[1]; Define a function to
mu = params[2];
evaluate the above
sigma = params[3];
function, given certain
return
(a1/np.sqrt(2*np.pi*sigma**2))*np.exp(-(x- parameter values
mu)**2/(sigma**2))+a2 #params given as list
#Given x (a scalar) and params = [a1,a2,mu,sigma]
def obj_fun(params): Function to be minimised
sse = 0; (SSE).
m = len(exp_data[:,0]);
for i in range(0,m):
y_model =
eval_model_equation(exp_data[i,0], params);
sse = sse + (exp_data[i,1] - y_model)**2;
return sse;
init_guess = [10,13,20,1]; Try various initial guesses.
results = minimize(obj_fun, init_guess, For example [10,10,10,1]
method='nelder-mead', options = {'xatol':1e-8, results in a successful fit,
'disp':True});
but completely
print(results.x); unsatisfactory result in
terms of the model
equation fitting the data
points.
regr_curve = np.zeros(len(exp_data[:,0])); Observe behaviour
for i in range(0,len(regr_curve)): obtained with each guess,
regr_curve[i] = and plot results finally.
eval_model_equation(exp_data[i,0],results.x);
print(results.x);
x = np.arange(-5,5.1,0.1); NON EXAMINABLE !
y = np.arange(-5,5.1,0.1);
Plotting in 3D plot here
X, Y = np.meshgrid(x,y);
Z = obj_fun([X, Y]);
ax = plt.axes(projection='3d');
ax.plot_surface(X,Y,Z,rstride=2,cstride=2,cmap='v
iridis',edgecolor='none');