Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Coursework
Views: 63
import random # imports random function import numpy # imports numerical computing capabilities global chanceoffarleft # defines these variables as global global chanceoffarright global chanceofcentreleft global chanceofcentreright global natchanceoffarleft global natchanceoffarright global natchanceofcentreleft global natchanceofcentreright global natchanceofnationalist global averageelectorate global seatsinparliament global welshseats global scotseats global northernirishseats chanceoffarleft = 0.1 # establishes standard chances of each party based loosely on polling data chanceoffarright = 0.1 chanceofcentreleft = 0.3 chanceofcentreright = 0.3 natchanceoffarleft = 0 natchanceoffarright = 0 natchanceofcentreleft = 0 natchanceofcentreright = 0 natchanceofnationalist = 0 averageelectorate = 700 # 1/100 of the average electorate (reduced to reduce computations needed) seatsinparliament = 650 # seats in uk parliament welshseats = 40 # number of constituencies in Wales scotseats = 59 # number of constituencies in Scotland northernirishseats = 18 # number of constituencies in Northern Ireland def vote(): """ A function to simulate one vote Arguments: self explanatory: probabilities of each party Output: vote """ chance = [chanceoffarleft,chanceoffarright,chanceofcentreleft,chanceofcentreright, (1-(chanceoffarleft+chanceoffarright+chanceofcentreleft+chanceofcentreright))] result = ["farleft","farright","centreleft","centreright", "disenfranchised"] return (numpy.random.choice(result, p=chance)) # chooses result at random with probability given def natvote(): """ A function to simulate one vote in Wales, Scotland or Northern Ireland Arguments: self explanatory: probabilities of each party Output: vote """ natchance = [natchanceoffarleft,natchanceoffarright,natchanceofcentreleft,natchanceofcentreright,natchanceofnationalist, (1-(natchanceoffarleft+natchanceoffarright+natchanceofcentreleft+natchanceofcentreright+natchanceofnationalist))] result = ["farleft","farright","centreleft","centreright", "nationalist", "disenfranchised"] return (numpy.random.choice(result, p=natchance)) # chooses result at random with probability given def constituencyelection(): """ A function to simulate a constituency election, decided by individual voters Arguments: votes as decided by function 'vote' Output: one winner: whichever party has the most votes, followed by the vote data for use in calculating misrepresentation error """ votescast = 0 farleftvote = 0 farrightvote = 0 centreleftvote = 0 centrerightvote = 0 spoiltballots = 0 while votescast < averageelectorate: v = vote() if v == "farleft": farleftvote+=1 elif v == "farright": farrightvote+=1 elif v == "centreleft": centreleftvote+=1 elif v == "centreright": centrerightvote+=1 elif v == "disenfranchised": spoiltballots+=1 votescast+=1 if farleftvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "farleft victory", farleftvote, farrightvote, centreleftvote, centrerightvote elif farrightvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "farright victory", farleftvote, farrightvote, centreleftvote, centrerightvote elif centreleftvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "centreleft victory", farleftvote, farrightvote, centreleftvote, centrerightvote elif centrerightvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "centreright victory", farleftvote, farrightvote, centreleftvote, centrerightvote def natconstituencyelection(): """ A function to simulate a constituency election in Wales, Scotland or Northern Ireland, decided by individual voters Arguments: votes as decided by function 'vote' Output: one winner: whichever party has the most votes, followed by the vote data for use in calculating misrepresentation error """ votescast = 0 farleftvote = 0 farrightvote = 0 centreleftvote = 0 centrerightvote = 0 nationalistvote = 0 spoiltballots = 0 while votescast < averageelectorate: v = natvote() if v == "farleft": farleftvote+=1 elif v == "farright": farrightvote+=1 elif v == "centreleft": centreleftvote+=1 elif v == "centreright": centrerightvote+=1 elif v == "nationalist": nationalistvote +=1 elif v == "disenfranchised": spoiltballots+=1 votescast+=1 if farleftvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote): return "farleft victory", farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote elif farrightvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote): return "farright victory", farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote elif centreleftvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote): return "centreleft victory", farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote elif centrerightvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote): return "centreright victory", farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote elif nationalistvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote): return "nationalist victory", farleftvote, farrightvote, centreleftvote, centrerightvote, nationalistvote def generalelection(numberofelections = 3): """ A function to simulate the results of a general election based on results from constituency elections Arguments: seats decided by constituency elections Output: one winner: whichever party has won the most seats, misrepresentation error- the combined difference between the proportion of votes gained and seats gained per party """ global chanceoffarleft global chanceoffarright global chanceofcentreleft global chanceofcentreright global natchanceoffarleft global natchanceoffarright global natchanceofcentreleft global natchanceofcentreright global natchanceofnationalist global averageelectorate global seatsinparliament global welshseats global scotseats global northernirishseats misrepresentationerror = [] electionnumber = [] for i in range(numberofelections): electionnumber.append(i+1) while numberofelections > 0: seatsdecided = 0 welshseatsdecided = 0 scotseatsdecided = 0 northernirishseatsdecided = 0 farleftseats = 0 farrightseats = 0 centreleftseats = 0 centrerightseats = 0 welshnatseats = 0 scotnatseats = 0 irishnatseats = 0 totalfarleftvote = 0 totalfarrightvote = 0 totalcentrerightvote = 0 totalcentreleftvote = 0 totalnationalistvote = 0 while seatsdecided < seatsinparliament: while welshseatsdecided < welshseats: natchanceoffarleft = 0.1 natchanceoffarright = 0.1 natchanceofcentreleft = 0.3 natchanceofcentreright = 0.3 natchanceofnationalist = 0.1 # redefines chances of each party based loosely on polling data s = natconstituencyelection() totalfarleftvote += s[1] # adds the second element in the output of constituencyelection() totalfarrightvote += s[2] totalcentreleftvote += s[3] totalcentrerightvote += s[4] totalnationalistvote += s[5] if s[0] == "farleft victory": farleftseats += 1 elif s[0] == "farright victory": farrightseats += 1 elif s[0] == "centreleft victory": centreleftseats += 1 elif s[0] == "centreright victory": centrerightseats += 1 elif s[0] == "nationalist victory": welshnatseats += 1 welshseatsdecided += 1 seatsdecided += 1 while scotseatsdecided < scotseats: natchanceoffarleft = 0.05 natchanceoffarright = 0.05 natchanceofcentreleft = 0.2 natchanceofcentreright = 0.2 natchanceofnationalist = 0.4 s = natconstituencyelection() totalfarleftvote += s[1] totalfarrightvote += s[2] totalcentreleftvote += s[3] totalcentrerightvote += s[4] totalnationalistvote += s[5] if s[0] == "farleft victory": farleftseats += 1 elif s[0] == "farright victory": farrightseats += 1 elif s[0] == "centreleft victory": centreleftseats += 1 elif s[0] == "centreright victory": centrerightseats += 1 elif s[0] == "nationalist victory": scotnatseats += 1 scotseatsdecided += 1 seatsdecided += 1 while northernirishseatsdecided < northernirishseats: natchanceoffarleft = 0.05 natchanceoffarright = 0.05 natchanceofcentreleft = 0.25 natchanceofcentreright = 0.25 natchanceofnationalist = 0.25 s = natconstituencyelection() totalfarleftvote += s[1] totalfarrightvote += s[2] totalcentreleftvote += s[3] totalcentrerightvote += s[4] totalnationalistvote += s[5] if s[0] == "farleft victory": farleftseats += 1 elif s[0] == "farright victory": farrightseats += 1 elif s[0] == "centreleft victory": centreleftseats += 1 elif s[0] == "centreright victory": centrerightseats += 1 elif s[0] == "nationalist victory": irishnatseats += 1 northernirishseatsdecided += 1 seatsdecided += 1 s = constituencyelection() totalfarleftvote += s[1] totalfarrightvote += s[2] totalcentreleftvote += s[3] totalcentrerightvote += s[4] if s[0] == "farleft victory": farleftseats += 1 elif s[0] == "farright victory": farrightseats += 1 elif s[0] == "centreleft victory": centreleftseats += 1 elif s[0] == "centreright victory": centrerightseats += 1 seatsdecided += 1 error = 100 * float(abs(totalfarleftvote/(seatsinparliament * averageelectorate) - farleftseats/seatsinparliament) + abs(totalfarrightvote/(seatsinparliament * averageelectorate) - farrightseats/seatsinparliament) + abs(totalcentreleftvote/(seatsinparliament * averageelectorate) - centreleftseats/seatsinparliament) + abs(totalcentrerightvote/(seatsinparliament * averageelectorate) - centrerightseats/seatsinparliament) + abs(totalnationalistvote/(seatsinparliament * averageelectorate) - (welshnatseats + scotnatseats + irishnatseats)/seatsinparliament)) misrepresentationerror.append(error) # creates list of misrepresentation errors in each election held print bar_chart([totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote]) """ if farleftseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("farleft government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote, farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats, error) elif farrightseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("farright government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote, farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats, error) elif centreleftseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("centreleft government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote, farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats, error) elif centrerightseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("centreright government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote, farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats, error) elif welshnatseats + scotnatseats + irishnatseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("monarchy is overthrown", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote, farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats, error) if farleftseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("farleft government", bar_chart([totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote]) bar_chart([farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats]) elif farrightseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("farright government", bar_chart([totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote]) bar_chart([farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats]) elif centreleftseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("centreleft government", bar_chart([totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote]) bar_chart([farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats]) elif centrerightseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("centreright government", bar_chart([totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote]) bar_chart([farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats]) elif welshnatseats + scotnatseats + irishnatseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats): print("monarchy is overthrown", bar_chart([totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, totalnationalistvote]) bar_chart([farleftseats, farrightseats, centreleftseats, centrerightseats, welshnatseats + scotnatseats + irishnatseats]) """ chanceoffarleft = (2/3) * (totalfarleftvote / (averageelectorate * seatsinparliament)) # represents fringe party voters losing support over time chanceoffarright = (2/3) * (totalfarrightvote / (averageelectorate * seatsinparliament)) chanceofcentreleft = (totalcentreleftvote / (averageelectorate * seatsinparliament)) + (1/6) * (totalfarleftvote / (averageelectorate * seatsinparliament)) # gains votes from fringe party supporters chanceofcentreright = (totalcentrerightvote / (averageelectorate * seatsinparliament)) + (1/6) * (totalfarrightvote / (averageelectorate * seatsinparliament)) numberofelections -= 1 print(electionnumber) print(misrepresentationerror) for_plot = zip(electionnumber, misrepresentationerror) # plots misrepresentation error changing at each subsequent election return list_plot(for_plot) generalelection()
Error in lines 104-240 Traceback (most recent call last): File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "<string>", line 164 chanceoffarleft = (Integer(2)/Integer(3)) * (totalfarleftvote / (averageelectorate * seatsinparliament)) ^ IndentationError: unexpected indent
= [1, 2, 3] misrepresentationerror = [55.9389010989011, 49.001538461538466, 45.79956043956044] for_plot = zip(electionnumber, misrepresentationerror) # plots misrepresentation error changing at each subsequent election list_plot(for_plot)