CamDavidsonPilon / Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
MIT License
26.67k stars 7.86k forks source link

3 Switchpoints for Text Example in Ch1 #331

Open ericxtang opened 7 years ago

ericxtang commented 7 years ago

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)
friedhelmvictor commented 7 years ago

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

DeepHaeJoong commented 6 years ago

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

woodquest commented 3 years ago

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

ZhiqiangZhangCUGB commented 3 years ago

How to plot the result???

woodquest commented 3 years ago

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