Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made homework3 solution public

  • Loading branch information...
commit 528847c3ee394d6bdf003d85f23b7a6487e96996 1 parent 49696ac
John Stitt authored

Showing 2 changed files with 117 additions and 0 deletions. Show diff stats Hide diff stats

  1. +57 0 homework/homework3/goals.dat
  2. +60 0 homework/homework3/hw3.py
57 homework/homework3/goals.dat
... ... @@ -0,0 +1,57 @@
  1 +0,1872
  2 +1,1874
  3 +0,1876
  4 +2,1878
  5 +4,1880
  6 +1,1882
  7 +0,1884
  8 +1,1886
  9 +5,1888
  10 +1,1890
  11 +4,1892
  12 +2,1894
  13 +1,1896
  14 +3,1898
  15 +1,1900
  16 +1,1904
  17 +1,1906
  18 +1,1908
  19 +0,1910
  20 +1,1912
  21 +1,1914
  22 +NA,1916
  23 +NA,1918
  24 +0,1921
  25 +2,1923
  26 +0,1925
  27 +2,1927
  28 +0,1929
  29 +0,1931
  30 +1,1933
  31 +0,1935
  32 +1,1937
  33 +2,1939
  34 +NA,1941
  35 +NA,1943
  36 +NA,1945
  37 +2,1948
  38 +1,1950
  39 +2,1952
  40 +4,1954
  41 +1,1956
  42 +4,1958
  43 +1,1960
  44 +0,1962
  45 +0,1964
  46 +4,1966
  47 +1,1968
  48 +0,1970
  49 +1,1972
  50 +0,1974
  51 +1,1976
  52 +1,1978
  53 +2,1980
  54 +1,1982
  55 +1,1984
  56 +0,1985
  57 +0,1987
60 homework/homework3/hw3.py
... ... @@ -0,0 +1,60 @@
  1 +#1/usr/bin/env python
  2 +
  3 +import math
  4 +import matplotlib.pyplot as plt
  5 +
  6 +SQRT_2PI = math.sqrt(2.0 * math.pi)
  7 +
  8 +def gaussian(x):
  9 + return math.exp(-0.5*x*x)/SQRT_2PI
  10 +
  11 +def kernel(x,y,h,binpoints):
  12 + bins = [0] * len(binpoints)
  13 + i = 0
  14 + for b in binpoints:
  15 + bins[i] = (float(y)/float(h))*gaussian((b-x)/float(h))
  16 + i += 1
  17 + return bins
  18 +
  19 +filename = 'goals.dat'
  20 +
  21 +years = [] #x-axis
  22 +scores = [] #y-axis
  23 +
  24 +for line in file(filename):
  25 + line = line.strip()
  26 + [score, year] = line.split(",")
  27 + if score != 'NA':
  28 + scores.append(float(score))
  29 + years.append(float(year))
  30 +
  31 +n = len(years)
  32 +nbins = n * 1
  33 +low = min(years)
  34 +high = max(years)
  35 +
  36 +binpoints = [0] * nbins
  37 +masterbin = [0] * nbins
  38 +
  39 +delta = (high - low) / (nbins - 1)
  40 +
  41 +b = low
  42 +i = 0
  43 +while b <= high:
  44 + binpoints[i] = b
  45 + b += delta
  46 + i += 1
  47 +
  48 +for i in range(n):
  49 + # I like using 10 the best, the line is smoothed the most and seems to
  50 + # show trends best, 1 is not smoothed much, 3 still seems too messy
  51 + bins = kernel(years[i],scores[i], 10, binpoints)
  52 + for j in range(nbins):
  53 + masterbin[j] += bins[j] # I had "=+" here and could not get the KDE to plot, took forever to find the problem
  54 +
  55 +plt.plot(binpoints,masterbin,'b-')
  56 +plt.plot(years, scores,'g-')
  57 +plt.xlabel("Year")
  58 +plt.ylabel("Score")
  59 +plt.title("England's Score")
  60 +plt.show()

0 comments on commit 528847c

Please sign in to comment.
Something went wrong with that request. Please try again.