コード例 #1
0
def BornAtButNotBefore(week):
    table, firsts, others = first.MakeTables(".")
    first.ProcessTables(firsts, others)
    DropValues(firsts, week)
    DropValues(others, week)

    firstBabies = Pmf.MakePmfFromList(firsts.lengths, name="First babies")
    otherBabies = Pmf.MakePmfFromList(others.lengths, name="Other babies")
    return (firstBabies, otherBabies)
コード例 #2
0
ファイル: descriptive.py プロジェクト: devhotmail/Python
def MakeTables(data_dir='.'):
    """Reads survey data and returns a tuple of Tables"""
    table, firsts, others = first.MakeTables(data_dir)
    pool = PoolRecords(firsts, others)

    Process(pool, 'live births')
    Process(firsts, 'first babies')
    Process(others, 'others')

    return pool, firsts, others
コード例 #3
0
def Summarize(data_dir):
    """Prints summary statistics for first babies and others.
    
    Returns:
        tuple of Tables
    """
    table, firsts, others = first.MakeTables(data_dir)
    ProcessTables(firsts, others)

    print('Number of first babies', firsts.n)
    print('Number of others', others.n)

    mu1, mu2 = firsts.mu, others.mu

    print('Mean gestation in weeks:')
    print('First babies', mu1)
    print('Others', mu2)

    print('Difference in days', (mu1 - mu2) * 7.0)

    print('Standard deviation of first babies :', firsts.std)
    print('Standard deviation of other babies :', others.std)
コード例 #4
0
ファイル: 2-2.py プロジェクト: qrsforever/workspace
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import math
import first
import thinkstats


def Process(table):
    table.lengths = [p.prglength for p in table.records]
    table.n = len(table.lengths)
    table.mu, table.var = thinkstats.MeanVar(table.lengths)
    table.svar = math.sqrt(table.var)


def ProcessTables(*tables):
    for table in tables:
        Process(table)


if __name__ == "__main__":
    table, firsts, others = first.MakeTables("res")
    ProcessTables(firsts, others)
    print("first table: n[{}] mu[{}] var[{}] svar[{}]".format(
        firsts.n, firsts.mu, firsts.var, firsts.svar))
    print("other table: n[{}] mu[{}] var[{}] svar[{}]".format(
        others.n, others.mu, others.var, others.svar))
コード例 #5
0
def ProbOnTime(pmf):
    return ProbInRange(pmf, lambda week: 38 <= week <= 40)


def ProbLate(pmf):
    return ProbInRange(pmf, lambda week: week >= 41)


def FindAndPrintProbs(pmfName, probEarly, probOnTime, probLate):
    print '{0} pregnancy length probabilities:\nEarly {1:.2f}%\nOn Time {2:.2f}%\nLate {3:.2f}%\n'.format(
        pmfName, probEarly * 100, probOnTime * 100, probLate * 100)


import first

all, firsts, others = first.MakeTables()
first.ProcessTables(all, firsts, others)

import Pmf

pmfFirst = Pmf.MakePmfFromList(firsts.lengths, "First Babies")
firstProbEarly = ProbEarly(pmfFirst)
firstProbOnTime = ProbOnTime(pmfFirst)
firstProbLate = ProbLate(pmfFirst)
FindAndPrintProbs(pmfFirst.name, firstProbEarly, firstProbOnTime,
                  firstProbLate)

pmfOthers = Pmf.MakePmfFromList(others.lengths, "Other Babies")
otherProbEarly = ProbEarly(pmfOthers)
otherProbOnTime = ProbOnTime(pmfOthers)
otherProbLate = ProbLate(pmfOthers)
コード例 #6
0
def firstBabiesPFM():
    all, firsts, others = first.MakeTables()
    first.ProcessTables(all, firsts, others)
    return Pmf.MakePmfFromList(firsts.lengths, "First Babies")
コード例 #7
0
def otherBabiesPMF():
    all, firsts, others = first.MakeTables()
    first.ProcessTables(all, firsts, others)
    return Pmf.MakePmfFromList(others.lengths, "Other Babies")
コード例 #8
0
    >= 41
    """
    rate = 0.0
    for w, p in pmf.Items():
        if w < 41:
            continue
        rate += p
    return rate

def ProcessTables(table, firsts, others):
    firsts.prglengths = [p.prglength for p in firsts.records]
    others.prglengths = [p.prglength for p in others.records]
    #  table.prglengths.extend(firsts.prglengths)
    #  table.prglengths.extend(others.prglengths)


if __name__ == "__main__":
    alltbl, firsts, others = first.MakeTables(data_dir='res')
    ProcessTables(alltbl, firsts, others)
    #  alltbl_pmf = Pmf.MakePmfFromList(alltbl.prglengths, name='pool')
    firsts_pmf = Pmf.MakePmfFromList(firsts.prglengths, name='first')
    others_pmf = Pmf.MakePmfFromList(others.prglengths, name='other')

    print("ProbEarly(firsts) = ", ProbEarly(firsts_pmf))
    print("ProbOnTime(firsts) = ", ProbOnTime(firsts_pmf))
    print("ProbLate(firsts) = ", ProbLate(firsts_pmf))

    print("ProbEarly(others) = ", ProbEarly(others_pmf))
    print("ProbOnTime(others) = ", ProbOnTime(others_pmf))
    print("ProbLate(others) = ", ProbLate(others_pmf))
コード例 #9
0
 def testMakeTables(self):
     table, firsts, others = first.MakeTables()
     self.assertEquals(len(table.records), 13593)
     self.assertEquals(len(firsts.records), 4413)
     self.assertEquals(len(others.records), 4735)
コード例 #10
0
ファイル: myrisk.py プロジェクト: dodysw/thinkstats
import first, survey, Pmf

def ProbEarly(pmf):
    return sum([prob for val, prob in pmf.Items() if val <= 37]) 

def ProbOnTime(pmf):
    return sum([prob for val, prob in pmf.Items() if val in (38, 39,40)]) 

def ProbLate(pmf):
    return sum([prob for val, prob in pmf.Items() if val >= 41]) 

if __name__ == '__main__':
    table, firsts, others = first.MakeTables(".")

    alls = survey.Pregnancies()
    for p in firsts.records + others.records:
        alls.AddRecord(p)

    #easy access to gestation length (e.g. firsts.lengths)
    first.ProcessTables(firsts, others, alls)

    pmfs = {}
    pmfs['First babies'] = Pmf.MakePmfFromList(firsts.lengths)
    pmfs['Others'] = Pmf.MakePmfFromList(others.lengths)
    pmfs['Alls'] = Pmf.MakePmfFromList(alls.lengths)

    for name, pmf in pmfs.items():
        print name
        print "Probability of early arrival", ProbEarly(pmf)
        print "Probability of ontime arrival", ProbOnTime(pmf)
        print "Probability of late arrival", ProbLate(pmf)