def stoye_CI(lower_estim, upper_estim, varcov, signif_level): # Stoye (2009) confidence interval for partially identified parameter # Inputs to routine Delta = upper_estim - lower_estim # Point estimate of length of identif. set sigma_lower = np.sqrt(varcov[0, 0]) # Std. dev. of lower bound estimate sigma_upper = np.sqrt(varcov[1, 1]) # Std. dev. of upper bound estimate rho = varcov[0, 1] / (sigma_lower * sigma_upper ) # Correlation of lower and upper bound estimates # Numerically minimize CI length subject to coverage constraints con = lambda c: [ 0, 1 - signif_level - np.r_[stoye_bound(c, rho, Delta / sigma_upper), stoye_bound(np.fliplr(c), rho, Delta / sigma_lower)] ] nlc = NonlinearConstraint(con, -np.inf, 1.9) c_opt = opt.minimize( lambda c: np.c_[sigma_lower, sigma_upper] @ c.T, x0=np.array([ lower_estim - invgauss.cdf(1 - signif_level / 2) * sigma_lower, upper_estim + invgauss.cdf(1 - signif_level / 2) * sigma_upper ]), constraints=nlc) # Confidence interval CI = np.c_[lower_estim - sigma_lower * c_opt(1), upper_estim + sigma_upper * c_opt(2)] return CI
def truncinvgaussprior_pdf(data, mu, sigma): epsilon = 1e-200 term2 = (invgauss.pdf(data, sigma, scale=mu, loc=0.0) / (invgauss.cdf(1.0, sigma, scale=mu, loc=0.0) - invgauss.cdf(0.0, sigma, scale=mu, loc=0.0))) * (data < 1.0) return term2 + epsilon
def ChisTest_gauss(x, r, mu, sigma): n = len(x) k = int(round(1 + 3.3*np.log10(n))) #change this estimate laters prob = 1.0/k probs = [xi/float(k) for xi in range(k)] x = np.array(x) quantiles = invgauss.cdf(probs, mu) e = np.ones(k)*n*(prob) o = np.zeros(len(e)) for i in range(len(o)): if i == len(o)-1: o[i] = len(x[x >= quantiles[i]]) else: o[i] = len(x[(x < quantiles[i+1]) & (x >= quantiles[i]) ]) Chis = sum(((o - e)**2)/e) alpha = gammainc((k-r-1)/2, Chis/2) if (alpha > 0.05): return True else: return False
def integral(int_intensity_diff, shape): return invgauss.cdf(mu=shape, x=int_intensity_diff)
def CI_fun(bounds_boot, bounds_OLS, settings): # Bootstrap confidence intervals # ---------------------------------------------------------------- # Get Inputs # ---------------------------------------------------------------- fields = list(bounds_OLS.__dict__.keys()) signif_level = settings.signif_level # optimopts = settings.optimopts # ---------------------------------------------------------------- # Quantiles of Bootstrap Draws # ---------------------------------------------------------------- class bounds_boot_mean(): for j in range(len(fields)): locals()[fields[j]] = np.squeeze( np.mean(getattr(bounds_boot, fields[j]), 2)) # Average class bounds_boot_plow(): for j in range(len(fields)): if type(getattr(bounds_OLS, fields[j])) == np.float64: locals()[fields[j]] = np.squeeze( np.quantile(getattr(bounds_boot, fields[j]) - (getattr(bounds_OLS, fields[j])), signif_level / 2, axis=2)) # Lower quantile elif type(getattr(bounds_OLS, fields[j])) == np.ndarray: locals()[fields[j]] = np.squeeze( np.quantile( getattr(bounds_boot, fields[j]) - (getattr(bounds_OLS, fields[j]))[:, :, np.newaxis], signif_level / 2, axis=2)) # Lower quantile else: locals()[fields[j]] = [] class bounds_boot_phigh(): for j in range(len(fields)): if type(getattr(bounds_OLS, fields[j])) == np.float64: locals()[fields[j]] = np.squeeze( np.quantile(getattr(bounds_boot, fields[j]) - (getattr(bounds_OLS, fields[j])), 1 - signif_level / 2, axis=2)) # Upper quantile elif type(getattr(bounds_OLS, fields[j])) == np.ndarray: locals()[fields[j]] = np.squeeze( np.quantile( getattr(bounds_boot, fields[j]) - (getattr(bounds_OLS, fields[j]))[:, :, np.newaxis], 1 - signif_level / 2, axis=2)) # Upper quantile else: locals()[fields[j]] = [] bounds_boot_mean = bounds_boot_mean() bounds_boot_plow = bounds_boot_plow() bounds_boot_phigh = bounds_boot_phigh() # ---------------------------------------------------------------- # CI for IS # ---------------------------------------------------------------- class bounds_CI_IS(): class OLS_biascorr(): for j in range(len(fields)): locals()[ fields[j]] = 2 * getattr(bounds_OLS, fields[j]) - getattr( bounds_boot_mean, fields[j]) # # Bias correction class lower(): for j in range(len(fields)): if getattr(bounds_OLS, fields[j]) == []: locals()[fields[j]] = [] else: locals()[fields[j]] = getattr( bounds_OLS, fields[j]) - getattr( bounds_boot_phigh, fields[j]) # Hall's # bootstrap percentile interval, lower bound class upper(): for j in range(len(fields)): if getattr(bounds_OLS, fields[j]) == []: locals()[fields[j]] = [] else: locals()[fields[j]] = getattr( bounds_OLS, fields[j]) - getattr( bounds_boot_plow, fields[j]) # Hall's bounds_CI_IS = bounds_CI_IS() bounds_CI_IS.OLS_biascorr = bounds_CI_IS.OLS_biascorr() bounds_CI_IS.lower = bounds_CI_IS.lower() bounds_CI_IS.upper = bounds_CI_IS.upper() # ---------------------------------------------------------------- # CI for Parameter (Stoye, 2009) # ---------------------------------------------------------------- bounds_CI_para = {'lower': {}, 'upper': {}} if not settings.CI_para: return bounds_CI_IS, bounds_CI_para for j in range(len(fields)): lb_pos = fields[j].index('_LB') if lb_pos == None: continue else: the_param = fields[j][:lb_pos] # Name of parameter field_LB = fields[j] # Lower bound field field_UB = the_param + '_UB' # Upper bound field if type(getattr(bounds_OLS, field_LB)) == np.float64: bounds_CI_para['lower'][the_param] = np.empty(1) bounds_CI_para['upper'][the_param] = np.empty(1) size_l = np.size(getattr(bounds_OLS, field_LB)) size_m = np.size(getattr(bounds_OLS, field_LB)) else: bounds_CI_para['lower'][the_param] = np.empty( getattr(bounds_OLS, field_LB).shape) bounds_CI_para['upper'][the_param] = np.empty( getattr(bounds_OLS, field_LB).shape) size_l = np.size(getattr(bounds_OLS, field_LB), 0) size_m = np.size(getattr(bounds_OLS, field_LB), 1) for l in range(size_l): for m in range(size_m): # Bootstrap var-cov matrix of estimated lower and upper bounds varcov = np.cov( np.column_stack( (np.squeeze(getattr(bounds_boot, field_LB)[l, m, :]), np.squeeze(getattr(bounds_boot, field_UB)[l, m, :])))) # Enforce parameter in [0,1] (except alpha) if the_param != 'alpha': if type(getattr(bounds_CI_IS.OLS_biascorr, field_LB)) == np.float64: getattr(bounds_CI_IS.OLS_biascorr, field_LB)[l, m] = max( 0, getattr(bounds_CI_IS.OLS_biascorr, field_LB)) getattr(bounds_CI_IS.OLS_biascorr, field_UB)[l, m] = max( 0, getattr(bounds_CI_IS.OLS_biascorr, field_UB)) else: getattr(bounds_CI_IS.OLS_biascorr, field_LB)[l, m] = max( 0, getattr(bounds_CI_IS.OLS_biascorr, field_LB)[l, m]) getattr(bounds_CI_IS.OLS_biascorr, field_UB)[l, m] = max( 0, getattr(bounds_CI_IS.OLS_biascorr, field_UB)[l, m]) if not (('FVD_LB' == field_LB) or ('R2_recov_LB' == field_LB)): # Compute Stoye (2009) confidence interval if type(getattr(bounds_CI_IS.OLS_biascorr, field_LB)) == np.float64: CI = stoye_CI( getattr(bounds_CI_IS.OLS_biascorr, field_LB), getattr(bounds_CI_IS.OLS_biascorr, field_UB), varcov, signif_level) bounds_CI_para['lower'][the_param] = CI[0] bounds_CI_para['upper'][the_param] = CI[1] else: CI = stoye_CI( getattr(bounds_CI_IS.OLS_biascorr, field_LB)[l, m], getattr(bounds_CI_IS.OLS_biascorr, field_UB)[l, m], varcov, signif_level) bounds_CI_para['lower'][the_param][l, m] = CI[0] bounds_CI_para['upper'][the_param][l, m] = CI[1] else: # FVD and R2_recov: one-sided lower confidence interval, # since upper bound is always 1 if type(getattr(bounds_CI_IS.OLS_biascorr, field_LB)) == np.float64: bounds_CI_para['lower'][the_param] = getattr( bounds_CI_IS.OLS_biascorr, field_LB ) + invgauss.cdf(signif_level) * np.sqrt(varcov[0, 0]) bounds_CI_para['lower'][the_param] = 1 else: bounds_CI_para['lower'][the_param][l, m] = getattr( bounds_CI_IS.OLS_biascorr, field_LB ) + invgauss.cdf(signif_level) * np.sqrt(varcov[0, 0]) bounds_CI_para['lower'][the_param][l, m] = 1 return bounds_CI_IS, bounds_CI_para
# Display the probability density function (``pdf``): x = np.linspace(invgauss.ppf(0.01, mu), invgauss.ppf(0.99, mu), 100) ax.plot(x, invgauss.pdf(x, mu), 'r-', lw=5, alpha=0.6, label='invgauss 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 = invgauss(mu) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = invgauss.ppf([0.001, 0.5, 0.999], mu) np.allclose([0.001, 0.5, 0.999], invgauss.cdf(vals, mu)) # True # Generate random numbers: r = invgauss.rvs(mu, size=1000) # And compare the histogram: ax.hist(r, density=True, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) plt.show()