def main(): tau = pm.rdiscrete_uniform(0, 80) print tau alpha = 1. / 20. lambda_1, lambda_2 = pm.rexponential(alpha, 2) print lambda_1, lambda_2 data = np.r_[pm.rpoisson(lambda_1, tau), pm.rpoisson(lambda_2, 80 - tau)] def plot_artificial_sms_dataset(): tau = pm.rdiscrete_uniform(0, 80) alpha = 1. / 20. lambda_1, lambda_2 = pm.rexponential(alpha, 2) data = np.r_[pm.rpoisson(lambda_1, tau), pm.rpoisson(lambda_2, 80 - tau)] plt.bar(np.arange(80), data, color="#348ABD") plt.bar(tau - 1, data[tau - 1], color="r", label="user behaviour changed") plt.xlim(0, 80) plt.title("More example of artificial datasets") for i in range(1, 5): plt.subplot(4, 1, i) plot_artificial_sms_dataset() plt.show()
def step(self): """ Slice step method """ y = self.loglike - rexponential(1) # Stepping out procedure L = self.stochastic.value - self.w*runiform(0,1) R = L + self.w J = floor(self.m*runiform(0,1)) K = (self.m-1)-J while(J>0 and y<self.fll(L)): L = L - self.w J = J - 1 while(K>0 and y<self.fll(R)): R = R + self.w K = K - 1 #self.stochastic.last_value = self.stochastic.value self.stochastic.value = runiform(L,R) try: y_new = self.loglike except ZeroProbability: y_new = -infty while(y_new<y): if (self.stochastic.value < self.stochastic.last_value): L = float(self.stochastic.value) else: R = float(self.stochastic.value) self.stochastic.revert() self.stochastic.value = runiform(L,R) try: y_new = self.loglike except ZeroProbability: y_new = -infty
def plot_artificial_sms_dataset(): tau = pm.rdiscrete_uniform(0, 80) alpha = 1. / 20. lambda_1, lambda_2 = pm.rexponential(alpha, 2) data = np.r_[pm.rpoisson(lambda_1, tau), pm.rpoisson(lambda_2, 80 - tau)] plt.bar(np.arange(80), data, color="#348ABD") plt.bar(tau - 1, data[tau - 1], color="r", label="user behaviour changed") plt.xlim(0, 80)
def plot_artificial_sms_dataset(): maxdays = 80 tau = pm.rdiscrete_uniform( 0, maxdays ) alpha = 1 / 20. lambda_1, lambda_2 = pm.rexponential( alpha, 2 ) data = np.r_[ pm.rpoisson( lambda_1, tau ), pm.rpoisson( lambda_2, maxdays-tau )] plt.bar( np.arange(maxdays), data ) plt.bar( tau - 1, data[tau-1], color = 'r', label='change point' ) plt.xlim( 0, 80 )
def test_selection(): N = 40 r = pymc.rexponential(2.0, size=N) def prior_x(x): """Triangle distribution. A = h*L/2 = 1 """ L=5 h = 2./L if np.all(0.< x) and np.all(x < L): return np.sum(log(h - h/L *x)) else: return -np.inf W = select_distribution(r, [pymc.Exponweib, pymc.Exponential, pymc.Weibull, pymc.Chi2], prior_x) return W
def plot_artificail_sms_dataset(): #---------------------------------- # initialize both deterministic and stochastic variables tau = pm.rdiscrete_uniform(0, 80) print("tau = {0}".format(tau)) alpha = 1. / 20. lambda_1, lambda_2 = pm.rexponential(alpha, 2) print("lambda_1 = {0}\nlambda_2 = {1}".format(lambda_1, lambda_2)) lambda_ = np.r_[lambda_1 * np.ones(tau), lambda_2 * np.ones(80 - tau)] print("lambda = \n{0}".format(lambda_)) data = pm.rpoisson(lambda_) print("data = \n{0}".format(data)) #----------------------------------- # plot the artificial plt.bar(np.arange(80), data, color="#348ABD") plt.bar(tau - 1, data[tau - 1], color="r", label="user behavior changed") plt.xlabel("Time(days)") plt.ylabel("Text messages received") plt.xlim(0, 80)
def test_selection(): N = 40 r = pymc.rexponential(2.0, size=N) def prior_x(x): """Triangle distribution. A = h*L/2 = 1 """ L = 5 h = 2. / L if np.all(0. < x) and np.all(x < L): return np.sum(log(h - h / L * x)) else: return -np.inf W = select_distribution( r, [pymc.Exponweib, pymc.Exponential, pymc.Weibull, pymc.Chi2], prior_x) return W
def plot_artifical_sms_dataset(): # specify when the user's behaviour (amount of sms received) switches by sampling from DiscreteUniform tau = rdiscrete_uniform(0, 80) print('τ = {}'.format(tau,)) alpha = 1. / 20. lambda_1, lambda_2 = rexponential(alpha, 2) print(lambda_1, lambda_2) # for days before tau, repr. the user's received sms count by sampling from a # Poisson(lambda_1), and for days after tau by sampling from Poisson(lambda_2) data = np.r_[rpoisson(lambda_1, tau), rpoisson(lambda_2, 80 - tau)] print(data) # plot artificial data set pyplot.bar(np.arange(80), data, color="#348ABD") pyplot.bar(tau - 1, data[tau - 1], color="r", label="user behaviour changed") pyplot.xlabel("time (days)") pyplot.ylabel("count of sms received") pyplot.xlim(0, 80) pyplot.legend()
import numpy as np import pymc as pm from matplotlib import pyplot as plt tau = pm.rdiscrete_uniform(0, 80) print(tau) alpha = 1. / 20. lambda_1, lambda_2 = pm.rexponential(alpha, 2) print(lambda_1, lambda_2) data = np.r_[pm.rpoisson(lambda_1, tau), pm.rpoisson(lambda_2, 80 - tau)] plt.bar(np.arange(80), data, color="#348ABD") plt.bar(tau - 1, data[tau - 1], color="r", label="user behaviour changed") plt.xlabel("Time (days)") plt.ylabel("count of text-msgs received") plt.title("Artificial dataset") plt.xlim(0, 80) plt.legend(); plt.show()
data = np.array( [10, 20, 15, 20, 49] ) obs = pm.Poisson( "obs", lambda_, value=data, observed=True ) obs.value # # Model class - analyze variables as a single unit model = pm.Model( [obs, lambda_, lambda_1, lambda_2, taus] ) # # Creating new datasets # maxdays = 80 tau = pm.rdiscrete_uniform( 0, maxdays ) alpha = 1 / 20. lambda_1, lambda_2 = pm.rexponential( alpha, 2 ) data = np.r_[ pm.rpoisson( lambda_1, tau ), pm.rpoisson( lambda_2, maxdays-tau )] plt.bar( np.arange(maxdays), data ) plt.bar( tau - 1, data[tau-1], color = 'r', label='change point' ) plt.xlabel( "Time (days)" ) plt.ylabel( "count" ) plt.title( "Artificial Data" ) plt.xlim( 0, 80 ) plt.legend() plt.show() def plot_artificial_sms_dataset():
import numpy as np import pymc as pm from matplotlib import pyplot as plt alpha = 1. / 20. lambda_ = pm.rexponential(alpha) print(lambda_) data = np.r_[pm.rpoisson(lambda_, 80)] np.savetxt("txtdata_sim.csv", data) plt.bar(np.arange(80), data, color="#348ABD") plt.xlabel("Time (days)") plt.ylabel("count of text-msgs received") plt.title("Artificial dataset") plt.xlim(0, 80) plt.show()