def linear_1_dim(): # uniform distribution of 1 dimension Linear = pytwalk.pytwalk(n=1, U=LinearU, Supp=LinearSupp) Linear.Run(T=1000, x0=0.5 * ones(1), xp0=1.5 * ones(1)) ### This will do a basic output analsis # Linear.Ana() Linear.Save("Lineartwalk-1-dim.dat")
def linear_1_dim(): # uniform distribution of 1 dimension Linear = pytwalk.pytwalk(n=1, U=LinearU, Supp=LinearSupp) Linear.Run( T=1000, x0=0.5*ones(1), xp0=1.5*ones(1)) ### This will do a basic output analsis # Linear.Ana() Linear.Save("Lineartwalk-1-dim.dat")
def normal_1_dim(): # normal distribution of 1 dimension Normal = pytwalk.pytwalk(n=1) Normal.Run( T=10000, x0=10*ones(1), xp0=15*ones(1)) ### This does a Random Walk Metriopolis Hastings with 10000 iterations ### initial point x0 and standar dev's for the normal jumps = sigma Normal.RunRWMH( T=10000, x0=10*ones(1), sigma=ones(1)) ### This will do a basic output analsis Normal.Ana() Normal.Save("Exptwalk-1-dim.dat")
def normal_1_dim(): # normal distribution of 1 dimension Normal = pytwalk.pytwalk(n=1) Normal.Run(T=10000, x0=10 * ones(1), xp0=15 * ones(1)) ### This does a Random Walk Metriopolis Hastings with 10000 iterations ### initial point x0 and standar dev's for the normal jumps = sigma Normal.RunRWMH(T=10000, x0=10 * ones(1), sigma=ones(1)) ### This will do a basic output analsis Normal.Ana() Normal.Save("Exptwalk-1-dim.dat")
return rt def SimInit(): """ Function to simulate initial values for the gamma distribution """ r = gamma.rvs(alp_r, scale=1 / bet_r) return np.array([r]) # ============================================================================= # Run the twalk MCMC with T iterations # ============================================================================= # First, we run the chain using the exact solution for the forward mapping # changing numeric = False twalk = pytwalk(n=d, U=Energy, Supp=Supp) # Open the t-walk object T = 350000 # Number of simulations in the MCMC (Chain length) twalk.Run(T=T, x0=SimInit(), xp0=SimInit()) # Run the t-walk with two initial values for theta hpd_index = np.argsort(twalk.Output[:, -1]) # Maximum a posteriori index MAP = twalk.Output[hpd_index[0], :-1] # MAP def plot_post(): plt.figure() start = int(.4 * T) # Burning Out_r = twalk.Output[:, 0][start:] # Output without burning rt = plt.hist(Out_r, bins=20, label='Numeric', density=True) # Histogram of the simulations plt.axvline(theta, ymax=rt[0].max() * 0.07, color='r',
def Energy(x, data=data): return -1 * (loglikelihood(data, x) + logprior(x)) def Supp(x): return all(x > 0.0) def LG_Init(): ###Simulate from the prior sim = gamma.rvs(alpha, scale=1 / beta) return sim.ravel() #""" d = len(alpha) # number of parameters LG_twalk = pytwalk(n=d, U=Energy, Supp=Supp) LG_twalk.Run(T=500000, x0=LG_Init(), xp0=LG_Init()) # Posterior Analisys burnin = 50000 thini = 300 Output = LG_twalk.Output[burnin::thini, :] Output_theta = Output[:, :d] #plt.figure() #plt.plot(LG_twalk.Output[burnin:,-1]) # Estadísticas i = np.argmax(-LG_twalk.Output[:, -1]) th_map_ = LG_twalk.Output[i, :]
def plumMCMC(dirt, plomo, T_mod, num_sup, det_lim, iterations, by, shape1_m, mean_m, shape_acc, mean_acc, fi_mean, fi_acc, As_mean, As_acc, resolution, seeds): seed(int(seeds)) fimean = fi_mean shapefi = fi_acc ASmaean = As_mean shapeAS = As_acc shape2_m = (shape1_m * (1 - mean_m)) / mean_m scale_acc = mean_acc / shape_acc scale_fi = fimean / shapefi scale_As = ASmaean / shapeAS Data = genfromtxt(dirt + plomo, delimiter=',') ##################### Data definition 210Pb if num_sup == 0: supp = Data[num_sup:, 5] sd_supp = Data[num_sup:, 6] if num_sup != 0: supp = Data[num_sup:, 2] sd_supp = Data[num_sup:, 3] num_sup = len(Data[:, 0]) - num_sup density = Data[:num_sup, 1] * 10. activity = Data[:num_sup, 2] sd_act = Data[:num_sup, 3] thic = Data[:num_sup, 4] depth = Data[:num_sup, 0] supp = Data[num_sup:, 2] sd_supp = Data[num_sup:, 3] print("The following data will be use to estimate the supported activity") print(supp) print((sum(supp) / len(supp))) activity = activity * density sd_act = sd_act * density lam = 0.03114 dep_time_data = append(depth - thic, depth) dep_time_data = list(set(dep_time_data)) X1, X0 = [], [] for i1 in range(len(depth)): for k1 in range(len(dep_time_data)): if depth[i1] == dep_time_data[k1]: X1 = append(X1, int(k1)) if (depth - thic)[i1] == dep_time_data[k1]: X0 = append(X0, int(k1)) m = 1 breaks = array(m * by) while m * by < depth[-1]: m += 1 breaks = append(breaks, m * by) #################### Functions def support(param): tmp3 = True for i in param: if i <= 0.: tmp3 = False if param[2] >= 1.: tmp3 = False if times([depth[-1]], param)[-1] > last_t(param[0]): tmp3 = False return tmp3 def last_t(fi): return (1. / lam) * log(fi / det_lim) def ln_like_data(param): Asup = param[1] * density loglike = 0. tmp2 = param[0] / lam ts = times(dep_time_data, param) for i in range(len(activity)): A_i = Asup[i] + tmp2 * (exp(-lam * ts[int(X0[i])]) - exp(-lam * ts[int(X1[i])])) Tau = .5 * (sd_act[i]**(-2.)) loglike = loglike + Tau * ((A_i - activity[i])**2.) return loglike def ln_like_T(param): Asup = param[1] * density loglike = 0. tmp2 = param[0] / lam ts = times(dep_time_data, param) for i in range(len(activity)): A_i = Asup[i] + tmp2 * (exp(-lam * ts[int(X0[i])]) - exp(-lam * ts[int(X1[i])])) Tau = .5 * (sd_act[i]**(-2.)) loglike = loglike + 3.5 * log(4. + Tau * ((A_i - activity[i])**2.)) return loglike def ln_like_supp(param): logsupp = 0. for i in range(len(supp)): Tau = .5 * (sd_supp[i]**-2.) logsupp = logsupp + Tau * ((param[1] - supp[i])**2.) return logsupp def times(x, param): w = param[2] a = param[3:] t1 = m - 1 ms1 = array([a[m - 1]]) while t1 > 0: ms1 = append(ms1, w * ms1[-1] + (1 - w) * a[t1 - 1]) t1 -= 1 ms = ms1[::-1] ages = array([]) y_last = append([0], array([sum(ms[:i + 1] * by) for i in range(len(ms))])) for i in range(len(x)): k1 = 0 while breaks[k1] < x[i]: k1 += 1 ages = append(ages, y_last[k1] + (ms[k1] * (by - (breaks[k1] - x[i])))) return ages def pendi(param): w = param[2] a = param[3:] t1 = m - 1 ms1 = array([a[m - 1]]) while t1 > 0: ms1 = append(ms1, w * ms1[-1] + (1 - w) * a[t1 - 1]) t1 -= 1 ms = ms1[::-1] return ms def ln_prior_supp(param): prior = 0. prior = prior - ((shapefi - 1.) * log(param[0]) - (param[0] / scale_fi)) # prior for fi prior = prior - ((shapeAS - 1.) * log(param[1]) - (param[1] / scale_As)) # prior for supp prior = prior - (((1. / by) - 1.) * log(param[2]) - log(by) + ((1. / by) * (shape1_m - 1.)) * log(param[2]) + (shape2_m - 1.) * log(1. - param[2]**(1. / by)) ) # prior for w # for ms in range(m): prior = prior - ((shape_acc - 1.) * log(param[ms + 3]) - (param[ms + 3] / scale_acc)) return prior if T_mod: log_data = ln_like_T else: log_data = ln_like_data def obj(param): objval = ln_like_supp(param) + ln_prior_supp(param) + log_data(param) return objval #################### Initial valules print("Seaching initial values") fi_ini_1 = uniform.rvs(size=1, loc=50, scale=200) #200. fi_ini_2 = uniform.rvs(size=1, loc=250, scale=150) #100. supp_ini_1 = uniform.rvs(size=1, loc=15, scale=30) #5. supp_ini_2 = uniform.rvs(size=1, loc=1, scale=15) #20. w_ini = uniform.rvs(size=1, loc=.2, scale=.3) #.3 w_ini0 = uniform.rvs(size=1, loc=.3, scale=.3) #.7 m_ini_1 = uniform.rvs(size=m, loc=0, scale=15) # repeat(array(3.1),m,axis=0) m_ini_2 = uniform.rvs(size=m, loc=0, scale=15) # repeat(array(.5),m,axis=0) x = append(append(append(fi_ini_1, supp_ini_1), w_ini), m_ini_1) xp = append(append(append(fi_ini_2, supp_ini_2), w_ini0), m_ini_2) while not support(x): m_ini_1 = uniform.rvs(size=m, loc=0, scale=3) x = append(append(append(fi_ini_1, supp_ini_1), w_ini), m_ini_1) while not support(xp): m_ini_2 = uniform.rvs(size=m, loc=0, scale=3) xp = append(append(append(fi_ini_2, supp_ini_2), w_ini0), m_ini_2) print("initial values were obtained") ################## New MCMC test print("the number of itrations,") print(iterations) thi = int((len(x))) * 50 #100 print("Thining,") print(thi) burnin = 10000 * len(xp) #20000 print("Burnin,") print(burnin) print("Total iterations,") print(burnin + iterations * thi) leadchrono = pytwalk.pytwalk(n=len(x), U=obj, Supp=support) i, k, k0, n = 0, 0, 0, len(x) U, Up = obj(x), obj(xp) por = int(iterations / 10.) Output = zeros((iterations + 1, n + 1)) Output[0, 0:n] = x.copy() Output[0, n] = U por2 = int(burnin / 5.) while i < iterations: onemove = leadchrono.onemove(x, U, xp, Up) k += 1 if (all([k < burnin, k % por2 == 0])): print("burn in progress") print int(100 * (k + .0) / burnin) if (uniform.rvs() < onemove[3]): x, xp, ke, A, U, Up = onemove k0 += 1 if all([k % thi == 0, k > int(burnin)]): Output[i + 1, 0:n] = x.copy() Output[i + 1, n] = U if any([i % por == 0, i == 0]): print int(100 * (i + .0) / iterations), "%" #print((time.clock()-tiempomedir)/60) i += 1 else: if all([k % thi == 0, k > int(burnin)]): Output[i + 1, 0:n] = x.copy() Output[i + 1, n] = U if any([i % por == 0, i == 0]): print int(100 * (i + .0) / iterations), "%" #print((time.clock()-tiempomedir)/60) i += 1 #Output=array(Output) print("Acceptance rate") print(k0 / (i + .0)) print("The twalk did", k, "iterations") ################## """ out 0 -> Fi out 1 -> Supported Activity out 2 -> w out 3-n -> dates out -1 -> Energy """ savetxt(dirt + 'Results/Results_output.csv', Output, delimiter=',') estim = [] for i in range((iterations - 1)): estim.append(times(breaks, Output[(i + 1), :-1])) estim = array(estim) savetxt(dirt + 'Results/dates.csv', estim) intervals = [] for i in range(len(estim[1, ])): sort = sorted(estim[:, (i)]) mean = sum(sort) / len(sort) disc = int(len(sort) * .025) + 1 disc1 = int(len(sort) * .975) sort = sort[disc:disc1] intervals.append([breaks[i], sort[0], mean, sort[-1]]) savetxt(dirt + 'Results/intervals.csv', intervals, delimiter=',') depths = array([append([0.0], breaks)]) savetxt(dirt + "Results/depths.csv", depths, delimiter=',') grafdepts = linspace(0, breaks[-1], resolution) grafdepts2 = grafdepts + (grafdepts[1] - grafdepts[0]) / 2 grafdepts2 = grafdepts2[0:(len(grafdepts2) - 1)] grafage = linspace(0, (max(estim[:, -1]) + .10), resolution) y = [] for i in range(len(depths[0, :]) - 1): logvect = array(grafdepts2 > depths[0, i]) * array( grafdepts2 <= depths[0, i + 1]) for k in range(len(logvect)): if logvect[k]: if i != 0: y1 = estim[:, i - 1] + ( (estim[:, i] - estim[:, i - 1]) / (depths[0, i + 1] - depths[0, i])) * (grafdepts[k] - depths[0, i]) porc = [] for posi in range(len(grafage) - 1): porc.append( sum( array(y1 >= grafage[posi]) * array(y1 < grafage[posi + 1]))) y.append(porc / (max(porc) + 0.0)) else: y1 = ((estim[:, i] / depths[0, i + 1]) * (grafdepts[k])) porc = [] for posi in range(len(grafage) - 1): porc.append( sum( array(y1 >= grafage[posi]) * array(y1 < grafage[posi + 1]))) y.append(porc / (max(porc) + 0.0)) savetxt(dirt + "Results/Graphs.csv", array(y), delimiter=',') slopes = [] for i in range(iterations - 1): slopes.append(pendi(Output[(i + 1), :-1])) savetxt(dirt + "Results/Slopes.csv", array(slopes), delimiter=',')
Res = Sol_Climate(theta=theta, nmrec=k) C1M = Res[0] V1M = Res[1] T2M = Res[3] ## Cálculo de la energía U = 0.99*( log(theta[0]) + log(theta[1]) + log(theta[2]) ) \ + (1 / (2*0.08**2))*sum( (T2M - T2)**2 ) + (1 / (2*0.08**2))*sum( (V1M - V1)**2 ) + (1 / (2*5**2))*sum( (C1M - C1)**2 ) \ + 3.3e-6*theta[0] + 5e-4*theta[1] + 4.34783e-8*theta[2] return U ## Ejecución del MCMC ### #Init0 = array([1000, 5, 1.3e5]) #Init1 = array([5000, 40, 3.3e5]) # Init0 y Init1 son valores iniciales cercanos a los nominales Init0 = array([2000, 15, 1.9e5]) Init1 = array([4000, 25, 2.7e5]) # Init0 y Init1 son valores iniciales cercanos a los nominales Climate = pytwalk.pytwalk( n=3, U=ClimateU, Supp=ClimateSupp) Climate.Run( T=3, x0=Init0, xp0=Init1) import pdb; pdb.set_trace() # Análsis resultados #Climate.Ana() ## Extracción de resultados param = Climate.Output() # Las primeras columnas son los paramétros y la última los valores de la energía #Climate.Save('param_1k') #param = loadtxt('param_1k') alpha1 = param[: , 0] phi2 = param[: , 1] psi2 = param[: , 2] U = param[: , 3] ## Se guardan los resultados -- Los datos pueden cargarse con la funcion loadtxt de numpy savetxt('alpha1_1k', alpha1)
def __init__(self, problem): self.twalk = pytwalk.pytwalk(n=len(problem.parameters), U=problem.nllf, Supp=problem.valid)
k = di Res = Sol_Pdn(theta=theta) m = Res[0] n = Res[1] h = Res[2] ## Cálculo de la energía U = 0.99*log(theta[1]) + (k - 15)*log(theta[3]) + 3.03e-3*theta[1] + 16*theta[3] \ + sum( (16*n)/(m + 1e-6) ) + 0.5*sum( ( (h - 380*n)/( sqrt(n + 1e-6)*theta[3] ) )**2 ) \ + sum( 16*log(m + 1e-6) - 14.5*log(n + 1e-6) ) return U ## Ejecución del MCMC ### Init0 = array([0.8, 3.5, 0.3, 1.2]) Init1 = array([0.6, 3.1, 0.2, 0.8]) Production = pytwalk.pytwalk(n=4, U=ProductionU, Supp=ProductionSupp) Production.Run(T=1200, x0=Init0, xp0=Init1) # Análsis resultados #Climate.Ana() ## Extracción de resultados param = Production.Output # Las primeras columnas son los paramétros y la última los valores de la energía #Production.Save('param_10k', start=1000) # se esta haciendo un burn-in de 1000 #param = loadtxt('param_10k') start = 200 nu = param[start:, 0] a = param[start:, 1] b = param[start:, 2] sigma_F = param[start:, 3] U = param[start:, 4]
def fU(parametros): #Distribucion log posterior eta, mu2, tau = parametros[0:8],parametros[8], parametros[9] log_prior = -np.sum(scipy.stats.norm(0, 1).logpdf(parametros)) log_likelihood = -np.sum(scipy.stats.norm(mu2 + eta *tau, desviacion_ejemplo).logpdf(y)) return(log_prior + log_likelihood) def fUSupp(x): return all( x) ### we define the objective function with the U function ### which is -log of the density function. ### The support is defined in a separate function. ### The dimension of the parameter space is n hh= pt.pytwalk( n=5, U=fU, Supp=fUSupp) A=pt.pytwalk( n=10, U=fU, Supp=fUSupp) np.random.seed(0) random.seed(0) t0 = time.time() A.Run( T=500000, x0=3*np.ones(10), xp0=2*np.ones(10)) t1 = time.time() print("--- %s segundos ---" % (t1- t0)) A.Ana() AA=A.Output vector_verosim2 = np.zeros(1000)
rt &= 0.0 < q[4] <1.0 return rt def init(): q = np.zeros(5) q[0] = np.random.uniform(low=0.0, high=0.1) q[1] = np.random.uniform(low=0.0, high=0.1) q[2] = np.random.uniform(low=0.0, high=0.1) q[3] = np.random.uniform(low=0.0, high=1.0) q[4] = np.random.uniform(low=0.0, high=1.0) return q burnin = 1000000 T = 2000000 covid = pytwalk.pytwalk(n=5,U=energy,Supp=support) y0=init() yp0=init() covid.Run(T,y0,yp0) cadena=TemporaryFile() np.save('covid/cadena',covid.Output) chain = covid.Output energy = chain[:,-1] ############################################# ### Computing the MAP estimate energy_MAP = min(energy)
def __init__(self, *args, **kwargs): import pytwalk super(TWalkSampler, self).__init__(*args, **kwargs) self.walker = pytwalk.pytwalk(n=1, U=self.ll, Supp=self.supp)
import pytwalk from numpy import ones, zeros, log, array from numpy.random import uniform ## You may get the inline help: # pytwalk.pytwalk? # pytwalk.pytwalk.Run? ################################################################## ### This sets a MCMC for n=10 independent normals (default) Normal = pytwalk.pytwalk(n=10) ## This would run the twalk #Normal.Run( T=10000, x0=10*ones(10), xp0=15*ones(10)) ### This does a Random Walk Metriopolis Hastings with 10000 iterations ### initial point x0 and standar dev's for the normal jumps = sigma #Normal.RunRWMH( T=10000, x0=10*ones(10), sigma=ones(10)) ### This will do a basic output analsis #Normal.Ana()
np.append([sdab, sdab], scl_dat[:, 1] / 15)) support(ini1) ini2 = np.random.normal(np.append([0, 0], scl_dat[:, 0]), np.append([sdab, sdab], scl_dat[:, 1] / 15)) support(ini2) scl_dat[:, 1] = .5 * (scl_dat[:, 1] + (scl_center / scl_scale))**-2 ###### Ini MCMC n = len(ini1) burnin, thin = 200000, 100 #get burnin if Run == True: twalk = pytwalk.pytwalk(n=len(ini1), U=obj, Supp=support, ww=[0.0, 0.4918, 0.4918, 0.0082 + 0.082, 0.0]) # output = np.array([]) energy = np.array([]) i, k, k0, n, x0, xp0, U, Up = 0, 0, 0, len(ini1), ini1, ini2, obj( ini1), obj(ini2) while i < it: onemove = twalk.onemove(x0, obj(x0), xp0, obj(xp0)) k += 1 if (sp.stats.uniform.rvs() < onemove[3]): x0, xp0, ke, A, U, Up = onemove k0 += 1 if all([k % thin == 0, k > int(burnin)]): #This indicates the burnin