Skip to content

Start value doesn't work for Uniform distribution #2042

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

Closed
davidbrochart opened this issue Apr 16, 2017 · 13 comments
Closed

Start value doesn't work for Uniform distribution #2042

davidbrochart opened this issue Apr 16, 2017 · 13 comments

Comments

@davidbrochart
Copy link
Contributor

It looks like when a variable has a Uniform distribution, its start value is ignored. This example shows that alpha never gets initialized to 5 when it has a Uniform distribution, but it does when it has a Normal distribution.

import numpy as np
import pymc3 as pm
import theano

# Initialize random number generator
np.random.seed(123)

# True parameter values
alpha, sigma = 1, 1
beta = [1, 2.5]

# Size of dataset
size = 100

# Predictor variable
X1 = np.random.randn(size)
X2 = np.random.randn(size) * 0.2

# Simulate outcome variable
Y = alpha + beta[0]*X1 + beta[1]*X2 + np.random.randn(size)*sigma

# Specify model
@theano.as_op([theano.tensor.dscalar, theano.tensor.dscalar, theano.tensor.dscalar], [theano.tensor.dvector])
def _mu(alpha, beta_0, beta_1):
    print(alpha, beta_0, beta_1)
    return alpha + beta_0*X1 + beta_1*X2

basic_model = pm.Model()

with basic_model:
    alpha = pm.Uniform('alpha', -10, 10)
    #alpha = pm.Normal('alpha', mu=0, sd=10)
    beta_0 = pm.Normal('beta_0', mu=0, sd=10)
    beta_1 = pm.Normal('beta_1', mu=0, sd=10)
    sigma = pm.HalfNormal('sigma', sd=1)
    mu = pm.Deterministic('mu', _mu(alpha, beta_0, beta_1))
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=Y)

with basic_model:
    step = pm.Metropolis([alpha, beta_0, beta_1, sigma])
    start = {'alpha': 5., 'beta_0': 4., 'beta_1': 3.}
    trace = pm.sample(10, step=step, start=start, init=None)
@fonnesbeck
Copy link
Member

I will have a peek at it.

BTW, is there a reason you are creating an op function for mu? You should just be able to specify:

mu = Deterministic('mu', alpha + beta_0*X1 + beta_1*X2)

directly in the model.

@fonnesbeck
Copy link
Member

Yes, it appears to happen for any variables that are auto-transformed prior to sampling (i.e. those not supported over the real line).

@davidbrochart
Copy link
Contributor Author

I created an op function in order to insert a print statement.

@fonnesbeck
Copy link
Member

Just pushed a fix; please test.

@davidbrochart
Copy link
Contributor Author

With your fix, I get this error in the previous example:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-da07ba7e1eef> in <module>()
      2     step = pm.Metropolis([alpha, beta_0, beta_1, sigma])
      3     start = {'alpha': 5., 'beta_0': 4., 'beta_1': 3.}
----> 4     trace = pm.sample(10, step=step, start=start, init=None)

/home/david/soft/anaconda3/lib/python3.5/site-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain, njobs, tune, nuts_kwargs, step_kwargs, progressbar, model, random_seed, live_plot, **kwargs)
    257         sample_func = _sample
    258 
--> 259     return sample_func(**sample_args)
    260 
    261 

/home/david/soft/anaconda3/lib/python3.5/site-packages/pymc3/sampling.py in _sample(draws, step, start, trace, chain, tune, progressbar, model, random_seed, live_plot, **kwargs)
    272     try:
    273         strace = None
--> 274         for it, strace in enumerate(sampling):
    275             if live_plot:
    276                 if it >= skip_first:

/home/david/soft/anaconda3/lib/python3.5/site-packages/tqdm/_tqdm.py in __iter__(self)
    828 """, fp_write=getattr(self.fp, 'write', sys.stderr.write))
    829 
--> 830             for obj in iterable:
    831                 yield obj
    832                 # Update and print the progressbar.

/home/david/soft/anaconda3/lib/python3.5/site-packages/pymc3/sampling.py in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
    352         _soft_update(start, strace.point(-1))
    353     else:
--> 354         _soft_update(start, model.test_point)
    355 
    356     try:

/home/david/soft/anaconda3/lib/python3.5/site-packages/pymc3/sampling.py in _soft_update(a, b)
    485     """As opposed to dict.update, don't overwrite keys if present.
    486     """
--> 487     b = _transformed_init(a, b)
    488     a.update({k: v for k, v in b.items() if k not in a})
    489 

/home/david/soft/anaconda3/lib/python3.5/site-packages/pymc3/sampling.py in _transformed_init(a, b)
    477             if tname.startswith(name) and tname!=name:
    478                 transform = tname.split(name)[-1][1:-1]
--> 479                 transform_func = distributions.__dict__[transform]
    480                 b[tname] = transform_func.forward(a[name]).eval()
    481 

KeyError: 'interval'

@davidbrochart
Copy link
Contributor Author

Sorry, closed the issue by mistake.

@davidbrochart davidbrochart reopened this Apr 16, 2017
@fonnesbeck
Copy link
Member

Looks like that transform was missing from __all__. Fixed now.

@fonnesbeck
Copy link
Member

Actually, there is a problem with the interval transform in that the bounds of the transformation is not included in the name. Need to address this.

@fonnesbeck
Copy link
Member

OK, please test the updated PR.

@davidbrochart
Copy link
Contributor Author

It works fine, thanks a lot!

@fonnesbeck
Copy link
Member

Ok, good. I want the PR to be reviewed before it is merged, as my solution is a bit hacky.

@denadai2
Copy link
Contributor

same error here

@fonnesbeck
Copy link
Member

Fixed in master now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants