ThinkStats (9)

descriptive.py 確認。これ 24p で出てくる妊娠期間の PMF なグラフを描画するコード、ということで紹介されています。手続きとしては以下が定義されてますね。

  • MakeDiffFigure
    • Plot the difference between the PMFs
  • Shift
    • Adds a constant to a sequence of values
  • Hists
    • Plot two histograms on the same axes
  • MakeFigures
    • Plot Hists and Pmfs for the pregnancy length
  • Summarize
    • Print various summary statistics
  • MakeTables
    • Reads survey data and returns a tuple of Tables
    • PoolRecords と Process を呼び出してます
  • PoolRecords
    • Construct a table with records from all tables
    • 引数は table のリスト
  • Process
    • Runs various analyses on this table

とりあえず中身を確認なのかな。下の三つあたりから始めてみる方向で。

Process 手続き

これ、引数で取得した table の属性を設定しているのか。定義が以下。

def Process(table, name):
    """Runs various analyses on this table."""
    first.Process(table)
    table.name = name

    table.var = thinkstats.Var(table.lengths, table.mu)
    table.trim = thinkstats.TrimmedMean(table.lengths)

    table.hist = Pmf.MakeHistFromList(table.lengths, name=name)
    table.pmf = Pmf.MakePmfFromHist(table.hist)

first.Process の定義は以下。

def Process(table):
    """Runs analysis on the given table.
    
    Args:
        table: table object
    """
    table.lengths = [p.prglength for p in table.records]
    table.n = len(table.lengths)
    table.mu = Mean(table.lengths)

length というか引数な table は first.MakeTables が戻すナニ。survey.Pregnancies なオブジェクトですね。この prglength な属性のリストが length に設定されるのか。
あとはそのリストの要素の数とリスト要素の値の算術平均か。
で、descriptive.Process 手続きではこれらに加えて

  • name
  • var (thinkstats.Var)
  • trim (thinkstats.TrimmedMean)
  • hist (Pmf.MakeHistFromList)
  • pmf (Pmf.MakePmfFromHist)

という属性が設定されている模様。ええと、それぞれの定義も掘削してみます。どこまで体力がもつのかどうなのか。

thinkstats.Var

Computes the variance of a sequence of numbers. とある。定義は以下 (コメント除く)。

def Var(t, mu=None):
    if mu is None:
        mu = Mean(t)

    dev2 = [(x - mu)**2 for x in t]
    var = Mean(dev2)
    return var

最初に (x - mu)**2 のリストを作っておいて、その算術平均を取得してますね。

thinkstats.TrimmedMean

Computes the trimmed mean of a sequence of numbers. とのこと。定義は以下なんですが (コメント除く)、Trim て何かな。

def TrimmedMean(t, p=0.01):
    t = Trim(t, p)
    return Mean(t)

定義が以下 (コメント除く)。

def Trim(t, p=0.01):
    t.sort()
    n = int(p * len(t))
    t = t[n:-n]
    return t

あ、t[n:-n] は sequence の n 番目と -n 番目までを除いたリストを戻しますね。

>>> t = [1, 2, 3, 4, 5]
>>> t[1:-1]
[2, 3, 4]

しかも sort をカマしてるので端の値のいくつかが、という事なのか。

その他

以下はスルー。

  • Pmf.MakeHistFromList
  • Pmf.MakePmfFromHist

ええと、prglength なリストから Hist を作ってそれを元に PMF を作っているのか。うーん、便利メソドが沢山あるんですが、それを活かす脳が微妙。

descriptive.PoolProcess

Construct a table with records from all tables. とあります。定義が以下 (コメント除く)。

def PoolRecords(*tables):
    pool = survey.Pregnancies()
    for table in tables:
        pool.ExtendRecords(table.records)
    return pool

呼び出し元は以下なカンジなんですが

    pool = PoolRecords(firsts, others)

何て言うんでしょ。和集合?

descriptive.MakeTables

定義は略。これ、第一子とそれ以外のデータ (descriptive.Process メソドで設定される属性) を用意してくれる便利メソドですね。
残りは明日なのかどうか。つうか 3 章以降カンニングですすめたい。
てか、読む方向にするのか考える方向にするのか再検討必要。