Skip to content

Commit 42a42b6

Browse files
committed
Merge branch 'chap2-style' into examples-style
Conflicts: Chapter2_MorePyMC/MorePyMC.ipynb
2 parents 3f3daac + 685a55f commit 42a42b6

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

Chapter2_MorePyMC/MorePyMC.ipynb

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
"import numpy as np\n",
301301
"n_data_points = 5 # in CH1 we had ~70 data points\n",
302302
"\n",
303+
"\n",
303304
"@pm.deterministic\n",
304305
"def lambda_(tau=tau, lambda_1=lambda_1, lambda_2=lambda_2):\n",
305306
" out = np.zeros(n_data_points)\n",
@@ -532,7 +533,7 @@
532533
"cell_type": "code",
533534
"collapsed": false,
534535
"input": [
535-
"alpha = 1./20.\n",
536+
"alpha = 1. / 20.\n",
536537
"lambda_1, lambda_2 = pm.rexponential(alpha, 2)\n",
537538
"print lambda_1, lambda_2"
538539
],
@@ -579,7 +580,7 @@
579580
"collapsed": false,
580581
"input": [
581582
"plt.bar(np.arange(80), data, color=\"#348ABD\")\n",
582-
"plt.bar(tau-1, data[tau - 1], color=\"r\", label=\"user behaviour changed\")\n",
583+
"plt.bar(tau - 1, data[tau - 1], color=\"r\", label=\"user behaviour changed\")\n",
583584
"plt.xlabel(\"Time (days)\")\n",
584585
"plt.ylabel(\"count of text-msgs received\")\n",
585586
"plt.title(\"Artificial dataset\")\n",
@@ -616,18 +617,18 @@
616617
"input": [
617618
"def plot_artificial_sms_dataset():\n",
618619
" tau = pm.rdiscrete_uniform(0, 80)\n",
619-
" alpha = 1./20.\n",
620+
" alpha = 1. / 20.\n",
620621
" lambda_1, lambda_2 = pm.rexponential(alpha, 2)\n",
621622
" data = np.r_[pm.rpoisson(lambda_1, tau), pm.rpoisson(lambda_2, 80 - tau)]\n",
622623
" plt.bar(np.arange(80), data, color=\"#348ABD\")\n",
623-
" plt.bar(tau - 1, data[tau-1], color=\"r\", label=\"user behaviour changed\")\n",
624+
" plt.bar(tau - 1, data[tau - 1], color=\"r\", label=\"user behaviour changed\")\n",
624625
" plt.xlim(0, 80)\n",
625626
"\n",
626627
"figsize(12.5, 5)\n",
627628
"plt.title(\"More example of artificial datasets\")\n",
628629
"for i in range(4):\n",
629630
" plt.subplot(4, 1, i)\n",
630-
" plot_artificial_sms_dataset()\n"
631+
" plot_artificial_sms_dataset()"
631632
],
632633
"language": "python",
633634
"metadata": {},
@@ -709,7 +710,7 @@
709710
"cell_type": "code",
710711
"collapsed": false,
711712
"input": [
712-
"#set constants\n",
713+
"# set constants\n",
713714
"p_true = 0.05 # remember, this is unknown.\n",
714715
"N = 1500\n",
715716
"\n",
@@ -775,10 +776,10 @@
775776
"cell_type": "code",
776777
"collapsed": false,
777778
"input": [
778-
"#include the observations, which are Bernoulli\n",
779+
"# include the observations, which are Bernoulli\n",
779780
"obs = pm.Bernoulli(\"obs\", p, value=occurrences, observed=True)\n",
780781
"\n",
781-
"#To be explained in chapter 3\n",
782+
"# To be explained in chapter 3\n",
782783
"mcmc = pm.MCMC([p, obs])\n",
783784
"mcmc.sample(18000, 1000)"
784785
],
@@ -860,15 +861,15 @@
860861
"import pymc as pm\n",
861862
"figsize(12, 4)\n",
862863
"\n",
863-
"#these two quantities are unknown to us.\n",
864+
"# these two quantities are unknown to us.\n",
864865
"true_p_A = 0.05\n",
865866
"true_p_B = 0.04\n",
866867
"\n",
867-
"#notice the unequal sample sizes -- no problem in Bayesian analysis.\n",
868+
"# notice the unequal sample sizes -- no problem in Bayesian analysis.\n",
868869
"N_A = 1500\n",
869870
"N_B = 750\n",
870871
"\n",
871-
"#generate some observations\n",
872+
"# generate some observations\n",
872873
"observations_A = pm.rbernoulli(true_p_A, N_A)\n",
873874
"observations_B = pm.rbernoulli(true_p_B, N_B)\n",
874875
"print \"Obs from Site A: \", observations_A[:30].astype(int), \"...\"\n",
@@ -978,7 +979,7 @@
978979
"input": [
979980
"figsize(12.5, 10)\n",
980981
"\n",
981-
"#histogram of posteriors\n",
982+
"# histogram of posteriors\n",
982983
"\n",
983984
"ax = plt.subplot(311)\n",
984985
"\n",
@@ -1257,7 +1258,7 @@
12571258
" fc=first_coin_flips,\n",
12581259
" sc=second_coin_flips):\n",
12591260
"\n",
1260-
" observed = fc*t_a + (1-fc)*sc\n",
1261+
" observed = fc * t_a + (1 - fc) * sc\n",
12611262
" return observed.sum() / float(N)"
12621263
],
12631264
"language": "python",
@@ -1360,7 +1361,7 @@
13601361
"input": [
13611362
"figsize(12.5, 3)\n",
13621363
"p_trace = mcmc.trace(\"freq_cheating\")[:]\n",
1363-
"plt.hist(p_trace, histtype=\"stepfilled\", normed=True, alpha=0.85, bins=30, \n",
1364+
"plt.hist(p_trace, histtype=\"stepfilled\", normed=True, alpha=0.85, bins=30,\n",
13641365
" label=\"posterior distribution\", color=\"#348ABD\")\n",
13651366
"plt.vlines([.05, .35], [0, 0], [5, 5], alpha=0.3)\n",
13661367
"plt.xlim(0, 1)\n",
@@ -1415,9 +1416,10 @@
14151416
"input": [
14161417
"p = pm.Uniform(\"freq_cheating\", 0, 1)\n",
14171418
"\n",
1419+
"\n",
14181420
"@pm.deterministic\n",
14191421
"def p_skewed(p=p):\n",
1420-
" return 0.5*p + 0.25"
1422+
" return 0.5 * p + 0.25"
14211423
],
14221424
"language": "python",
14231425
"metadata": {},
@@ -1491,7 +1493,7 @@
14911493
"input": [
14921494
"figsize(12.5, 3)\n",
14931495
"p_trace = mcmc.trace(\"freq_cheating\")[:]\n",
1494-
"plt.hist(p_trace, histtype=\"stepfilled\", normed=True, alpha=0.85, bins=30, \n",
1496+
"plt.hist(p_trace, histtype=\"stepfilled\", normed=True, alpha=0.85, bins=30,\n",
14951497
" label=\"posterior distribution\", color=\"#348ABD\")\n",
14961498
"plt.vlines([.05, .35], [0, 0], [5, 5], alpha=0.2)\n",
14971499
"plt.xlim(0, 1)\n",
@@ -1539,7 +1541,7 @@
15391541
"N = 10\n",
15401542
"x = np.empty(N, dtype=object)\n",
15411543
"for i in range(0, N):\n",
1542-
" x[i] = pm.Exponential('x_%i' % i, (i+1)**2)"
1544+
" x[i] = pm.Exponential('x_%i' % i, (i + 1) ** 2)"
15431545
],
15441546
"language": "python",
15451547
"metadata": {},
@@ -1575,10 +1577,10 @@
15751577
"challenger_data = np.genfromtxt(\"data/challenger_data.csv\", skip_header=1,\n",
15761578
" usecols=[1, 2], missing_values=\"NA\",\n",
15771579
" delimiter=\",\")\n",
1578-
"#drop the NA values\n",
1580+
"# drop the NA values\n",
15791581
"challenger_data = challenger_data[~np.isnan(challenger_data[:, 1])]\n",
15801582
"\n",
1581-
"#plot it, as a function of tempature (the first column)\n",
1583+
"# plot it, as a function of tempature (the first column)\n",
15821584
"print \"Temp (F), O-Ring failure?\"\n",
15831585
"print challenger_data\n",
15841586
"\n",
@@ -1587,7 +1589,7 @@
15871589
"plt.yticks([0, 1])\n",
15881590
"plt.ylabel(\"Damage Incident?\")\n",
15891591
"plt.xlabel(\"Outside temperature (Fahrenheit)\")\n",
1590-
"plt.title(\"Defects of the Space Shuttle O-Rings vs temperature\")\n"
1592+
"plt.title(\"Defects of the Space Shuttle O-Rings vs temperature\")"
15911593
],
15921594
"language": "python",
15931595
"metadata": {},
@@ -1769,9 +1771,9 @@
17691771
"parameters = zip(mu, tau, colors)\n",
17701772
"\n",
17711773
"for _mu, _tau, _color in parameters:\n",
1772-
" plt.plot(x, nor.pdf(x, _mu, scale=1./_tau),\n",
1774+
" plt.plot(x, nor.pdf(x, _mu, scale=1. / _tau),\n",
17731775
" label=\"$\\mu = %d,\\;\\\\tau = %.1f$\" % (_mu, _tau), color=_color)\n",
1774-
" plt.fill_between(x, nor.pdf(x, _mu, scale=1./_tau), color=_color,\n",
1776+
" plt.fill_between(x, nor.pdf(x, _mu, scale=1. / _tau), color=_color,\n",
17751777
" alpha=.33)\n",
17761778
"\n",
17771779
"plt.legend(loc=\"upper right\")\n",
@@ -1820,14 +1822,14 @@
18201822
"temperature = challenger_data[:, 0]\n",
18211823
"D = challenger_data[:, 1] # defect or not?\n",
18221824
"\n",
1823-
"#notice the`value` here. We explain why below.\n",
1825+
"# notice the`value` here. We explain why below.\n",
18241826
"beta = pm.Normal(\"beta\", 0, 0.001, value=0)\n",
18251827
"alpha = pm.Normal(\"alpha\", 0, 0.001, value=0)\n",
18261828
"\n",
18271829
"\n",
18281830
"@pm.deterministic\n",
18291831
"def p(t=temperature, alpha=alpha, beta=beta):\n",
1830-
" return 1.0 / (1. + np.exp(beta*t + alpha))\n"
1832+
" return 1.0 / (1. + np.exp(beta * t + alpha))"
18311833
],
18321834
"language": "python",
18331835
"metadata": {},
@@ -1920,7 +1922,7 @@
19201922
"\n",
19211923
"figsize(12.5, 6)\n",
19221924
"\n",
1923-
"#histogram of the samples:\n",
1925+
"# histogram of the samples:\n",
19241926
"plt.subplot(211)\n",
19251927
"plt.title(r\"Posterior distributions of the variables $\\alpha, \\beta$\")\n",
19261928
"plt.hist(beta_samples, histtype='stepfilled', bins=35, alpha=0.85,\n",
@@ -1963,7 +1965,7 @@
19631965
"cell_type": "code",
19641966
"collapsed": false,
19651967
"input": [
1966-
"t = np.linspace(temperature.min() - 5, temperature.max()+5, 50)[:, None]\n",
1968+
"t = np.linspace(temperature.min() - 5, temperature.max() + 5, 50)[:, None]\n",
19671969
"p_t = logistic(t.T, beta_samples, alpha_samples)\n",
19681970
"\n",
19691971
"mean_prob_t = p_t.mean(axis=0)"
@@ -2164,8 +2166,8 @@
21642166
"plt.title(\"Simulated dataset using posterior parameters\")\n",
21652167
"figsize(12.5, 6)\n",
21662168
"for i in range(4):\n",
2167-
" ax = plt.subplot(4, 1, i+1)\n",
2168-
" plt.scatter(temperature, simulations[1000*i, :], color=\"k\",\n",
2169+
" ax = plt.subplot(4, 1, i + 1)\n",
2170+
" plt.scatter(temperature, simulations[1000 * i, :], color=\"k\",\n",
21692171
" s=50, alpha=0.6)"
21702172
],
21712173
"language": "python",
@@ -2369,7 +2371,7 @@
23692371
"plt.title(\"Random model\")\n",
23702372
"\n",
23712373
"# constant model\n",
2372-
"constant_prob = 7./23*np.ones(23)\n",
2374+
"constant_prob = 7. / 23 * np.ones(23)\n",
23732375
"separation_plot(constant_prob, D)\n",
23742376
"plt.title(\"Constant-prediction model\")"
23752377
],
@@ -2448,7 +2450,7 @@
24482450
"cell_type": "code",
24492451
"collapsed": false,
24502452
"input": [
2451-
"#type your code here.\n",
2453+
"# type your code here.\n",
24522454
"figsize(12.5, 4)\n",
24532455
"\n",
24542456
"plt.scatter(alpha_samples, beta_samples, alpha=0.1)\n",

0 commit comments

Comments
 (0)