def __init__(self, cdts, Rcut, hyp, Dist, centre_init): """ Constructor of the logposteriorModule """ rad, thet = Deg2pc(cdts, centre_init, Dist) c, r, t, self.Rmax = TruncSort(cdts, rad, thet, Rcut) self.pro = c[:, 2] self.cdts = c[:, :2] self.Dist = Dist #------------- poisson ---------------- self.quadrants = [ 0, np.pi / 2.0, np.pi, 3.0 * np.pi / 2.0, 2.0 * np.pi ] self.poisson = st.poisson(len(r) / 4.0) #-------------- priors ---------------- self.Prior_0 = st.norm(loc=centre_init[0], scale=hyp[0]) self.Prior_1 = st.norm(loc=centre_init[1], scale=hyp[1]) self.Prior_2 = st.uniform(loc=-0.5 * np.pi, scale=np.pi) self.Prior_3 = st.halfcauchy(loc=0.01, scale=hyp[2]) self.Prior_4 = st.halfcauchy(loc=0.01, scale=hyp[3]) self.Prior_5 = st.halfcauchy(loc=0.01, scale=hyp[2]) self.Prior_6 = st.halfcauchy(loc=0.01, scale=hyp[3]) self.Prior_7 = st.truncexpon(b=hyp[4], loc=0.01, scale=hyp[5]) self.Prior_8 = st.truncexpon(b=hyp[4], loc=0.01, scale=hyp[5]) print("Module Initialized")
def __init__(self,cdts,Rcut,hyp,Dist,centre_init): """ Constructor of the logposteriorModule """ rad,thet = Deg2pc(cdts,centre_init,Dist) c,r,t,self.Rmax = TruncSort(cdts,rad,thet,Rcut) self.pro = c[:,2] self.cdts = c[:,:2] self.Dist = Dist #-------------- Finds the mode of the band ------- band_all = c[:,3] idv = np.where(np.isfinite(c[:,3]))[0] band = c[idv,3] kde = st.gaussian_kde(band) x = np.linspace(np.min(band),np.max(band),num=1000) self.mode = x[kde(x).argmax()] print "Mode of band at ",self.mode #---- repleace NANs by mode ----- idnv = np.setdiff1d(np.arange(len(band_all)),idv) band_all[idnv] = self.mode self.delta_band = band_all - self.mode #------------- poisson ---------------- self.quadrants = [0,np.pi/2.0,np.pi,3.0*np.pi/2.0,2.0*np.pi] self.poisson = st.poisson(len(r)/4.0) #-------------- priors ---------------- self.Prior_0 = st.norm(loc=centre_init[0],scale=hyp[0]) self.Prior_1 = st.norm(loc=centre_init[1],scale=hyp[1]) self.Prior_2 = st.uniform(loc=-0.5*np.pi,scale=np.pi) self.Prior_3 = st.halfcauchy(loc=0.01,scale=hyp[2]) self.Prior_4 = st.halfcauchy(loc=0.01,scale=hyp[2]) self.Prior_5 = st.truncexpon(b=hyp[3],loc=2.01,scale=hyp[4]) self.Prior_6 = st.norm(loc=hyp[5],scale=hyp[6]) print "Module Initialized"
def __init__(self, cdts, Rcut, hyp, Dist, centre): """ Constructor of the logposteriorModule """ rad, thet = Deg2pc(cdts, centre, Dist) c, r, t, self.Rmax = TruncSort(cdts, rad, thet, Rcut) self.pro = c[:, 2] self.rad = r # -------- Priors -------- self.Prior_0 = st.halfcauchy(loc=0, scale=hyp[0]) self.Prior_1 = st.halfcauchy(loc=0, scale=hyp[1]) print("Module Initialized")
def initpos(self,nwalkers): # pick nwalkers random positions in the range pos=[] for i in range(len(self.rmin)): pos.append(np.random.uniform(self.rmin[i],self.rmax[i],size=nwalkers)) pos.append(halfcauchy(scale=self.variance_prior_scale).rvs(size=nwalkers)) return np.asarray(pos).T
def __init__(self, cdts, Rmax, hyp, Dist): """ Constructor of the logposteriorModule """ self.pro = cdts[:, 2] self.rad = cdts[:, 3] self.Rmax = Rmax self.Prior_0 = st.halfcauchy(loc=0, scale=hyp[0]) self.Prior_1 = st.uniform(loc=0.01, scale=hyp[1]) self.Prior_2 = st.uniform(loc=0, scale=2) print("Module Initialized")
def setup_class(cls): np.random.seed(42) pdf_a = stats.halfcauchy(loc=0, scale=1) pdf_b = stats.uniform(loc=0, scale=100) n_a = 50 n_b = 200 params = [n_a, n_b] X = np.concatenate([pdf_a.rvs(size=n_a), pdf_b.rvs(size=n_b), ])[:, np.newaxis] cls.X = X cls.params = params cls.pdf_a = pdf_a cls.pdf_b = pdf_b
def setup_class(cls): np.random.seed(42) pdf_a = stats.halfcauchy(loc=0, scale=1) pdf_b = stats.uniform(loc=0, scale=100) n_a = 50 n_b = 200 params = [n_a, n_b] X = np.concatenate([ pdf_a.rvs(size=n_a), pdf_b.rvs(size=n_b), ])[:, np.newaxis] cls.X = X cls.params = params cls.pdf_a = pdf_a cls.pdf_b = pdf_b
def __init__(self, cdts, Rcut, hyp, Dist, centre_init): """ Constructor of the logposteriorModule """ rad, thet = Deg2pc(cdts, centre_init, Dist) c, r, t, self.Rmax = TruncSort(cdts, rad, thet, Rcut) self.pro = c[:, 2] self.cdts = c[:, :2] self.Dist = Dist print "There are ", len(self.cdts), " observations." # sys.exit() #------------- poisson ---------------- self.quadrants = [ 0, np.pi / 2.0, np.pi, 3.0 * np.pi / 2.0, 2.0 * np.pi ] self.poisson = st.poisson(len(r) / 4.0) #-------------- priors ---------------- self.Prior_0 = st.norm(loc=centre_init[0], scale=hyp[0]) self.Prior_1 = st.norm(loc=centre_init[1], scale=hyp[1]) self.Prior_2 = st.halfcauchy(loc=0.01, scale=hyp[2]) self.Prior_3 = st.truncexpon(b=hyp[3], loc=2.01, scale=hyp[4]) print "Module Initialized"
lambda probs, total_count: osp.binom(n=total_count, p=probs), dist.BinomialLogits: lambda logits, total_count: osp.binom(n=total_count, p=_to_probs_bernoulli(logits)), dist.Cauchy: lambda loc, scale: osp.cauchy(loc=loc, scale=scale), dist.Chi2: lambda df: osp.chi2(df), dist.Dirichlet: lambda conc: osp.dirichlet(conc), dist.Exponential: lambda rate: osp.expon(scale=np.reciprocal(rate)), dist.Gamma: lambda conc, rate: osp.gamma(conc, scale=1. / rate), dist.HalfCauchy: lambda scale: osp.halfcauchy(scale=scale), dist.HalfNormal: lambda scale: osp.halfnorm(scale=scale), dist.InverseGamma: lambda conc, rate: osp.invgamma(conc, scale=rate), dist.LogNormal: lambda loc, scale: osp.lognorm(s=scale, scale=np.exp(loc)), dist.MultinomialProbs: lambda probs, total_count: osp.multinomial(n=total_count, p=probs), dist.MultinomialLogits: lambda logits, total_count: osp.multinomial(n=total_count, p=_to_probs_multinom(logits)), dist.MultivariateNormal: _mvn_to_scipy, dist.Normal: lambda loc, scale: osp.norm(loc=loc, scale=scale),
cov = jax_dist.covariance_matrix return osp.multivariate_normal(mean=mean, cov=cov) _DIST_MAP = { dist.BernoulliProbs: lambda probs: osp.bernoulli(p=probs), dist.BernoulliLogits: lambda logits: osp.bernoulli(p=_to_probs_bernoulli(logits)), dist.Beta: lambda con1, con0: osp.beta(con1, con0), dist.BinomialProbs: lambda probs, total_count: osp.binom(n=total_count, p=probs), dist.BinomialLogits: lambda logits, total_count: osp.binom(n=total_count, p=_to_probs_bernoulli(logits)), dist.Cauchy: lambda loc, scale: osp.cauchy(loc=loc, scale=scale), dist.Chi2: lambda df: osp.chi2(df), dist.Dirichlet: lambda conc: osp.dirichlet(conc), dist.Exponential: lambda rate: osp.expon(scale=np.reciprocal(rate)), dist.Gamma: lambda conc, rate: osp.gamma(conc, scale=1./rate), dist.HalfCauchy: lambda scale: osp.halfcauchy(scale=scale), dist.HalfNormal: lambda scale: osp.halfnorm(scale=scale), dist.InverseGamma: lambda conc, rate: osp.invgamma(conc, scale=rate), dist.LogNormal: lambda loc, scale: osp.lognorm(s=scale, scale=np.exp(loc)), dist.MultinomialProbs: lambda probs, total_count: osp.multinomial(n=total_count, p=probs), dist.MultinomialLogits: lambda logits, total_count: osp.multinomial(n=total_count, p=_to_probs_multinom(logits)), dist.MultivariateNormal: _mvn_to_scipy, dist.LowRankMultivariateNormal: _lowrank_mvn_to_scipy, dist.Normal: lambda loc, scale: osp.norm(loc=loc, scale=scale), dist.Pareto: lambda alpha, scale: osp.pareto(alpha, scale=scale), dist.Poisson: lambda rate: osp.poisson(rate), dist.StudentT: lambda df, loc, scale: osp.t(df=df, loc=loc, scale=scale), dist.Uniform: lambda a, b: osp.uniform(a, b - a), }
# Calculate a few first moments: mean, var, skew, kurt = halfcauchy.stats(moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(halfcauchy.ppf(0.01), halfcauchy.ppf(0.99), 100) ax.plot(x, halfcauchy.pdf(x), 'r-', lw=5, alpha=0.6, label='halfcauchy pdf') # Alternatively, the distribution object can be called (as a function) # to fix the shape, location and scale parameters. This returns a "frozen" # RV object holding the given parameters fixed. # Freeze the distribution and display the frozen ``pdf``: rv = halfcauchy() ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = halfcauchy.ppf([0.001, 0.5, 0.999]) np.allclose([0.001, 0.5, 0.999], halfcauchy.cdf(vals)) # True # Generate random numbers: r = halfcauchy.rvs(size=1000) # And compare the histogram: ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
import matplotlib.pyplot as plt import numpy as np from scipy.stats import halfcauchy """ Example of transformation of constrained variables. From ADVI Section 2.3 """ theta = np.linspace(0 + 1e-2, 10, 1000) zeta = np.linspace(-5, 5, 1000) pdf_original = halfcauchy(loc=0, scale=5).pdf(theta) pdf_transformed = halfcauchy(loc=0, scale=5).pdf(np.exp(zeta)) * np.exp(zeta) plt.plot(theta, pdf_original, label='p(tau)') plt.plot(zeta, pdf_transformed, label='p(log(tau))') plt.legend() plt.show()
def all_dists(): # dists param were taken from scipy.stats official # documentaion examples # Total - 89 return { "alpha": stats.alpha(a=3.57, loc=0.0, scale=1.0), "anglit": stats.anglit(loc=0.0, scale=1.0), "arcsine": stats.arcsine(loc=0.0, scale=1.0), "beta": stats.beta(a=2.31, b=0.627, loc=0.0, scale=1.0), "betaprime": stats.betaprime(a=5, b=6, loc=0.0, scale=1.0), "bradford": stats.bradford(c=0.299, loc=0.0, scale=1.0), "burr": stats.burr(c=10.5, d=4.3, loc=0.0, scale=1.0), "cauchy": stats.cauchy(loc=0.0, scale=1.0), "chi": stats.chi(df=78, loc=0.0, scale=1.0), "chi2": stats.chi2(df=55, loc=0.0, scale=1.0), "cosine": stats.cosine(loc=0.0, scale=1.0), "dgamma": stats.dgamma(a=1.1, loc=0.0, scale=1.0), "dweibull": stats.dweibull(c=2.07, loc=0.0, scale=1.0), "erlang": stats.erlang(a=2, loc=0.0, scale=1.0), "expon": stats.expon(loc=0.0, scale=1.0), "exponnorm": stats.exponnorm(K=1.5, loc=0.0, scale=1.0), "exponweib": stats.exponweib(a=2.89, c=1.95, loc=0.0, scale=1.0), "exponpow": stats.exponpow(b=2.7, loc=0.0, scale=1.0), "f": stats.f(dfn=29, dfd=18, loc=0.0, scale=1.0), "fatiguelife": stats.fatiguelife(c=29, loc=0.0, scale=1.0), "fisk": stats.fisk(c=3.09, loc=0.0, scale=1.0), "foldcauchy": stats.foldcauchy(c=4.72, loc=0.0, scale=1.0), "foldnorm": stats.foldnorm(c=1.95, loc=0.0, scale=1.0), # "frechet_r": stats.frechet_r(c=1.89, loc=0.0, scale=1.0), # "frechet_l": stats.frechet_l(c=3.63, loc=0.0, scale=1.0), "genlogistic": stats.genlogistic(c=0.412, loc=0.0, scale=1.0), "genpareto": stats.genpareto(c=0.1, loc=0.0, scale=1.0), "gennorm": stats.gennorm(beta=1.3, loc=0.0, scale=1.0), "genexpon": stats.genexpon(a=9.13, b=16.2, c=3.28, loc=0.0, scale=1.0), "genextreme": stats.genextreme(c=-0.1, loc=0.0, scale=1.0), "gausshyper": stats.gausshyper(a=13.8, b=3.12, c=2.51, z=5.18, loc=0.0, scale=1.0), "gamma": stats.gamma(a=1.99, loc=0.0, scale=1.0), "gengamma": stats.gengamma(a=4.42, c=-3.12, loc=0.0, scale=1.0), "genhalflogistic": stats.genhalflogistic(c=0.773, loc=0.0, scale=1.0), "gilbrat": stats.gilbrat(loc=0.0, scale=1.0), "gompertz": stats.gompertz(c=0.947, loc=0.0, scale=1.0), "gumbel_r": stats.gumbel_r(loc=0.0, scale=1.0), "gumbel_l": stats.gumbel_l(loc=0.0, scale=1.0), "halfcauchy": stats.halfcauchy(loc=0.0, scale=1.0), "halflogistic": stats.halflogistic(loc=0.0, scale=1.0), "halfnorm": stats.halfnorm(loc=0.0, scale=1.0), "halfgennorm": stats.halfgennorm(beta=0.675, loc=0.0, scale=1.0), "hypsecant": stats.hypsecant(loc=0.0, scale=1.0), "invgamma": stats.invgamma(a=4.07, loc=0.0, scale=1.0), "invgauss": stats.invgauss(mu=0.145, loc=0.0, scale=1.0), "invweibull": stats.invweibull(c=10.6, loc=0.0, scale=1.0), "johnsonsb": stats.johnsonsb(a=4.32, b=3.18, loc=0.0, scale=1.0), "johnsonsu": stats.johnsonsu(a=2.55, b=2.25, loc=0.0, scale=1.0), "ksone": stats.ksone(n=1e03, loc=0.0, scale=1.0), "kstwobign": stats.kstwobign(loc=0.0, scale=1.0), "laplace": stats.laplace(loc=0.0, scale=1.0), "levy": stats.levy(loc=0.0, scale=1.0), "levy_l": stats.levy_l(loc=0.0, scale=1.0), "levy_stable": stats.levy_stable(alpha=0.357, beta=-0.675, loc=0.0, scale=1.0), "logistic": stats.logistic(loc=0.0, scale=1.0), "loggamma": stats.loggamma(c=0.414, loc=0.0, scale=1.0), "loglaplace": stats.loglaplace(c=3.25, loc=0.0, scale=1.0), "lognorm": stats.lognorm(s=0.954, loc=0.0, scale=1.0), "lomax": stats.lomax(c=1.88, loc=0.0, scale=1.0), "maxwell": stats.maxwell(loc=0.0, scale=1.0), "mielke": stats.mielke(k=10.4, s=3.6, loc=0.0, scale=1.0), "nakagami": stats.nakagami(nu=4.97, loc=0.0, scale=1.0), "ncx2": stats.ncx2(df=21, nc=1.06, loc=0.0, scale=1.0), "ncf": stats.ncf(dfn=27, dfd=27, nc=0.416, loc=0.0, scale=1.0), "nct": stats.nct(df=14, nc=0.24, loc=0.0, scale=1.0), "norm": stats.norm(loc=0.0, scale=1.0), "pareto": stats.pareto(b=2.62, loc=0.0, scale=1.0), "pearson3": stats.pearson3(skew=0.1, loc=0.0, scale=1.0), "powerlaw": stats.powerlaw(a=1.66, loc=0.0, scale=1.0), "powerlognorm": stats.powerlognorm(c=2.14, s=0.446, loc=0.0, scale=1.0), "powernorm": stats.powernorm(c=4.45, loc=0.0, scale=1.0), "rdist": stats.rdist(c=0.9, loc=0.0, scale=1.0), "reciprocal": stats.reciprocal(a=0.00623, b=1.01, loc=0.0, scale=1.0), "rayleigh": stats.rayleigh(loc=0.0, scale=1.0), "rice": stats.rice(b=0.775, loc=0.0, scale=1.0), "recipinvgauss": stats.recipinvgauss(mu=0.63, loc=0.0, scale=1.0), "semicircular": stats.semicircular(loc=0.0, scale=1.0), "t": stats.t(df=2.74, loc=0.0, scale=1.0), "triang": stats.triang(c=0.158, loc=0.0, scale=1.0), "truncexpon": stats.truncexpon(b=4.69, loc=0.0, scale=1.0), "truncnorm": stats.truncnorm(a=0.1, b=2, loc=0.0, scale=1.0), "tukeylambda": stats.tukeylambda(lam=3.13, loc=0.0, scale=1.0), "uniform": stats.uniform(loc=0.0, scale=1.0), "vonmises": stats.vonmises(kappa=3.99, loc=0.0, scale=1.0), "vonmises_line": stats.vonmises_line(kappa=3.99, loc=0.0, scale=1.0), "wald": stats.wald(loc=0.0, scale=1.0), "weibull_min": stats.weibull_min(c=1.79, loc=0.0, scale=1.0), "weibull_max": stats.weibull_max(c=2.87, loc=0.0, scale=1.0), "wrapcauchy": stats.wrapcauchy(c=0.0311, loc=0.0, scale=1.0), }
def p(log_tau, theta): return norm(loc=theta, scale=SIGMA).pdf(Y1) \ * norm(loc=MU, scale=np.exp(log_tau)).pdf(theta) \ * norm(loc=0, scale=5).pdf(MU) \ * halfcauchy(loc=0, scale=5).pdf(np.exp(log_tau)) \ * np.exp(log_tau)
axs[1, 0].axhline(y=Y[2], color='black', linestyle='-') axs[1, 0].set_xlabel(r'$\mu$') axs[1, 0].set_ylabel(r'$\theta_{3}$') axs[1, 0].annotate(r'$y_{3}$', xy=(-4, Y[2] + 1), xycoords='data') axs[1, 1].scatter(np.log(mcmc_trace['mu'][1000:]), mcmc_trace['theta'][:, -2][1000:], color='tan', alpha=0.6) axs[1, 1].axhline(y=Y[-2], color='black', linestyle='-') axs[1, 1].set_xlabel(r'$\mu$') axs[1, 1].set_ylabel(r'$\theta_{7}$') axs[1, 1].annotate(r'$y_{7}$', xy=(-4, Y[-2] + 1), xycoords='data') # MU & THETA mu_true_pdf = norm(loc=0, scale=5).pdf mu_linspace = np.linspace(-15, 15, 2000) tau_true_pdf = halfcauchy(loc=0, scale=5).pdf tau_linspace = np.linspace(-2, 30, 2000) f, (ax1, ax2) = plt.subplots(1, 2) sns.kdeplot(mcmc_trace['mu'][1000:], color='steelblue', ax=ax1, label=r'kde($\mu$)') count, bins, ignored = ax1.hist(mcmc_trace['mu'][1000:], 50, density=True, color='skyblue', alpha=0.6) ax1.plot(mu_linspace, mu_true_pdf(mu_linspace), color='firebrick', label=r'$\mathcal{N}(0,5)$') ax1.title.set_text(r'Distribution $\mu$') ax1.set_xlabel(r'$\mu$') ax1.set_ylabel(r'p($\mu$)') ax1.legend() sns.kdeplot(mcmc_trace['tau'][1000:], color='steelblue', ax=ax2, label=r'kde($\tau$)') count, bins, ignored = ax2.hist(mcmc_trace['tau'][1000:], 50, density=True, color='skyblue', alpha=0.6) ax2.plot(tau_linspace, tau_true_pdf(tau_linspace), color='firebrick', label=r'Half-Cauchy$(0,5)$') ax2.title.set_text(r'Distribution $\tau$')
n_bs = 5 q = 95 ln_par, ln_lo, ln_up = bootstrap_fit( stats.lognorm, resid, n_iter=n_bs, quant=q ) hc_par, hc_lo, hc_up = bootstrap_fit( stats.halfcauchy, resid, n_iter=n_bs, quant=q ) gam_par, gam_lo, gam_up = bootstrap_fit( stats.gamma, resid, n_iter=n_bs, quant=q ) ################################################################## hc = stats.halfcauchy(*stats.halfcauchy.fit(resid)) lg = stats.lognorm(*stats.lognorm.fit(resid)) dens = KDEUnivariate(resid) dens.fit() ecdf = ECDF(resid) ################################################################## # prepare X axes for plotting ex = ecdf.x x = np.linspace(min(resid), max(resid), 2000) ################################################################## # Fit a Landau distribution with ROOT if HAS_ROOT: