Probabilistic-Programming-and-Bayesian-Methods-for-Hackers icon indicating copy to clipboard operation
Probabilistic-Programming-and-Bayesian-Methods-for-Hackers copied to clipboard

3 Switchpoints for Text Example in Ch1

Open ericxtang opened this issue 9 years ago • 5 comments

Hi,

I see there is a PyMC example using the .deterministic annotation. Is there an easy way to do this for PyMC3?

I tried this but it's incorrect.

lambda1 = pm.Exponential("lambda_1", alpha)
lambda2 = pm.Exponential("lambda_2", alpha)
lambda3 = pm.Exponential("lambda_3", alpha)
tau1 = pm.DiscreteUniform("tau1", lower=0, upper=69)
tau2 = pm.DiscreteUniform("tau2", lower=tau1, upper=70)

idx1 = np.arange(n_count_data)
lambda_s1 = pm.math.switch(tau1 >= idx1, lambda1, lambda2)
idx2 = np.arange(n_count_data)
lambda_s2 = pm.math.switch(tau2 >= idx2, lambda2, lambda3)

obs1 = pm.Poisson("obs1", lambda_s1, observed=count_data)
obs2 = pm.Poisson("obs2", lambda_s2, observed=count_data)

ericxtang avatar Dec 11 '16 00:12 ericxtang

Not sure if this is what you are looking for. I did it this way:

with pm.Model() as model:
    alpha = 1.0/count_data.mean()  # Recall count_data is the
                                   # variable that holds our txt counts
    lambda_1 = pm.Exponential("lambda_1", alpha)
    lambda_2 = pm.Exponential("lambda_2", alpha)
    lambda_3 = pm.Exponential("lambda_3", alpha)
    
    tau_1 = pm.DiscreteUniform("tau_1", lower=0, upper=n_count_data)
    tau_2 = pm.DiscreteUniform("tau_2", lower=tau_1, upper=n_count_data)

and

with model:
    idx = np.arange(n_count_data) # Index
    lambda_ = pm.math.switch(tau_2 >= idx, pm.math.switch(tau_1 >= idx, lambda_1, lambda_2), lambda_3)

Which i don't really find very neat. But it seems to work. index

friedhelmvictor avatar Feb 06 '17 14:02 friedhelmvictor

Hi. I'm study bayseian theory... Can i see the plot code?

DeepHaeJoong avatar Feb 20 '18 09:02 DeepHaeJoong

Hi @friedhelmvictor can you please share the code you used to plot the text message graph above? It looks pretty OK to me and it is similar to a problem I am trying to solve. Thanks, in advance. Woodquest

woodquest avatar Sep 28 '20 22:09 woodquest

How to plot the result???

ZhiqiangZhangCUGB avatar Apr 26 '21 12:04 ZhiqiangZhangCUGB

Yes please. How to plot the result, have you any idea? Thanks Victor

woodquest avatar Apr 26 '21 12:04 woodquest