def testNormalPdf(self):
        pdf = thinkstats2.NormalPdf(mu=1, sigma=2)
        self.assertEqual(len(str(pdf)), 29)
        self.assertAlmostEqual(pdf.Density(3), 0.12098536226)

        pmf = pdf.MakePmf()
        self.assertAlmostEqual(pmf[1.0], 0.0239951295619)
        xs, ps = pdf.Render()
        self.assertEqual(xs[0], -5.0)
        self.assertAlmostEqual(ps[0], 0.0022159242059690038)

        pmf = thinkstats2.Pmf(pdf)
        self.assertAlmostEqual(pmf[1.0], 0.0239951295619)
        xs, ps = pmf.Render()
        self.assertEqual(xs[0], -5.0)
        self.assertAlmostEqual(ps[0], 0.00026656181123)

        cdf = thinkstats2.Cdf(pdf)
        self.assertAlmostEqual(cdf[1.0], 0.51199756478094904)
        xs, ps = cdf.Render()
        self.assertEqual(xs[0], -5.0)
        self.assertAlmostEqual(ps[0], 0.0)
Beispiel #2
0
def MakePdfExample(n=500):
    """Plots a normal density function and a KDE estimate.

    n: sample size
    """
    # mean and var of women's heights in cm, from the BRFSS
    mean, var = 163, 52.8
    std = math.sqrt(var)

    # make a PDF and compute a density, FWIW
    pdf = thinkstats2.NormalPdf(mean, std)
    print(pdf.Density(mean + std))

    # make a PMF and plot it
    thinkplot.PrePlot(2)
    thinkplot.Pdf(pdf, label='normal')

    # make a sample, make an estimated PDF, and plot it
    sample = [random.gauss(mean, std) for _ in range(n)]
    sample_pdf = thinkstats2.EstimatedPdf(sample)
    thinkplot.Pdf(sample_pdf, label='sample KDE')

    thinkplot.Save(root='pdf_example', xlabel='Height (cm)', ylabel='Density')
Beispiel #3
0
#%% [markdown]
# ## 6.1 PDF
#
# - probability dencity function
#
# $PDF_{normal}(x) = \frac{1}{\sigma \sqrt{2\pi}}\exp[-\frac{1}{2}(\frac{x-\mu}{\sigma})^2]$

#%%
import thinkstats2
import math
mean, var = 163, 52.8
std = math.sqrt(var)
pdf = thinkstats2.NormalPdf(mean, std)
pdf.Density(mean + std)

#%%
import thinkplot
thinkplot.Pdf(pdf, label='normal')
thinkplot.Show(xlabel='height (cm)', ylabel='dencity')

#%%
pmf = pdf.MakePmf()

#%% [markdown]
# ## 6.2 KDE
#
# - Kernel density estimation

#%%
import random
sample = [random.gauss(mean, std) for _ in range(500)]