print generator for rep in range(params['num_reps']): subnet = generators[generator].sample(as_network = True) subnet.offset_extremes() results_by_method[generator].record(size, rep, subnet) # Output results print for method_name in params['sampling_methods']: print method_name results = results_by_method[method_name] results.summary() if params['plot_network']: results.plot([('Density', {'ymin': 0, 'plot_mean': True}), (['Out-degree', 'Max row-sum', 'Min row-sum'], {'ymin': 0, 'plot_mean': True}), (['In-degree', 'Max col-sum', 'Min col-sum'], {'ymin': 0, 'plot_mean': True}), ('Self-loop density', {'ymin': 0, 'plot_mean': True}), ('# Active', {'ymin': 0 })]) print # Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field]))
'baseline': 1 }), ('Class mismatch', { 'ymin': 0, 'ymax': 2 })] if model == 'n': to_plot.pop(2) results.plot(to_plot) # Plot network statistics as well as sparsity parameter if params['plot_network']: s_results.title = None s_results.plot([('Average out-degree', { 'ymin': 0, 'plot_mean': True }), ('Average in-degree', { 'ymin': 0, 'plot_mean': True }), (['Out-degree', 'Max out-degree', 'Min out-degree'], { 'ymin': 0, 'plot_mean': True }), (['In-degree', 'Max out-degree', 'Min in-degree'], { 'ymin': 0, 'plot_mean': True }), ('Self-loop density', { 'ymin': 0, 'plot_mean': True }), 'Subnetwork kappa'])
}), ('Rel. MSE(logit_P)', { 'ymin': 0, 'ymax': 2, 'baseline': 1 }), ('Class mismatch', { 'ymin': 0, 'ymax': 2 })] if model == 'n': to_plot.pop(2) results.plot(to_plot) # Plot network statistics as well as sparsity parameter if params['plot_network']: s_results.title = None s_results.plot([('Average row-sum', { 'ymin': 0, 'plot_mean': True }), ('Average col-sum', { 'ymin': 0, 'plot_mean': True }), (['row-sum', 'Max row-sum', 'Min row-sum'], { 'ymin': 0, 'plot_mean': True }), (['col-sum', 'Max row-sum', 'Min col-sum'], { 'ymin': 0, 'plot_mean': True }), 'Subnetwork kappa'])
to_plot = [] if not params['fit_method'] == 'none': to_plot.append((['MSE(theta_i)'] + covariate_mses, {'ymin': 0, 'ymax': 0.5, 'plot_mean': True})) if params['baseline']: to_plot.append(('Rel. MSE(P_ij)', {'ymin': 0, 'ymax': 2, 'baseline': 1})) to_plot.append(('Rel. MSE(logit P_ij)', {'ymin':0, 'ymax': 2, 'baseline': 1})) to_plot.append(('# Active', {'ymin': 0})) to_plot.append(('Separated', {'ymin': 0, 'ymax': 1, 'plot_mean': True})) if params['fisher_information']: to_plot.append((['Info theta_i'] + \ ['Info theta_{%s}' % b for b in fit_model.beta], {'ymin': 0, 'plot_mean': True})) results.plot(to_plot) to_plot = [] to_plot.append((['MSE(theta_i)'] + covariate_mses, {'loglog': True, 'plot_mean': True})) if params['fisher_information']: to_plot.append((['Info theta_i'] + \ ['Info theta_{%s}' % b for b in fit_model.beta], {'plot_mean': True, 'loglog': True})) results.plot(to_plot) results.plot([(['MAE(theta_i)'] + covariate_maes, {'loglog': True, 'plot_mean': True})]) # Plot network statistics if params['plot_network']:
# Compute MSEs covariate_mses = [] for c in covariates: name = 'MSE(beta_{%s})' % c covariate_mses.append(name) results.estimate_mse(name, 'True beta_{%s}' % c, 'Estimated beta_{%s}' % c) # Plot inference performace, in terms of MSE(beta) and MSE(P_ij) if params['plot_mse']: results.plot([(['MSE(beta_i)'] + covariate_mses, { 'ymin': 0, 'ymax': 3.0, 'plot_mean': True }), ('MSE(P_{ij})', { 'ymin': 0, 'ymax': 1 }), ('MSE(logit_P_{ij})', { 'ymin': 0, 'ymax': 5 })]) # Plot network statistics as well as sparsity parameter if params['plot_network']: results.plot([('Density', { 'ymin': 0, 'plot_mean': True }), (['Row-sum', 'Max row-sum', 'Min row-sum'], { 'ymin': 0, 'plot_mean': True
fit_model.ignore_inner_offset = True fit_model.fit(subnet, params['cycles'], params['sweeps'], T = 10) i_results.record(size, rep, subnet, fit_model = fit_model) print 'I: ', fit_model.Theta print if params['fit_nonstationary']: initialize(subnet, n_fit_model, offset_extremes = True) fit_model.ignore_inner_offset = False n_fit_model.fit(subnet, params['cycles'], params['sweeps']) n_results.record(size, rep, subnet, fit_model = n_fit_model) print 'NS: ', n_fit_model.Theta print # Plot inferred class disagreement and report results for model in all_results: results = all_results[model] print results.title results.summary() print results.plot([('Class mismatch', {'ymin': 0.0, 'ymax': 1.0})]) # Plot network statistics as well as sparsity parameter if params['plot_network']: s_results.title = 'Network statistics' s_results.plot([('Density', {'ymin': 0, 'plot_mean': True}), (['Out-degree', 'Max row-sum', 'Min row-sum'], {'ymin': 0, 'plot_mean': True}), (['In-degree', 'Max col-sum', 'Min col-sum'], {'ymin': 0, 'plot_mean': True})])
fit_model.fit_conditional(subnet, verbose = True) c_results.record(sub_size, rep, subnet, fit_model = fit_model) print if params['fit_conditional_is']: fit_model.fit_conditional(subnet, T = 50, verbose = True) i_results.record(sub_size, rep, subnet, fit_model = fit_model) print if params['fit_nonstationary']: subnet.offset_extremes() n_fit_model.fit_convex_opt(subnet, verbose = True) n_results.record(sub_size, rep, subnet, fit_model = n_fit_model) print for model in all_results: results = all_results[model] results.plot(['%s' % c for c in covariates]) # Plot network statistics as well as sparsity parameter if params['plot_network']: result = all_results[all_results.keys()[0]] result.title = None result.plot([('Average out-degree', {'ymin': 0, 'plot_mean': True}), ('Average in-degree', {'ymin': 0, 'plot_mean': True}), (['Out-degree', 'Max out-degree', 'Min out-degree'], {'ymin': 0, 'plot_mean': True}), (['In-degree', 'Max in-degree', 'Min in-degree'], {'ymin': 0, 'plot_mean': True}), ('Self-loop density', {'ymin': 0, 'plot_mean': True})])
# Determine margins according to specified scaling and subnetwork size scaling, value = params['margin_scaling'] if scaling == 'degree': r, c = np.repeat(value, sub_size), np.repeat(value, sub_size) elif scaling == 'density': m = int(value * sub_size) r, c = np.repeat(m, sub_size), np.repeat(m, sub_size) subnet.row_covariates['r'][:] = r subnet.col_covariates['c'][:] = c for gibbs_cover in params['gibbs_covers']: data_model.coverage = gibbs_cover subnet.generate(data_model, arbitrary_init = params['arbitrary_init']) subnet.offset_extremes() fit_model.fit_conditional(subnet) gibbs_results[gibbs_cover].record(size, rep, subnet, data_model, fit_model) # Compute beta MSEs and plot performance in terms of MSE(beta) for sub, gibbs_cover in enumerate(sorted(gibbs_results)): results = gibbs_results[gibbs_cover] covariate_mses = [] for c in covariates: name = 'MSE(beta_{%s})' % c covariate_mses.append(name) results.estimate_mse(name, 'True beta_{%s}' % c, 'Est. beta_{%s}' % c) results.plot([(['MSE(beta_i)'] + covariate_mses, {'ymin': 0, 'ymax': 3.0, 'plot_mean': True})])
subnet = gen.sample() data_model.match_kappa(subnet, ('density', target_dens)) subnet.generate(data_model) fit_model.fit(subnet) results.record(size, rep, subnet, data_model, fit_model) # Compute MSEs covariate_mses = [] for c in covariates: name = 'MSE(beta_{%s})' % c covariate_mses.append(name) results.estimate_mse(name, 'True beta_{%s}' % c, 'Estimated beta_{%s}' % c) # Plot inference performace, in terms of MSE(beta) and MSE(P_ij) if params['plot_mse']: results.plot([(['MSE(beta_i)'] + covariate_mses, {'ymin': 0, 'ymax': 3.0, 'plot_mean': True}), ('MSE(P_{ij})', {'ymin': 0, 'ymax': 1}), ('MSE(logit_P_{ij})', {'ymin': 0, 'ymax': 5})]) # Plot network statistics as well as sparsity parameter if params['plot_network']: results.plot([('Density', {'ymin': 0, 'plot_mean': True}), (['Row-sum', 'Max row-sum', 'Min row-sum'], {'ymin': 0, 'plot_mean': True}), (['Col-sum', 'Max col-sum', 'Min col-sum'], {'ymin': 0, 'plot_mean': True})])
results = all_results[model] print results.title results.summary() print # Plot inference performace, in terms of MSE(beta), MSE(P_ij), and # inferred class disagreement; also plot kappas chosen for data models if params['plot_mse']: for model in all_results: results = all_results[model] to_plot = [(['MSE(beta_i)'] + covariate_mses, {'ymin': 0, 'ymax': 0.5, 'plot_mean': True}), ('Rel. MSE(P)', {'ymin': 0, 'ymax': 2, 'baseline': 1}), ('Rel. MSE(logit_P)', {'ymin': 0, 'ymax': 2, 'baseline': 1}), ('Class mismatch', {'ymin': 0, 'ymax': 2})] if model == 'n': to_plot.pop(2) results.plot(to_plot) # Plot network statistics as well as sparsity parameter if params['plot_network']: s_results.title = None s_results.plot([('Average out-degree', {'ymin': 0, 'plot_mean': True}), ('Average in-degree', {'ymin': 0, 'plot_mean': True}), (['Out-degree', 'Max out-degree', 'Min out-degree'], {'ymin': 0, 'plot_mean': True}), (['In-degree', 'Max out-degree', 'Min in-degree'], {'ymin': 0, 'plot_mean': True}), ('Self-loop density', {'ymin': 0, 'plot_mean': True}), 'Subnetwork kappa'])
print for method_name in params['sampling_methods']: print method_name results = results_by_method[method_name] results.summary() if params['plot_network']: results.plot([('Density', { 'ymin': 0, 'plot_mean': True }), (['Out-degree', 'Max row-sum', 'Min row-sum'], { 'ymin': 0, 'plot_mean': True }), (['In-degree', 'Max col-sum', 'Min col-sum'], { 'ymin': 0, 'plot_mean': True }), ('Self-loop density', { 'ymin': 0, 'plot_mean': True }), ('# Active', { 'ymin': 0 })]) print # Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field]))
if scaling == 'degree': r, c = np.repeat(value, sub_size), np.repeat(value, sub_size) elif scaling == 'density': m = int(value * sub_size) r, c = np.repeat(m, sub_size), np.repeat(m, sub_size) subnet.row_covariates['r'][:] = r subnet.col_covariates['c'][:] = c for gibbs_cover in params['gibbs_covers']: data_model.coverage = gibbs_cover subnet.generate(data_model, arbitrary_init=params['arbitrary_init']) subnet.offset_extremes() fit_model.fit_conditional(subnet) gibbs_results[gibbs_cover].record(size, rep, subnet, data_model, fit_model) # Compute beta MSEs and plot performance in terms of MSE(beta) for sub, gibbs_cover in enumerate(sorted(gibbs_results)): results = gibbs_results[gibbs_cover] covariate_mses = [] for c in covariates: name = 'MSE(beta_{%s})' % c covariate_mses.append(name) results.estimate_mse(name, 'True beta_{%s}' % c, 'Est. beta_{%s}' % c) results.plot([(['MSE(beta_i)'] + covariate_mses, { 'ymin': 0, 'ymax': 3.0, 'plot_mean': True })])
print if params['fit_conditional_is']: fit_model.fit_conditional(subnet, T=50, verbose=True) i_results.record(sub_size, rep, subnet, fit_model=fit_model) print if params['fit_nonstationary']: subnet.offset_extremes() n_fit_model.fit_convex_opt(subnet, verbose=True) n_results.record(sub_size, rep, subnet, fit_model=n_fit_model) print for model in all_results: results = all_results[model] results.plot(['%s' % c for c in covariates]) # Plot network statistics as well as sparsity parameter if params['plot_network']: result = all_results[all_results.keys()[0]] result.title = None result.plot([('Average out-degree', { 'ymin': 0, 'plot_mean': True }), ('Average in-degree', { 'ymin': 0, 'plot_mean': True }), (['Out-degree', 'Max out-degree', 'Min out-degree'], { 'ymin': 0, 'plot_mean': True