Simple Models

Free Content

Sign up here

Population Predictor

Simulate the increase or decrease in the size of the population.

This will be based on initial population with factors that will affect this like birth rate, death rate, immigration and emmigration rates.

#Enter the relevent details
startpopulation = input('Enter initial population - ')
birthrate = input('Birth Rate per year - ')
deathrate = input('Death Rate per year - ')
immigrationrate = input('Immigration Rate per year - ')
migrationrate = input('Emmigration Rate per year - ')
startyear = input('Enter start year - ')
runyears = input('Years to model - ')
print()

#loop for number of years to model
for i in range(int(runyears)):
    #Calculate the population for each year
    startpopulation = int(startpopulation) + int(birthrate) - int(deathrate) + int(immigrationrate) - int(migrationrate)
    #Show the predicted population for that year
    print('In',int(startyear)+i,'the population will be',startpopulation)
Enter initial population - 10000
Birth Rate per year - 350
Death Rate per year - 250
Immigration Rate per year - 75
Emmigration Rate per year - 55
Enter start year - 2023
Years to model - 7

In 2023 the population will be 10120
In 2024 the population will be 10240
In 2025 the population will be 10360
In 2026 the population will be 10480
In 2027 the population will be 10600
In 2028 the population will be 10720
In 2029 the population will be 10840

Lets improve our model, by allowing birth,death etc. to be incremented or decreased each year

Allow the user to choose the change in rates.

#Enter the relevent details
startpopulation = input('Enter initial population - ')
birthrate = input('Birth Rate per year - ')
deathrate = input('Death Rate per year - ')
immigrationrate = input('Immigration Rate per year - ')
migrationrate = input('Emmigration Rate per year - ')
startyear = input('Enter start year - ')
runyears = input('Years to model - ')

#Allow change within the model
br_change = input('Enter birth rate change (as a percentage) - ')
dr_change  = input('Enter death rate change (as a percentage) - ')
im_change  = input('Enter immagration rate change (as a percentage) - ')
em_change  = input('Enter emmigration rate change (as a percentage) - ')
print()

Now to apply this within our model.

#loop for number of years to model
for i in range(int(runyears)):
    #Calculate the population for each year
    startpopulation = float(startpopulation) + float(birthrate) - float(deathrate) + float(immigrationrate) - float(migrationrate)
    #Change in rates for every year
    birthrate = float(birthrate) + float(birthrate) * (float(br_change)/100)
    deathrate = float(deathrate) + float(deathrate) * (float(dr_change)/100)
    immigrationrate = float(immigrationrate) + float(immigrationrate) * (float(im_change)/100)
    migrationrate = float(migrationrate) + float(migrationrate) * (float(em_change)/100)
    #Show the predicted population for that year
    print('In',int(startyear)+i,'the population will be',int(startpopulation))

Output.

Enter initial population - 10000
Birth Rate per year - 650
Death Rate per year - 475
Immigration Rate per year - 110
Emmigration Rate per year - 125
Enter start year - 2023
Years to model - 7
Enter birth rate change (as a percentage) - 10
Enter death rate change (as a percentage) - 5
Enter immagration rate change (as a percentage) - 2
Enter emmigration rate change (as a percentage) - .5

In 2023 the population will be 10160
In 2024 the population will be 10362
In 2025 the population will be 10613
In 2026 the population will be 10918
In 2027 the population will be 11284
In 2028 the population will be 11718
In 2029 the population will be 12228

Analytics.

Create a chart which shows the rise/decline of the population.

For this in our loop, we need to add the data to the lists.

#Create 2 empty lists to hold the data for a chart
years = []
population = []
#loop for number of years to model
for i in range(int(runyears)):
    #Calculate the population for each year
    startpopulation = float(startpopulation) + float(birthrate) - float(deathrate) + float(immigrationrate) - float(migrationrate)
    #Change in rates for every year
    birthrate = float(birthrate) + float(birthrate) * (float(br_change)/100)
    deathrate = float(deathrate) + float(deathrate) * (float(dr_change)/100)
    immigrationrate = float(immigrationrate) + float(immigrationrate) * (float(im_change)/100)
    migrationrate = float(migrationrate) + float(migrationrate) * (float(em_change)/100)
    #Show the predicted population for that year
    print('In',int(startyear)+i,'the population will be',int(startpopulation))
    #Add the data to the list
    years.append(int(startyear)+i)
    population.append(int(startpopulation))

We can now create a line chart of that data using the lists.

import matplotlib.pyplot as plt
plt.title("Population")
plt.plot(years, population)
plt.xlabel('Year')
plt.ylabel('Population')
plt.show()

Output of chart.