Skip to content

Commit 010de60

Browse files
authored
Merge pull request #1546 from usptact/master
Adding hierarhical partial pooling example
2 parents a54a53e + aab8625 commit 010de60

File tree

4 files changed

+271
-1
lines changed

4 files changed

+271
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ _build
2323
mcmc.sqlite
2424

2525
# Docker development
26-
notebooks/
26+
#notebooks/

docs/source/notebooks/hierarchical_partial_pooling.ipynb

Lines changed: 214 additions & 0 deletions
Large diffs are not rendered by default.

pymc3/examples/baseball.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# Demonstrates the usage of hierarchical partial pooling
3+
# See http://mc-stan.org/documentation/case-studies/pool-binary-trials.html for more details
4+
#
5+
6+
import pymc3 as pm
7+
import numpy as np
8+
9+
data = np.loadtxt( 'data/efron-morris-75-data.tsv', delimiter="\t", skiprows=1, usecols=(2,3) )
10+
11+
atBats = data[:,0]
12+
hits = data[:,1]
13+
14+
N = len( hits )
15+
16+
model = pm.Model()
17+
18+
# we want to bound the kappa below
19+
BoundedKappa = pm.Bound( pm.Pareto, lower=1.0 )
20+
21+
with model:
22+
phi = pm.Uniform( 'phi', lower=0.0, upper=1.0 )
23+
kappa = BoundedKappa( 'kappa', alpha=1.0001, m=1.5 )
24+
thetas = pm.Beta( 'thetas', alpha=phi*kappa, beta=(1.0-phi)*kappa, shape=N )
25+
ys = pm.Binomial( 'ys', n=atBats, p=thetas, observed=hits )
26+
27+
def run( n=100000 ):
28+
with model:
29+
# initialize NUTS() with ADVI under the hood
30+
trace = pm.sample( n )
31+
32+
# drop some first samples as burnin
33+
pm.traceplot( trace[1000:] )
34+
35+
if __name__ == '__main__':
36+
run()
37+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FirstName LastName At-Bats Hits BattingAverage RemainingAt-Bats RemainingAverage SeasonAt-Bats SeasonHits SeasonAverage
2+
Roberto Clemente 45 18 0.4 367 0.346 412 145 0.352
3+
Frank Robinson 45 17 0.378 426 0.2981 471 144 0.306
4+
Frank Howard 45 16 0.356 521 0.2764 566 160 0.283
5+
Jay Johnstone 45 15 0.333 275 0.2218 320 76 0.238
6+
Ken Berry 45 14 0.311 418 0.2727 463 128 0.276
7+
Jim Spencer 45 14 0.311 466 0.2704 511 140 0.274
8+
Don Kessinger 45 13 0.289 586 0.2645 631 168 0.266
9+
Luis Alvarado 45 12 0.267 138 0.2101 183 41 0.224
10+
Ron Santo 45 11 0.244 510 0.2686 555 148 0.267
11+
Ron Swaboda 45 11 0.244 200 0.23 245 57 0.233
12+
Rico Petrocelli 45 10 0.222 538 0.2639 583 152 0.261
13+
Ellie Rodriguez 45 10 0.222 186 0.2258 231 52 0.225
14+
George Scott 45 10 0.222 435 0.3034 480 142 0.296
15+
Del Unser 45 10 0.222 277 0.2635 322 83 0.258
16+
Billy Williams 45 10 0.222 591 0.3299 636 205 0.251
17+
Bert Campaneris 45 9 0.2 558 0.2849 603 168 0.279
18+
Thurman Munson 45 8 0.178 408 0.3162 453 137 0.302
19+
Max Alvis 45 7 0.156 70 0.2 115 21 0.183

0 commit comments

Comments
 (0)