def _class_sizes():
    # start with the actual distribution of class sizes from the book
    d = {
        7: 8,
        12: 8,
        17: 14,
        22: 4,
        27: 6,
        32: 12,
        37: 8,
        42: 3,
        47: 2,
    }

    # form the pmf
    pmf = _04_Pmf._make_pmf_from_dict(d, 'actual')
    print('mean', pmf._mean())
    print('var', pmf._var())

    # compute the biased pmf
    biased_pmf = _bias_pmf(pmf, 'observed')
    print('mean', biased_pmf._mean())
    print('var', biased_pmf._var())

    # unbias the biased pmf
    unbiased_pmf = _unbias_pmf(biased_pmf, 'unbiased')
    print('mean', unbiased_pmf._mean())
    print('var', unbiased_pmf._var())

    # plot the Pmfs
    _05_myplot._pmfs([pmf, biased_pmf])
    _05_myplot._show(xlabel='Class size', ylabel='PMF')
Beispiel #2
0
    def testRender(self):
        t = [1, 2, 2, 3, 5]
        pmf = _04_Pmf._make_pmf_from_list(t)
        xs, ps = pmf._render()

        d = dict(zip(xs, ps))
        new_pmf = _04_Pmf._make_pmf_from_dict(d)
        self.checkPmf(new_pmf)
Beispiel #3
0
def _expected(pa, q, n):
    """
    Makes a Pmf with the expected number of trials in each of four bins.

    Args:
        pa: probability of offering version A
        q:  probability of success for both A and B
        n:  number of trials

    Returns:
        hist that maps (version, outcome) to expected number,
        where version is string A or B and outcome is string Y or N.
    """
    versions = _04_Pmf._make_pmf_from_dict(dict(A=pa, B=1 - pa))
    outcomes = _04_Pmf._make_pmf_from_dict(dict(Y=q, N=1 - q))

    hist = _04_Pmf.Hist()
    for version, pp in versions._items():
        for outcome, qq in outcomes._items():
            hist._incr((version, outcome), pp * qq * n)
    return hist
Beispiel #4
0
    def testMakePmf(self):
        t = [1, 2, 2, 3, 5]
        pmf = _04_Pmf._make_pmf_from_list(t)
        self.checkPmf(pmf)

        d = pmf._get_dict()
        self.assertAlmostEquals(d[2], 0.4)

        vals = pmf._values()
        self.assertEquals(sorted(vals), [1, 2, 3, 5])

        items = pmf._items()
        d = dict(items)
        new_pmf = _04_Pmf._make_pmf_from_dict(d)
        self.checkPmf(new_pmf)