Created
May 12, 2013 07:49
-
-
Save anonymous/5562777 to your computer and use it in GitHub Desktop.
This is the script that iterates to find the fits. The positional arguments are: 1. The file to process (we used SPR_P_only.csv)
2. Number of iterations to find fits.
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
from __future__ import division | |
from klimek import * | |
def getMean(series): | |
mean = sum(series)/len(series) | |
return mean | |
def getSD(series, expectedValue): | |
try: | |
variance = map(lambda x: (x - expectedValue) ** 2, series) | |
except: | |
print variance # this is what happens when you have too many tequilla shots and are unable to figure out wtf just happened to your code. Drinking + Coding == BAD! | |
sys.exit() | |
sd = math.sqrt(getMean(variance)) | |
return sd | |
def pdfNormal(sd, mean, x): | |
pdf = (1/(sd * math.sqrt(2 * math.pi))) * math.exp(-0.5 * (((x - mean)/sd) **2)) | |
return pdf | |
def main2(): | |
electorates = readFile() | |
winningRatios = [e['WinningRatio'] for e in electorates] | |
meanWinningRatio = getMean(winningRatios) | |
sdWinningRatio = getSD(winningRatios, meanWinningRatio) | |
pdfWinningRatio = [pdfNormal(sdWinningRatio, meanWinningRatio, x) for x in winningRatios] | |
listOfSimulatedElectorates = [] | |
# this part of the code generates a list of simulated electorates | |
for i in range(1, 100): | |
fi = i/100 | |
for j in range(1,100): | |
fe = j/100 | |
# sys.stdout.write( '\rNOW AT: (%s, %s) - %s, %s \r' % (fi, fe, i, j)) | |
simulatedElectorates = {} | |
simulatedElectorates = simulate(electorates, fi=fi, fe=fe) | |
d = {'fi': fi, 'fe': fe, 'sElectorates': simulatedElectorates} | |
listOfSimulatedElectorates.append(d) | |
sList = [] # for indexing purposes | |
for sElectorate in listOfSimulatedElectorates: | |
sEWinningRatios = [e['WinningRatio'] for e in sElectorate['sElectorates']] | |
meanSElectorate = getMean(sEWinningRatios) | |
sdSElectorate = getSD(sEWinningRatios, meanSElectorate) | |
pdfSElectorate = [pdfNormal(sdSElectorate, meanSElectorate, x) for x in sEWinningRatios] | |
toMinimize = [((vi - vm)/vi) ** 2 for vi, vm in zip(pdfWinningRatio, pdfSElectorate)] | |
sfifea = sum(toMinimize)/len(toMinimize) | |
sList.append(sfifea) | |
## MINIMUM! | |
minS = min(sList) | |
simulatedElectorateWithMin = listOfSimulatedElectorates[sList.index(minS)] | |
# Write to file (for nice charts) | |
# This list is returned to only render the final chart | |
listOfDictsToWrite = [] | |
for s in sList: | |
index = sList.index(s) | |
d = { | |
'fi': listOfSimulatedElectorates[index]['fi'], | |
'fe': listOfSimulatedElectorates[index]['fe'], | |
'S': s | |
} | |
listOfDictsToWrite.append(d) | |
return (simulatedElectorateWithMin['fi'], simulatedElectorateWithMin['fe'], minS, listOfDictsToWrite) | |
if __name__ == '__main__': | |
fi = [] | |
fe = [] | |
lodtw = [] | |
iterations = [] | |
try: | |
repeats = int(sys.argv[2]) | |
except: | |
repeats = 100 | |
for repeat in range(repeats): | |
(i, e, minS, lodtw) = main2() | |
fi.append(i) | |
fe.append(e) | |
iterationDict = { | |
'iteration': repeat, | |
'fi': i, | |
'fe': e, | |
'S': minS | |
} | |
iterations.append(iterationDict) | |
# Write last simulation result. | |
# this is really, just for fun. Serves no real good purpose | |
# Hey let's waste computing cycles! | |
g = open('%s_SimulationResult_%s.csv' % (sys.argv[1], repeats), 'w') | |
fieldNames = ['fi', 'fe', 'S'] | |
writer = csv.DictWriter(g, fieldnames=fieldNames) | |
writer.writerow(dict(zip(fieldNames, fieldNames))) # seriously, why isn't writeheader() in python 2.7 | |
for d in lodtw: | |
writer.writerow(d) | |
g.close() | |
h = open('%s_Fits_%s.csv' % (sys.argv[1], repeats), 'w') | |
fieldNames2 = ['iteration', 'fi', 'fe', 'S'] | |
writer2 = csv.DictWriter(h, fieldnames=fieldNames2) | |
writer2.writerow(dict(zip(fieldNames2, fieldNames2))) | |
for d in iterations: | |
writer2.writerow(d) | |
h.close() | |
print 'RESULTS, after %s iterations: (fi, fe) = (%s, %s)' % (repeats, getMean(fi), getMean(fe)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment