You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/survival_analysis/weibull_aft.myst.md
+49-26Lines changed: 49 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,9 @@ kernelspec:
8
8
display_name: pymc
9
9
language: python
10
10
name: python3
11
+
myst:
12
+
substitutions:
13
+
extra_dependencies: statsmodels
11
14
---
12
15
13
16
(weibull_aft)=
@@ -25,11 +28,18 @@ import arviz as az
25
28
import numpy as np
26
29
import pymc as pm
27
30
import pytensor.tensor as pt
28
-
import statsmodels.api as sm
29
31
30
32
print(f"Running on PyMC v{pm.__version__}")
31
33
```
32
34
35
+
:::{include} ../extra_installs.md
36
+
:::
37
+
38
+
```{code-cell} ipython3
39
+
# These dependencies need to be installed separately from PyMC
40
+
import statsmodels.api as sm
41
+
```
42
+
33
43
```{code-cell} ipython3
34
44
%config InlineBackend.figure_format = 'retina'
35
45
RANDOM_SEED = 8927
@@ -71,7 +81,9 @@ censored[:5]
71
81
72
82
We have an unique problem when modelling censored data. Strictly speaking, we don't have any _data_ for censored values: we only know the _number_ of values that were censored. How can we include this information in our model?
73
83
74
-
One way do this is by making use of `pm.Potential`. The [PyMC2 docs](https://pymc-devs.github.io/pymc/modelbuilding.html#the-potential-class) explain its usage very well. Essentially, declaring `pm.Potential('x', logp)` will add `logp` to the log-likelihood of the model.
84
+
One way do this is by making use of `pm.Potential`. The [PyMC2 docs](https://pymc-devs.github.io/pymc/modelbuilding.html#the-potential-class) explain its usage very well. Essentially, declaring `pm.Potential('x', logp)` will add `logp` to the log-likelihood of the model.
85
+
86
+
However, `pm.Potential` only effect probability based sampling this excludes using `pm.sample_prior_predictice` and `pm.sample_posterior_predictive`. We can overcome these limitations by using `pm.Censored` instead. We can model our right-censored data by defining the `upper` argument of `pm.Censored`.
75
87
76
88
+++
77
89
@@ -80,36 +92,40 @@ One way do this is by making use of `pm.Potential`. The [PyMC2 docs](https://pym
80
92
This parameterization is an intuitive, straightforward parameterization of the Weibull survival function. This is probably the first parameterization to come to one's mind.
81
93
82
94
```{code-cell} ipython3
83
-
def weibull_lccdf(x, alpha, beta):
84
-
"""Log complementary cdf of Weibull distribution."""
85
-
return -((x / beta) ** alpha)
95
+
# normalize the event time between 0 and 1
96
+
y_norm = y / np.max(y)
97
+
```
98
+
99
+
```{code-cell} ipython3
100
+
# If censored then observed event time else maximum time
101
+
right_censored = [x if x > 0 else np.max(y_norm) for x in y_norm * censored]
- Originally collated by [Junpeng Lao](https://junpenglao.xyz/) on Apr 21, 2018. See original code [here](https://github.com/junpenglao/Planet_Sakaar_Data_Science/blob/65447fdb431c78b15fbeaef51b8c059f46c9e8d6/PyMC3QnA/discourse_1107.ipynb).
184
206
- Authored and ported to Jupyter notebook by [George Ho](https://eigenfoo.xyz/) on Jul 15, 2018.
185
207
- Updated for compatibility with PyMC v5 by Chris Fonnesbeck on Jan 16, 2023.
208
+
- Updated to replace `pm.Potential` with `pm.Censored` by Jonathan Dekermanjian on Nov 25, 2024.
0 commit comments