def plot_bootstrap(self, fig=None): """Plot bootstrapped distribution.""" lower95CI, upper95CI = self.bootstrapped_CIs(alpha=0.05) if fig is None: fig, ax = plt.subplots(1, 1) #, figsize=(4,4)) else: fig.clf() ax = fig.add_subplot(1, 1, 1) ax.hist(self.boot, 20, density=True) xmin = self.bootstrap_mean - 4 * np.std(self.boot, ddof=1) xmax = self.bootstrap_mean + 4 * np.std(self.boot, ddof=1) increase = (xmax - xmin) / 100 x = np.arange(xmin, xmax, increase) pdf = [ bs._pdf(x1, self.bootstrap_mean, np.std(self.boot, ddof=1)) for x1 in x ] ax.plot(x, pdf, 'k', label='normal pdf') ax.axvline(x=self.ratio, color='r', label='observed ratio') ax.axvline(x=self.bootstrap_mean, color='r', linestyle="dashed", label='bootstrapped ratio') ax.axvline(x=lower95CI, color='k', linestyle="dashed", label='2.5% limits') ax.axvline(x=upper95CI, color='k', linestyle="dashed") ax.set_ylabel("Frequency") ax.set_xlabel('Ratio (mean A / mean B)') ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, borderaxespad=0.) plt.tight_layout() return fig
def plot_rantest(self, fig=None): """Plot randomisation distribution.""" if fig is None: fig, ax = plt.subplots(1, 1) #, figsize=(4,4)) else: fig.clf() ax = fig.add_subplot(1, 1, 1) ax.hist(self.randiff, 20, density=True) mu = np.mean(self.randiff) sd = np.std(self.randiff, ddof=1) xmin = mu - 4 * sd xmax = mu + 4 * sd increase = (xmax - xmin) / 100 x = np.arange(xmin, xmax, increase) pdf = [bs._pdf(x1, mu, sd) for x1 in x] ax.plot(x, pdf, 'k', label='normal pdf') ax.axvline(x=self.dbar, color='r', label='observed difference') ax.axvline(x=-self.dbar, color='r') ax.axvline(x=self.lo95lim, color='k', linestyle='--', label='2.5% limits') ax.axvline(x=self.hi95lim, color='k', linestyle='--') ax.set_xlabel('difference between means') ax.set_ylabel('frequency') ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, borderaxespad=0.) plt.tight_layout() return fig
def plot_bootstrapped_distributions(self, fig=None): sample1 = Sample(self.A) sample1.run_bootstrap(self.runs) lower95CI1, upper95CI1 = sample1.bootstrapped_CIs(alpha=0.05) sample2 = Sample(self.B) sample2.run_bootstrap(self.runs) lower95CI2, upper95CI2 = sample2.bootstrapped_CIs(alpha=0.05) if fig is None: fig = plt.Figure() else: fig.clf() ax1 = fig.add_subplot(1, 2, 1) ax1.hist(sample1.boot, bins=20, density=True) mu = np.mean(sample1.boot) sd = np.std(sample1.boot, ddof=1) xmin = mu - 4 * sd xmax = mu + 4 * sd increase = (xmax - xmin) / 100 x = np.arange(xmin, xmax, increase) pdf = [bs._pdf(x1, mu, sd) for x1 in x] ax1.plot(x, pdf, 'k', label='normal pdf') ax1.axvline(x=sample1.meanA, color='r', label='observed mean') ax1.axvline(x=sample1.bootstrap_mean, color='r', linestyle="dashed", label='bootstrapped mean') ax1.axvline(x=lower95CI1, color='k', linestyle="dashed", label='2.5% limits') ax1.axvline(x=upper95CI1, color='k', linestyle="dashed") ax1.set_ylabel("Frequency") ax1.set_xlabel('Mean') ax1.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, borderaxespad=0.) ax2 = fig.add_subplot(1, 2, 2) ax2.hist(sample2.boot, bins=20, density=True) mu = np.mean(sample2.boot) sd = np.std(sample2.boot, ddof=1) xmin = mu - 4 * sd xmax = mu + 4 * sd increase = (xmax - xmin) / 100 x = np.arange(xmin, xmax, increase) pdf = [bs._pdf(x1, mu, sd) for x1 in x] ax2.plot(x, pdf, 'k', label='normal pdf') ax2.axvline(x=sample2.meanA, color='r', label='observed mean') ax2.axvline(x=sample2.bootstrap_mean, color='r', linestyle="dashed", label='bootstrapped mean') ax2.axvline(x=lower95CI2, color='k', linestyle="dashed", label='2.5% limits') ax2.axvline(x=upper95CI2, color='k', linestyle="dashed") ax2.set_ylabel("Frequency") ax2.set_xlabel('Mean') ax2.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, borderaxespad=0.) plt.tight_layout() return fig