Skip to content

Instantly share code, notes, and snippets.

@CamDavidsonPilon
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CamDavidsonPilon/9315065 to your computer and use it in GitHub Desktop.
Save CamDavidsonPilon/9315065 to your computer and use it in GitHub Desktop.
Generate exponential survival data with probability alpha of censorship
def exponential_survival_data(n, cr=0.05, scale=1.):
t = stats.expon.rvs(scale=scale, size=n)
if cr == 0.0:
return t, np.ones(n, dtype=bool)
def pF(h):
v = 1.0*h/scale
return v / (np.exp(v)-1) - cr
#find the threshold:
h = newton(pF, 1., maxiter=500)
#generate truncated data
R = (1-exp(-h/scale))*stats.uniform.rvs(size=n)
entrance = -log(1-R)*scale
C = (t+entrance) < h #should occur 1-cr of the time.
T = np.minimum(h-entrance,t)
return T,C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment