Ejemplo n.º 1
0
def ci_umle(X, v, theta_grid, alpha_level):
    arr = array_from_data(X, [v])
    arr.offset_extremes()
    alpha_zero(arr)

    fit_model = NonstationaryLogistic()

    umle = np.empty_like(theta_grid)
    for l, theta_l in enumerate(theta_grid):
        fit_model.beta['x_0'] = theta_l
        fit_model.fit(arr, fix_beta = True)
        umle[l] = -fit_model.nll(arr)

    crit = -0.5 * chi2.ppf(1 - alpha_level, 1)
    ci = invert_test(theta_grid, umle - umle.max(), crit)
    if params['plot']:
        plot_statistics(ax_umle, theta_grid, umle - umle.max(), crit)
        umle_coverage_data['cis'].append(ci)
        umle_coverage_data['theta_grid'] = theta_grid
        umle_coverage_data['crit'] = crit
    return ci
data_model.kappa = -7.0
covariates = ['x_%d' % i for i in range(5)]
for covariate in covariates:
    data_model.beta[covariate] = normal(0, 1.0)

    x_node = 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)
net.offset_extremes()
net.show()
print 'True theta_0: %.2f' % data_model.beta['x_0']

# Initialize the fit model; specify which covariates it should have terms for
fit_model = NonstationaryLogistic()
for covariate in covariates:
    fit_model.beta[covariate] = None

# Set up random subnetwork generator, and run fitting experiments
gen = RandomSubnetworks(net, (200, 200))
for rep in range(5):
    subnet = gen.sample()

    fit_model.fit(subnet)
    print 'Estimated theta_0: %.2f' % fit_model.beta['x_0']

    fit_model.confidence(subnet, n_bootstrap = 5)
    ci = fit_model.conf['x_0']['normal']
    print 'Normal CI for theta_0: (%.2f, %.2f)' % ci
Ejemplo n.º 3
0
for covariate in covariates:
    data_model.beta[covariate] = normal(0, 1.0)

    x_node = 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)
net.offset_extremes()
net.show()
print 'True theta_0: %.2f' % data_model.beta['x_0']

# Initialize the fit model; specify which covariates it should have terms for
fit_model = NonstationaryLogistic()
for covariate in covariates:
    fit_model.beta[covariate] = None

# Set up random subnetwork generator, and run fitting experiments
gen = RandomSubnetworks(net, (200, 200))
for rep in range(5):
    subnet = gen.sample()

    fit_model.fit(subnet)
    print 'Estimated theta_0: %.2f' % fit_model.beta['x_0']

    fit_model.confidence(subnet, n_bootstrap=5)
    ci = fit_model.conf['x_0']['normal']
    print 'Normal CI for theta_0: (%.2f, %.2f)' % ci
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)
print 'NLL: %.2f' % ns_model.nll(net)
print 'kappa: %.2f' % ns_model.kappa
for cov_name in cov_names:
    print '%s: %.2f' % (cov_name, ns_model.beta[cov_name])
print

print 'Fitting conditional model'
c_model = StationaryLogistic()
for cov_name in cov_names:
    c_model.beta[cov_name] = None
c_model.fit_conditional(net, T=0, verbose=True)
print 'NLL: %.2f' % c_model.nll(net)
print 'kappa: %.2f' % c_model.kappa
for cov_name in cov_names:
    print '%s: %.2f' % (cov_name, c_model.beta[cov_name])
Ejemplo n.º 5
0
    c_samples[rep,:,:] = c_model.generate(net, coverage = 0.1)
c_model.confidence_boot(net, n_bootstrap = params['n_bootstrap'])
c_model.confidence_wald(net)
for cov_name in cov_names:
    c_model.confidence_cons(net, cov_name, L = 121, test = 'score')
    c_model.confidence_cons(net, cov_name, L = 121, test = 'lr')
display_cis(c_model)

# Offset extreme substructure only for Nonstationary model
net.offset_extremes()

print 'Fitting nonstationary model'
ns_model = NonstationaryLogistic()
for cov_name in cov_names:
    ns_model.beta[cov_name] = None
ns_model.fit(net)
print 'NLL: %.2f' % ns_model.nll(net)
print 'kappa: %.2f' % ns_model.kappa
for cov_name in cov_names:
    print '%s: %.2f' % (cov_name, ns_model.beta[cov_name])
print
for rep in range(params['n_samples']):
    ns_samples[rep,:,:] = ns_model.generate(net)
ns_model.confidence_boot(net, n_bootstrap = params['n_bootstrap'])
ns_model.confidence_wald(net)
display_cis(ns_model)

# Calculate sample means and variances
s_samples_mean = np.mean(s_samples, axis = 0)
s_samples_sd = np.sqrt(np.var(s_samples, axis = 0))
ns_samples_mean = np.mean(ns_samples, axis = 0)
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)
print 'NLL: %.2f' % ns_model.nll(net)
print 'kappa: %.2f' % ns_model.kappa
for cov_name in cov_names:
    print '%s: %.2f' % (cov_name, ns_model.beta[cov_name])
print

print 'Fitting conditional model'
c_model = FixedMargins(StationaryLogistic())
for cov_name in cov_names:
    c_model.base_model.beta[cov_name] = None
c_model.base_model.fit_conditional(net, verbose = True)
print 'NLL: %.2f' % c_model.nll(net)
for cov_name in cov_names:
    print '%s: %.2f' % (cov_name, c_model.base_model.beta[cov_name])
print
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])
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)
print 'NLL: %.2f' % ns_model.nll(net)
print 'kappa: %.2f' % ns_model.kappa
for cov_name in cov_names:
    print '%s: %.2f' % (cov_name, ns_model.beta[cov_name])
print
for rep in range(params['n_samples']):
    ns_samples[rep, :, :] = ns_model.generate(net)
ns_model.confidence(net, n_bootstrap=params['n_bootstrap'])
print 'Pivotal:'
for cov_name in cov_names:
    ci = ns_model.conf[cov_name]['pivotal']
    print ' %s: (%.2f, %.2f)' % (cov_name, ci[0], ci[1])
print

# Calculate sample means and variances