Skip to content

Commit

Permalink
Make homework 5 public
Browse files Browse the repository at this point in the history
  • Loading branch information
sp0rus committed Jan 12, 2012
1 parent b57ac95 commit 9790fea
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
84 changes: 84 additions & 0 deletions homework/homework5/bivariate.py
@@ -0,0 +1,84 @@
import matplotlib.pyplot as plt
import math

def linreg(X, Y):
assert len(X)==len(Y)
n = len(X)
EX = sum(X)
EY = sum(Y)
EX2 = sum([x * x for x in X])
EXY = sum([ X[i] * Y[i] for i in range(n)])
m = (n * EXY - EX * EY) / (n * EX2 - EX * EX)
b = (EY - m * EX) / float(n)
return (m, b)

def corr(X, Y):
mux = sum(X)/float(len(X))
muy = sum(Y)/float(len(Y))
SUMXX = sum([(x - mux) * (x - mux) for x in X])
SUMYY = sum([(y - muy) * (y - muy) for y in Y])
SUMXY = sum([(X[i] - mux) * (Y[i] - muy) for i in range(len(X))])
return SUMXY/(SUMXX * SUMYY)**0.5

def estimatedFit(X, m, b):
return [x * m + b for x in X]

def bivariate(X, Y):
print "Linear Regression: "
print "Plot color: blue"
(m, b) = linreg(X, Y)
fit = estimatedFit(X, m, b)
r = corr(fit, Y)
print "M: %s" %(m)
print "B: %s" %(b)
print "R-squared: ", r*r
plt.plot(X, fit, "b-")

# y = (a/x)+b -> 1/X
print "\nInverse Regression: "
print "Plot color: red"
Xp = [1.0/float(x) for x in X]
(m,b) = linreg(Xp, Y)
fit = estimatedFit(Xp, m, b)
r = corr(fit, Y)
print "M: %s" %(m)
print "B: %s" %(b)
print "R-squared: ", r*r
plt.plot(X, fit, "r-")

# y = x^m + b
print "\nPower Law: "
print "Plot color: green"
Xp = [math.log(x) for x in X]
Yp = [math.log(y) for y in Y]
(m,b) = linreg(Xp, Yp)
fit = estimatedFit(Xp, m, b)
r = corr(fit, Yp)
print "M: %s" %(m)
print "B: %s" %(b)
print "R-squared: ", r*r
plt.plot(X, [math.exp(y) for y in fit], "g-")

# y = a*sqrt(x)+b -> X*X
print "\nSquare root: "
print "Plot color: magenta"
Xp = [math.sqrt(x) for x in X]
(m,b) = linreg(Xp, Y)
fit = estimatedFit(Xp, m, b)
r = corr(fit, Y)
print "M: %s" %(m)
print "B: %s" %(b)
print "R-squared: ", r*r
plt.plot(X, fit, 'm-')

# y = a*log(x)+b -> e^X
print "\nLogarithm: "
print "Plot color: cyan"
Xp = [math.log(x) for x in X]
(m,b) = linreg(Xp, Y)
fit = estimatedFit(Xp, m, b)
r = corr(fit, Y)
print "M: %s" %(m)
print "B: %s" %(b)
print "R-squared: ", r*r
plt.plot(X, fit, 'c-')
Binary file added homework/homework5/bivariate.pyc
Binary file not shown.
18 changes: 18 additions & 0 deletions homework/homework5/diamond.py
@@ -0,0 +1,18 @@
import numpy as np
import matplotlib.pyplot as plt
import bivariate
import math

carat = np.loadtxt('diamond.tab', delimiter='\t', skiprows=1,usecols=[0])
price = np.loadtxt('diamond.tab', delimiter='\t', skiprows=1,usecols=[1])

data = zip(carat, price) #sort
data.sort()
(carat, price) = zip(* data) # unzip
bivariate.bivariate(carat,price)

plt.plot(carat, price, 'k+')
plt.xlabel("Carats")
plt.ylabel("Price")
plt.title("Price of Diamonds by Carat")
plt.show()
48 changes: 48 additions & 0 deletions homework/homework5/diamond.tab
@@ -0,0 +1,48 @@
0.17 355
0.16 328
0.17 350
0.18 325
0.25 642
0.16 342
0.15 322
0.19 485
0.21 483
0.15 323
0.18 462
0.28 823
0.16 336
0.2 498
0.23 595
0.29 860
0.12 223
0.26 663
0.25 750
0.27 720
0.18 468
0.16 345
0.17 352
0.16 332
0.17 353
0.18 438
0.17 318
0.18 419
0.17 346
0.15 315
0.17 350
0.32 918
0.32 919
0.15 298
0.16 339
0.16 338
0.23 595
0.23 553
0.17 345
0.33 945
0.25 655
0.35 1086
0.18 443
0.25 678
0.25 675
0.15 287
0.26 693
0.15 316

0 comments on commit 9790fea

Please sign in to comment.