# Translating Censored Data example from PyMC2 to PyMC3

**URL:** <https://discourse.pymc.io/t/translating-censored-data-example-from-pymc2-to-pymc3/196>\
**Category:** Questions\
**Created:** [July 27, 2017, 3:09am UTC](https://discourse.pymc.io/t/translating-censored-data-example-from-pymc2-to-pymc3/196 "2017-07-27T03:09:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tokoromouri](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tokoromouri](https://discourse.pymc.io/u/tokoromouri)\
**Post date:** [July 27, 2017, 3:09am UTC](https://discourse.pymc.io/t/translating-censored-data-example-from-pymc2-to-pymc3/196/1 "2017-07-27T03:09:03Z")

</div>

Hi there,

I have found a great example of Bayesian parametric survival analysis in pymc2 here:  
[http://blog.yhat.com/posts/estimating-user-lifetimes-with-pymc.html](http://blog.yhat.com/posts/estimating-user-lifetimes-with-pymc.html)

I want to reproduce it in pymc3. I am struggling with encoding the censored events. How would you translate this code to PyMC3?

```
N = 2500 #125 times more data than before
lifetime = mc.rweibull( 2, 5, size = N )
birth = mc.runiform(0, 10, N)
censor = ((birth + lifetime) >= 10) #an individual is right-censored if this is True 
lifetime_ = lifetime.copy()
lifetime_[censor] = 10 - birth[censor] #we only see this part of their lives.

alpha = mc.Uniform('alpha', 0, 20) #lets just use uninformative priors again
beta = mc.Uniform('beta', 0, 20)

@mc.observed
def survival(value=lifetime_, alpha = alpha, beta = beta ):
    return sum( (1-censor)(log( alpha/beta) + (alpha-1)*log(value/beta)) - (value/beta)**(alpha)

mcmc = mc.MCMC([alpha, beta, survival] )
mcmc.sample(50000, 30000)

```

My understanding is that the @mc.observed decorator is not supported in PyMC3.

How would you translate this code?  
What is the most elegant and scalable way to handle censored data in PyMC3?

Thanks,  
Tokoro

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 27, 2017, 8:11am UTC](https://discourse.pymc.io/t/translating-censored-data-example-from-pymc2-to-pymc3/196/2 "2017-07-27T08:11:21Z")

</div>

Hi Tokoro,  
There is a quite detail example of modelling censored data in the [PyMC3/examples](https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/censored_data.py), it should answer most of your question.

---

<div class="post-metadata">

**Author:** ![tokoromouri](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tokoromouri](https://discourse.pymc.io/u/tokoromouri)\
**Post date:** [July 27, 2017, 11:00am UTC](https://discourse.pymc.io/t/translating-censored-data-example-from-pymc2-to-pymc3/196/3 "2017-07-27T11:00:57Z")

</div>

Junpenglao,

Thanks for the quick reply and for the pointer. I do think the answer is in that example. I will tinker a bit with it and come back here if I have any further questions.

Thank you!  
Tokoro
