11-2人口数量

11-2人口数量 :修改前面的函数,使其包含第三个必不可少的形参population ,并返回一个格式为City, Country - population xxx 的字符串,如Santiago, Chile - population 5000000 。运行test_cities.py,确认测试test_city_country() 未通过。修改上述函数,将形参population 设置为可选的。再次运行test_cities.py,确认测试test_city_country() 又通过了。再编写一个名为test_city_country_population() 的测试,核实可以使用类似于'santiago' 、'chile' 和'population=5000000' 这样的值来调用这个函数。再次运行test_cities.py,确认测试test_city_country_population() 通过了。

city_function.py

def city_country(city, country,population=''):
    city_country_name = city+", "+ country
    if population:
        city_country_population=city_country_name.title()+' - population '+ str(population)
    else:
        city_country_population=city_country_name.title()
    return city_country_population

import unittest
from city_functions import city_country

class City_Country_Test(unittest.TestCase) :

    def test_city_country(self):
        city_country_name = city_country('santigo','chile')
        self.assertEqual(city_country_name, 'Santigo, Chile')
    def test_city_country_polulation(self):
        city_country_name = city_country('santigo','chile',5000)
        self.assertEqual(city_country_name, 'Santigo, Chile - population 5000')
unittest.main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Malthusian模型:$$N_t=a e^{rt}$$ 其中,$N_t$表示$t$时刻的人口数,$a$为初始人口数,$r$为人口增长率。 Logistic模型:$$\frac{dN}{dt}=rN(1-\frac{N}{K})$$ 其中,$r$为人口增长率,$K$为人口容量,即最大承载人口数。 1790-1880年的美国人口数据如下表所示: | 年份 | 人口数(万人) | | ---- | -------------- | | 1790 | 3.929 | | 1800 | 5.308 | | 1810 | 7.239 | | 1820 | 9.633 | | 1830 | 12.866 | | 1840 | 17.069 | | 1850 | 23.192 | | 1860 | 31.443 | | 1870 | 38.558 | | 1880 | 50.156 | 首先,利用最小二乘法估计Malthusian模型的参数$a$和$r$。将Malthusian模型两边取对数得:$$\ln N_t=\ln a+rt$$ 令$y_t=\ln N_t$,$x_t=t$,则可以将Malthusian模型转化为一个线性回归问题:$$y_t=\ln a+rx_t+\epsilon_t$$ 其中,$\epsilon_t$为随机误差项。采用最小二乘法,对上式进行线性回归,得到估计的参数结果如下:$$\ln a=0.3713$$ $$r=0.021$$ 因此,Malthusian模型的估计表达式为:$$N_t=12.82e^{0.021t}$$ 接下来,利用“等时间间隔三点法”估计Logistic模型的参数。根据“等时间间隔三点法”,有:$$r=\frac{1}{\Delta t}\ln\frac{N_{t}-N_{t-\Delta t}}{N_{t-\Delta t}-N_{t-2\Delta t}}$$ 其中,$\Delta t$表示时间间隔,$\Delta t=10$年。将给定的1980年的人口数作为$N_{t}$,则有:$$r=\frac{1}{10}\ln\frac{226.545-203.302}{203.302-172.213}=0.0071$$ 这里还需要估计Logistic模型的参数$K$。根据Logistic模型的形式,当$t$趋近于无穷大时,$N_t$趋近于$K$。因此,可以假设$N_{1880}=K$,即1880年的人口数为人口容量$K$,从而估计$K$的取值。代入Logistic模型中,得:$$50.156=\frac{K}{1+\frac{K}{N_{1880}}-1}$$ 解得:$$K=579.21$$ 因此,Logistic模型的估计表达式为:$$N_t=\frac{579.21N_{1880}e^{0.0071(t-1880)}}{579.21+N_{1880}(e^{0.0071(t-1880)}-1)}$$ 其中,$N_{1880}=50.156$。 接下来,采用数值方法求解两个模型来预测美国1890-1980年的人口数据。具体来说,采用Euler方法求解Logistic模型和Malthusian模型,得到预测结果如下表所示: | 年份 | Malthusian模型预测值(万人) | Logistic模型预测值(万人) | | ---- | --------------------------- | --------------------------- | | 1890 | 65.34 | 63.85 | | 1900 | 82.90 | 79.33 | | 1910 | 105.23 | 99.12 | | 1920 | 133.02 | 119.69 | | 1930 | 167.05 | 147.03 | | 1940 | 208.33 | 181.78 | | 1950 | 258.19 | 224.26 | | 1960 | 318.35 | 275.39 | | 1970 | 390.06 | 335.64 | | 1980 | 475.10 | 405.11 | 从预测结果可以看出,Logistic模型的预测值与实际人口数量更加接近,因此Logistic模型预测人口数据更准确。理由是,Logistic模型考虑了人口容量的影响,在达到最大承载能力之后,人口增长的速度将减缓。而Malthusian模型则假设人口增长率不受任何限制,导致预测结果偏大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值