Exercise from Think Stats, 2nd Edition (thinkstats2.com)
Allen Downey

Read the female respondent file.


In [1]:
%matplotlib inline

import chap01soln
resp = chap01soln.ReadFemResp()

Make a PMF of numkdhh, the number of children under 18 in the respondent's household.


In [4]:
import thinkstats2 as ts

children_PMF = ts.Pmf(resp.numkdhh)

Display the PMF.


In [5]:
import thinkplot as tp

tp.Pmf(children_PMF, label='numkdhh')
tp.Show()


<matplotlib.figure.Figure at 0x109536210>

In [1]:

Define BiasPmf.


In [6]:
def BiasPmf(pmf, label=''):
    """Returns the Pmf with oversampling proportional to value.

    If pmf is the distribution of true values, the result is the
    distribution that would be seen if values are oversampled in
    proportion to their values; for example, if you ask students
    how big their classes are, large classes are oversampled in
    proportion to their size.

    Args:
      pmf: Pmf object.
      label: string label for the new Pmf.

     Returns:
       Pmf object
    """
    new_pmf = pmf.Copy(label=label)

    for x, p in pmf.Items():
        new_pmf.Mult(x, x)
        
    new_pmf.Normalize()
    return new_pmf

Make a the biased Pmf of children in the household, as observed if you surveyed the children instead of the respondents.


In [7]:
biasChildrenPmf = BiasPmf(children_PMF, label = 'biased')

Display the actual Pmf and the biased Pmf on the same axes.


In [9]:
tp.preplot(2)
tp.Pmfs([children_PMF, biasChildrenPmf])
tp.Show()


<matplotlib.figure.Figure at 0x10447d310>

Compute the means of the two Pmfs.


In [2]:
child

In [2]:


In [2]: