Created
November 12, 2017 16:22
-
-
Save aijony/cdeb1700ea1dffbfbb76e2b4a1742338 to your computer and use it in GitHub Desktop.
Minumum Least Squares Through Trial and Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = [(0, 0), (1, 2), (2, 4), (3, 6), (4, 8)] | |
m = 0 | |
b = 0 | |
def testFunc(x): | |
return m * x + b | |
def leastSquares(func, data): | |
results = 0 | |
for datum in data: | |
x = datum[0] | |
y = datum[1] | |
results = results + (y - func(x))**2 | |
return results | |
def adjustFunc(): | |
global data, m, b | |
best = leastSquares(testFunc, data) | |
mod = 1 | |
while(True): | |
m = m + mod | |
curr = leastSquares(testFunc, data) | |
if(best > curr): | |
best = curr | |
elif(best < curr): | |
m = m - mod | |
mod = -(1/10) * mod | |
else: | |
return m | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment