pymc3

Using custom likelihood in PYMC3 leads to error with “expected ndarray”

China☆狼群 提交于 2019-12-11 09:39:55
问题 I'm trying to use a custom distribution (Generalized Extreme Value or GEV distribution) in PYMC3. I have written some code to compute this, but I get an error of ValueError: expected an ndarray Apply node that caused the error: MakeVector{dtype='float64'}( logp_sigma_log , __logp_mu, __logp_xi, __logp_x) Here's the code for reference: @theano.as_op(itypes=[tt.dvector, tt.dscalar, tt.dscalar, tt.dscalar], otypes=[tt.dscalar]) def likelihood_op(values, mu, sigma, xi): logp = 0. for val in

pymc3 generate stochastic variables with array of parameters

半世苍凉 提交于 2019-12-11 08:03:26
问题 In pymc3, a stochastic variable of array shape say 3 can be generated as follows y = Normal('y', mu, sigma, shape=3, observed=some_data) Now suppose that y depends on an array of parameters mu = [1,2,3] and sigma = [4,5,6] instead of single values, how would I specify it? 回答1: I wanted a way to do the something like following for i in range(3): y[i] = Normal(mu[i], sigma[i], observed=somedata[i]) The way to do it is to simply pass in a list of values. y=Normal(mu, sigma, observed=somedata)

Defining grad of a custom Op theano

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:50:11
问题 I am trying to define a custom theano Op with a gradient to use it with pymc3 but I don't understand how to define the grad method. The code below is where I'm stuck. The function phi() is a mock function (in practice, it is an external program); for a scalar input x it returns a vector (phi_0(x), phi_1(x), ...) . The function phi_diff() (also a mock function) returns the vector (dphi_0/dx, dphi_1/dx, ...) . I wrapped phi() and phi_diff() in a theano.Op object but my implementation of the

PyMC3- Custom theano Op to do numerical integration

我是研究僧i 提交于 2019-12-11 04:08:40
问题 I am using PyMC3 for parameter estimation using a particular likelihood function which has to be defined. I googled it and found out that I should use the densitydist method for implementing the user defined likelihood functions but it is not working. How to incorporate a user defined likelihood function in PyMC3 and to find out the maximum a posteriori (MAP) estimate for my model? My code is given below. Here L is the analytic form of my Likelihood function. I have some observational data

Running a multivariate ordered logit in PyMC3

强颜欢笑 提交于 2019-12-11 02:49:22
问题 I'm trying to build a Bayesian multivariate ordered logit model using PyMC3. I have gotten a toy multivariate logit model working based on the examples in this book. I've also gotten an ordered logistic regression model running based on the example at the bottom of this page. However, I cannot get an ordered, multivariate logistic regression to run. I think the issue could be the way the cutpoints are specified, specifically the shape parameter, but I'm not sure why it would be different if

PyMC3 how to implement latent dirichlet allocation?

半腔热情 提交于 2019-12-11 02:19:24
问题 I am trying to implement lda using PyMC3. However, when defining the last part of the model in which words are sampled based on their topics, I keep getting the error: TypeError: list indices must be integers, not TensorVariable How to tackle the problem? The code is as follows: ## Data Preparation K = 2 # number of topics N = 4 # number of words D = 3 # number of documents import numpy as np data = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]]) Wd = [len(doc) for doc in data] # length

Simple Bayesian Network via Monte Carlo Markov Chain ported to PyMC3

試著忘記壹切 提交于 2019-12-10 20:26:59
问题 I was porting the example of a Simple Bayesian Network via Monte Carlo Markov Chain from PyMC2 to PyMC3 and it works. The result can be found in the following gist on GitHub in the file pymc3_rain_sprinkler_grass_simple_bayesian_network.py. I wanted to extend the original example by providing evidence, e.g. that the grass is wet and then let PyMC3 give me the answer for questions like "given grass is wet, what is the probability that it has rained?". It seems that the resulting trace is

How to use pymc to parameterize a probabilistic graphical model?

元气小坏坏 提交于 2019-12-10 14:43:26
问题 How can one use pymc to parameterize a probabilistic graphical model? Suppose I have a PGM with two nodes X and Y . Lets say X->Y is the graph. And X takes two values {0,1} , and Y also takes two values {0,1} . I want to use pymc to learn the parameters of the distribution and populate the graphical model with it for running inferences. The way I could think of is as follows: X_p = pm.Uniform("X_p", 0, 1) X = pm.Bernoulli("X", X_p, values=X_Vals, observed=True) Y0_p = pm.Uniform("Y0_p", 0, 1)

Convergence issues on hierarchical probit model with NUTS and Metropolis

痞子三分冷 提交于 2019-12-10 11:38:22
问题 I am trying to extend the hierarchical model from Gelman and Hill reproduced in PyMC3 here to binary outcome data by adding a probit transformation and making the outcome follow a Bernoulli distribution. Right now I'm using toy data, so I know the true values. Alpha should be .1, beta should be .5. The model runs fine with a NUTS sampler before the extension. Once I add it, the estimates slowly increase and keep increasing until the model stalls anywhere between 10 and 200 iterations. Here's

How to Simulate a Biased 6-sided Dice using Pymc3?

99封情书 提交于 2019-12-10 10:55:51
问题 How do I simulate a 6-side Dice roll using Pymc3? Also, what is I know that different sides of the dice have different distributions? 回答1: The easiest way to simulate 1000 rolls of a fair 6-sided die in PyMC3 is import pymc3 as pm with pm.Model(): rolls = pm.DiscreteUniform('rolls', lower=1, upper=6) trace = pm.sample(1000) trace['rolls'] # shows you the result of 1000 rolls Note that this is slower, but equivalent, to just calling np.random.randint(1, 7, size=1000) . For 1000 rolls of an