Skip to content
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

Implement default moment function for CustomDist provided with a dist function #6804

Closed
ricardoV94 opened this issue Jun 29, 2023 · 2 comments · Fixed by #6873
Closed

Implement default moment function for CustomDist provided with a dist function #6804

ricardoV94 opened this issue Jun 29, 2023 · 2 comments · Fixed by #6873

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Jun 29, 2023

Description

import pymc as pm

def dist(mu, sigma, size):
  return pm.math.exp(pm.Normal.dist(mu, sigma size=size))`

with pm.Model() as m:
  ...
  x = pm.CustomDist("x", mu, sigma, dist=dist)

m.initial_point()

Since the dist is a symbolic forward graph, we could implement the moment automatically as

def moment_dist(mu, sigma, size):
  return pm.math.exp(moment(pm.Normal.dist(mu, sigma, size))

Would just need to replace the RVs in the dist graph, by their moments.

@lucianopaz
Copy link
Contributor

I'm kind of late to this issue but I need to comment that the expected value of the exponential of a Normally distributed random variable is not the exponential of the normal's location.

>>> moment(pm.math.exp(pm.Normal.dist(mu, sigma, size)))
pm.math.exp(mu + sigma **2 / 2)

Just like the Lognormal distribution's moment. I do like the idea of being able to deduce the moments from the data generation process, and I like the approach of using a rewrite to do so, as what we do with logp graphs. The problem is that moments are integrals on the altered measures of some basic random variables. Maybe @maresb has some input on this topic, but to me it sounds like a mathematically challenging endeavor.

@ricardoV94
Copy link
Member Author

With moment we're just looking for a nonzero density point to start sampling. We can call it something else, but definitely not confusing it with mean.

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

Successfully merging a pull request may close this issue.

2 participants