宇宙學中的線性模型
不到 100 年前,人們以為宇宙只由一個靜止的銀河系組成,裡面大約有一百萬顆恆星。如今我們已觀測到數千億個星系,每個星系都有數千億顆恆星,而且都在運動中。
現代宇宙學作為一門物理科學的開端,源自 Edwin Hubble 在 1929 年發表的一篇論文,其中運用了線性模型(論文連結)。
在這個練習中,你將建立一個模型,其斜率對應到哈伯常數(Hubble's Constant),用來描述星系速度如何隨與地球的距離線性變化。

本練習屬於課程
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))