Skip to content

Instantly share code, notes, and snippets.

@silgon
Last active November 12, 2017 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save silgon/1f4a1cb17a8cf119607c to your computer and use it in GitHub Desktop.
Save silgon/1f4a1cb17a8cf119607c to your computer and use it in GitHub Desktop.
Basic Least Squares Implementation with pseudo inverse
"""
Basic Least Squares Implementation with pseudo inverse
"""
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
t = np.linspace(0, 3, 20) # horizontal axis
x = np.array([3, 0, 4]) # a,b,c of f(t)=a+b*t+c*t^2
f = lambda t, x: x[0]+x[1]*t+x[2]*t**2 # f(t)
# apply function
y = f(t, x)
# adding noise
noise_factor = 2
noise = np.random.randn(t.shape[0])* noise_factor
y_noise = y + noise
# construct A matrix
f2 = lambda t: np.array([[1]*len(t), t, t**2]).T
A = f2(t)
# Least Squares x=inv(A)y
new_x = np.dot(np.linalg.pinv(A), y_noise)
# apply new values
new_y = f(t, new_x)
print "real values: {}\n noise: {}\n least squares: {}".format(x, noise_factor, new_x)
plt.plot(t,y,'-b', label="perfect function")
plt.plot(t,y_noise,'or', label="function with noise")
plt.plot(t,new_y,'--k', label="least squares")
plt.legend(loc='upper left')
plt.show()
@silgon
Copy link
Author

silgon commented Sep 24, 2015

Result (least squares is applied to the noisy function):
figure_1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment