Skip to content

Commit 9b230ff

Browse files
committed
autopep8 fixes for the code examples with added imports
1 parent 66cff78 commit 9b230ff

File tree

5 files changed

+82
-73
lines changed

5 files changed

+82
-73
lines changed

ExamplesFromChapters/Chapter1/SMS_behaviour.py

+15-14
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

+16-18
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)
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)
+18-14
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)
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1+
import numpy as np
12
import pymc as pm
23

34

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

67

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

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

11-
taus = 1.0/mc.Uniform( "stds", 0, 100, size= 2)**2 #notice the size!
12-
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)
1314

1415
"""
1516
The below deterministic functions map a assingment, in this case 0 or 1,
1617
to a set of parameters, located in the (1,2) arrays `taus` and `centers.`
1718
"""
1819

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

2321
@pm.deterministic
24-
def tau_i( assignment = assignment, taus = taus ):
25-
return taus[ assignment]
22+
def center_i(assignment=assignment, centers=centers):
23+
return centers[assignment]
2624

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

30-
#below we create a model class
31-
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])
3236

3337

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

0 commit comments

Comments
 (0)