def model_gen(): variables = [] intercept = pymc.Normal("intercept", mu=0, tau=50**-2) sd = pymc.Gamma("sd", alpha=3, beta=2.0) responses = pymc.Normal("responses", mu=zeros((nPredictors, 1)), tau=ones((nPredictors, 1)) * 5**-2) variables.append(intercept) variables.append(responses) variables.append(sd) obsMeans = intercept + pymc.sum(responses * observedPredictors, axis=0) obs = pymc.Normal("obs", mu=obsMeans, tau=sd**-2, observed=True, value=data) variables.append(obs) return variables
def poisson_regression(targets, predictors, iters=2000): """ Return the posterior of a Bayesian Poisson regression model. This function takes the targets and predictors and builds a Poisson regression model. The predictor coefficients are found by sampling from the posterior using PyMC (NUTS in particular). The posterior is returned as an MxN array, where M is the number of samples and N is the number of predictors. The first column is the coefficient for the first predictor and so on. Requires PyMC3 """ with pm.Model() as poisson_model: # priors for coefficients coeffs = pm.Uniform('coeffs', -10, 10, shape=(1, predictors.shape[1])) p = t.exp(pm.sum(coeffs*predictors.values, 1)) obs = pm.Poisson('obs', p, observed=targets) start = pm.find_MAP() step = pm.NUTS(scaling=start) poisson_trace = pm.sample(iters, step, start=start, progressbar=False) return poisson_trace['coeffs'].squeeze()
def model_gen(): variables = [] intercept = pymc.Normal("intercept",mu = 0, tau = 50**-2) sd = pymc.Gamma("sd", alpha = 3 , beta = 2.0) responses = pymc.Normal("responses", mu = zeros((nPredictors,1)), tau = ones((nPredictors,1)) * 5**-2 ) variables.append(intercept) variables.append(responses) variables.append (sd) obsMeans = intercept + pymc.sum(responses * observedPredictors, axis = 0) obs = pymc.Normal ("obs", mu = obsMeans, tau = sd**-2, observed = True, value = data) variables.append(obs) return variables
''' Created on Jan 20, 2010 @author: johnsalvatier ''' from numpy import * from pymc import * import pymc a = ones((3,4)) j= NumpyDeterministics.sum_jacobian('a', a = a, axis = 0) print j * 1 x = Normal('z', mu = ones((3,4)), tau = ones((3,4))) y = pymc.sum(x, axis = 1) print y
''' Created on Jan 20, 2010 @author: johnsalvatier ''' from numpy import * from pymc import * import pymc a = ones((3, 4)) j = NumpyDeterministics.sum_jacobian('a', a=a, axis=0) print j * 1 x = Normal('z', mu=ones((3, 4)), tau=ones((3, 4))) y = pymc.sum(x, axis=1) print y