def ci_cons(X, v, alpha_level, L, theta_l, theta_u, K, test="lr", corrected=True, verbose=False): arr = array_from_data(X, [v]) fit_model = StationaryLogistic() fit_model.beta["x_0"] = None fit_model.confidence_cons(arr, "x_0", alpha_level, K, L, theta_l, theta_u, test, verbose) method = "conservative-%s" % test return fit_model.conf["x_0"][method]
def ci_cons(X, v, alpha_level, L, theta_l, theta_u, K, test = 'lr', corrected = True, verbose = False): arr = array_from_data(X, [v]) fit_model = StationaryLogistic() fit_model.beta['x_0'] = None fit_model.confidence_cons(arr, 'x_0', alpha_level, K, L, theta_l, theta_u, test, verbose) method = 'conservative-%s' % test return fit_model.conf['x_0'][method]
def ci_cons(X, v, alpha_level, L, theta_l, theta_u, K, test = 'lr', corrected = True, verbose = False): arr = array_from_data(X, [v]) fit_model = StationaryLogistic() fit_model.beta['x_0'] = None fit_model.confidence_cons(arr, 'x_0', alpha_level, K, L, theta_l, theta_u, test, corrected, verbose) method = 'conservative-%s' % test return fit_model.conf['x_0'][method]
def ci_cmle_wald(X, v, alpha_level): arr = array_from_data(X, [v]) A = arr.as_dense() r = A.sum(1) c = A.sum(0) s_model = StationaryLogistic() s_model.beta["x_0"] = None fit_model = FixedMargins(s_model) arr.new_row_covariate("r", np.int)[:] = r arr.new_col_covariate("c", np.int)[:] = c fit_model.fit = fit_model.base_model.fit_conditional fit_model.confidence_wald(arr, alpha_level=alpha_level) return safe_ci(fit_model, "x_0", "wald")
def ci_cmle_boot(X, v, alpha_level): arr = array_from_data(X, [v]) A = arr.as_dense() r = A.sum(1) c = A.sum(0) s_model = StationaryLogistic() s_model.beta['x_0'] = None fit_model = FixedMargins(s_model) arr.new_row_covariate('r', np.int)[:] = r arr.new_col_covariate('c', np.int)[:] = c fit_model.fit = fit_model.base_model.fit_conditional fit_model.confidence_boot(arr, alpha_level = alpha_level) return fit_model.conf['x_0']['pivotal']
def generate_data(case, theta, seed): # Advance random seed for parameter and covariate construction seed.next() case = params['case'] alpha = beta = kappa = offset = 0 conditional_sample = False if 'fixed_example' in case: # Load parameters and covariates with open(case['fixed_example'], 'r') as example_file: example = json.load(example_file) v = np.array(example['nu']) M, N = v.shape if 'alpha' in example: alpha = np.array(example['alpha']).reshape((M,1)) if 'beta' in example: beta = np.array(example['beta']).reshape((1,N)) if 'kappa' in example: kappa = example['kappa'] if 'offset' in example: offset = example['offset'] if ('r' in example) and ('c' in example): conditional_sample = True r = example['r'] c = example['c'] else: # Generate parameters and covariates M, N = case['M'], case['N'] if 'alpha_min' in case: alpha = np.random.uniform(size = (M,1)) + case['alpha_min'] if 'beta_min' in case: beta = np.random.uniform(size = (1,N)) + case['beta_min'] if 'kappa' in case: kappa = case['kappa'] if case['v_discrete']: v = np.sign(np.random.random(size = (M,N)) - 0.5) elif case['v_uniform']: v = np.random.uniform(size = (M,N)) elif case['v_normal']: v = np.random.normal(size = (M,N)) if 'v_scale' in case: v *= case['v_scale'] if 'v_loc' in case: v += case['v_loc'] if ('r' in case) and ('c' in case): conditional_sample = True r = case['r'] c = case['c'] # Generate Bernoulli probabilities from logistic regression model logit_P = np.zeros((M,N)) + kappa logit_P += alpha logit_P += beta logit_P += theta * v logit_P += offset if conditional_sample: arr = Array(M, N) arr.new_edge_covariate('x_0')[:] = logit_P arr.new_row_covariate('r', dtype = np.int)[:] = r arr.new_col_covariate('c', dtype = np.int)[:] = c base_model = StationaryLogistic() base_model.beta['x_0'] = 1.0 data_model = FixedMargins(base_model) while True: # Advance random seed for data generation seed.next() # Generate data for this trial if conditional_sample: X = data_model.generate(arr, coverage = 100.0) else: P = 1.0 / (1.0 + np.exp(-logit_P)) X = np.random.random((M,N)) < P yield X, v
def fit_and_summarize(name, fit_model, use_covs): print name if use_covs: for cov_name in cov_names: fit_model.beta[cov_name] = None fit_model.fit(net, verbose=params['verbose']) print 'NLL: %.2f' % fit_model.nll(net) print 'kappa: %.2f' % fit_model.kappa if use_covs: for cov_name in cov_names: print '%s: %.2f' % (cov_name, fit_model.beta[cov_name]) print '\n' fit_and_summarize('Stationary', Stationary(), False) fit_and_summarize('Stationary', StationaryLogistic(), True) if params['offset_extremes']: print 'Detecting subnetworks associated with infinite parameter estimates.\n' net.offset_extremes() if params['plot']: net.show_offset('pub_date') fit_and_summarize('Nonstationary', NonstationaryLogistic(), False) fit_and_summarize('Nonstationary', NonstationaryLogistic(), True) # Redisplay heatmap, ordered by estimated alphas from last fit, i.e., # NonstationaryLogistic with publication date difference covariates # XX: Following plots are broken #if params['plot']: # net.show_heatmap('alpha_out') # net.show_heatmap('alpha_in') outfile = open('scratch.json', 'w')
'plot_mse_beta': True } # Set random seed for reproducible output np.random.seed(137) # Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field])) # Initialize full network net = Network(params['N']) # Generate covariates and associated coefficients data_base_model = StationaryLogistic() covariates = [] for b in range(params['B']): name = 'x_%d' % b covariates.append(name) data_base_model.beta[name] = np.random.normal(0, params['beta_sd']) x_node = np.random.normal(0, 1, params['N']) def f_x(i_1, i_2): return abs(x_node[i_1] - x_node[i_2]) < params['x_diff_cutoff'] net.new_edge_covariate(name).from_binary_function_ind(f_x) data_model = FixedMargins(data_base_model) net.new_node_covariate_int('r')
def f_x(i_1, i_2): return np.random.uniform(-np.sqrt(3), np.sqrt(3)) arr.new_edge_covariate(name).from_binary_function_ind(f_x) data_model.match_kappa(arr, params['kappa_target']) # Specify parameter of interest that the confidence interval will try # to capture for c in params['covariates_of_interest']: theta_true = data_model.beta[c] print '%s theta_true: %.2f' % (c, theta_true) # Setup fit model if params['fit_method'] == 'conditional': fit_model = StationaryLogistic() for c in covariates: fit_model.beta[c] = None fit_model.generate = fit_model.generate_margins fit_model.fit = fit_model.fit_conditional else: if params['fit_nonstationary']: fit_model = NonstationaryLogistic() else: fit_model = StationaryLogistic() for c in covariates: fit_model.beta[c] = None # Test coverage methods = [] if params['do_wald']:
# contour expects x, y, z generated by meshgrid... CS = plt.contour(x, y, np.transpose(z), colors='k') plt.plot(theta_star_1, theta_star_2, 'b*', markersize=12) plt.plot(theta_opt_1, theta_opt_2, 'ro', markersize=12) # plt.clabel(CS, inline = 1, fontsize = 10, fmt = '%1.1f') plt.xlabel(r'$\theta_2$', fontsize=14) plt.ylabel(r'$\theta_1$', fontsize=14) return theta_opt_1, theta_opt_2 # Grid search for stationary and non-stationary fits plt.subplot(142) plt.title('Stationary') grid_fit(StationaryLogistic(), lambda n, m: m.nll(n), profile=True) plt.subplot(143) plt.title('Nonstationary') grid_fit(NonstationaryLogistic(), lambda n, m: m.nll(n), profile=True, pre_offset=True) # Grid search for conditional fit plt.subplot(144) plt.title('Conditional') def f_nll(n, m): P = m.edge_probabilities(n) w = P / (1.0 - P)
# Set random seed for reproducible output np.random.seed(137) # Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field])) # Initialize political blogs network from file. The "value" covariate # is the ground truth membership to the left-leaning (0) or # right-leaning (1) class. net = network_from_file_gml('data/polblogs/polblogs.gml', ['value']) net.new_node_covariate_int('truth')[:] = net.node_covariates['value'][:] # Initialize fitting model fit_base_model = StationaryLogistic() fit_model = Blockmodel(fit_base_model, params['fit_K']) if params['fit_nonstationary']: n_fit_base_model = NonstationaryLogistic() n_fit_model = Blockmodel(n_fit_base_model, params['fit_K']) net.new_node_covariate_int('z') if params['blockmodel_fit_method'] == 'kl': fit_model.fit = fit_model.fit_kl n_fit_model.fit = n_fit_model.fit_kl # Set up recording of results from experiment s_results = Results(params['sub_sizes'], params['sub_sizes'], params['num_reps'], 'Stationary fit') add_array_stats(s_results) def class_mismatch(n): truth = n.node_covariates['truth'][:]
def display_cis(model): procedures = set() for par in model.conf: for procedure in model.conf[par]: procedures.add(procedure) for procedure in procedures: print '%s:' % procedure for par in model.conf: if not procedure in model.conf[par]: continue ci = model.conf[par][procedure] print ' %s: (%.2f, %.2f)' % (par, ci[0], ci[1]) print print 'Fitting stationary model' s_model = StationaryLogistic() for cov_name in cov_names: s_model.beta[cov_name] = None s_model.fit(net) print 'NLL: %.2f' % s_model.nll(net) print 'kappa: %.2f' % s_model.kappa for cov_name in cov_names: print '%s: %.2f' % (cov_name, s_model.beta[cov_name]) print for rep in range(params['n_samples']): s_samples[rep,:,:] = s_model.generate(net) s_model.confidence_boot(net, n_bootstrap = params['n_bootstrap']) s_model.confidence_wald(net) display_cis(s_model) print 'Fitting conditional model'
def do_experiment(params): if params['dump_fits'] and params['load_fits']: print 'Warning: simultaneously dumping and loading is a bad idea.' if params['dump_fits']: fits = [] if params['load_fits']: with open(params['load_fits'], 'r') as fits_file: loaded_params_pick, loaded_fits = json.load(fits_file) loaded_params = dict([(k, unpick(v)) for (k, v) in loaded_params_pick]) # Compare on parameters that control data generation and inference run_params = [ 'N', 'B', 'theta_sd', 'theta_fixed', 'alpha_unif_sd', 'alpha_norm_sd', 'alpha_gamma_sd', 'cov_unif_sd', 'cov_norm_sd', 'cov_disc_sd', 'kappa_target', 'pre_offset', 'post_fit', 'fit_nonstationary', 'fit_method', 'num_reps', 'is_T', 'sampling', 'sub_sizes_r', 'sub_sizes_c', 'random_seed' ] for p in run_params: if not np.all(loaded_params[p] == params[p]): print 'Warning: load mismatch on', p # Set random seed for reproducible output seed = Seed(params['random_seed']) # Initialize full network arr = Network(params['N']) # Generate node-level propensities to extend and receive edges if params['alpha_norm_sd'] > 0.0: alpha_norm(arr, params['alpha_norm_sd']) elif params['alpha_unif_sd'] > 0.0: alpha_unif(arr, params['alpha_unif_sd']) elif params['alpha_gamma_sd'] > 0.0: # Choosing location somewhat arbitrarily to give unit skewness alpha_gamma(arr, 4.0, params['alpha_gamma_sd']) else: alpha_zero(arr) # Generate covariates and associated coefficients data_model = NonstationaryLogistic() covariates = [] for b in range(params['B']): name = 'x_%d' % b covariates.append(name) if name in params['theta_fixed']: data_model.beta[name] = params['theta_fixed'][name] else: data_model.beta[name] = np.random.normal(0, params['theta_sd']) if params['cov_unif_sd'] > 0.0: c = np.sqrt(12) / 2 def f_x(i_1, i_2): return np.random.uniform(-c * params['cov_unif_sd'], c * params['cov_unif_sd']) elif params['cov_norm_sd'] > 0.0: def f_x(i_1, i_2): return np.random.normal(0, params['cov_norm_sd']) elif params['cov_disc_sd'] > 0.0: def f_x(i_1, i_2): return (params['cov_disc_sd'] * (np.sign(np.random.random() - 0.5))) else: print 'Error: no covariate distribution specified.' sys.exit() arr.new_edge_covariate(name).from_binary_function_ind(f_x) # Generate large network, if necessary if not params['sampling'] == 'new': data_model.match_kappa(arr, params['kappa_target']) arr.generate(data_model) if params['fit_nonstationary']: fit_model = NonstationaryLogistic() else: fit_model = StationaryLogistic() for c in covariates: fit_model.beta[c] = None # Set up recording of results from experiment results = Results(params['sub_sizes_r'], params['sub_sizes_c'], params['num_reps'], interactive=params['interactive']) add_array_stats(results) if params['plot_sig']: from scipy.stats import chi2 crit = lambda dof: -0.5 * chi2.ppf(0.95, dof) umle_f = lambda n, f: f.nll(n, ignore_offset=True) umle_d = lambda n, d: d.nll(n, ignore_offset=True) umle_n = lambda n: NonstationaryLogistic().nll(n, ignore_offset=True) results.new('UMLE F-N', 'nm', lambda n, d, f: umle_f(n, f) - umle_n(n)) results.new('UMLE F-D', 'nm', lambda n, d, f: umle_f(n, f) - umle_d(n, d)) cmle_a_f = lambda n, f: acnll(n.as_dense(), np.exp(f.edge_probabilities(n))) cmle_a_d = lambda n, d: acnll(n.as_dense(), np.exp(d.edge_probabilities(n))) cmle_a_n = lambda n: acnll(n.as_dense(), np.ones_like(n.as_dense())) results.new('CMLE-A F-N', 'nm', lambda n, d, f: cmle_a_f(n, f) - cmle_a_n(n)) results.new('CMLE-A F-D', 'nm', lambda n, d, f: cmle_a_f(n, f) - cmle_a_d(n, d)) cmle_is_f = lambda n, f: f.fit_conditional(n, evaluate=True, T=50) cmle_is_d = lambda n, d: d.fit_conditional(n, evaluate=True, T=50) cmle_is_n = lambda n: NonstationaryLogistic().fit_conditional( n, evaluate=True, T=50) results.new('CMLE-IS F-N', 'nm', lambda n, d, f: cmle_is_f(n, f) - cmle_is_n(n)) results.new('CMLE-IS F-D', 'nm', lambda n, d, f: cmle_is_f(n, f) - cmle_is_d(n, d)) c_cmle_f = lambda n, f: f.fit_c_conditional(n, evaluate=True) c_cmle_d = lambda n, d: d.fit_c_conditional(n, evaluate=True) c_cmle_n = lambda n: NonstationaryLogistic().fit_c_conditional( n, evaluate=True) results.new('C-CMLE F-N', 'nm', lambda n, d, f: c_cmle_f(n, f) - c_cmle_n(n)) results.new('C-CMLE F-D', 'nm', lambda n, d, f: c_cmle_f(n, f) - c_cmle_d(n, d)) results.new('UMLE sig.', 'dof', lambda M, N, B: crit((M - 1) + (N - 1) + 1 + B)) results.new('CMLE sig.', 'dof', lambda M, N, B: crit(B)) results.new('C-CMLE sig.', 'dof', lambda M, N, B: crit((M - 1) + B)) if params['sampling'] == 'new': results.new('Subnetwork kappa', 'm', lambda d, f: d.kappa) def true_est_theta_c(c): return (lambda d, f: d.beta[c]), (lambda d, f: f.beta[c]) for c in covariates: # Need to do this hackily to avoid for-loop/lambda-binding weirdness. f_true, f_est = true_est_theta_c(c) results.new('True theta_{%s}' % c, 'm', f_true) results.new('Est. theta_{%s}' % c, 'm', f_est) if params['pre_offset'] or params['post_fit']: results.new('# Active', 'n', lambda n: np.isfinite(n.offset.matrix()).sum()) else: results.new('# Active', 'n', lambda n: n.M * n.N) if params['fisher_information']: def info_theta_c(c): def f_info_theta_c(d, f): return d.I_inv['theta_{%s}' % c] return f_info_theta_c for c in covariates: results.new('Info theta_{%s}' % c, 'm', info_theta_c(c)) if params['baseline']: def rel_mse_p_ij(n, d, f): P = d.edge_probabilities(n) return rel_mse(f.edge_probabilities(n), f.baseline(n), P) results.new('Rel. MSE(P_ij)', 'nm', rel_mse_p_ij) if not (params['pre_offset'] or params['post_fit']): def rel_mse_logit_p_ij(n, d, f): logit_P = d.edge_probabilities(n, logit=True) logit_Q = f.baseline_logit(n) return rel_mse(f.edge_probabilities(n, logit=True), logit_Q, logit_P) results.new('Rel. MSE(logit P_ij)', 'nm', rel_mse_logit_p_ij) if params['fit_method'] in [ 'convex_opt', 'conditional', 'c_conditional', 'irls', 'conditional_is' ]: results.new('Wall time (sec.)', 'm', lambda d, f: f.fit_info['wall_time']) if params['fit_method'] in ['convex_opt', 'conditional', 'conditional_is']: def work(f): w = 0 for work_type in ['nll_evals', 'grad_nll_evals', 'cnll_evals']: if work_type in f.fit_info: w += f.fit_info[work_type] return w results.new('Work', 'm', lambda d, f: work(f)) results.new('||ET_final - T||_2', 'm', lambda d, f: l2(f.fit_info['grad_nll_final'])) for sub_size in zip(results.M_sizes, results.N_sizes): print 'subnetwork size =', sub_size if params['sampling'] == 'new': gen = RandomSubnetworks(arr, sub_size) else: gen = RandomSubnetworks(arr, sub_size, method=params['sampling']) for rep in range(params['num_reps']): seed.next() sub = gen.sample() if params['fisher_information']: data_model.fisher_information(sub) if params['sampling'] == 'new': data_model.match_kappa(sub, params['kappa_target']) sub.generate(data_model) if params['load_fits']: fit, loaded_fits = loaded_fits[0], loaded_fits[1:] fit_model.beta = unpick(fit['theta']) if params['fix_broken_cmle_is']: for b_n in fit_model.beta: fit_model.beta[b_n] += 0.1474 if 'alpha' in fit: sub.row_covariates['alpha_out'] = unpick(fit['alpha']) if 'beta' in fit: sub.col_covariates['alpha_in'] = unpick(fit['beta']) if 'kappa' in fit: fit_model.kappa = fit['kappa'] if 'offset' in fit: sub.offset = unpick(fit['offset']) if 'fit_info' in fit: fit_model.fit_info = unpick(fit['fit_info']) else: if params['pre_offset']: sub.offset_extremes() if params['fit_method'] == 'convex_opt': fit_model.fit_convex_opt(sub, verbose=params['verbose']) elif params['fit_method'] == 'irls': fit_model.fit_irls(sub, verbose=params['verbose']) elif params['fit_method'] == 'logistic': fit_model.fit_logistic(sub) elif params['fit_method'] == 'logistic_l2': fit_model.fit_logistic_l2(sub, prior_precision=1.0) elif params['fit_method'] == 'conditional': fit_model.fit_conditional(sub, verbose=params['verbose']) elif params['fit_method'] == 'conditional_is': fit_model.fit_conditional(sub, T=params['is_T'], verbose=params['verbose']) elif params['fit_method'] == 'c_conditional': fit_model.fit_c_conditional(sub, verbose=params['verbose']) elif params['fit_method'] == 'composite': fit_model.fit_composite(sub, T=100, verbose=params['verbose']) elif params['fit_method'] == 'brazzale': fit_model.fit_brazzale(sub) elif params['fit_method'] == 'saddlepoint': fit_model.fit_saddlepoint(sub) elif params['fit_method'] == 'none': pass if params['post_fit']: sub.offset_extremes() fit_model.fit_convex_opt(sub, fix_beta=True) if params['dump_fits']: fit = {} fit['theta'] = pick(fit_model.beta) if 'alpha_out' in sub.row_covariates: fit['alpha'] = pick(sub.row_covariates['alpha_out']) if 'alpha_in' in sub.row_covariates: fit['beta'] = pick(sub.col_covariates['alpha_in']) if not fit_model.kappa is None: fit['kappa'] = fit_model.kappa if not sub.offset is None: sub.offset.dirty() fit['offset'] = pick(sub.offset) if not fit_model.fit_info is None: fit['fit_info'] = pick(fit_model.fit_info) fits.append(fit) if params['find_good'] > 0: abs_err = abs(fit_model.beta['x_0'] - data_model.beta['x_0']) if abs_err < params['find_good']: print abs_err sub.offset = None fit_model.fit_conditional(sub, T=1000, verbose=True) print fit_model.beta['x_0'] print fit_model.fit_info f = file('goodmat.mat', 'wb') import scipy.io Y = np.array(sub.as_dense(), dtype=np.float) X = sub.edge_covariates['x_0'].matrix() scipy.io.savemat(f, {'Y': Y, 'X': X}) sys.exit() if params['find_bad'] > 0: abs_err = abs(fit_model.beta['x_0'] - data_model.beta['x_0']) if abs_err > params['find_bad']: print abs_err sub.offset = None fit_model.fit_conditional(sub, T=1000, verbose=True) print fit_model.beta['x_0'] print fit_model.fit_info f = file('badmat.mat', 'wb') import scipy.io Y = np.array(sub.as_dense(), dtype=np.float) X = sub.edge_covariates['x_0'].matrix() scipy.io.savemat(f, {'Y': Y, 'X': X}) sys.exit() results.record(sub_size, rep, sub, data_model, fit_model) if params['verbose']: print if params['dump_fits']: with open(params['dump_fits'], 'w') as outfile: json.dump(([(p, pick(params[p])) for p in params], fits), outfile) # Compute beta MSEs covariate_naming = [] for c in covariates: mse_name = 'MSE(theta_{%s})' % c true_name = 'True theta_{%s}' % c est_name = 'Est. theta_{%s}' % c results.estimate_mse(mse_name, true_name, est_name) covariate_naming.append((c, mse_name, true_name, est_name)) # Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field])) # Should not vary between runs with the same seed and same number # of arrays tested seed.final() results.summary() return results, covariate_naming
heatmap(A) plt.subplot(332) plt.title('Network') graph = nx.DiGraph() for n1, n2 in edges: graph.add_edge(n1, n2) pos = nx.graphviz_layout(graph, prog='neato') nx.draw(graph, pos, node_size=10, with_labels=False) # Store sampled typical networks from fit models s_samples = np.empty((params['n_samples'], net.N, net.N)) ns_samples = np.empty((params['n_samples'], net.N, net.N)) c_samples = np.empty((params['n_samples'], net.N, net.N)) print 'Fitting stationary model' s_model = StationaryLogistic() for cov_name in cov_names: s_model.beta[cov_name] = None s_model.fit(net) print 'NLL: %.2f' % s_model.nll(net) print 'kappa: %.2f' % s_model.kappa for cov_name in cov_names: print '%s: %.2f' % (cov_name, s_model.beta[cov_name]) print for rep in range(params['n_samples']): s_samples[rep, :, :] = s_model.generate(net) s_model.confidence(net, n_bootstrap=params['n_bootstrap']) print 'Pivotal:' for cov_name in cov_names: ci = s_model.conf[cov_name]['pivotal'] print ' %s: (%.2f, %.2f)' % (cov_name, ci[0], ci[1])
data_model.beta[name] = np.random.normal(0, params['beta_sd']) def f_x(i_1, i_2): return np.random.uniform(-np.sqrt(3), np.sqrt(3)) arr.new_edge_covariate(name).from_binary_function_ind(f_x) data_model.match_kappa(arr, params['kappa_target']) # Specify parameter of interest that the confidence interval will try # to capture for c in params['covariates_of_interest']: theta_true = data_model.beta[c] print '%s theta_true: %.2f' % (c, theta_true) # Setup fit model if params['fit_method'] == 'conditional': fit_model = StationaryLogistic() for c in covariates: fit_model.beta[c] = None fit_model.generate = fit_model.generate_margins fit_model.fit = fit_model.fit_conditional else: if params['fit_nonstationary']: fit_model = NonstationaryLogistic() else: fit_model = StationaryLogistic() for c in covariates: fit_model.beta[c] = None # Test coverage methods = [] if params['do_wald']:
# Initialize the data model; generate covariates and associated coefficients data_model = NonstationaryLogistic() data_model.kappa = -7.0 covariates = ['x_1', 'x_2', 'x_3', 'x_4', 'x_5'] for covariate in covariates: data_model.beta[covariate] = np.random.normal(0, 1.0) x_node = np.random.normal(0, 1.0, N) def f_x(i_1, i_2): return abs(x_node[i_1] - x_node[i_2]) < 0.3 net.new_edge_covariate(covariate).from_binary_function_ind(f_x) net.generate(data_model) print 'True beta_1: %.2f' % data_model.beta['x_1'] # Initialize the fit model; specify which covariates it should have terms for fit_model = StationaryLogistic() for covariate in covariates: fit_model.beta[covariate] = None # Set up plotting f = plt.figure() ax = f.add_subplot(1, 1, 1, aspect = 'equal') ax.set_title('95% confidence regions') ax.set_xlim(data_model.beta['x_1'] - 2.0, data_model.beta['x_1'] + 2.0) ax.set_ylim(data_model.beta['x_2'] - 2.0, data_model.beta['x_2'] + 2.0) ax.set_xlabel('beta_1') ax.set_ylabel('beta_2') # Set up random subnetwork generator, and run fitting experiments gen = RandomSubnetworks(net, (sub_N, sub_N)) for rep in range(10):
med_degree = np.median(degree) net.new_node_covariate('low_degree').from_pairs(net.names, degree < med_degree) for v_1, v_2, name in [(0, 0, 'high_to_high'), (1, 1, 'low_to_low'), (0, 1, 'high_to_low')]: covariates.append(name) def f_x(i_1, i_2): return ((net.node_covariates['low_degree'][i_1] == v_1) and (net.node_covariates['low_degree'][i_2] == v_2)) net.new_edge_covariate(name).from_binary_function_ind(f_x) # Initialize fitting model fit_model = StationaryLogistic() n_fit_model = NonstationaryLogistic() for c in covariates: fit_model.beta[c] = None n_fit_model.beta[c] = None # Set up recording of results from experiment results = Results(params['sub_sizes'], params['num_reps'], 'Stationary fit') add_network_stats(results) def est_theta_c(c): return lambda d, f: f.beta[c] for c in covariates: f_est = est_theta_c(c) results.new('%s' % c, 'm', f_est) all_results = {}
# Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field])) # Initialize full network net = Network(params['N']) # Generate underlying edge probabilities as offsets P_alpha, P_beta = params['P_beta_params'] P = np.random.beta(P_alpha, P_beta, (params['N'],params['N'])) net.initialize_offset()[:,:] = logit(P) # Generate covariates and associated coefficients data_model = StationaryLogistic() covariates = [] data_model.beta = {} for b in range(params['B']): name = 'x_%d' % b covariates.append(name) data_model.beta[name] = np.random.normal(0, params['beta_sd']) x_node = np.random.normal(0, 1, params['N']) def f_x(i_1, i_2): return abs(x_node[i_1] - x_node[i_2]) < params['x_diff_cutoff'] net.new_edge_covariate(name).from_binary_function_ind(f_x) if params['fit_nonstationary']: fit_model = NonstationaryLogistic()
class_probs = np.random.dirichlet(np.repeat(params['class_conc'], params['K'])) z = np.where(np.random.multinomial(1, class_probs, params['N']) == 1)[1] net.new_node_covariate_int('z_true')[:] = z data_model = Blockmodel(data_base_model, params['K'], 'z_true') Theta = np.random.normal(params['Theta_mean'], params['Theta_sd'], (params['K'],params['K'])) Theta += params['Theta_diag'] * np.identity(params['K']) Theta -= np.mean(Theta) data_model.Theta = Theta net.generate(data_model) if params['plot_network']: net.show_heatmap('z_true') # Initialize fitting model fit_base_model = StationaryLogistic() for c in covariates: fit_base_model.beta[c] = None fit_model = Blockmodel(fit_base_model, params['fit_K']) if params['fit_nonstationary']: n_fit_base_model = NonstationaryLogistic() for c in covariates: n_fit_base_model.beta[c] = None n_fit_model = Blockmodel(n_fit_base_model, params['fit_K']) net.new_node_covariate_int('z') # Set up recording of results from experiment s_results = Results(params['sub_sizes'], params['num_reps'], 'Stationary fit') add_network_stats(s_results) s_results.new('Subnetwork kappa', 'm', lambda d, f: d.base_model.kappa) def f_c(c):
def display_cis(model): procedures = set() for par in model.conf: for procedure in model.conf[par]: procedures.add(procedure) for procedure in procedures: print '%s:' % procedure for par in model.conf: if not procedure in model.conf[par]: continue ci = model.conf[par][procedure] print ' %s: (%.2f, %.2f)' % (par, ci[0], ci[1]) print print 'Fitting stationary model' s_model = StationaryLogistic() for cov_name in cov_names: s_model.beta[cov_name] = None s_model.fit(net) print 'NLL: %.2f' % s_model.nll(net) print 'kappa: %.2f' % s_model.kappa for cov_name in cov_names: print '%s: %.2f' % (cov_name, s_model.beta[cov_name]) print for rep in range(params['n_samples']): s_samples[rep, :, :] = s_model.generate(net) s_model.confidence_boot(net, n_bootstrap=params['n_bootstrap']) s_model.confidence_wald(net) display_cis(s_model) print 'Fitting conditional model'
def generate_data(case, theta, seed): # Advance random seed for parameter and covariate construction seed.next() case = params["case"] alpha = beta = kappa = offset = 0 conditional_sample = False if "fixed_example" in case: # Load parameters and covariates with open(case["fixed_example"], "r") as example_file: example = json.load(example_file) v = np.array(example["nu"]) M, N = v.shape if "alpha" in example: alpha = np.array(example["alpha"]).reshape((M, 1)) if "beta" in example: beta = np.array(example["beta"]).reshape((1, N)) if "kappa" in example: kappa = example["kappa"] if "offset" in example: offset = example["offset"] if ("r" in example) and ("c" in example): conditional_sample = True r = example["r"] c = example["c"] else: # Generate parameters and covariates M, N = case["M"], case["N"] if "alpha_min" in case: alpha = np.random.uniform(size=(M, 1)) + case["alpha_min"] if "beta_min" in case: beta = np.random.uniform(size=(1, N)) + case["beta_min"] if "kappa" in case: kappa = case["kappa"] if case["v_discrete"]: v = np.random.random(size=(M, N)) < 0.5 else: v = np.random.uniform(size=(M, N)) if "v_min" in case: v += case["v_min"] if ("r" in case) and ("c" in case): conditional_sample = True r = case["r"] c = case["c"] # Generate Bernoulli probabilities from logistic regression model logit_P = np.zeros((M, N)) + kappa logit_P += alpha logit_P += beta logit_P += theta * v logit_P += offset if conditional_sample: arr = Array(M, N) arr.new_edge_covariate("x_0")[:] = logit_P arr.new_row_covariate("r", dtype=np.int)[:] = r arr.new_col_covariate("c", dtype=np.int)[:] = c base_model = StationaryLogistic() base_model.beta["x_0"] = 1.0 data_model = FixedMargins(base_model) while True: # Advance random seed for data generation seed.next() # Generate data for this trial if conditional_sample: X = data_model.generate(arr, coverage=2.0) else: P = 1.0 / (1.0 + np.exp(-logit_P)) X = np.random.random((M, N)) < P yield X, v
else: data_model.beta[name] = np.random.normal(0, params['beta_sd']) def f_x(i_1, i_2): return np.random.uniform(-np.sqrt(3), np.sqrt(3)) arr.new_edge_covariate(name).from_binary_function_ind(f_x) data_model.match_kappa(arr, params['kappa_target']) # Specify parameter of interest that the confidence interval will try to capture for c in params['covariates_of_interest']: theta_true = data_model.beta[c] print '%s theta_true: %.2f' % (c, theta_true) # Setup fit model if params['fit_method'] == 'conditional': fit_model = StationaryLogistic() for c in covariates: fit_model.beta[c] = None fit_model.generate = fit_model.generate_margins fit_model.fit = fit_model.fit_conditional else: if params['fit_nonstationary']: fit_model = NonstationaryLogistic() else: fit_model = StationaryLogistic() for c in covariates: fit_model.beta[c] = None # Test coverage methods = [] if params['do_large_sample']:
'sub_sizes': np.arange(10, 70, 10, dtype=np.int), 'plot_mse_beta': True } # Set random seed for reproducible output np.random.seed(137) # Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field])) # Initialize full network net = Network(params['N']) # Generate covariates and associated coefficients data_base_model = StationaryLogistic() covariates = [] for b in range(params['B']): name = 'x_%d' % b covariates.append(name) data_base_model.beta[name] = np.random.normal(0, params['beta_sd']) x_node = np.random.normal(0, 1, params['N']) def f_x(i_1, i_2): return abs(x_node[i_1] - x_node[i_2]) < params['x_diff_cutoff'] net.new_edge_covariate(name).from_binary_function_ind(f_x) data_model = FixedMargins(data_base_model) net.new_node_covariate_int('r') net.new_node_covariate_int('c')
plt.title('Covariates') covariate_levels = np.zeros((net.N, net.N)) num_covariates = len(cov_names) for i, val in enumerate(np.linspace(1.0, 0.4, num_covariates)): covariate_levels[net.edge_covariates[cov_names[i]].matrix() == True] = val heatmap(covariate_levels, 'Blues') plt.subplot(333) plt.title('Network') graph = nx.DiGraph() for n1, n2 in edges: graph.add_edge(n1, n2) pos = nx.nx_pydot.graphviz_layout(graph, prog = 'neato') nx.draw(graph, pos, node_size = 10, with_labels = False) print 'Fitting stationary model' s_model = StationaryLogistic() for cov_name in cov_names: s_model.beta[cov_name] = None s_model.fit(net, verbose = True) print 'NLL: %.2f' % s_model.nll(net) print 'kappa: %.2f' % s_model.kappa for cov_name in cov_names: print '%s: %.2f' % (cov_name, s_model.beta[cov_name]) print print 'Fitting nonstationary model' alpha_zero(net) ns_model = NonstationaryLogistic() for cov_name in cov_names: ns_model.beta[cov_name] = None ns_model.fit(net, verbose = True)
n_rep = 100 n_boot = 10 alpha_level = 0.05 net = Network(N) alpha_norm(net, alpha_sd) for d in range(D): net.new_edge_covariate('x_%d' % d)[:,:] = np.random.normal(0, 1, (N, N)) data_model = NonstationaryLogistic() for d in range(D): data_model.beta['x_%d' % d] = np.random.normal(0, 1) data_model.beta['x_0'] = theta data_model.match_kappa(net, kappa_target) s_fit = StationaryLogistic() ns_fit = NonstationaryLogistic() for d in range(D): s_fit.beta['x_%d' % d] = None ns_fit.beta['x_%d' % d] = None def safe_ci(model, name, method): if name in model.conf: if method in model.conf[name]: return model.conf[name][method] else: return (0.0, 0.0) braz_covered = 0 ws_covered = 0 wn_covered = 0
(np.sign(np.random.random() - 0.5))) else: print 'Error: no covariate distribution specified.' sys.exit() net.new_edge_covariate(name).from_binary_function_ind(f_x) # Specify data model as generation of permuation networks net.new_node_covariate_int('r')[:] = 1 net.new_node_covariate_int('c')[:] = 1 data_model = FixedMargins(data_model, 'r', 'c', coverage = 2.0) if params['fit_nonstationary']: fit_model = NonstationaryLogistic() else: fit_model = StationaryLogistic() for b in data_model.base_model.beta: fit_model.beta[b] = 0.0 # Set up recording of results from experiment results = Results(params['sub_sizes'], params['sub_sizes'], params['num_reps']) add_array_stats(results) def true_est_theta_b(b): return (lambda d, f: d.base_model.beta[b]), (lambda d, f: f.beta[b]) for b in fit_model.beta: # Need to do this hackily to avoid for-loop/lambda-binding weirdness. f_true, f_est = true_est_theta_b(b) results.new('True theta_{%s}' % b, 'm', f_true) results.new('Est. theta_{%s}' % b, 'm', f_est) results.new('# Active', 'n', lambda n: n.N ** 2) results.new('Separated', 'm', lambda d, f: f.fit_info['separated'])
# Report parameters for the run print 'Parameters:' for field in params: print '%s: %s' % (field, str(params[field])) # Initialize full network net = Network(params['N']) # Generate underlying edge probabilities as offsets P_alpha, P_beta = params['P_beta_params'] P = np.random.beta(P_alpha, P_beta, (params['N'], params['N'])) net.initialize_offset()[:, :] = logit(P) # Generate covariates and associated coefficients data_model = StationaryLogistic() covariates = [] data_model.beta = {} for b in range(params['B']): name = 'x_%d' % b covariates.append(name) data_model.beta[name] = np.random.normal(0, params['beta_sd']) x_node = np.random.normal(0, 1, params['N']) def f_x(i_1, i_2): return abs(x_node[i_1] - x_node[i_2]) < params['x_diff_cutoff'] net.new_edge_covariate(name).from_binary_function_ind(f_x)
plt.title('Covariates') covariate_levels = np.zeros((net.N, net.N)) num_covariates = len(cov_names) for i, val in enumerate(np.linspace(1.0, 0.4, num_covariates)): covariate_levels[net.edge_covariates[cov_names[i]].matrix() == True] = val heatmap(covariate_levels, 'Blues') plt.subplot(333) plt.title('Network') graph = nx.DiGraph() for n1, n2 in edges: graph.add_edge(n1, n2) pos = nx.graphviz_layout(graph, prog='neato') nx.draw(graph, pos, node_size=10, with_labels=False) print 'Fitting stationary model' s_model = StationaryLogistic() for cov_name in cov_names: s_model.beta[cov_name] = None s_model.fit(net, verbose=True) print 'NLL: %.2f' % s_model.nll(net) print 'kappa: %.2f' % s_model.kappa for cov_name in cov_names: print '%s: %.2f' % (cov_name, s_model.beta[cov_name]) print print 'Fitting nonstationary model' alpha_zero(net) ns_model = NonstationaryLogistic() for cov_name in cov_names: ns_model.beta[cov_name] = None ns_model.fit(net, verbose=True)
c = case['c'] # Generate Bernoulli probabilities from logistic regression model logit_P = np.zeros((M,N)) + kappa logit_P += alpha logit_P += beta logit_P += theta * v logit_P += offset if conditional_sample: arr = Array(M, N) arr.new_edge_covariate('x_0')[:] = logit_P arr.new_row_covariate('r', dtype = np.int)[:] = r arr.new_col_covariate('c', dtype = np.int)[:] = c base_model = StationaryLogistic() base_model.beta['x_0'] = 1.0 data_model = FixedMargins(base_model) while True: # Advance random seed for data generation seed.next() # Generate data for this trial if conditional_sample: X = data_model.generate(arr, coverage = 100.0) else: P = 1.0 / (1.0 + np.exp(-logit_P)) X = np.random.random((M,N)) < P yield X, v
class_probs = np.random.dirichlet(np.repeat(params['class_conc'], params['K'])) z = np.where(np.random.multinomial(1, class_probs, params['N']) == 1)[1] net.new_node_covariate_int('z_true')[:] = z data_model = Blockmodel(data_base_model, params['K'], 'z_true') Theta = np.random.normal(params['Theta_mean'], params['Theta_sd'], (params['K'], params['K'])) Theta += params['Theta_diag'] * np.identity(params['K']) Theta -= np.mean(Theta) data_model.Theta = Theta net.generate(data_model) if params['plot_network']: net.show_heatmap('z_true') # Initialize fitting model fit_base_model = StationaryLogistic() for c in covariates: fit_base_model.beta[c] = None fit_model = Blockmodel(fit_base_model, params['fit_K']) if params['fit_nonstationary']: n_fit_base_model = NonstationaryLogistic() for c in covariates: n_fit_base_model.beta[c] = None n_fit_model = Blockmodel(n_fit_base_model, params['fit_K']) net.new_node_covariate_int('z') # Set up recording of results from experiment s_results = Results(params['sub_sizes'], params['num_reps'], 'Stationary fit') add_network_stats(s_results) s_results.new('Subnetwork kappa', 'm', lambda d, f: d.base_model.kappa)
degree = r + c med_degree = np.median(degree) net.new_node_covariate('low_degree').from_pairs(net.names, degree < med_degree) for v_1, v_2, name in [(0, 0, 'high_to_high'), (1, 1, 'low_to_low'), (0, 1, 'high_to_low')]: covariates.append(name) def f_x(i_1, i_2): return ((net.node_covariates['low_degree'][i_1] == v_1) and (net.node_covariates['low_degree'][i_2] == v_2)) net.new_edge_covariate(name).from_binary_function_ind(f_x) # Initialize fitting model fit_model = StationaryLogistic() n_fit_model = NonstationaryLogistic() for c in covariates: fit_model.beta[c] = None n_fit_model.beta[c] = None # Set up recording of results from experiment results = Results(params['sub_sizes'], params['num_reps'], 'Stationary fit') add_network_stats(results) def est_theta_c(c): return lambda d, f: f.beta[c] for c in covariates: