def MakeExpoCdf(): """Generates a plot of the exponential CDF.""" thinkplot.PrePlot(3) for lam in [2.0, 1, 0.5]: xs, ps = thinkstats2.RenderExpoCdf(lam, 0, 3.0, 50) thinkplot.Plot(xs, ps, label='lam=%g' % lam) thinkplot.Save(root='analytic_expo_cdf', title='Exponential CDF', xlabel='x', ylabel='CDF')
import nsfg import first import analytic import thinkstats2 import thinkplot #%% [markdown] # ## Exponential distribution # # Here's what the exponential CDF looks like with a range of parameters. #%% thinkplot.PrePlot(3) for lam in [2.0, 1, 0.5]: xs, ps = thinkstats2.RenderExpoCdf(lam, 0, 3.0, 50) label = r'$\lambda=%g$' % lam thinkplot.Plot(xs, ps, label=label) thinkplot.Config(title='Exponential CDF', xlabel='x', ylabel='CDF', loc='lower right') #%% [markdown] # Here's the distribution of interarrival times from a dataset of birth times. #%% df = analytic.ReadBabyBoom() diffs = df.minutes.diff() cdf = thinkstats2.Cdf(diffs, label='actual')
def CDFVisualDist(cdf): xs, ps = cdf.xs, cdf.ps # set up subplots PrePlot(num=6, cols=3, rows=2) # linear plot SubPlot(1) Cdf(cdf, color='C0') Config(xlabel='x', ylabel='CDF', title='Linear Plot') # lognormal plot SubPlot(2) xs_log = np.log10(xs) cdf_log = thinkstats2.Cdf(xs_log, ps, label='data') median = cdf_log.Percentile(50) iqr = thinkstats2.IQRFromCDF(cdf_log) std = thinkstats2.StdFromIQR(iqr) low = np.nanmin(xs_log[xs_log != -np.inf]) high = np.nanmax(xs_log[xs_log != np.inf]) x_norm, p_norm = thinkstats2.RenderNormalCdf(median, std, low=low, high=high) Plot(x_norm, p_norm, label='model', color='0.8') Cdf(cdf_log, color='C0') Config(xlabel='log10 x', ylabel='CDF', title='Lognormal Plot') # pareto plot SubPlot(3) scale = Cdf(cdf, transform='pareto', color='C0') Config(xlabel='x', ylabel='CCDF', title='Pareto Plot', **scale) # exponential plot SubPlot(4) mean = cdf.NaNMean() lam = 1 / mean low = np.nanmin(xs[xs != -np.inf]) high = np.nanmax(xs[xs != np.inf]) expo_xs, expo_ps = thinkstats2.RenderExpoCdf(lam, low, high) Plot(expo_xs, 1 - expo_ps, label='model', color='0.8') scale = Cdf(cdf, transform='exponential', color='C0') Config(xlabel='x', ylabel='CCDF', title='Exponential Plot', **scale) # normal plot SubPlot(5) var = cdf.NaNVar() std = np.sqrt(var) low = mean - 4 * std high = mean + 4 * std norm_xs, norm_ps = thinkstats2.RenderNormalCdf(mean, std, low, high) Cdf(cdf, color='C0') Plot(norm_xs, norm_ps, label='model', linewidth=4, color='0.8') Config(xlabel='x', ylabel='CDF', title='Normal Plot') # weibull plot SubPlot(6) scale = Cdf(cdf, transform='weibull', color='C0') Config(title='weibull transform', xlabel='log x', ylabel='log log CCDF', **scale) Show()