Question: PYTHON CODE PLEASE!!! This year is an election year, and it looks like it might be a close one, so here are some election statistics

PYTHON CODE PLEASE!!!
This year is an election year, and it looks like it might be a close one, so here are some
election statistics to ponder.
Suppose a million votes are cast for two opposing candidates. Suppose that voters are
slightly less likely to vote for one candidate than for the other. Specifically, assume
that on average 49.9% of voters will choose the less favored candidate. What are the
chances that there is a tie (each candidate gets exactly 500,000 votes)? What are the
chances that the less-favored candidate will actually win?
To answer these questions, assume that the votes for the less-favored candidate follow
a binomial distribution such that the chance PB of the candidate getting exactly k
votes out of a total of n votes is
PB(k;n,p)=n!k!(n-k)!pk(1-p)(n-k)
where p=0.499 is the probability that any individual voter will vote for this candi-
date. This equation may be difficult to solve numerically, owing to the large numbers
involved. To mitigate this difficulty, you may want to make use of the approximation
to the binomial distribution given in Exercise 3 of Lab 5, which should be very good
for p near 0.5 and n=1,000,000.
Write a Python script
elect.py which takes command-line input from the user to
specify p. Have your code print out the following:
probability p that voter chooses candidate: XXX
probability of tie: YYY
probability of win: ZZZ
Note: this probabilistic interpretation of voters does not mean that voters themselves
necessarily act in a random fashion. Instead, probabilities are assigned because the
many influences on individual choices prevent us from knowing much more than how
voters behave on average, as gleaned from surveys and polls. In this sense the situation
is similar to a coin-flipping experiment, wherein unpredictable processes influence the
outcome and we only know (if the coin is a fair/"honest" one) that on average, we'll
get 50% heads. (p=0.5 in the above equation). Play around with different input values
for p. I think it is amazing how finely tuned p must be for there to be a close election!
PYTHON CODE PLEASE!!! This year is an election

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!