Created
November 9, 2012 16:41
-
-
Save ostronom/4046750 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from datetime import date | |
| import numpy as np | |
| from scipy import polyval, polyfit | |
| from django.utils.datastructures import SortedDict | |
| days, values = np.array([ | |
| (date(2012, 4, 1).toordinal(), 45540), | |
| (date(2012, 5, 1).toordinal(), 125598), | |
| (date(2012, 6, 1).toordinal(), 195935), | |
| (date(2012, 7, 1).toordinal(), 206702), | |
| (date(2012, 8, 1).toordinal(), 682483-300000), | |
| (date(2012, 9, 1).toordinal(), 657762), | |
| (date(2012, 10, 1).toordinal(), 1599805-300000), | |
| ]).T | |
| predicted = [ | |
| date(2012, 11, 1).toordinal(), | |
| date(2012, 12, 1).toordinal(), | |
| date(2013, 1, 1).toordinal() | |
| ] | |
| csv = SortedDict() | |
| for d in days: | |
| csv[d] = [] | |
| for d in predicted: | |
| csv[d] = [] | |
| for degree in [2,3,4,5,6,7]: | |
| coeff = polyfit(days, values, degree) | |
| ys = polyval(coeff, days) | |
| for x,y in zip(days, ys): | |
| csv[x].append(y) | |
| for d in predicted: | |
| csv[d].append(polyval(coeff, d)) | |
| for d, vals in csv.items(): | |
| arith_mean = sum(vals) / len(vals) | |
| harm_mean = len(vals) / sum(map(lambda y: 1/y, vals)) | |
| geom_mean = pow(reduce(lambda x,y: x*y, vals), 1/float(len(vals))) | |
| print '%s,%s,%d,%d,%d' % (date.fromordinal(d), ','.join(map(str,map(int,vals))), arith_mean, harm_mean, geom_mean) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment