markdregan / bayesian-modelling-in-python Goto Github PK
View Code? Open in Web Editor NEWA python tutorial on bayesian modeling techniques (PyMC3)
A python tutorial on bayesian modeling techniques (PyMC3)
Add examples of questions that can be asked of the posterior predictive distribution for the hierarchal negative binomial distribution. such as:
Check out: http://pymc-devs.github.io/pymc3/posterior_predictive/
This might be a flexible solution when sampling form the posterior predictive for various covariates
For the current pymc3
in pm.traceplot
it should be varnames=
, not vars=
.
Running win 10x64, anaconda, python3.5, theano 0.9.0dev4, pymc3 3.0rc4 updated 10-dec-2106.
Have confirmed
pm.switch(tau........
not found.
Perhaps an alternative syntax is used.
Code section and error output follows.
franc
with pm.Model() as model:
# Index to true model
prior_model_prob = 0.5
#tau = pm.DiscreteUniform('tau', lower=0, upper=1)
tau = pm.Bernoulli('tau', prior_model_prob)
# Poisson parameters
mu_p = pm.Uniform('mu_p', 0, 60)
# Negative Binomial parameters
alpha = pm.Exponential('alpha', lam=0.2)
mu_nb = pm.Uniform('mu_nb', lower=0, upper=60)
y_like = pm.DensityDist('y_like',
lambda value: pm.switch(tau,
pm.Poisson.dist(mu_p).logp(value),
pm.NegativeBinomial.dist(mu_nb, alpha).logp(value)
),
observed=messages['time_delay_seconds'].values)
start = pm.find_MAP()
step1 = pm.Metropolis([mu_p, alpha, mu_nb])
step2 = pm.ElemwiseCategorical(vars=[tau], values=[0,1])
trace = pm.sample(200000, step=[step1, step2], start=start)
_ = pm.traceplot(trace[burnin:], varnames=['tau'])
AttributeError Traceback (most recent call last)
in ()
18 pm.NegativeBinomial.dist(mu_nb, alpha).logp(value)
19 ),
---> 20 observed=messages['time_delay_seconds'].values)
21
22 start = pm.find_MAP()
C:\Anaconda3\lib\site-packages\pymc3\distributions\distribution.py in new(cls, name, *args, **kwargs)
29 data = kwargs.pop('observed', None)
30 dist = cls.dist(*args, **kwargs)
---> 31 return model.Var(name, dist, data)
32 else:
33 raise TypeError("Name needs to be a string but got: %s" % name)
C:\Anaconda3\lib\site-packages\pymc3\model.py in Var(self, name, dist, data)
301 else:
302 var = ObservedRV(name=name, data=data,
--> 303 distribution=dist, model=self)
304 self.observed_RVs.append(var)
305 if var.missing_values:
C:\Anaconda3\lib\site-packages\pymc3\model.py in init(self, type, owner, index, name, data, distribution, model)
584 self.missing_values = data.missing_values
585
--> 586 self.logp_elemwiset = distribution.logp(data)
587 self.model = model
588 self.distribution = distribution
in (value)
14
15 y_like = pm.DensityDist('y_like',
---> 16 lambda value: pm.switch(tau,
17 pm.Poisson.dist(mu_p).logp(value),
18 pm.NegativeBinomial.dist(mu_nb, alpha).logp(value)
AttributeError: module 'pymc3' has no attribute 'switch'
Add poisson regression to the tutorial with covariates such as: day of week, time of day, number of people in conversation etc.
I am using pymc3 version 3.0 together with anaconda 4.0.0 & python 2.7 and theano 0.8.2.
In the section 2 notebook:
with pm.Model() as model:
mu = pm.Uniform('mu', lower=0, upper=100)
y_est = pm.Poisson('y_est', mu=mu, observed=messages['time_delay_seconds'].values)
y_pred = pm.Poisson('y_pred', mu=mu)
start = pm.find_MAP()
step = pm.Metropolis()
trace = pm.sample(50000, step, start=start, progressbar=True)
Leaving out the y_ptred = ...
statement removes the crash but also removes the purpose of the example...
Since I'm trying to learn pymc3 I currently have no idea where to start.
Something goes wrong in theano (strack-trace):
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-76dad9c91fda> in <module>()
7 start = pm.find_MAP()
8 step = pm.Metropolis()
----> 9 trace = pm.sample(50000, step, start=start, progressbar=True)
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\pymc3\sampling.pyc in sample(draws, step, start, trace, chain, njobs, tune, progressbar, model, random_seed)
148 sample_func = _sample
149
--> 150 return sample_func(**sample_args)
151
152
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\pymc3\sampling.pyc in _sample(draws, step, start, trace, chain, tune, progressbar, model, random_seed)
157 progress = progress_bar(draws)
158 try:
--> 159 for i, strace in enumerate(sampling):
160 if progressbar:
161 progress.update(i)
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\pymc3\sampling.pyc in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
239 if i == tune:
240 step = stop_tuning(step)
--> 241 point = step.step(point)
242 strace.record(point)
243 yield strace
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\pymc3\step_methods\compound.pyc in step(self, point)
12 def step(self, point):
13 for method in self.methods:
---> 14 point = method.step(point)
15 return point
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\pymc3\step_methods\arraystep.pyc in step(self, point)
116 bij = DictToArrayBijection(self.ordering, point)
117
--> 118 apoint = self.astep(bij.map(point))
119 return bij.rmap(apoint)
120
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\pymc3\step_methods\metropolis.pyc in astep(self, q0)
123 q = q0 + delta
124
--> 125 q_new = metrop_select(self.delta_logp(q, q0), q, q0)
126
127 if q_new is q:
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\theano\compile\function_module.pyc in __call__(self, *args, **kwargs)
869 node=self.fn.nodes[self.fn.position_of_error],
870 thunk=thunk,
--> 871 storage_map=getattr(self.fn, 'storage_map', None))
872 else:
873 # old-style linkers raise their own exceptions
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\theano\gof\link.pyc in raise_with_op(node, thunk, exc_info, storage_map)
312 # extra long error message in that case.
313 pass
--> 314 reraise(exc_type, exc_value, exc_trace)
315
316
C:\Users\egbert\AppData\Local\Continuum\Anaconda\py27_64\lib\site-packages\theano\compile\function_module.pyc in __call__(self, *args, **kwargs)
857 t0_fn = time.time()
858 try:
--> 859 outputs = self.fn()
860 except Exception:
861 if hasattr(self.fn, 'position_of_error'):
Model fails
14
15 y_like = pm.DensityDist('y_like',
---> 16 lambda value: pm.switch(tau,
17 pm.Poisson.dist(mu_p).logp(value),
18 pm.NegativeBinomial.dist(mu_nb, alpha).logp(value)
AttributeError: module 'pymc3' has no attribute 'switch'
Found a fix suggested by Cameron Davidson-Pilson which looks like it was intended to be merged into your code (but seems it wasn't?). The suggested revision to lambda works fine.
y_like = pm.DensityDist('y_like',
lambda value: pm.math.switch(tau,
pm.Poisson.dist(mu_p).logp(value),
pm.NegativeBinomial.dist(mu_nb, alpha).logp(value)
),
observed=messages['time_delay_seconds'].values)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.