コード例 #1
0
ファイル: example_1.py プロジェクト: tcompa/nested_sampling
    except AssertionError:
        theta_val = 1.0
    return random.uniform(- theta_val, theta_val)


# collect input
n_samples = 50
f = 2e-3
n_runs = 1000

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

# perform runs
list_logZ = []
for i_run in xrange(n_runs):
    Z, X_i, P_i = nested_sampling_run(P, sample_theta, n_samples=n_samples,
                                      f=f)
    list_logZ.append(math.log(Z))
    if i_run < 10:
        ax2.plot(X_i, P_i)

# Gaussian with same mean/variance as data
list_logZ = numpy.array(list_logZ)
av = list_logZ.mean()
var = (list_logZ ** 2).mean() - av ** 2
x = numpy.linspace(list_logZ.min(), list_logZ.max(), 1000)
y = numpy.exp(- (x - av) ** 2 / (2.0 * var)) / math.sqrt(2.0 * math.pi * var)

ax1.hist(list_logZ, bins=30, alpha=0.75, normed=True)
ax1.plot(x, y, 'r-', lw=2, zorder=11)
ax1.grid()
ax1.set_xlabel('$\log(Z)$')
コード例 #2
0
ファイル: example_2.py プロジェクト: tcompa/nested_sampling
    return random.uniform(- theta_val, theta_val)


# collect input
if len(sys.argv) != 4 or '-h' in sys.argv or '--help' in sys.argv:
    sys.exit('Required arguments: n_samples, f, n_runs\n' +
             '(f: small control parameter which fixes run duration)')
n_samples = int(sys.argv[1])
f = float(sys.argv[2])
n_runs = int(sys.argv[3])
ID = 'N%07i_f%s_runs%s' % (n_samples, f, n_runs)

# perform runs
list_logZ = []
for run in xrange(n_runs):
    Z, _, _ = nested_sampling_run(P, sample_theta, n_samples=n_samples, f=f)
    list_logZ.append(math.log(Z))

# exact results
Z_exact = math.sqrt(math.pi / (beta * 2.0)) * math.erf(math.sqrt(beta / 2.0))
logZ_exact = math.log(Z_exact)

# statistic of logZ over runs
logZ_mean = sum(list_logZ) / float(n_runs)
logZ_sq_mean = sum(x ** 2 for x in list_logZ) / float(n_runs)
var_logZ = logZ_sq_mean - logZ_mean ** 2
err_logZ = (var_logZ / float(n_runs - 1.0)) ** 0.5

# store output
out = open('data_%s.dat' % ID, 'w')
out.write('%08i %.8f %08i ' % (n_samples, f, n_runs))