def add_bootstrap_plot_full(aeps, aep_qs, skew, mu, sigma, nbs=1, nsamples=50, color="brown", s=25): """ Analyitcal Bootstrap with Empirical Plotting """ f, a = plot_aep_full(aeps, aep_qs, skew, mu, sigma) for i in range(0, nbs): rvs = pearson3.rvs(skew, loc=mu, scale=sigma, size=nsamples) lp3_data = LP3(rvs) log_peaks = lp3_data.log skew_new = lp3_data.weighted_skew(rskew, rskew_mse) mu_new = lp3_data.log.mean() sigma_new = lp3_data.log.std() x = np.linspace( pearson3.ppf(0.0001, skew_new, loc=mu_new, scale=sigma_new), pearson3.ppf(0.9999, skew, loc=mu, scale=sigma), 100) fax2.plot(pearson3.pdf(x, skew_new, loc=mu_new, scale=sigma_new), 10**x, "black", lw=2, alpha=1, label="pearson3 pdf") a.set_title(f"{nbs} Bootstraps", fontsize=20) return f, a
def pearson3(x, a, b, c, d): '''General Pearson Type 3 function, the probability density function (PDF) of Pearson type III distribution. https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearson3.html Parameters: x: independent variable a, b, c, d: parameters for function returns: y: dependent variable ''' y = a * sci_pearson3.pdf(x, skew=b, loc=c, scale=d) return y
def main(): assert exists(SDCIT_RESULT_DIR + '/kcipt_chaotic_5000.csv'), 'run_SDCIT first' assert exists(SDCIT_RESULT_DIR + '/kcipt_chaotic_20000.csv'), 'run_SDCIT first' from experiments.draw_figures import color_palettes, method_color_codes obj_filename = SDCIT_RESULT_DIR + '/right_power.pickle' experiment(obj_filename) time.sleep(3) with open(obj_filename, 'rb') as f: # Python 3: open(..., 'rb') sdcit_mmd, sdcit_null, mmds100, outer_null100, desired_B, mmds_B, outer_null_B, distr_boot = pickle.load(f) print(desired_B) print('SKEW SDCIT NULL: {}'.format(scipy.stats.skew(sdcit_null))) print('SKEW KCIPT NULL: {}'.format(scipy.stats.skew(outer_null_B))) names_kcipt_chaotic = ['independent', 'gamma', 'trial', 'N', 'statistic', 'pvalue', 'B'] names_sdcit_chaotic = ['independent', 'gamma', 'trial', 'N', 'statistic', 'pvalue'] df_kcipt_desired_B = pd.read_csv(SDCIT_RESULT_DIR + '/kcipt_chaotic_{}.csv'.format(desired_B), names=names_kcipt_chaotic, ) df_kcipt_5000 = pd.read_csv(SDCIT_RESULT_DIR + '/kcipt_chaotic_5000.csv', names=names_kcipt_chaotic, ) df_kcipt_20000 = pd.read_csv(SDCIT_RESULT_DIR + '/kcipt_chaotic_20000.csv', names=names_kcipt_chaotic, ) df_sdcit = pd.read_csv(SDCIT_RESULT_DIR + '/sdcit_chaotic.csv', names=names_sdcit_chaotic, ) df_sdcit = df_sdcit[df_sdcit['N'] == 400] df_sdcit = df_sdcit[df_sdcit['independent'] == 1] df_sdcit = df_sdcit[df_sdcit['gamma'] == 0.0] assert len(df_sdcit) == 300 xs_sdcit = np.linspace(1.3 * sdcit_null.min(), 1.3 * sdcit_null.max(), 1000) ys_sdcit_pearson3 = pearson3.pdf(xs_sdcit, *pearson3.fit(sdcit_null)) xs_kcipt = np.linspace(1.3 * outer_null_B.min(), 1.3 * outer_null_B.max(), 1000) ys_kcipt_pearson3 = pearson3.pdf(xs_kcipt, *pearson3.fit(outer_null_B)) # 20000's null is inferred from known one... factor_20000 = np.sqrt(20000 / desired_B) ys_kcipt_20000_gamma = gamma.pdf(xs_kcipt, *gamma.fit(outer_null_B / factor_20000)) sns.set(style='white', font_scale=1.2) paper_rc = {'lines.linewidth': 0.8, 'lines.markersize': 2, 'patch.linewidth': 1} sns.set_context("paper", rc=paper_rc) plt.rc('text', usetex=True) plt.rc('text.latex', preamble=r'\usepackage{cmbright}') if True: fig = plt.figure(figsize=[5, 3.5]) ################################## fig.add_subplot(2, 2, 1, adjustable='box') plt.plot(xs_sdcit, ys_sdcit_pearson3, label='SDCIT null', lw=1.5, color=color_palettes[method_color_codes['SDCIT']]) plt.plot([sdcit_mmd, sdcit_mmd], [0, 1000], label='SDCIT TS', color=color_palettes[method_color_codes['SDCIT']]) plt.plot(xs_kcipt, ys_kcipt_pearson3, label='KCIPT null', lw=1.5, color=color_palettes[method_color_codes['KCIPT']]) sns.distplot(distr_boot, hist=True, kde=False, hist_kws={'histtype': 'stepfilled'}, norm_hist=True, label='KCIPT TS', color=color_palettes[method_color_codes['KCIPT']]) plt.gca().set_xlim([-0.0003, 0.0005]) plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.gca().set_ylabel('density') plt.setp(plt.gca(), 'yticklabels', []) plt.legend(loc=1) ################################## fig.add_subplot(2, 2, 2, adjustable='box') pvals_B = [p_value_of(t, outer_null_B) for t in distr_boot] pval_sdcit = p_value_of(sdcit_mmd, sdcit_null) sns.distplot(pvals_B, bins=20, hist=True, kde=False, hist_kws={'histtype': 'stepfilled'}, norm_hist=True, color=color_palettes[method_color_codes['KCIPT']], label='KCIPT p-values') plt.plot([pval_sdcit, pval_sdcit], [0, 1], label='SDCIT p-value', color=color_palettes[method_color_codes['SDCIT']]) plt.gca().set_ylim([0, 2.2]) plt.gcf().subplots_adjust(wspace=0.3) plt.legend(loc=2) sns.despine() ################################## fig.add_subplot(2, 2, 3, adjustable='box') sns.distplot(df_sdcit['statistic'], hist=True, bins=20, kde=False, color=color_palettes[method_color_codes['SDCIT']], label='SDCIT TS') sns.distplot(df_kcipt_desired_B['statistic'], hist=True, bins=20, kde=False, color=color_palettes[method_color_codes['KCIPT']], label='KCIPT TS') plt.legend() plt.gca().set_xlim([-0.0003, 0.0005]) plt.gca().set_xlabel('MMD') plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.gca().set_ylabel('density') plt.setp(plt.gca(), 'yticklabels', []) ################################## fig.add_subplot(2, 2, 4, adjustable='box') sns.distplot(df_sdcit['pvalue'], hist=True, bins=20, kde=False, color=color_palettes[method_color_codes['SDCIT']], norm_hist=True, label='SDCIT p-values') sns.distplot(df_kcipt_desired_B['pvalue'], hist=True, bins=20, kde=False, color=color_palettes[method_color_codes['KCIPT']], norm_hist=True, label='KCIPT p-values') plt.gca().set_xlabel('p-value') plt.gcf().subplots_adjust(wspace=0.3, hspace=0.3) plt.gca().set_ylim([0, 2.2]) plt.legend(loc=0) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/kcipt_{}_ps.pdf'.format(desired_B), transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() ############################################### ############################################### ############################################### ############################################### ############################################### ############################################### if True: sns.set(style='white', font_scale=1.2) paper_rc = {'lines.linewidth': 0.8, 'lines.markersize': 2, 'patch.linewidth': 1} sns.set_context("paper", rc=paper_rc) plt.rc('text', usetex=True) plt.rc('text.latex', preamble=r'\usepackage{cmbright}') fig = plt.figure(figsize=[5, 1.6]) ################################## fig.add_subplot(1, 2, 1, adjustable='box') sns.distplot(df_kcipt_5000['statistic'], hist=True, bins=20, kde=False, color=color_palettes[method_color_codes['KCIPT']], label='TS') plt.legend() plt.gca().set_xlabel('MMD') plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.gca().set_ylabel('density') plt.gca().set_xlim([-0.0002, 0.0003]) plt.setp(plt.gca(), 'yticklabels', []) ## fig.add_subplot(1, 2, 2, adjustable='box') sns.distplot(df_kcipt_5000['pvalue'], hist=True, bins=20, kde=False, color=color_palettes[method_color_codes['KCIPT']], norm_hist=True, label='p-value') plt.gca().set_xlabel('p-value') plt.gcf().subplots_adjust(wspace=0.3, hspace=0.3) plt.legend(loc=0) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/kcipt_5000_ps.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close() if True: sns.set(style='white', font_scale=1.2) paper_rc = {'lines.linewidth': 0.8, 'lines.markersize': 2, 'patch.linewidth': 1} sns.set_context("paper", rc=paper_rc) plt.rc('text', usetex=True) plt.rc('text.latex', preamble=r'\usepackage{cmbright}') fig = plt.figure(figsize=[5, 1.6]) # left subplot fig.add_subplot(1, 2, 1, adjustable='box') plt.plot(xs_sdcit, ys_sdcit_pearson3, label='SDCIT null', lw=1.5, color=color_palettes[method_color_codes['SDCIT']]) plt.plot(xs_kcipt, ys_kcipt_20000_gamma, label='KCIPT null', lw=1.5, color=color_palettes[method_color_codes['KCIPT']]) sns.distplot(df_kcipt_20000['statistic'], hist=True, bins=20, kde=False, norm_hist=True, color=color_palettes[method_color_codes['KCIPT']], label='KCIPT TS') plt.legend(loc=1) plt.gca().set_xlabel('MMD') plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.gca().set_ylabel('density') plt.gca().set_xlim([-0.0002, 0.0003]) plt.setp(plt.gca(), 'yticklabels', []) # right subplot fig.add_subplot(1, 2, 2, adjustable='box') sns.distplot(df_kcipt_20000['pvalue'], hist=True, bins=20, kde=False, color=color_palettes[method_color_codes['KCIPT']], norm_hist=True, label='KCIPT p') sns.distplot([p_value_of(ss, sdcit_null) for ss in df_kcipt_20000['statistic']], hist=True, bins=20, kde=False, color='k', norm_hist=True, label='KCIPT p on SDCIT null') plt.gca().set_xlabel('p-value') plt.gcf().subplots_adjust(wspace=0.3, hspace=0.3) plt.legend(loc=0) sns.despine() plt.savefig(SDCIT_FIGURE_DIR + '/kcipt_20000_ps.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02) plt.close()
from scipy.stats import pearson3 import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: skew = 0.1 mean, var, skew, kurt = pearson3.stats(skew, moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(pearson3.ppf(0.01, skew), pearson3.ppf(0.99, skew), 100) ax.plot(x, pearson3.pdf(x, skew), 'r-', lw=5, alpha=0.6, label='pearson3 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 = pearson3(skew) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = pearson3.ppf([0.001, 0.5, 0.999], skew) np.allclose([0.001, 0.5, 0.999], pearson3.cdf(vals, skew)) # True # Generate random numbers:
def plot_aep_full(aeps, aep_qs, skew, mu, sigma, color="Brown", ci=95): # PDF fig, ax = plt.subplots(figsize=(30, 8)) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) gs = gridspec.GridSpec(ncols=6, nrows=1, figure=fig) fax1 = fig.add_subplot(gs[0:5]) fax2 = fig.add_subplot(gs[5], sharey=fax1) fax2.axes.yaxis.set_visible(False) fax2.axes.xaxis.set_visible(False) x = np.linspace(pearson3.ppf(0.0001, skew, loc=mu, scale=sigma), pearson3.ppf(0.9999, skew, loc=mu, scale=sigma), 100) fax2.plot(pearson3.pdf(x, skew, loc=mu, scale=sigma), 10**x, "black", lw=2, alpha=1, label="pearson3 pdf") density_range = pearson3.pdf(x, skew, loc=mu, scale=sigma) # ----------------------------------------------------------------------------------------------------------------------------------------------------- # fax2.fill_between(, 0, 100, color='black', alpha = 0.2) # fax2.fill_between(density_range, q90lower, q90upper, color='gray', alpha = 0.2) # fax2.fill_between(density_range, q90lower, pdfmin, color='lightgray', alpha = 0.2) fax2.set_xlim([0, np.max(density_range)]) fax1.set_ylim([20000, 1200000]) fax2.set_yscale("log") pdfmin = 10**x[0] pdfmax = 10**x[-1] # adjust the line for to match the extent of peakfq plots fax1.plot(aeps[1:-10], aep_qs[1:-10], "black", linewidth=2, alpha=0.8) # Plot Formatting # fax1.invert_xaxis() fax1.set_xscale("logit") # fax1.xaxis.set_major_formatter(ticker.FuncFormatter(myLogitFormat)) fax1.set_yscale("log") fax1.set_xlim([0.990, 0.002]) fax1.set_ylim([20000, 1200000]) # fax1.set_xlabel('Annual exceedance probability, [%]');fax1.set_ylabel('Discharge, [cfs]') fax1.set_title("Flow Frequency Curve") fax1.grid(True, which="both") fax1.xaxis.set_minor_formatter(ticker.FuncFormatter(logit_plot_format)) fax1.yaxis.set_major_formatter(ticker.FuncFormatter(log_plot_format)) fax1.set_xlabel("Annual Exceendence Probability") fax1.set_ylabel("Flow (cfs)") # fax1.axhline(pdfmax, 0, 1e9, color='purple'); upper_ci, lower_ci = ci * 0.01, (100 - ci) * 0.01 qupper = int(10**pearson3.ppf(upper_ci, skew, loc=mu, scale=sigma)) qlower = int(10**pearson3.ppf(lower_ci, skew, loc=mu, scale=sigma)) fax1.fill_between(np.arange(0.990, 0.002, -0.001), pdfmax, qupper, color="gray", alpha=0.2) fax1.fill_between(np.arange(0.990, 0.002, -0.001), qlower, qupper, color="black", alpha=0.2) fax1.fill_between(np.arange(0.990, 0.002, -0.001), qlower, pdfmin, color="gray", alpha=0.2) fax2.grid() fig.tight_layout(pad=1.00, h_pad=0, w_pad=-1, rect=None) return fig, fax1