-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
BUG: Input dimension mismatch due to sampler proposing invalid values for discrete variable #7066
Comments
The problem is that you're sampling a discrete variable with a generic mcmc sampler, and the sampler will sometimes propose values for One proposed solution was to have discrete transforms: #6102 (comment) For your case you can post-process import arviz, pymc, numpy, pytensor
data=numpy.concatenate([numpy.random.randn(300)/10-0.1,numpy.random.randn(450)/10+0.2,numpy.random.randn(250)/10+0.6])
with pymc.Model() as model:
K, N = 3, 1000
r = pymc.Normal("r", mu=0, sigma=1, shape=K)
w = pymc.Dirichlet("w", numpy.ones(K))
p = pymc.DirichletMultinomial("p", N, w)
# Avoid index errors: if p does not sum to N, replace by [N, 0, 0, ...]
p = pymc.math.switch(pymc.math.eq(p.sum(), N), p, [N] + [0]*(K-1))
idx = pymc.Deterministic("idx", pymc.math.concatenate(
[pytensor.tensor.tile(i,p[i]) for i in range(K)]))
obs = pymc.Normal("obs", mu=r[idx], sigma=1e-3, observed=data)
trace = pymc.sample(1000) The model will probably sample horribly, because the DirichletMultinomial is very constrained and the Metropolis sampler doesn't know about these contraints. It may be better to transform the Dirichlet deterministically into a valid |
Can't your model be recast as a Normal-Mixture model? That would avoid the discrete variable all-together |
Describe the issue:
I wanted to test the detection for several breaking points in a time series, so I came up with the following model (cf. code example) to detect 3 switching points.
As long as I don't add any observation data, the model runs fine and generates all the variables at sampling time. The idx tensor variable seems to be of the proper shape (1 dimension and size of 1000).
However, adding them generates an error (cf. message). The dimension mismatch is also variable, depending on the execution, it can say input[1].shape[0] = 998, or input[1].shape[0] = 1004, it seems pretty random.
The problem seems to be with the idx index, but generating it in another way works. If I do for instance
everything ends up being fine.
This index is basically used to map the proper value of the mean on a given interval.$a_0$ , $a_1$ ,..., $a_{K-1}$ ) such as $$\sum_{i=0}^{K-1}a_i=N$$ $a_0$ times, 1 is repeated $a_1$ times, etc...
Based on the outputs from the DirichletMultinomial, which is a tuple of K numbers (
I create an array that would look like [0,0,0,1,1,1,1,1,........,K-1,K-1,K-1]. 0 is repeated
Reproduceable code example:
Error message:
PyMC version information:
I am running a conda environment, set up from miniforge.
Context for the issue:
No response
The text was updated successfully, but these errors were encountered: