def plot3(A,B,S,T,D, tMAP=None): States = D.Y pMAP = [9.75961269, 0.0583687877, 78.4901534, 78.1696975] alpha, beta, sig2, ome2 = pMAP tMAP = (alpha, beta, sig2, ome2, States, D.Y) x1 = np.linspace(norm.ppf(0.01), norm.ppf(0.99), 100) x2 = np.linspace(invgamma.ppf(0.01, S.prior.p1/2), invgamma.ppf(0.99, S.prior.p1/2), 100) x3 = np.linspace(invgamma.ppf(0.01, T.prior.p1/2), invgamma.ppf(0.99, T.prior.p1/2), 100) tmp = lambda (a, b, s, t, X, Y): 1/(1/A.prior.var + D.n/S.prior.p2) print tMAP print tmp(tMAP) print A.cond.var(tMAP) print A.cond.m(tMAP) print A.cond.rv((tMAP)) plt.plot(x1, A.cond.rv(tMAP).pdf(x1), 'r-', lw=5, alpha=0.6, label='a prior') plt.title('alpha conditional at MAP') plt.show() plt.plot(x1, B.cond.rv(tMAP).pdf(x1), 'r-', lw=5, alpha=0.6, label='b prior') plt.title('beta conditional at MAP') plt.show() plt.plot(x2, S.cond.rv(tMAP).pdf(x2), 'r-', lw=5, alpha=0.6, label='sig2 prior') plt.title('sig2 conditional at MAP') plt.show() plt.plot(x3, T.cond.rv(tMAP).pdf(x3), 'r-', lw=5, alpha=0.6, label='sig2 prior') plt.title('sig2 conditional at MAP') plt.show()
def calc_estimates(n, t): # posterior rate (lambda) is a Gamma distributiona (with Jeffreys prior of Poisson observations) a = n + 0.5 # b = t p10 = invgamma.ppf(0.1, a=a, scale=b) p90 = invgamma.ppf(0.9, a=a, scale=b) mean = invgamma.mean(a=a, scale=b) return mean, p10, p90
def equitailed_cs(self, alpha2): """ Calculates the equitailed credible set of a parameter. The alpha to be inserted should be between (0-100). """ alpha_split = (100 - alpha2) / 200 lower_bound = invgamma.ppf(alpha_split, self.alpha, scale=self.beta) upper_bound = invgamma.ppf(1 - alpha_split, self.alpha, scale=self.beta) return (lower_bound, upper_bound)
def pre_process_data(xdata, ydata, train_test_ratio): test_index = np.random.choice(len(ydata), int(np.ceil(len(ydata) * train_test_ratio)), replace=False) train_index = np.delete(np.arange(len(ydata)), test_index) xdata_train = xdata[train_index] ydata_train = ydata[train_index] xdata_test = xdata[test_index] ydata_test = ydata[test_index] ydata_train_min = np.min(ydata_train) ydata_train_max = np.max(ydata_train) ydata_train_mean = (ydata_train_max + ydata_train_min) / 2 dd = ydata_train_max - ydata_train_min ydata_train = (ydata_train - ydata_train_mean) / dd ydata_test = (ydata_test - ydata_train_mean) / dd # set up the hyper-parameters regr = linear_model.LinearRegression() regr.fit(xdata_train, ydata_train) y_train_predict = regr.predict(xdata_train) variance_hat = np.var(y_train_predict - ydata_train) hyper_sigma_1 = 1.5 percentile_val = 0.9 val1 = invgamma.ppf(percentile_val, a=hyper_sigma_1, scale=1) hyper_sigma_2 = variance_hat / val1 # invgamma.cdf(variance_hat, a=hyper_sigma_1, scale=hyper_sigma_2) # Calculate the standard deviation for least square regression return xdata_train, ydata_train, xdata_test, ydata_test, ydata_train_mean, dd, hyper_sigma_1, hyper_sigma_2, variance_hat
def Friedman_pre_process(xdata_train, ydata_train, xdata_test, ydata_test): ydata_train_min = np.min(ydata_train) ydata_train_max = np.max(ydata_train) ydata_train_mean = (ydata_train_max + ydata_train_min) / 2 dd = ydata_train_max - ydata_train_min ydata_train = (ydata_train - ydata_train_mean) / dd ydata_test = (ydata_test - ydata_train_mean) / dd # set up the hyper-parameters regr = linear_model.LinearRegression() regr.fit(xdata_train, ydata_train) y_train_predict = regr.predict(xdata_train) variance_hat = np.var(y_train_predict - ydata_train) hyper_sigma_1 = 1.5 percentile_val = 0.9 val1 = invgamma.ppf(percentile_val, a=hyper_sigma_1, scale=1) hyper_sigma_2 = variance_hat / val1 # invgamma.cdf(variance_hat, a=hyper_sigma_1, scale=hyper_sigma_2) # Calculate the standard deviation for least square regression return xdata_train, ydata_train, xdata_test, ydata_test, ydata_train_mean, dd, hyper_sigma_1, hyper_sigma_2, variance_hat
def main(): """ Main CLI handler. """ parser = argparse.ArgumentParser(description=__description__) parser.add_argument("--version", action="version", version="%(prog)s " + __version__) parser.add_argument("alpha", type=float, default=3.00, help="Alpha parameter value [default=%(default)s].") parser.add_argument("beta", type=float, default=0.001, help="Beta parameter value [default=%(default)s].") parser.add_argument("-f", "--input-format", type=str, default="newick", choices=["nexus", "newick"], help="Input data format (default='%(default)s')") args = parser.parse_args() args.output_prefix = None args.show_plot_on_screen = True a = args.alpha b = args.beta rv = invgamma(a, loc=0, scale=b) print("Mean: {}".format(rv.mean())) print("Variance: {}".format(rv.var())) fig, ax = plt.subplots(1, 1) x = np.linspace(invgamma.ppf(0.01, a, scale=b), invgamma.ppf(0.99, a, scale=b), 100) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') ax.plot(x, invgamma.pdf(x, a, scale=b), 'r-', lw=5, alpha=0.6, label='invgamma pdf') spdw.render_output(args, "InverseGamma")
def plot2(A,B,S,T): x = np.linspace(norm.ppf(0.01), norm.ppf(0.99), 100) plt.plot(x, A.prior.rv.pdf(x), 'b-', lw=5, alpha=0.6) plt.title('alpha prior') plt.show() plt.plot(x, B.prior.rv.pdf(x), 'b-', lw=5, alpha=0.6) plt.title('beta prior') plt.show() x = np.linspace(invgamma.ppf(0.01, S.prior.p1/2), invgamma.ppf(0.99, S.prior.p1/2), 100) plt.plot(x, S.prior.rv.pdf(x), 'b-', lw=5, alpha=0.6) plt.title('sig2 prior') plt.show() x = np.linspace(invgamma.ppf(0.01, T.prior.p1/2), invgamma.ppf(0.99, T.prior.p1/2), 100) plt.plot(x, T.prior.rv.pdf(x), 'b-', lw=5, alpha=0.6) plt.title('omg2 prior') plt.show()
def true_posterior(data, m_0, s_sq_0, v_0, k_0, n): m_data = np.mean(data) n_data = len(data) sum_sq_diff_data = np.sum([(x - m_data)**2 for x in data]) k_n = k_0 + n_data v_n = v_0 + n_data m_n = k_0 * m_0 / k_n + n_data * m_data / k_n v_n_times_s_sq_n = v_0 * s_sq_0 + sum_sq_diff_data + k_0 * n_data / k_n * ( m_data - m_0)**2 alpha = v_n / 2 beta = v_n_times_s_sq_n / 2 x = np.linspace(invgamma.ppf(1 / n, alpha, scale=beta), invgamma.ppf(1 - 1 / n, alpha, scale=beta), n * 10) y = invgamma.pdf(x, alpha, scale=beta) return alpha, beta, m_n, k_n, x, y
def __init__(self, domain, alpha, q, delta=0.001): self._domain = self._target = DomainTuple.make(domain) self._alpha, self._q, self._delta = \ float(alpha), float(q), float(delta) self._xmin, self._xmax = -8.2, 8.2 # Precompute xs = np.arange(self._xmin, self._xmax+2*delta, delta) self._table = np.log(invgamma.ppf(norm.cdf(xs), self._alpha, scale=self._q)) self._deriv = (self._table[1:]-self._table[:-1]) / delta
def quantile(self, p): """ Return the p-quantile of the Gauss posterior. .. warning:: Very experimental, I am not sure of what I did here... .. note:: I recommend to NOT use :class:`BayesUCB` with Gauss posteriors... """ # warn("Gauss.quantile() : Not implemented for a 2 dimensional distribution!", RuntimeWarning) quantile_on_sigma2 = invgamma.ppf(p, self.alpha, scale=self.beta) scale = self.beta / float(self.alpha) # mean of the Inverse-gamma quantile_on_x = norm.ppf(p, loc=self.mu, scale=scale / float(self.nu)) return quantile_on_x * quantile_on_sigma2 raise ValueError( "Gauss.quantile() : Not implemented for a 2 dimensional distribution!" )
def pre_process_data(xdata, ydata, train_test_ratio): # Usage: pre-process the data # Input: # xdata, ydata: the original full data # Output: # xdata_train, ydata_train, xdata_test, ydata_test: the train and test data # ydata_train_mean: the "mean" of the original training data # dd: the difference between the maximum and minimum ydata_train # hyper_sigma_1, hyper_sigma_2: hyper-parameters for infer the variance # variance_hat: rough estimated variance for ydata_train test_index = np.random.choice(len(ydata), int(np.ceil(len(ydata) * train_test_ratio)), replace=False) train_index = np.delete(np.arange(len(ydata)), test_index) xdata_train = xdata[train_index] ydata_train = ydata[train_index] xdata_test = xdata[test_index] ydata_test = ydata[test_index] ydata_train_min = np.min(ydata_train) ydata_train_max = np.max(ydata_train) ydata_train_mean = (ydata_train_max + ydata_train_min) / 2 dd = ydata_train_max - ydata_train_min ydata_train = (ydata_train - ydata_train_mean) / dd ydata_test = (ydata_test - ydata_train_mean) / dd # set up the hyper-parameters regr = linear_model.LinearRegression() regr.fit(xdata_train, ydata_train) y_train_predict = regr.predict(xdata_train) variance_hat = np.var(y_train_predict - ydata_train) hyper_sigma_1 = 1.5 percentile_val = 0.9 val1 = invgamma.ppf(percentile_val, a=hyper_sigma_1, scale=1) hyper_sigma_2 = variance_hat / val1 # invgamma.cdf(variance_hat, a=hyper_sigma_1, scale=hyper_sigma_2) # Calculate the standard deviation for least square regression return xdata_train, ydata_train, xdata_test, ydata_test, ydata_train_mean, dd, hyper_sigma_1, hyper_sigma_2, variance_hat
def make_invgamma_prior(lower_bound: float = 0.1, upper_bound: float = 0.5) -> rv_frozen: """Create an inverse gamma distribution prior with 98% density inside the bounds. Not all combinations of (lower_bound, upper_bound) are feasible and some of them could result in a RuntimeError. Parameters ---------- lower_bound : float, default=0.1 Lower bound at which 1 % of the cumulative density is reached. upper_bound : float, default=0.5 Upper bound at which 99 % of the cumulative density is reached. Returns ------- scipy.stats._distn_infrastructure.rv_frozen The frozen distribution with shape parameters already set. Raises ------ ValueError Either if any of the bounds is 0 or negative, or if the upper bound is equal or smaller than the lower bound. """ if lower_bound <= 0 or upper_bound <= 0: raise ValueError("The bounds cannot be equal to or smaller than 0.") if lower_bound >= upper_bound: raise ValueError( "Lower bound needs to be strictly smaller than the upper " "bound.") with warnings.catch_warnings(): warnings.simplefilter("ignore") (a_out, scale_out), pcov = curve_fit( lambda xdata, a, scale: invgamma.ppf(xdata, a=a, scale=scale), [0.01, 0.99], [lower_bound, upper_bound], ) return invgamma(a=a_out, scale=scale_out)
def test_quantile(self): ys = np.arange(0.01, 0.99, 0.1) shapes = np.arange(0.01, 20, 0.5) scales = np.arange(0.01, 10, 0.5) test_params = cartesian([ys, shapes, scales]).astype(np.float64) python_results = np.zeros(test_params.shape[0]) for ind in range(test_params.shape[0]): y = test_params[ind, 0] shape = test_params[ind, 1] scale = test_params[ind, 2] python_results[ind] = invgamma.ppf(y, shape, scale=scale) opencl_results = invgamma_ppf().evaluate( { 'y': test_params[:, 0], 'shape': test_params[:, 1], 'scale': test_params[:, 2] }, test_params.shape[0]) assert_allclose(opencl_results, python_results, atol=1e-7, rtol=1e-7)
def IG(field, alpha, q): foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q) return Field.from_local_data(field.domain, foo)
def inverseGammaVisualize(alpha,beta): x = np.linspace(invgamma.ppf(0.01, alpha,scale=beta),invgamma.ppf(0.99,alpha,scale=beta), 100) plt.plot(x, invgamma.pdf(x, alpha,scale=beta), 'r-', lw=5, alpha=0.6, label='IG density for alpha={0}, beta={1}'.format(alpha,beta)) plt.legend() return plt.gca()
bp = norm(loc=b0[0], scale=np.sqrt(b0[1])) s2p = invgamma(s20[0]/2, loc=0, scale=s20[0]*s20[1]/2) # In[138]: x = np.linspace(norm.ppf(0.01), norm.ppf(0.99), 100) plt.plot(x, ap.pdf(x), 'b-', lw=5, alpha=0.6) plt.title('alpha prior') plt.show() plt.plot(x, bp.pdf(x), 'b-', lw=5, alpha=0.6) plt.title('beta prior') plt.show() x = np.linspace(invgamma.ppf(0.01, s20[0]/2), invgamma.ppf(0.99, s20[1]/2), 100) plt.plot(x, s2p.pdf(x), 'b-', lw=5, alpha=0.6) plt.title('sig2 prior') plt.show() ### Full conditionals # In[139]: aC = lambda (a, b, s2): 1/(1/a0[1] + D.n/s20[1]) am = lambda (a, b, s2): aC((a,b,s2))*(sum(D.Y-b*D.X)/s20[1] + a0[0]/a0[1]) bC = lambda (a, b, s2): 1/(1/b0[1] + sum(D.X**2)/s20[1]) bm = lambda (a, b, s2): bC((a,b,s2))*(sum((D.Y-a)*D.X)/s20[1]+b0[0]/b0[1]) sA = lambda (a, b, s2): s20[0] + D.n sB = lambda (a, b, s2): s20[0]*s20[1] + sum((D.Y-a-b*D.X)**2)
from scipy.stats import invgamma import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: a = 4.07 mean, var, skew, kurt = invgamma.stats(a, moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(invgamma.ppf(0.01, a), invgamma.ppf(0.99, a), 100) ax.plot(x, invgamma.pdf(x, a), 'r-', lw=5, alpha=0.6, label='invgamma 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 = invgamma(a) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = invgamma.ppf([0.001, 0.5, 0.999], a) np.allclose([0.001, 0.5, 0.999], invgamma.cdf(vals, a)) # True # Generate random numbers:
from scipy.stats import invgamma import matplotlib.pyplot as plt import numpy as np x = np.linspace(invgamma.ppf(0.01, a), invgamma.ppf(0.99, a), 100) rv = invgamma(a) fig, ax = plt.subplots(1, 1) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
def ppf_inv_gamma(x, a, b): return b * invgamma.ppf(x, a)