宇宙学中的线性模型
不到 100 年前,人们认为宇宙只由一个静止的星系组成,或许包含数百万颗恒星。如今,我们已经观测到数千亿个星系,每个星系都有数千亿颗恒星,并且都在运动。
现代宇宙学这门物理科学的开端,源于 Edwin Hubble 在 1929 年发表的论文,其中使用了线性模型。
在本练习中,您将构建一个模型,其斜率给出哈勃常数,用于描述星系速度与其到地球距离之间的线性关系。

本练习是课程的一部分
Python 线性建模入门
练习说明
- 使用预加载的
DataFrame,其包含列names、distances和velocities。 - 使用
ols().fit()构建并拟合模型,参数设置为formula="velocities ~ distances"和data=df。 - 使用
model_fit.params提取截距和斜率的参数估计值,分别赋给a0和a1。 - 针对相应的不确定性值重复上述过程,这次使用
model_fit.bse。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Fit the model, based on the form of the formula
model_fit = ols(formula="velocities ~ ____", data=df).fit()
# Extract the model parameters and associated "errors" or uncertainties
a0 = model_fit.params['Intercept']
a1 = model_fit.params['____']
e0 = model_fit.bse['____']
e1 = model_fit.bse['distances']
# Print the results
print('For slope a1={:.02f}, the uncertainty in a1 is {:.02f}'.format(a1, e1))
print('For intercept a0={:.02f}, the uncertainty in a0 is {:.02f}'.format(a0, e0))