Visualizing Median Votes and Primaries

Party vs. Jungle Primaries

Fill in the parameters and then click evaluate to start the interactive voting model.
[random() for x in range(20)] candidates=[random() for x in range(5)] candidates.sort() def vote(candidates,n,interval): votes=[] for i in range(n): votes.append(0) votes[0]=candidates[0]-interval[0] votes[-1]=interval[1]-candidates[-1] for i in range(n-1): votes[i]=votes[i] + (candidates[i+1]-candidates[i])/2 votes[i+1]=votes[i+1]+ (candidates[i+1]-candidates[i])/2 return votes, votes.index(max(votes)) vote([1,2,4,8],4,[0,10]) def party_primary(num_candidates,interval,party_line): can1=[uniform(interval[0],party_line) for x in range(num_candidates[0])] can1.sort() can2=[uniform(party_line,interval[1]) for x in range(num_candidates[1])] can2.sort() votes1,primary1=vote(can1,num_candidates[0],[interval[0],party_line]) votes2,primary2=vote(can2,num_candidates[1],[party_line,interval[1]]) election=[can1[primary1],can2[primary2]] wvotes,winner=vote(election,2,interval) winner=election[winner] return winner def jungle_primary(num_candidates,interval,party_line): cans=[] for i in range(num_candidates[0]): cans.append(uniform(interval[0],party_line)) for i in range(num_candidates[1]): cans.append(uniform(party_line,interval[1])) cans.sort() #print(cans) votes1,primary1=vote(cans,num_candidates[0]+num_candidates[1],interval) #print(votes1) #print(primary1) votes1[primary1]=0 primary2=votes1.index(max(votes1)) #print(votes1) #print(primary2) election=[cans[primary1],cans[primary2]] election.sort() #print(election) wvotes,winner=vote(election,2,interval) #print(winner) winner=election[winner] return winner @interact def primary_diffs(Dcan=input_box(2,label='Democratic Candidates:'),Rcan=input_box(2,label='Republican Candidates:'),party_line=input_box(.5,label='Proportion Democratic:' ),num_samples=input_box(1000,label='# trials'),auto_update=False): samples=[] for i in range(1000): samples.append(party_primary([Dcan,Rcan],[0,1],party_line)) H=histogram([x for x in samples if x < party_line], bins=20,color='blue') H=H+histogram([x for x in samples if x > party_line], bins=20,color='red') H.show() samples=[] for i in range(1000): samples.append(jungle_primary([Dcan,Rcan],[0,1],party_line)) H=histogram([x for x in samples if x < party_line], bins=20,color='blue') H=H+histogram([x for x in samples if x > party_line], bins=20,color='red') H.show()