4

I'm working with PyMC3 and unsure about how to mark certain variables as "observed". In a simple example, I could have two input variables modeled as a uniform distribution on [0, 1]. I know a third "output" random variable is equal to the product of the two inputs, and let's say that I observed that the first input is 1 and the output is 0. Then I want to use PyMC3 to predict the second input, which in this case must be 0.

It is unclear for me how to tell PyMC3 that the output is observed since it is the result of a mathematical expression and not created explicitly from a constructor.

import pymc3 as pm

with pm.Model() as model:
  input1 = pm.Uniform('RV1', lower=0, upper=1, observed=1)  # API is clear how to mark it observed
  input2 = pm.Uniform('RV2', lower=0, upper=1)  # This one is not observed
  output = input1 * input2  # How to tell PyMC3 the observed value of "output"?
  # Now I will do variational inference, sampling, etc... on the model

The random variables are technically boolean random variables but I need to model them as continuous in order to do variational inference. And I have a lot of them, this is a minimum example. Setting output.observed = 0 doesn't seem to work, although it doesn't crash.

  • 1
    Did you ever find the solution to this problem? – Jsevillamol Aug 01 '21 at 12:15
  • 1
    Doesn't solve all problems, but one limited way I got through this was to calculate the convolution of the two distributions analytically. This works when I was adding two random variables, but doing anything complicated could get tricky fast. I'm looking for an answer too! – user3148185 Nov 04 '21 at 05:10

1 Answers1

0

When doing a deterministic (i.e. closed form) mathematical operation with the variables, you can use pymc3.Deterministic('your_deterministic_var', input1 * input2) probability distribution. As far as I know, this does not allow the observed keyword as you requested, although after the sampling is done, (pm.sample()) you can run pm.sample_posterior_predictive(chain, var_names=['your_deterministic_var']) and take means of the variable entries, or whatever you need.

For more information on the Deterministic check out the documentation and also the PyMC3 Discourse forum which is a better place for PyMC3 questions than stackoverflow anyway.