Skip to content

Commit 6eedcb6

Browse files
Merge pull request CamDavidsonPilon#199 from eli-b/examples-style
ExamplesFromChapters coding style
2 parents 2d84415 + e2ef50c commit 6eedcb6

12 files changed

+249
-237
lines changed

ExamplesFromChapters/Chapter1/SMS_behaviour.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
count_data = np.loadtxt("../../Chapter1_Introduction/data/txtdata.csv")
55
n_count_data = len(count_data)
66

7-
alpha = 1.0/count_data.mean() #recall count_data is
8-
#the variable that holds our txt counts
9-
10-
lambda_1 = pm.Exponential( "lambda_1", alpha )
11-
lambda_2 = pm.Exponential( "lambda_2", alpha )
7+
alpha = 1.0 / count_data.mean() # recall count_data is
8+
# the variable that holds our txt counts
9+
10+
lambda_1 = pm.Exponential("lambda_1", alpha)
11+
lambda_2 = pm.Exponential("lambda_2", alpha)
12+
13+
tau = pm.DiscreteUniform("tau", lower=0, upper=n_count_data)
1214

13-
tau = pm.DiscreteUniform( "tau", lower = 0, upper = n_count_data )
1415

1516
@pm.deterministic
16-
def lambda_( tau = tau, lambda_1 = lambda_1, lambda_2 = lambda_2 ):
17-
out = np.zeros( n_count_data )
18-
out[:tau] = lambda_1 #lambda before tau is lambda1
19-
out[tau:] = lambda_2 #lambda after tau is lambda2
17+
def lambda_(tau=tau, lambda_1=lambda_1, lambda_2=lambda_2):
18+
out = np.zeros(n_count_data)
19+
out[:tau] = lambda_1 # lambda before tau is lambda1
20+
out[tau:] = lambda_2 # lambda after tau is lambda2
2021
return out
21-
22-
observation = pm.Poisson( "obs", lambda_, value = count_data, observed = True)
23-
model = pm.Model( [observation, lambda_1, lambda_2, tau] )
22+
23+
observation = pm.Poisson("obs", lambda_, value=count_data, observed=True)
24+
model = pm.Model([observation, lambda_1, lambda_2, tau])
2425

2526

2627
mcmc = pm.MCMC(model)
27-
mcmc.sample( 100000, 50000, 1 )
28+
mcmc.sample(100000, 50000, 1)

ExamplesFromChapters/Chapter2/ABtesting.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,35 @@
55

66
import pymc as pm
77

8-
#these two quantities are unknown to us.
9-
true_p_A = 0.05
8+
# these two quantities are unknown to us.
9+
true_p_A = 0.05
1010
true_p_B = 0.04
1111

12-
#notice the unequal sample sizes -- no problem in Bayesian analysis.
12+
# notice the unequal sample sizes -- no problem in Bayesian analysis.
1313
N_A = 1500
14-
N_B = 1000
14+
N_B = 1000
1515

16-
#generate data
17-
observations_A = pm.rbernoulli( true_p_A, N_A )
18-
observations_B = pm.rbernoulli( true_p_B, N_B )
16+
# generate data
17+
observations_A = pm.rbernoulli(true_p_A, N_A)
18+
observations_B = pm.rbernoulli(true_p_B, N_B)
1919

2020

21-
22-
#set up the pymc model. Again assume Uniform priors for p_A and p_B
23-
21+
# set up the pymc model. Again assume Uniform priors for p_A and p_B
2422
p_A = pm.Uniform("p_A", 0, 1)
2523
p_B = pm.Uniform("p_B", 0, 1)
2624

2725

28-
#define the deterministic delta function. This is our unknown of interest.
26+
# define the deterministic delta function. This is our unknown of interest.
2927

3028
@pm.deterministic
31-
def delta( p_A = p_A, p_B = p_B ):
29+
def delta(p_A=p_A, p_B=p_B):
3230
return p_A - p_B
3331

3432

35-
#set of observations, in this case we have two observation datasets.
36-
obs_A = pm.Bernoulli( "obs_A", p_A, value = observations_A, observed = True )
37-
obs_B = pm.Bernoulli( "obs_B", p_B, value = observations_B, observed = True )
33+
# set of observations, in this case we have two observation datasets.
34+
obs_A = pm.Bernoulli("obs_A", p_A, value=observations_A, observed=True)
35+
obs_B = pm.Bernoulli("obs_B", p_B, value=observations_B, observed=True)
3836

39-
#to be explained in chapter 3.
40-
mcmc = pm.MCMC( [p_A, p_B, delta, obs_A, obs_B] )
41-
mcmc.sample( 20000, 1000)
37+
# to be explained in chapter 3.
38+
mcmc = pm.MCMC([p_A, p_B, delta, obs_A, obs_B])
39+
mcmc.sample(20000, 1000)
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import pymc as pm
22

3-
p = pm.Uniform( "freq_cheating", 0, 1)
3+
p = pm.Uniform("freq_cheating", 0, 1)
4+
45

56
@pm.deterministic
6-
def p_skewed( p = p ):
7-
return 0.5*p + 0.25
8-
9-
yes_responses = pm.Binomial( "number_cheaters", 100, p_skewed, value = 35, observed = True )
10-
11-
model = pm.Model( [yes_responses, p_skewed, p ] )
12-
13-
### To Be Explained in Chapter 3!
7+
def p_skewed(p=p):
8+
return 0.5 * p + 0.25
9+
10+
yes_responses = pm.Binomial(
11+
"number_cheaters", 100, p_skewed, value=35, observed=True)
12+
13+
model = pm.Model([yes_responses, p_skewed, p])
14+
15+
# To Be Explained in Chapter 3!
1416
mcmc = pm.MCMC(model)
15-
mcmc.sample( 50000, 25000 )
17+
mcmc.sample(50000, 25000)
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1+
import numpy as np
12
import pymc as pm
23

34

4-
challenger_data = np.genfromtxt("../../Chapter2_MorePyMC/data/challenger_data.csv", skip_header = 1, usecols=[1,2], missing_values="NA", delimiter=",")
5-
#drop the NA values
6-
challenger_data = challenger_data[ ~np.isnan(challenger_data[:,1]) ]
5+
challenger_data = np.genfromtxt(
6+
"../../Chapter2_MorePyMC/data/challenger_data.csv",
7+
skip_header=1, usecols=[1, 2], missing_values="NA", delimiter=",")
8+
# drop the NA values
9+
challenger_data = challenger_data[~np.isnan(challenger_data[:, 1])]
710

811

9-
temperature = challenger_data[:,0]
10-
D = challenger_data[:,1] #defect or not?
12+
temperature = challenger_data[:, 0]
13+
D = challenger_data[:, 1] # defect or not?
14+
15+
beta = pm.Normal("beta", 0, 0.001, value=0)
16+
alpha = pm.Normal("alpha", 0, 0.001, value=0)
1117

12-
beta = pm.Normal( "beta", 0, 0.001, value = 0 )
13-
alpha = pm.Normal( "alpha", 0, 0.001, value = 0 )
1418

1519
@pm.deterministic
16-
def p( temp = temperature, alpha = alpha, beta = beta):
17-
return 1.0/( 1. + np.exp( beta*temperature + alpha) )
20+
def p(temp=temperature, alpha=alpha, beta=beta):
21+
return 1.0 / (1. + np.exp(beta * temperature + alpha))
1822

1923

20-
observed = pm.Bernoulli( "bernoulli_obs", p, value = D, observed=True)
24+
observed = pm.Bernoulli("bernoulli_obs", p, value=D, observed=True)
2125

22-
model = pm.Model( [observed, beta, alpha] )
26+
model = pm.Model([observed, beta, alpha])
2327

24-
#mysterious code to be explained in Chapter 3
28+
# mysterious code to be explained in Chapter 3
2529
map_ = pm.MAP(model)
2630
map_.fit()
27-
mcmc = pm.MCMC( model )
28-
mcmc.sample( 260000, 220000, 2 )
31+
mcmc = pm.MCMC(model)
32+
mcmc.sample(260000, 220000, 2)

ExamplesFromChapters/Chapter3/ClusteringWithGaussians.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,40 @@
22
import pymc as pm
33

44

5-
data = np.loadtxt( "../../Chapter3_MCMC/data/mixture_data.csv", delimiter="," )
5+
data = np.loadtxt("../../Chapter3_MCMC/data/mixture_data.csv", delimiter=",")
66

77

8-
p = pm.Uniform( "p", 0, 1)
8+
p = pm.Uniform("p", 0, 1)
99

10-
assignment = pm.Categorical("assignment", [p, 1-p], size = data.shape[0] )
10+
assignment = pm.Categorical("assignment", [p, 1 - p], size=data.shape[0])
1111

12-
taus = 1.0/pm.Uniform( "stds", 0, 100, size= 2)**2 #notice the size!
13-
centers = pm.Normal( "centers", [150, 150], [0.001, 0.001], size =2 )
12+
taus = 1.0 / pm.Uniform("stds", 0, 100, size=2) ** 2 # notice the size!
13+
centers = pm.Normal("centers", [150, 150], [0.001, 0.001], size=2)
1414

1515
"""
1616
The below deterministic functions map a assingment, in this case 0 or 1,
1717
to a set of parameters, located in the (1,2) arrays `taus` and `centers.`
1818
"""
1919

20-
@pm.deterministic
21-
def center_i( assignment = assignment, centers = centers ):
22-
return centers[ assignment]
2320

2421
@pm.deterministic
25-
def tau_i( assignment = assignment, taus = taus ):
26-
return taus[ assignment]
22+
def center_i(assignment=assignment, centers=centers):
23+
return centers[assignment]
2724

28-
#and to combine it with the observations:
29-
observations = pm.Normal( "obs", center_i, tau_i, value = data, observed = True )
3025

31-
#below we create a model class
32-
model = pm.Model( [p, assignment, taus, centers ] )
26+
@pm.deterministic
27+
def tau_i(assignment=assignment, taus=taus):
28+
return taus[assignment]
29+
30+
# and to combine it with the observations:
31+
observations = pm.Normal("obs", center_i, tau_i,
32+
value=data, observed=True)
33+
34+
# below we create a model class
35+
model = pm.Model([p, assignment, taus, centers])
3336

3437

35-
map_ = pm.MAP( model )
38+
map_ = pm.MAP(model)
3639
map_.fit()
37-
mcmc = pm.MCMC( model )
38-
mcmc.sample( 100000, 50000 )
40+
mcmc = pm.MCMC(model)
41+
mcmc.sample(100000, 50000)

Prologue/Prologue.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@
187187
"collapsed": false,
188188
"input": [
189189
"from IPython.core.display import HTML\n",
190+
"\n",
191+
"\n",
190192
"def css_styling():\n",
191193
" styles = open(\"../styles/custom.css\", \"r\").read()\n",
192194
" return HTML(styles)\n",
@@ -280,4 +282,4 @@
280282
"metadata": {}
281283
}
282284
]
283-
}
285+
}

sandbox/ABCtests.ipynb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
"cell_type": "code",
3030
"collapsed": false,
3131
"input": [
32-
"obs = np.array( [0,0,1] )\n",
32+
"obs = np.array([0, 0, 1])\n",
3333
"\n",
3434
"uni = pm.Uniform(\"prop\", 0, 1)\n",
35-
"fake_obs = pm.Bernoulli(\"fake_obs\", uni, size=3 )\n",
35+
"fake_obs = pm.Bernoulli(\"fake_obs\", uni, size=3)\n",
36+
"\n",
3637
"\n",
3738
"@pm.deterministic\n",
38-
"def accept(uni = uni, fake_obs = fake_obs, obs = obs):\n",
39-
" if np.array_equal( fake_obs, obs ):\n",
39+
"def accept(uni=uni, fake_obs=fake_obs, obs=obs):\n",
40+
" if np.array_equal(fake_obs, obs):\n",
4041
" return uni\n",
4142
" else:\n",
4243
" return None"
@@ -50,7 +51,7 @@
5051
"cell_type": "code",
5152
"collapsed": false,
5253
"input": [
53-
"mcmc = pm.MCMC([uni, fake_obs, accept] )"
54+
"mcmc = pm.MCMC([uni, fake_obs, accept])"
5455
],
5556
"language": "python",
5657
"metadata": {},
@@ -99,7 +100,7 @@
99100
"cell_type": "code",
100101
"collapsed": false,
101102
"input": [
102-
"hist( samples[samples > 0] )"
103+
"hist(samples[samples > 0])"
103104
],
104105
"language": "python",
105106
"metadata": {},

0 commit comments

Comments
 (0)