0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで点推定の信頼区間を求めてみる。

Last updated at Posted at 2025-01-03

Python を使用して点推定の信頼区間を求めてみる。

一例として東京都の平均気温を点推定し、信頼区間を求めてみる。
東京都の気象データは気象庁よりダウンロードして使用した。 1

パッケージとライブラリをインポートする

まずは必要なライブラリと拡張機能をすべてインポートする。

import numpy as np
import pandas as pd
from scipy import stats

データを読み込み、サンプリングする

ついでなので、気温については日数が高々限られているし、マシンパワーに任せて計算させてもよいが、より大規模なデータに対応する為にもサンプリングも行っておく。
元のデータセットに含まれるすべてではなく、ランダムに選択した100日のみを計算対象にする。

df = pd.read_csv("Book1.csv", encoding="shift-jis")
sampled_data = df.sample(n=100, replace=True, random_state=1111)
# データの確認をしておく。
print(sampled_data.head(10))
出力例
           年月日  平均気温(度)  降水量の合計(mm)  日照時間(時間)  平均風速(m/s)  
311  2024/11/7     13.4         0.0       9.5        3.8         7.9
229  2024/8/17     30.4         0.0       8.5        2.7        31.8
337  2024/12/3     11.9         0.0       8.8        1.8        10.5
162  2024/6/11     24.0         0.0      12.8        3.2        21.4
180  2024/6/29     23.7         0.0       3.8        2.0        25.2
152   2024/6/1     20.9         0.0       7.0        2.6        18.1
276  2024/10/3     23.1        11.0       0.2        2.7        24.6
270  2024/9/27     23.3         8.0       0.0        2.1        27.3
264  2024/9/21     28.5         0.0       2.1        3.3        29.3
242  2024/8/30     26.0        84.0       0.0        3.2        33.4

信頼区間を求める

サンプルデータに基づいて、平均気温の信頼区間を求める。ここで信頼度は95%区間とする。

信頼区間を求める手順

  1. 信頼度を決める
  2. 点推定を行う
  3. 誤差を計算する
  4. 信頼区間を計算する
# 信頼度
confidence = 0.95

# 平均
sample_mean = sampled_data['平均気温(度)'].mean()

# 標準誤差
estimated_standard_error = sampled_data['平均気温(度)'].std() / np.sqrt(sampled_data.shape[0])

# サンプルサイズが 30 以上であれば一般的にstats.normを適用できる。
result = stats.norm.interval(confidence, loc=sample_mean, scale=estimated_standard_error)
print(result)
出力例
(17.27152129447444, 20.57247870552557)

つまり、母平均の信頼度95%の信頼区間は(17.27, 20.57)である。

  1. 気象庁|過去の気象データ・ダウンロード https://www.data.jma.go.jp/gmd/risk/obsdl/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?