<jupyter_start><jupyter_text>An Introduction to Probabilistic Programming  An Introduction to Probabilistic Programming  An Introduction to Probabilistic Programming  Probabilistic Programming<jupyter_code>@sampled
def commute():
    train_time = pm.SkewNormal('train_time', mu=40., sd=10., alpha=15.)
    takes_bike = pm.Binomial('takes_bike', n=1, p=0.1)
    bike_time = pm.Normal('bike_time', mu=20., sd=3.)
    walk_time = pm.Normal('walk_time', mu=5., sd=1.)
    t_time = pm.SkewNormal('t_time', mu=15., sd=5., alpha=4.)
    total_time = pm.Normal('total_time', 
                           mu=train_time + tt.switch(takes_bike, bike_time, walk_time+t_time), 
                           sd=1)<jupyter_output><empty_output><jupyter_text>Sampling from a generative model<jupyter_code>with commute():
    base_trace = pm.sample(2000)
pm.traceplot(base_trace, combined=True, figsize=(15, 8));<jupyter_output><empty_output><jupyter_text>Commute time<jupyter_code>base_trace['total_time'].mean()
sns.distplot(base_trace['total_time'], color='r');<jupyter_output><empty_output><jupyter_text>"My train is on time!"<jupyter_code>with commute(train_time=40):
    on_time_train = pm.sample(2000, njobs=4)<jupyter_output>Assigned Metropolis to takes_bike
Assigned NUTS to bike_time
Assigned NUTS to walk_time
Assigned NUTS to t_time
Assigned NUTS to total_time
Multiprocess sampling (4 chains in 4 jobs)
CompoundStep
>Metropolis: [takes_bike]
>NUTS: [total_time, t_time, walk_time, bike_time]
 99%|█████████▉| 2487/2500 [00:17<00:00, 140.07it/s]/Users/colin/anaconda3/envs/pydata_nyc20173.6/lib/python3.6/site-packages/pymc3/step_methods/hmc/nuts.py:452: UserWarning: The acceptance probability in chain 0 does not match the target. It is 0.879221708596, but should be close to 0.8. Try to increase the number of tuning steps.
  % (self._chain_id, mean_accept, target_accept))
100%|██████████| 2500/2500 [00:17<00:00, 139.96it/s]<jupyter_text><jupyter_code>on_time_train['bike_time'].shape
sns.distplot(on_time_train['total_time'], color='b');
sns.distplot(base_trace['total_time'], color='r');<jupyter_output><empty_output><jupyter_text>"My train is 10 minutes late!"<jupyter_code>with commute(train_time=50):
    late_train = pm.sample(2000)<jupyter_output>Assigned Metropolis to takes_bike
Assigned NUTS to bike_time
Assigned NUTS to walk_time
Assigned NUTS to t_time
Assigned NUTS to total_time
Multiprocess sampling (2 chains in 2 jobs)
CompoundStep
>Metropolis: [takes_bike]
>NUTS: [total_time, t_time, walk_time, bike_time]
100%|██████████| 2500/2500 [00:11<00:00, 216.75it/s]
/Users/colin/anaconda3/envs/pydata_nyc20173.6/lib/python3.6/site-packages/pymc3/step_methods/hmc/nuts.py:452: UserWarning: The acceptance probability in chain 1 does not match the target. It is 0.88675147028, but should be close to 0.8. Try to increase the number of tuning steps.
  % (self._chain_id, mean_accept, target_accept))<jupyter_text><jupyter_code>sns.kdeplot(on_time_train['total_time'], label='Train on time', color='b')
sns.kdeplot(base_trace['total_time'], label='Prior', color='r');
sns.kdeplot(late_train['total_time'], label='Train 10 minutes late', color='g');<jupyter_output><empty_output><jupyter_text>"You're early -- how was the bike ride?"<jupyter_code>with commute(total_time=55):
    early_trace = pm.sample(2000)<jupyter_output>Assigned NUTS to train_time
Assigned Metropolis to takes_bike
Assigned NUTS to bike_time
Assigned NUTS to walk_time
Assigned NUTS to t_time
Multiprocess sampling (2 chains in 2 jobs)
CompoundStep
>NUTS: [t_time, walk_time, bike_time, train_time]
>Metropolis: [takes_bike]
100%|██████████| 2500/2500 [00:07<00:00, 353.20it/s]<jupyter_text>"You're early -- how was the bike ride?"<jupyter_code>early_trace['takes_bike'].mean(), base_trace['takes_bike'].mean()<jupyter_output><empty_output><jupyter_text>What else changes when I am 15 minutes early?<jupyter_code>sns.kdeplot(early_trace['train_time'], label='Early Day', color='b');
sns.kdeplot(base_trace['train_time'], label='Prior', color='r');<jupyter_output><empty_output><jupyter_text>What else changes when I am 15 minutes early?<jupyter_code>sns.kdeplot(early_trace['bike_time'], label='Early Day', color='b');
sns.kdeplot(base_trace['bike_time'], label='Prior', color='r');<jupyter_output><empty_output><jupyter_text>What else changes when I am 15 minutes early?<jupyter_code>sns.kdeplot(base_trace['walk_time'], label='Prior', color='r');
sns.kdeplot(early_trace['walk_time'], label='Early Day', color='b');<jupyter_output><empty_output><jupyter_text>PyMC3 Supports Latex Representations in notebooks!<jupyter_code>commute()<jupyter_output><empty_output><jupyter_text>Exercise 4 and 5  Extreme data!<jupyter_code>with commute(total_time=0.):
    no_time = pm.sample(2000)<jupyter_output>Assigned NUTS to train_time
Assigned Metropolis to takes_bike
Assigned NUTS to bike_time
Assigned NUTS to walk_time
Assigned NUTS to t_time
Multiprocess sampling (2 chains in 2 jobs)
CompoundStep
>NUTS: [t_time, walk_time, bike_time, train_time]
>Metropolis: [takes_bike]
100%|██████████| 2500/2500 [00:08<00:00, 303.83it/s]<jupyter_text>Extreme data!<jupyter_code>pm.traceplot(no_time, combined=True, figsize=(15, 8));<jupyter_output><empty_output><jupyter_text>Fine tuning distributions<jupyter_code>@sampled
def bounded_commute():
    TrainSkewNormal = pm.Bound(pm.SkewNormal, lower=35.)
    BikeNormal = pm.Bound(pm.Normal, lower=15.)
    WalkNormal = pm.Bound(pm.Normal, lower=10.)
    TSkewNormal = pm.Bound(pm.SkewNormal, lower=10.)
    TotalNormal = pm.Bound(pm.Normal, lower=50.)
    
    train_time = TrainSkewNormal('train_time', mu=40., sd=10., alpha=15.)
    takes_bike = pm.Binomial('takes_bike', n=1, p=0.1)
    bike_time = BikeNormal('bike_time', mu=20., sd=3.)
    walk_time = WalkNormal('walk_time', mu=5., sd=1.)
    t_time = TSkewNormal('t_time', mu=15., sd=5., alpha=4.)
    total_time = TotalNormal('total_time', 
                           mu=train_time + tt.switch(takes_bike, bike_time, walk_time+t_time), 
                           sd=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1402439024390244, 'fraction_numerical': 0.045978905735003295, 'mean_word_length': 6.455773955773956, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '506320', 'license': 'MIT', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2285, 'n_words': 439, 'path': 'Probabilistic Programming.ipynb', 'repo_name': 'ColCarroll/pydata_nyc2017', 'size': '6068'}