LoginSignup
1
0

More than 5 years have passed since last update.

統計学入門9.2をPythonで解く試み

Posted at

概要

統計学入門(東京大学出版会)の練習問題9.2をPythonにて解きます。
問題は以下の通り。

多数のねじが入っている箱から6本を取り出してその直径を精密に測定したところ,次のような結果を得た.
1.22,1.24,1.25,1.19,1.17,1.18
標本平均Xと標本分散s^2を計算せよ.

環境

前と同じでJupyter notebookによります。

ソースコード

問題をそのままに解こうとするとサルでもできるので、「標本の個数によらず、複数個の標本を入力したとき、その標本平均と標本分散を計算する」プログラムをめざしました。

code.rb
a=input().split(" ")
b=[float(i) for i in a]
c=float(sum(b))
d=int(len(b))
X=c/d
print("標本平均は",X)
A=map(lambda x: (x-X)**2,b)
B=[float(j) for j in A]
C=float(sum(B))
print("標本分散は",C/(d-1))

結果

以下のような結果となりました。
スクリーンショット (6).png

多分もっとスマートに組む余地があるので、何かあればご一報ください。

(四捨五入すればよかったかな)

1
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
1
0