import random
import numpy
global chanceoffarleft
global chanceoffarright
global chanceofcentreleft
global chanceofcentreright
global chanceofnationalist
global averageelectorate
global seatsinparliament
global welshseats
global scotseats
global irishseats
chanceoffarleft = 0.1
chanceoffarright = 0.1
chanceofcentreleft = 0.3
chanceofcentreright = 0.3
chanceofnationalist = 0
averageelectorate = 700
seatsinparliament = 650
welshseats = 40
scotseats = 59
northernirishseats = 18
def vote():
"""
A function to simulate one vote
Arguments: self explanatory: probabilities of each party
Output: vote
"""
chance = [chanceoffarleft,chanceoffarright,chanceofcentreleft,chanceofcentreright, chanceofnationalist, (1-(chanceoffarleft+chanceoffarright+chanceofcentreleft+chanceofcentreright+chanceofnationalist))]
result = ["farleft","farright","centreleft","centreright", "nationalist", "disenfranchised"]
return (numpy.random.choice(result, p=chance))
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
nationalistvote = 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 == "nationalist":
nationalistvote = 0
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 averageelectorate
global seatsinparliament
global welshseats
global scotseats
global irishseats
misrepresentationerror = []
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:
chanceofnationalist = 0.1
s = constituencyelection()
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":
welshnatseats += 1
welshseatsdecided += 1
seatsdecided += 1
while scotseatsdecided < scotseats:
chanceofnationalist = 0.4
chanceoffarright = 0.05
chanceoffarleft = 0.05
chanceofcentreright = 0.2
chanceofcentreleft = 0.2
s = constituencyelection()
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:
chanceoffarright = chanceoffarleft = 0.05
chanceofcentreright = chanceofcentreleft = chanceofnationalist = 0.25
s = constituencyelection()
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
chanceoffarleft = 0.1
chanceoffarright = 0.1
chanceofcentreleft = 0.3
chanceofcentreright = 0.3
chanceofnationalist = 0
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)
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)
chanceoffarleft = (2/3) * (totalfarleftvote / (averageelectorate * seatsinparliament))
chanceoffarright = (2/3) * (totalfarrightvote / (averageelectorate * seatsinparliament))
chanceofcentreleft = (totalcentreleftvote / (averageelectorate * seatsinparliament)) + (1/6) * (totalfarleftvote / (averageelectorate * seatsinparliament))
chanceofcentreright = (totalcentrerightvote / (averageelectorate * seatsinparliament)) + (1/6) * (totalfarrightvote / (averageelectorate * seatsinparliament))
numberofelections -= 1
print misrepresentationerror
generalelection()