Home > Enterprise >  Writing a function to calculate a threshold
Writing a function to calculate a threshold

Time:05-08

I am aware that this question may be more maths than programming, but I am trying to solve it in order to write a simple Python program, so I was hoping I could find some help here.

I am trying to find out what score a candidate needs to achieve to enter the second round of an election given abstention. The rule is very simple: if a candidate achieves 12.5% of registered voters, then he can access the second round.

So if abstention is 50%, any given candidate needs to score 25% of the people who did vote in order to access the second round.

I can successfully calculate how to convert a percentage score among those who voted to a percentage score in the amount of registered given abstention. Thus the function below where percentage_candidate is the percentage of expressed votes achieved and abstention the percentage of total registered voters who did not vote:

def calc_percentage_registered(percentage_candidate, abstention):
    participation = 100-abstention
    registered_score = percentage_candidate*(participation/100)
    print(f'With a score of {percentage_candidate}% of the expressed voters and {abstention}% abstention, I have {registered_score}% of registered voters.')
    return registered_score

The function returns a correct output. I run it with a few example values:

calc_percentage_registered(25,0)
calc_percentage_registered(25,25)
calc_percentage_registered(25,50)
calc_percentage_registered(25,75)

The output is correct:

  With a score of 25% of the expressed voters and 0% abstention, I have 25.0% of registered voters.
With a score of 25% of the expressed voters and 25% abstention, I have 18.75% of registered voters.
With a score of 25% of the expressed voters and 50% abstention, I have 12.5% of registered voters.
With a score of 25% of the expressed voters and 75% abstention, I have 6.25% of registered voters.

Now how do I go about calculating the percentage of registered voters a candidate needs for a given abstention? I understand I need to write a function and pass it one argument (abstention) but I am somehow stuck at this step and feel like an idiot!

Many thanks to all for your help

CodePudding user response:

Is this the equation you are seeking?

def calc_percentage_registered_voters_needed(abstention, threshold=0.125):
    return threshold / (1-abstention)

calc_percentage_registered_voters_needed(abstention=0.6)

You'll need to divide by 100 from the percentage terms you are using.

CodePudding user response:

Let's say the total population is P, and A people decided not to vote. In that case, the number of people that did vote is P - A. And let's say you need W votes to win.

You're given the quantities a = A / P, and w = W / P. Your goal is to express x = W / (P - A) in terms of a and w instead of A, W and P.

Start with some substitutions:

x = w * P / (P - a * P)

You can divide out P to get

x = w / (1 - a)
  • Related