• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# pp20110927

a guest Sep 27th, 2011 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from math import sqrt, pow
2. from operator import mul, sub
3.
4. def mean(xs):
5.
6.         if len(xs) == 0: return 0
7.         return sum(xs) / len(xs)
8.
9. def stddev(xs):
10.
11.     if len(xs) == 0: return 0
12.     m = mean(xs)
13.     return sqrt(sum([pow(x - m,2) for x in xs])/float(len(xs)))
14.
15. def linreg(data):
16.
17.     xs = [x for x,y in data]
18.     ys = [y for x,y in data]
19.
20.     return (slope(xs, ys), intercept(xs, ys))
21.
22. def linregs(data):
23.
24.     return "f(x) = {0}x + {1}".format(*linreg(data))
25.
26. def slope(xs, ys):
27.
28.     n = len(xs)
29.     return (n * sum(map(mul,xs,ys)) - (sum(xs) * sum(ys)))/(n*sum(map(mul,xs,xs)) - pow(sum(xs),2))
30.
31. def intercept(xs, ys):
32.
33.     return mean(ys) - (slope(xs,ys)* mean(xs))
34.
35. def correlation(data):
36.
37.     xs = [x for x,y in data]
38.     ys = [y for x,y in data]
39.
40.     n = len(xs)
41.     xvar = map(sub, xs, [mean(xs)] * n)
42.     yvar = map(sub, ys, [mean(ys)] * n)
43.     return sum(map(mul, xvar, yvar))/(n*stddev(xs)*stddev(ys))
44.
RAW Paste Data
Top