Instantly share code, notes, and snippets.

# Tobias-K93/helpers.py Last active Aug 17, 2019

APA interactive plots
 import numpy as np def standardize(gamma, upper_val = 0.3, lower_val = 0.1): s = (gamma-np.min(gamma))/(np.max(gamma)-np.min(gamma)) out = s * (upper_val - lower_val) + lower_val return out def is_pos_def(x): return np.all(np.linalg.eigvals(x) > 0) def adjusting_assignment_level(level): adjustment_dict = {'low' : -0.545, 'medium' : 0, 'high' : 0.545} return adjustment_dict[level] def revert_string_prob(string): reverse_dict = {'low': 0.35, 'medium': 0.5, 'high': 0.65} return reverse_dict[string] # functions describing relation between X and y (g_0(X)) def linear_simple(self): return np.dot(self.X,self.weights_covariates_to_outputs) def linear_interaction(self): return np.dot(self.X, self.weights_covariates_to_outputs) \ + np.dot(self.X_interaction, self.weights_interaction) def partial_nonlinear_simple(self): return 2.5*np.cos(np.dot(self.X,self.weights_covariates_to_outputs))**3 \ + 2.5*0.2*np.dot(self.X,self.weights_covariates_to_outputs) def partial_nonlinear_interaction(self): return 2.5*np.cos(np.dot(self.X,self.weights_covariates_to_outputs) + \ np.dot(self.X_interaction, self.weights_interaction))**3 \ + 2.5*0.2*(np.dot(self.X,self.weights_covariates_to_outputs) + np.dot(self.X_interaction, self.weights_interaction)) def nonlinear_simple(self): return 3*np.cos(np.dot(self.X,self.weights_covariates_to_outputs))**3 def nonlinear_interaction(self): return 3*np.cos(np.dot(self.X,self.weights_covariates_to_outputs) + np.dot(self.X_interaction, self.weights_interaction))**3 relation_dict = {'linear_simple': linear_simple, 'linear_interaction': linear_interaction, 'partial_nonlinear_simple': partial_nonlinear_simple, 'partial_nonlinear_interaction': partial_nonlinear_interaction, 'nonlinear_simple': nonlinear_simple, 'nonlinear_interaction': nonlinear_interaction} # function to call in main class def relation_fct(self, x_y_relation): function = relation_dict.get(x_y_relation) return function(self)
 import matplotlib.pyplot as plt import seaborn as sns def output_difference_plt(y_treated_continuous, y_not_treated_continuous, y_treated_binary, y_not_treated_binary): fig, axes = plt.subplots(1,2, figsize=(10,4)) axes.set_title('Continuous Output distributions') axes.set_title('Binary output distributions') axes.set_xlabel('y') axes.set_xlabel('y') axes.set_ylabel('Density') axes.set_ylabel('Density') sns.distplot(y_not_treated_continuous, hist=False, kde=True, ax = axes, kde_kws={'linewidth': 4, 'color' : 'darkblue'}, label = 'y not treated') sns.distplot(y_treated_continuous, hist=False, kde=True, ax = axes, kde_kws={'linewidth': 4, 'color' : 'darkred'}, label = 'y treated') sns.distplot(y_not_treated_binary, hist=False, kde=True, ax = axes, kde_kws={'linewidth': 4, 'color' : 'darkblue'}, label = 'y not treated') sns.distplot(y_treated_binary, hist=False, kde=True, ax = axes, kde_kws={'linewidth': 4, 'color' : 'darkred'}, label = 'y treated') sns.despine(right=True, top=True) plt.setp(axes, xticks=[0,1]) plt.tight_layout() def x_y_relation_plot(y,g_0_X): fig, axes = plt.subplots(1,1, figsize=(6,5)) axes.set_title('y ~ X relation') axes.set_xlabel('X * b') axes.set_ylabel('y') axes.set_ylim([-7,7]) axes.set_xlim([-5,5]) sns.scatterplot(g_0_X, y, ax=axes, color='darkblue', s=18) sns.despine(left=False, right=True, top=True) plt.tight_layout(rect=[0, 0.03, 1, 0.95])  