I'm trying to create python function that calculates the growth factor (R) according to the Malthusian growth model $$ X_{t+1} = R X_t $$
The table I have is table with two countries population when each country has different growing factor that is given: $$ \begin{array}{cc} R_1=0.962\\ R_2=0.997 \end{array} $$ and both have the same initial population: 100M
I want to build function that calculates R between specific periods(years and months) based on the population size of each contries every year.
My population table looks like this:
>>> year pop1 pop2
0 0 1.000000e+08 1.000000e+08
1 1 9.620000e+07 9.970000e+07
2 2 9.254440e+07 9.940090e+07
3 3 8.902771e+07 9.910270e+07
4 4 8.564466e+07 9.880539e+07
And this is my function:
def find_r(df,init_pop,period,time_unit):
res=[]
if time_unit=='years':
pop1=df.loc[df['year']==period].iloc[:,1]
pop2=df.loc[df['year']==period].iloc[:,2]
R1=pop1/init_pop
res.append(R1)
R2=pop2/init_pop
res.append(R2)
elif time_unit=='months':
if 0<period<=12:
pop1=((((df.loc[data['year']==1].iloc[:,1]).values)-((data.loc[data['year']==0].iloc[:,1]).values))/12)*period
pop2=((((data.loc[data['year']==1].iloc[:,2]).values)-((data.loc[data['year']==0].iloc[:,2]).values))/12)*period
R1=pop1/init_pop
res.append(R1)
R2=pop2/init_pop
res.append(R2)
else:
print('to be continued')
return(pd.DataFrame(res))
The problem: When I check for eyars for example and put year 1, seems like it works correct and I get the correct R (0.962 aand 0.997), but when I run it for months (that are less than 1 year) I get negative results and that suprised me because I thought that if population shrinking I'll get less than 1 and for growing population I'll get greater than 1 but not negativ values.
For that reason I'm afriad the way I calculate it is wrong.
elaboratio nregard my calculation for months: I reduce the inital population from the 1 year population. divide by 12 and then multiply by number of months -> to get the number of people after certain month
Then I take this number and divide it by the initial pop to get the R.
I have also tried to add the initial pop to pop1 calculation:
pop1=((((df.loc[data['year']==1].iloc[:,1]).values)-((data.loc[data['year']==0].iloc[:,1]).values))/12)+init_pop*period
pop2=((((data.loc[data['year']==1].iloc[:,2]).values)-((data.loc[data['year']==0].iloc[:,2]).values))/12)+init_pop*period
but then I have gotten numbers like 6.55 ect. so I belive is also wrong,considering the trend of population shrinking.
Where am I wrong? is it the function that is bad or my logic?
The problem occurs because you subtract the wrong values.
What you currently do is $$ pop_1 = \left[pop(year=1)-pop(year=0)\right] \frac{period}{12} \\ R_1 = \frac{pop_1}{init\_pop} $$
What you need to do is $$ R_1 = \frac{pop(year=1)}{pop(year=0)}\frac{period}{12} $$