mcmcpyguy
mcmcpyguy

Reputation: 21

custom Distribution in PymC3 specific example

I am confused about custom distributions, basically because I am not able to wrap my head around how it works. Perhaps a post on it would be super useful.

I am trying to create a distribution that is a combination of

-15% to -5% with 25% probability

0 to 5% with 75 % probability

Essentially trying to solve the problem given in Crystal Ball Tutorial.pdf page 3-11.

Can you please help me how to go about doing it.

Upvotes: 1

Views: 995

Answers (2)

Austin Rochford
Austin Rochford

Reputation: 136

You can do this with pymc3's Mixture distribution as follows:

import numpy as np
import pymc3 as pm

with pm.Model() as model:
    dist = pm.Mixture('dist', np.array([0.25, 0.75]),
                      [pm.Uniform.dist(-0.15, -0.05), pm.Uniform.dist(0., 0.05)])

N = 10000
samples = dist.random(size=10000)

Which produces the following distribution, which I think is what you are looking for

distribution histogram

Upvotes: 1

aloctavodia
aloctavodia

Reputation: 2070

What about doing something like this:

with pm.Model() as model:
    idx = pm.Uniform('idx', 0, 1)
    a = pm.Uniform('a', np.array([-15, 0]), np.array([-5, 5]), shape=2)
    b = pm.Deterministic('b', pm.math.switch(idx < 0.25, a[0], a[1]))
    step = pm.Metropolis()
    trace = pm.sample(1000, step)

Upvotes: 1

Related Questions