pymc-devs / pymc

Bayesian Modeling and Probabilistic Programming in Python
https://docs.pymc.io/
Other
8.63k stars 1.99k forks source link

Deterministic transform of multi-dimensional variable #1532

Closed rsmulktis closed 7 years ago

rsmulktis commented 7 years ago

Hey guys, i'm new to pymc3 (and probabilistic programming in general) and i'm trying to understand how something works. I'm setting up a simple model with a few stochastic variables that have shape and then creating a deterministic variable that is a matrix transform of a constant vector and the stochastic variable(s). But, i think i'm clearly doing something wrong. I'm hoping it's simple!

The model seems to fit ok and the traceplot of the stochastic variables that were simulated looks reasonable, but the deterministic variable isn't calculating correctly. What I want is to take my 5 stochastic values held in "var1" and inner product each sample with a constant vector to get a single value for the sample.

Here's the code which hopefully helps more than my explanation.

Thanks for any tips.

import numpy
import random
import pymc3
import theano
%matplotlib inline

observations = numpy.array([[random.normalvariate(0., 0.5) for i in range(5)] for i in range(1000)])
with pymc3.Model() as model:
    var_const = theano.shared(numpy.array([2.,2.,2.,2.,2.]))
    mu = pymc3.Uniform('mu', -1., 1., shape=5)
    sd = pymc3.Uniform('sd', 0., 1., shape=5)
    var1 = pymc3.Normal('var1', mu=mu, sd=sd, shape=5, observed=observations)
    var2 = pymc3.Deterministic('var2', (var1*var_const))
    start = pymc3.find_MAP()
    step = pymc3.NUTS(scaling=start)
    trace = pymc3.sample(2000, step, start=start, progressbar=True)
twiecki commented 7 years ago

pymc3.math.dot(var1, var_const)?

fonnesbeck commented 7 years ago

@rsmulktis I'm going to close this because it is not a bug report. Please ask questions either on Gitter or StackOverflow.

rsmulktis commented 7 years ago

That's fine, I wasn't sure what the best way was; I'll use SO next time. Thanks for pointing out the answer, though!

One comment (as a newbie) having full api docs exposed, even with minimal descriptions, would be useful for browsing. The docs you guys have are exceptional, but either I'm looking in the wrong spot or the full apis aren't part of the generated set. For instance, i'm having a hard time finding pymc3.math.dot listed anywhere...

twiecki commented 7 years ago

@rsmulktis Good point, I added that here http://pymc-devs.github.io/pymc3/api.html#math