def test_parameter_estimation(self): X = np.random.uniform(0, 4, 1000) y = X + np.random.normal(0, 1, 1000) m = BayesianBootstrapBagging(LinearRegression(), 10000, 1000, low_mem=False) m.fit(X.reshape(-1, 1), y) coef_samples = [b.coef_ for b in m.base_models_] intercept_samples = [b.intercept_ for b in m.base_models_] self.assertAlmostEqual(np.mean(coef_samples), 1, delta=0.3) l, r = central_credible_interval(coef_samples, alpha=0.05) self.assertLess(l, 1) self.assertGreater(r, 1) l, r = highest_density_interval(coef_samples, alpha=0.05) self.assertLess(l, 1) self.assertGreater(r, 1) self.assertAlmostEqual(np.mean(intercept_samples), 0, delta=0.3) l, r = central_credible_interval(intercept_samples, alpha=0.05) self.assertLess(l, 0) self.assertGreater(r, 0) self.assertAlmostEqual(np.mean(intercept_samples), 0, delta=0.3) l, r = highest_density_interval(intercept_samples, alpha=0.05) self.assertLess(l, 0) self.assertGreater(r, 0)
def plot_regression_wrapper_bootstrap(): X = np.array([[0], [1], [2], [3]]) y = np.array([0, 1, 2, 3]) + np.random.normal(0, 1, 4) m = BayesianBootstrapBagging(LinearRegression(), 10000, 1000) m.fit(X, y) y_predicted = m.predict(X) y_predicted_interval = m.predict_central_interval(X, 0.05) plt.scatter(X.reshape(-1, 1), y) plt.plot(X.reshape(-1, 1), y_predicted) plt.plot(X.reshape(-1, 1), y_predicted_interval[:,0]) plt.plot(X.reshape(-1, 1), y_predicted_interval[:,1]) plt.show()
def plot_regression_slope_distribution_readme(): X = np.random.normal(0, 1, 5).reshape(-1, 1) y = X.reshape(1, -1).reshape(5) + np.random.normal(0, 1, 5) m = BayesianBootstrapBagging(LinearRegression(), 10000, 1000) m.fit(X, y) X_plot = np.linspace(min(X), max(X)) y_predicted = m.predict(X_plot.reshape(-1, 1)) y_predicted_interval = m.predict_highest_density_interval(X_plot.reshape(-1, 1), 0.05) plt.scatter(X.reshape(1, -1), y) plt.plot(X_plot, y_predicted, label='Mean') plt.plot(X_plot, y_predicted_interval[:,0], label='95% HDI Lower bound') plt.plot(X_plot, y_predicted_interval[:,1], label='95% HDI Upper bound') plt.legend() plt.savefig('readme_regression.png', bbox_inches='tight')
from bayesian_bootstrap.bootstrap import mean, highest_density_interval, BayesianBootstrapBagging posterior_samples = mean(X, 10000) l, r = highest_density_interval(posterior_samples) plt.title('Bayesian Bootstrap of mean') sns.distplot(posterior_samples, label='Bayesian Bootstrap Samples') plt.plot([l, r], [0, 0], linewidth=5.0, marker='o', label='95% HDI') # - from bayesian_bootstrap.bootstrap import bayesian_bootstrap posterior_samples = bayesian_bootstrap(X, np.mean, 10000, 100) X = np.random.normal(0, 1, 5).reshape(-1, 1) y = X.reshape(1, -1).reshape(5) + np.random.normal(0, 1, 5) m = BayesianBootstrapBagging(LinearRegression(), 10000, 1000) m.fit(X, y) import utils p_pre, p_post = utils.load_users_covid('politicians') rfriends_pre, rfriends_post = utils.load_users_covid('random-friends') rfollowers_pre, rfollowers_post = utils.load_users_covid('random-followers') # j_pre, j_post = utils.load_users_covid('journalists-new') rfriends_big_pre, rfriends_big_post = utils.load_users_covid('random-friends-big') rfollowers_big_pre, rfollowers_big_post = utils.load_users_covid('random-followers-big') rfollowers_big_post.to_pickle('random-followers-big_post.pkl') rfollowers_big_pre.to_pickle('random-followers-big_pre.pkl')