-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Update examples #2254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update examples #2254
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,33 +5,34 @@ | |
|
||
import pymc3 as pm | ||
import numpy as np | ||
import theano | ||
|
||
data = np.loadtxt(pm.get_data('efron-morris-75-data.tsv'), delimiter="\t", skiprows=1, usecols=(2,3)) | ||
|
||
atBats = data[:,0].astype(theano.config.floatX) | ||
hits = data[:,1].astype(theano.config.floatX) | ||
|
||
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 ): | ||
def build_model(): | ||
data = np.loadtxt(pm.get_data('efron-morris-75-data.tsv'), delimiter="\t", | ||
skiprows=1, usecols=(2,3)) | ||
|
||
atBats = pm.floatX(data[:,0]) | ||
hits = pm.floatX(data[:,1]) | ||
|
||
N = len(hits) | ||
|
||
# we want to bound the kappa below | ||
BoundedKappa = pm.Bound(pm.Pareto, lower=1.0) | ||
|
||
with pm.Model() as 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) | ||
return model | ||
|
||
def run(n=2000): | ||
model = build_model() | ||
with model: | ||
# initialize NUTS() with ADVI under the hood | ||
trace = pm.sample( n ) | ||
trace = pm.sample(n, nuts_kwargs={'target_accept':.99}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, does it require this to converge? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's fine without, but there is a divergent warning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem; just surprised. |
||
|
||
# drop some first samples as burnin | ||
pm.traceplot( trace[1000:] ) | ||
pm.traceplot(trace[1000:]) | ||
|
||
if __name__ == '__main__': | ||
run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid camelCase