forked from othercriteria/StochasticBlockmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_minimal.py
executable file
·50 lines (40 loc) · 1.49 KB
/
test_minimal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# Test of "new style" network inference, minimal code to demo something...
# Daniel Klein, 5/16/2012
from Network import Network
from Models import NonstationaryLogistic, alpha_unif
from Experiment import RandomSubnetworks
from numpy.random import normal, seed
# Seed random number for reproducible results
seed(137)
# Initialize full network
N = 300
net = Network(N)
alpha_unif(net, 0.5)
# Initialize the data model; generate covariates and associated coefficients
data_model = NonstationaryLogistic()
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