-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1546 from usptact/master
Adding hierarhical partial pooling example
- Loading branch information
Showing
4 changed files
with
271 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,4 @@ _build | |
mcmc.sqlite | ||
|
||
# Docker development | ||
notebooks/ | ||
#notebooks/ |
214 changes: 214 additions & 0 deletions
214
docs/source/notebooks/hierarchical_partial_pooling.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# | ||
# Demonstrates the usage of hierarchical partial pooling | ||
# See http://mc-stan.org/documentation/case-studies/pool-binary-trials.html for more details | ||
# | ||
|
||
import pymc3 as pm | ||
import numpy as np | ||
|
||
data = np.loadtxt( 'data/efron-morris-75-data.tsv', delimiter="\t", skiprows=1, usecols=(2,3) ) | ||
|
||
atBats = data[:,0] | ||
hits = data[:,1] | ||
|
||
N = len( hits ) | ||
|
||
model = pm.Model() | ||
|
||
# we want to bound the kappa below | ||
BoundedKappa = pm.Bound( pm.Pareto, lower=1.0 ) | ||
|
||
with model: | ||
phi = pm.Uniform( 'phi', lower=0.0, upper=1.0 ) | ||
kappa = BoundedKappa( 'kappa', alpha=1.0001, m=1.5 ) | ||
thetas = pm.Beta( 'thetas', alpha=phi*kappa, beta=(1.0-phi)*kappa, shape=N ) | ||
ys = pm.Binomial( 'ys', n=atBats, p=thetas, observed=hits ) | ||
|
||
def run( n=100000 ): | ||
with model: | ||
# initialize NUTS() with ADVI under the hood | ||
trace = pm.sample( n ) | ||
|
||
# drop some first samples as burnin | ||
pm.traceplot( trace[1000:] ) | ||
|
||
if __name__ == '__main__': | ||
run() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FirstName LastName At-Bats Hits BattingAverage RemainingAt-Bats RemainingAverage SeasonAt-Bats SeasonHits SeasonAverage | ||
Roberto Clemente 45 18 0.4 367 0.346 412 145 0.352 | ||
Frank Robinson 45 17 0.378 426 0.2981 471 144 0.306 | ||
Frank Howard 45 16 0.356 521 0.2764 566 160 0.283 | ||
Jay Johnstone 45 15 0.333 275 0.2218 320 76 0.238 | ||
Ken Berry 45 14 0.311 418 0.2727 463 128 0.276 | ||
Jim Spencer 45 14 0.311 466 0.2704 511 140 0.274 | ||
Don Kessinger 45 13 0.289 586 0.2645 631 168 0.266 | ||
Luis Alvarado 45 12 0.267 138 0.2101 183 41 0.224 | ||
Ron Santo 45 11 0.244 510 0.2686 555 148 0.267 | ||
Ron Swaboda 45 11 0.244 200 0.23 245 57 0.233 | ||
Rico Petrocelli 45 10 0.222 538 0.2639 583 152 0.261 | ||
Ellie Rodriguez 45 10 0.222 186 0.2258 231 52 0.225 | ||
George Scott 45 10 0.222 435 0.3034 480 142 0.296 | ||
Del Unser 45 10 0.222 277 0.2635 322 83 0.258 | ||
Billy Williams 45 10 0.222 591 0.3299 636 205 0.251 | ||
Bert Campaneris 45 9 0.2 558 0.2849 603 168 0.279 | ||
Thurman Munson 45 8 0.178 408 0.3162 453 137 0.302 | ||
Max Alvis 45 7 0.156 70 0.2 115 21 0.183 |