relay



In [1]:
from __future__ import print_function, division

import pandas as pd
import thinkstats2
import thinkplot

import relay

%matplotlib inline
formats = ['png', 'pdf']

In [2]:
def ObservedPmf(pmf, speed, label=None):
    """Returns a new Pmf representing speeds observed at a given speed.

    The chance of observing a runner is proportional to the difference
    in speed.

    Args:
        pmf: distribution of actual speeds
        speed: speed of the observing runner
        label: string label for the new dist

    Returns:
        Pmf object
    """
    new = pmf.Copy(label=label)
    for val in new.Values():
        diff = abs(val - speed)
        new.Mult(val, diff)
    new.Normalize()
    return new

In [7]:
results = relay.ReadResults()
speeds = relay.GetSpeeds(results)
pmf = thinkstats2.Pmf(speeds)
len(speeds)


Out[7]:
1596

In [8]:
pdf = thinkstats2.EstimatedPdf(speeds)
thinkplot.PrePlot(2)
thinkplot.Pdf(pdf)
thinkplot.Config(title='PDF of running speed',
                xlabel='speed (mph)',
                ylabel='PDF',
                xlim=[3, 12],
                loc='lower right')
#thinkplot.Save(root='relay1', formats=formats)



In [9]:
thinkplot.PrePlot(2)
thinkplot.Cdf(pmf.MakeCdf())
thinkplot.Config(root='observed_speeds',
                title='CDF of running speed',
                xlabel='speed (mph)',
                ylabel='CDF',
                loc='lower right')
#thinkplot.Save(root='relay2', formats=formats)



In [21]:
pmf = thinkstats2.Pmf(speeds, 'actual')
biased = ObservedPmf(pmf, 7.5, label='observed 7.5 mph')
biased2 = ObservedPmf(pmf, 6.5, label='observed 6.5 mph')

In [25]:
thinkplot.PrePlot(3)
thinkplot.Cdf(pmf.MakeCdf(), alpha=0.2)
thinkplot.Cdf(biased.MakeCdf())
thinkplot.Cdf(biased2.MakeCdf())
thinkplot.Config(root='observed_speeds',
                title='CDF of running speed',
                xlabel='speed (mph)',
                ylabel='CDF',
                loc='lower right')
thinkplot.Save(root='relay3', formats=formats)


Writing relay3.png
Writing relay3.pdf
<matplotlib.figure.Figure at 0x7f9c4fb80990>

In [ ]: