import numpy as np import pymc as pm from matplotlib import pyplot as plt N = 20 #create some artificial data. lifetime = pm.rweibull(2, 5, size=N) birth = pm.runiform(0, 10, N) censor = (birth + lifetime) > 10 #an individual is right-censored if this is True lifetime_ = np.ma.masked_array(lifetime, censor) #create the censorship event. lifetime_.set_fill_value(10) #good for computations later. #this begins the model alpha = pm.Uniform("alpha", 0, 20) #lets just use uninformative priors beta = pm.Uniform("beta", 0, 20) obs = pm.Weibull('obs', alpha, beta, value=lifetime_, observed=True) @pm.potential def censor_factor(obs=obs): if np.any((obs + birth < 10)[lifetime_.mask]): return -np.inf else: return 0 #perform Markov Chain Monte Carlo - see chapter 3 of BMH
The statistical distribution chosen here is the Weibull distribution, but the same can be done for any other distribution. """ from pymc import rweibull, Uniform, Weibull """ First, we will create a fake data set using some fixed parameters. In real life, of course, you already have the data ! """ alpha = 3 beta = 5 N = 100 dataset = rweibull(alpha, beta, N) """ Now we create a pymc model that defines the likelihood of the data set and prior assumptions about the value of the parameters. """ a = Uniform('a', lower=0, upper=10, value=5, doc='Weibull alpha parameter') b = Uniform('b', lower=0, upper=10, value=5, doc='Weibull beta parameter') like = Weibull('like', alpha=a, beta=b, value=dataset, observed=True) pred = Weibull('like', alpha=a, beta=b, value=dataset) if __name__ == '__main__': from pymc import MCMC, Matplot # Sample the parameters a and b and analyze the results
# from pymc.examples import disaster_model # M = MCMC(disaster_model) # M.sample(iter=10000, burn=1000, thin=10) # M.trace('switchpoint')[:] from pymc import rweibull, Uniform, Weibull alpha = 3 beta = 5 N = 100 dataset = rweibull(alpha, beta, N) print dataset """ Now we create a pymc model that defines the likelihood of the data set and prior assumptions about the value of the parameters. """ a = Uniform('a', lower=0, upper=10, value=5, doc='Weibull alpha parameter') b = Uniform('b', lower=0, upper=10, value=5, doc='Weibull beta parameter') like = Weibull('like', alpha=a, beta=b, value=dataset, observed=True) if __name__ == '__main__': from pymc import MCMC, Matplot
import numpy as np import matplotlib.pyplot as plt import pymc as mc import scipy.stats as stats import math # http://blog.yhathq.com/posts/estimating-user-lifetimes-with-pymc.html # artificial data N = 20 true_alpha = 2 true_beta = 5 lifetime = mc.rweibull(true_alpha, true_beta, size=N) birth = mc.runiform(0, 10, N) # an individual is right censored if this is true censor = (birth + lifetime) > 10 lifetime_ = np.ma.masked_array(lifetime, censor) lifetime_.set_fill_value(10) plt.clf() y = np.arange(0, N) for b, l, yy in zip(birth, lifetime, y): plt.plot([b, b + l], [yy, yy]) plt.plot(birth + lifetime, y, linestyle="", marker="o") plt.draw() plt.show(block=False) # begin the model # just use uniform priors alpha = mc.Uniform("alpha", 0, 20)
import pymc # Some fake data alpha = 3 beta = 5 N = 100 dataset = pymc.rweibull(alpha, beta, N) # Model a = pymc.Uniform('a', lower=0, upper=10, value=5, doc='Weibull alpha parameter') b = pymc.Uniform('b', lower=0, upper=10, value=5, doc='Weibull beta parameter') like = pymc.Weibull('like', alpha=a, beta=b, value=dataset, observed=True)
The statistical distribution chosen here is the Weibull distribution, but the same can be done for any other distribution. """ import pymc """ First, we will create a fake data set using some fixed parameters. In real life, of course, you already have the data ! """ alpha = 3 beta = 5 N = 100 dataset = pymc.rweibull(alpha,beta, N) """ Now we create a pymc model that defines the likelihood of the data set and prior assumptions about the value of the parameters. """ a = pymc.Uniform('a', lower=0, upper=10, value=5, doc='Weibull alpha parameter') b = pymc.Uniform('b', lower=0, upper=10, value=5, doc='Weibull beta parameter') like = pymc.Weibull('like', alpha=a, beta=b, value=dataset, observed=True) """ The last step is simply to sample the parameters a and b and analyze the results. """ if __name__=='__main__':
import numpy as np import matplotlib.pyplot as plt import pymc as mc import scipy.stats as stats import math # http://blog.yhathq.com/posts/estimating-user-lifetimes-with-pymc.html # artificial data N = 20 true_alpha = 2 true_beta = 5 lifetime = mc.rweibull( true_alpha, true_beta, size=N ) birth = mc.runiform( 0, 10, N ) # an individual is right censored if this is true censor = (birth + lifetime) > 10 lifetime_ = np.ma.masked_array( lifetime, censor ) lifetime_.set_fill_value( 10 ) plt.clf() y = np.arange( 0, N ) for b,l,yy in zip( birth, lifetime, y ): plt.plot( [b,b+l], [yy,yy] ) plt.plot( birth+lifetime, y, linestyle="", marker="o" ) plt.draw() plt.show( block=False ) # begin the model # just use uniform priors alpha = mc.Uniform( "alpha", 0, 20 )