Solution for finding coef w of the least-squares solution. Why i get differ results?

All other Source.Python topics and issues.
SKY_SHY
Junior Member
Posts: 5
Joined: Sun Sep 18, 2016 9:51 am

Solution for finding coef w of the least-squares solution. Why i get differ results?

Postby SKY_SHY » Sun Nov 27, 2016 8:39 am

Hello! I try to find my coeffients w for least squares and i use for this 2 ways described below.
Why do i get differ results?

Ef pol=5 then Dimension_of_pol = pol+1

Code: Select all

Dimension_of_pol = 5+1 #My actually dimension of the polynom must be greater in +1 cause the range function give me only last degree

X_val = [-1.         -0.85714286 -0.71428571 -0.57142857 -0.42857143 -0.28571429
 -0.14285714  0.          0.14285714  0.28571429  0.42857143  0.57142857
  0.71428571  0.85714286  1.        ] #My array of x

Arr_X_in_power__=[[x**i for i in reversed(range(Dimension_of_pol))] for x in X_val]

Y_val = [4.898587196589413e-16, 1.5636629649360598, 1.9498558243636472, 0.867767478235116, -0.8677674782351165, -1.949855824363647, -1.56366296493606, 0.0, 1.563662964936059, 1.9498558243636477, 0.8677674782351188, -0.867767478235116, -1.9498558243636468, -1.563662964936061, -4.898587196589413e-16]#My array of y


Code: Select all

#y is the function 2*sin(2*pi*x)


And for this purpose i use 2 function from numpy-library

Code: Select all

X_val = np.array(X_val)
Y_val = np.array(Y_val)
Arr_X_in_power__ =  np.array(Arr_X_in_power__)


1-way is https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html

Code: Select all

w = np.linalg.lstsq(Arr_X_in_power__, Y_val)[0]

print w


2-way is https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html

Code: Select all

coefs = np.polyfit(X_val, Y_val, Dimension_of_pol-1)
print coefs


And i get various results:

Code: Select all

#if i use  np.linalg.lstsq
[  2.46592817e+01  -1.77635684e-14  -3.18031627e+01  -1.98489667e-14
   7.39286826e+00  -3.87504418e-14]
#if i use  np.polyfit
[  2.46592817e+01   6.43490116e-15  -3.18031627e+01  -1.28060546e-15
   7.39286826e+00   3.79883424e-16]
Last edited by SKY_SHY on Mon Nov 28, 2016 3:10 pm, edited 1 time in total.
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: Solution for finding coef w of the least-squares solution. Why i get differ results?

Postby Predz » Mon Nov 28, 2016 10:01 am

Hey Sky. I have not covered linear regression in over 2 years, I will have a look into how to calculate the least squares coefficient again. If you are more fimiliar with it than I am then I would create the function yourself, rather than looking for a already existing function. You can see the mathematics here: http://www.stat.wmich.edu/s216/book/node126.html

X (bar) is the Average of Column X.
Y (bar) is the Average of Column Y.

For calculating Y (SD) and X (SD) look here: https://onlinecourses.science.psu.edu/stat501/node/252

Will construct the functions today ;)
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: Solution for finding coef w of the least-squares solution. Why i get differ results?

Postby Predz » Mon Nov 28, 2016 10:20 am

This is what I have so far, pretty definite that my math is wrong though, so please dont use this until I come back from work and have some more time to check it. :)

Syntax: Select all

array_x = [-1, -0.85714286, -0.71428571, -0.57142857, -0.42857143, -0.28571429, -0.14285714,  0, 0.14285714, 0.28571429, 0.42857143, 0.57142857, 0.71428571, 0.85714286, 1]
array_y = [4.898587196589413e-16, 1.5636629649360598, 1.9498558243636472, 0.867767478235116, -0.8677674782351165, -1.949855824363647, -1.56366296493606, 0.0, 1.563662964936059, 1.9498558243636477, 0.8677674782351188, -0.867767478235116, -1.9498558243636468, -1.563662964936061, -4.898587196589413e-16]

def average_of_array(array):
divisor = len(array)
return sum(array) / divisor

avg_x = average_of_array(array_x)
avg_y = average_of_array(array_y)

'''

y = ax + b

b = E(x-x(bar))(y-y(bar))
--------------------
E(x-x(bar))^2

'''

def sum_of_errors(array, avg):
new = []
for num in array:
new.append(num-avg)
return sum(new)

def sum_of_errors_squared(array, avg):
new = []
for num in array:
new.append((num-avg)**2)
return sum(new)

b = (sum_of_errors(array_x, avg_x) * sum_of_errors(array_y, avg_y)) / sum_of_errors_squared(array_x, avg_x)
SKY_SHY
Junior Member
Posts: 5
Joined: Sun Sep 18, 2016 9:51 am

Re: Solution for finding coef w of the least-squares solution. Why i get differ results?

Postby SKY_SHY » Mon Nov 28, 2016 3:17 pm

Predz, you need to install numpy library before using this specif functions, which were used in my code.

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 38 guests