def plot_sim_costs(costs, savefig=None): title = 'Program Outlay Validation Results' fig, ax = plt.subplots(figsize=(8, 6), dpi=100) ys = costs['ri'] zs = costs['nj'] ws = costs['ca'] dct_color = dict(zip(sts, ['indianred', 'slategray', 'tan'])) ind = np.arange(len(ys)) width = 0.2 bar1 = ax.bar(ind-width, ys, width, align='center', capsize=5, color=dct_color['ri'], ecolor='grey') bar2 = ax.bar(ind, zs, width, align='center', capsize=5, color=dct_color['nj'], ecolor='grey') bar3 = ax.bar(ind+width, ws, width, align='center', capsize=5, color=dct_color['ca'], ecolor='grey') ax.set_ylabel('Program Outlay') ax.set_xticks(ind) ax.set_xticklabels(('Logit', 'KNN', 'Naive Bayes', 'Random Forest', 'XGB', 'Ridge', 'SVC')) ax.yaxis.grid(False) ax.legend((bar1, bar2, bar3), ('Rhode Island','New Jersey', 'California' )) ax.ticklabel_format(style='plain', axis='y') # add horizontal bar for true numbers dct_cost = dict(zip(sts, [166.7, 502.2, 5681.7])) dct_offset = dict(zip(sts, [1.025, 1.025, 0.975])) for st in sts: y = dct_cost[st] plt.axhline(y=y, color=dct_color[st], linestyle='--') hline_offset = dct_offset[st] hline_text = 'Actual Program Outlay, %s: %s million' % (st.upper(), y) plt.text(2, y * hline_offset, hline_text, horizontalalignment='center', color='k') format_chart(fig, ax, title, bg_color='white', fg_color='k') if savefig is not None: dir_out = savefig # save plt.savefig(dir_out + 'program_outlay.png', facecolor='white', edgecolor='grey') # return None
def plot_ind_level(di, clf_class_names_plot, clf_profile, yvar, savefig=None): # get clfs with cv results (ready for plot), dct_clf, and clf_class_names from clf_profile clfs, clf_class_names_plot, dct_clf = clf_profile # get clf class names, and corresponding labels for plot, must align for proper plotting clf_class_names_plot = [type(z).__name__ for z in clfs] clf_labels_plot = tuple([dct_clf[x] for x in clf_class_names_plot]) # make plot title = 'Individual Level Validation Results - Performance Measures\nOutcome = %s' % yvar fig, ax = plt.subplots(figsize=(8, 6), dpi=100) di = di[clf_class_names_plot] # order to ensure ys = [x[yvar] for x in di.loc['precision'].values] zs = [x[yvar] for x in di.loc['recall'].values] ws = [x[yvar] for x in di.loc['f1'].values] ind = np.arange(len(ys)) width = 0.2 bar1 = ax.bar(ind - width, ys, width, align='center', capsize=5, color='indianred', ecolor='grey') bar2 = ax.bar(ind, zs, width, align='center', capsize=5, color='tan', ecolor='grey') bar3 = ax.bar(ind + width, ws, width, align='center', capsize=5, color='slategray', ecolor='grey') ax.set_ylabel('Performance Measure') ax.set_xticks(ind) ax.set_xticklabels(clf_labels_plot) ax.yaxis.grid(False) ax.legend((bar1, bar2, bar3), ('Precision', 'Recall', 'F1')) ax.ticklabel_format(style='plain', axis='y') # ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ','))) format_chart(fig, ax, title, bg_color='white', fg_color='k') if savefig is not None: dir_out, suffix = savefig # save plt.savefig(dir_out + 'ind_level%s.png' % suffix, facecolor='white', edgecolor='grey') # return None
def plot_pop_level_worker_counts(dp, clf_class_names_plot, clf_profile, savefig=None): ''' :param: clf_class_names: col of dp except 'true', numbers will follow this order in plot ''' # get clfs with cv results (ready for plot), dct_clf, and clf_class_names from clf_profile clfs, clf_class_names_plot, dct_clf = clf_profile # get clf class names, and corresponding labels for plot, must align for proper plotting clf_class_names_plot = [type(z).__name__ for z in clfs] clf_labels_plot = tuple([dct_clf[x] for x in clf_class_names_plot]) # make plot title = 'Population Level Validation Results - Worker Counts' fig, ax = plt.subplots(figsize=(8, 6), dpi=100) dp = dp[clf_class_names_plot + ['true']] ys = dp.drop(columns=['true']).loc['n_takers'].values zs = dp.drop(columns=['true']).loc['n_needers'].values ind = np.arange(len(ys)) width = 0.2 bar1 = ax.bar(ind - width / 2, ys, width, align='center', capsize=5, color='indianred', ecolor='grey') bar2 = ax.bar(ind + width / 2, zs, width, align='center', capsize=5, color='tan', ecolor='grey') ax.set_ylabel('Millions of workers') ax.set_xticks(ind) ax.set_xticklabels(clf_labels_plot) ax.yaxis.grid(False) ax.legend((bar1, bar2), ('Leave Takers', 'Leave Needers')) ax.ticklabel_format(style='plain', axis='y') ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ','))) format_chart(fig, ax, title, bg_color='white', fg_color='k') # add horizontal bar for true numbers n_takers_true = dp['true']['n_takers'] plt.axhline(y=n_takers_true, color='indianred', linestyle='--') hline_offset = 1.025 hline_text = 'Actual Number of Takers: %s million' % (round(n_takers_true, 1)) plt.text(2, n_takers_true * hline_offset, hline_text, horizontalalignment='center', color='k') n_needers_true = dp['true']['n_needers'] plt.axhline(y=n_needers_true, color='tan', linestyle='--') hline_offset = 1.025 hline_text = 'Actual Number of Needers: %s million' % (round(n_needers_true, 1)) plt.text(2, n_needers_true * hline_offset, hline_text, horizontalalignment='center', color='k') if savefig is not None: dir_out, suffix = savefig # save plt.savefig(dir_out + 'pop_level_workers%s.png' % suffix, facecolor='white', edgecolor='grey') # return None
def create_chart(self, out, sim_num): output_directory = self.output_directories[sim_num] # Plot costs and ci total_cost = round(list(out.loc[out['type'] == 'total', 'cost'])[0] / 10 ** 6, 1) spread = round((list(out.loc[out['type'] == 'total', 'ci_upper'])[0] - list(out.loc[out['type'] == 'total', 'ci_lower'])[0]) / 10 ** 6, 1) title = 'State: %s. Total Benefits Cost = $%s million (\u00B1%s).' % (self.st.upper(), total_cost, spread) fig, ax = plt.subplots(figsize=(8, 6), dpi=100) ind = np.arange(6) ys = out[:-1]['cost'] / 10 ** 6 es = 0.5 * (out[:-1]['ci_upper'] - out[:-1]['ci_lower']) / 10 ** 6 width = 0.5 ax.bar(ind, ys, width, yerr=es, align='center', capsize=5, color='#1aff8c', ecolor='white') ax.set_ylabel('$ millions') ax.set_xticks(ind) ax.set_xticklabels(('Own Health', 'Maternity', 'New Child', 'Ill Child', 'Ill Spouse', 'Ill Parent')) ax.yaxis.grid(False) format_chart(fig, ax, title) plt.savefig('%s/total_cost_%s_%s_%s' % (output_directory, self.yr, self.st, self.out_id), facecolor='#333333', edgecolor='white') self.figure = fig return fig
'New Born Child', 'Ill Child', 'Ill Spouse', 'Ill Parent', )) ax.yaxis.grid(False) ax.legend(( bar1, bar2, ), ( 'Simulated', 'Actual', )) ax.ticklabel_format(style='plain', axis='y') #ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ','))) format_chart(fig, ax, title, bg_color='white', fg_color='k') # save plt.savefig(fp_out + 'RI_validation_n_cases.png', facecolor='white', edgecolor='grey') # # plot for outlay fp_out = './output/' title = '' # 'Comparison of Simulation Results vs Program Data, Program Outlay in RI' fig, ax = plt.subplots(figsize=(8, 6), dpi=100) ys = list(sim['outlay'].values())[-3:] zs = list(actual['outlay'].values())[-3:] ind = np.arange(len(ys)) width = 0.2 bar1 = ax.bar(ind - width / 2, ys,
ind = np.arange(len(types)) ys = dct_bene.values() width = 0.5 ax.bar(ind, ys, width, align='center', capsize=5, color='#1aff8c', ecolor='white') ax.set_ylabel('Number of workers') ax.set_xticks(ind) ax.set_xticklabels(('Own Health', 'Maternity', 'New Child', 'Ill Child', 'Ill Spouse', 'Ill Parent')) ax.yaxis.grid(False) format_chart(fig, ax, title) plt.savefig('./draft/demo_external_20191213/MD_RI_rf_low_inc_bene_counts.png', facecolor='#333333', edgecolor='white') # ## How much benefit would go to low-income worker families? # set annual benefit = 0 for missing amount for t in types: acs.loc[acs['annual_benefit_%s' % t].isna(), 'annual_benefit_%s' % t] = 0 acs['annual_benefit_all'] = [ x.sum() for x in acs[['annual_benefit_%s' % t for t in types]].values ] ## Plot # Number of benes, by benefit amount title = 'Number of low-income worker recipients, by benefit levels' fig, ax = plt.subplots(figsize=(8, 6), dpi=100)