Example #1
0
import numpy as np
import matplotlib.pyplot as plt
import mcmc

# Potential for a 1dim Gaussian
def U(x):
    return x**2 / 2

def gradU(x):
    return x

h_metropolis = 5
num_samples = 50000
skip_n_samples = 5

#Ignore the further results of the chain, useful only for convergence analysis
X, runtime, _, _ = mcmc.chain_rwMetropolis(np.random.random_sample(1),
                 h_metropolis, U, num_samples, skip_n_samples)
info_str = "INFOSIMU: chain_rwMetropolis, h = " + str(h_metropolis) + \
            " runtime: " + runtime + " n_samples = " + str(num_samples) + '\n'

# Store the samples into a separate file, to incentivate a C approach
filename = "gaussian_stored_samples.smp"
samples_file = open(filename, "w")
samples_file.write(info_str)
for x in X:
    for i in x:
        print(i, file = samples_file)
samples_file.close()
print("Samples and information stored in " + filename)
Example #2
0
MALA = False
MULTICHAIN_RW = True
# 10 chains to produce in parallel
multich = mp.cpu_count()
#multich = 10
dim = 23

if SAMPLING_SINGLE_CHAIN:
    print("Constructing a single full chain")
    if METROPOLIS_RW:
        print("...Metropolis RW")
        #        X, runtime, _, _ = \
        #            mcmc.chain_rwMetropolis(np.random.random_sample(23),
        #                h, auxiliary_tot_cost, num_samples, skip_n_samples, L_domain)
        X, runtime, _, _ = mcmc.chain_rwMetropolis(np.random.random_sample(23),
                                                   h, U, num_samples,
                                                   skip_n_samples, L_domain)
        info_str = "INFOSIMU: chain_rwMetropolis, h = " + str(h) + \
            " runtime: " + runtime + " n_samples = " + str(num_samples) + '\n'
    elif ULA:
        print("...ULA")
        X, runtime, _ = mcmc.ulaChain(np.random.random_sample(23), h, U, gradU,
                                      num_samples, skip_n_samples, L_domain)
        info_str = "INFOSIMU: ULA, h = " + str(h) + \
                " runtime: " + runtime + " n_samples = " + str(num_samples)+'\n'
    elif MALA:
        print("...MALA")
        X, runtime, _, _ = mcmc.malaChain(np.random.random_sample(23), h, U,
                                          gradU, num_samples, skip_n_samples,
                                          L_domain)
        info_str = "INFOSIMU: MALA, h = " + str(h) + \
Example #3
0
METROPOLIS_RW = False #True
ULA = False #True
MALA = False
MULTICHAIN_RW = True #False #True
# 10 chains to produce in parallel
multich = mp.cpu_count()
#multich = 10
dim = 401

if SAMPLING_SINGLE_CHAIN:
    print("Constructing a single full chain")
    if METROPOLIS_RW:
        print("...Metropolis RW")
        X, runtime, _, _ = \
            mcmc.chain_rwMetropolis(np.random.random_sample(dim),
                h, auxiliary_tot_cost, num_samples, skip_n_samples, L_domain)
        info_str = "INFOSIMU: chain_rwMetropolis, h = " + str(h) + \
            " runtime: " + runtime + " n_samples = " + str(num_samples) + '\n'
    elif MULTICHAIN_RW:
        print("...multichain RW approach")
        X, arate, _  = \
            mcmc.multichainRW(dim, L_domain, h, U, num_samples, multich,
                                                        skip_n_samples, True)
        info_str = "INFOSIMU: Multichain RW"


    # Store the samples into a separate file, modular approach
    filename = "regression_chain.smp"
    if (len(sys.argv) == 2):
        filename = str(sys.argv[1]) + "_chain.smp"
    samples_file = open(filename, "w")
                                   W2, W3, W4, 10) * k_enlarge


# The gradient of the potential above
def auxiliary_gradient(params_together):
    b2, b3, b4, W2, W3, W4 = nn_potential.split_params(params_together)
    return nn_potential.grad_cost(nn_potential.x, nn_potential.y, b2, b3, b4,
                                  W2, W3, W4) * k_enlarge


# Sample candidated from HMC to minimize the potential
#X, runtime = mcmc.sample_uHMC(np.random.random_sample(23) * 0.5,
#                            0.005, 0.0005, 0.5, auxiliary_gradient, 500000)

X, runtime, _, _ = mcmc.chain_rwMetropolis(
    np.random.random_sample(23) * 0.5, h_metropolis, auxiliary_tot_cost,
    num_samples, skip_n_samples, L_domain)
info_str = "INFOSIMU: chain_rwMetropolis, h = " + str(h_metropolis) + \
            " runtime: " + runtime + " n_samples = " + str(num_samples) + '\n'

# Store the samples into a separate file, to incentivate a chain approach
filename = "nn_stored_samples.smp"
if (len(sys.argv) == 2):
    filename = str(sys.argv[1]) + ".smp"
samples_file = open(filename, "w")
samples_file.write(info_str)
for x in X:
    for i in range(len(x)):
        if i < (len(x) - 1):
            print(x[i], file=samples_file, end=' ')
        else:
METROPOLIS_RW = False  #True
ULA = False
MALA = False  #True
MULTICHAIN_RW = True
# 10 chains to produce in parallel
#multich = 20
multich = mp.cpu_count()
dim = 2

if SAMPLING_SINGLE_CHAIN:
    print("Sampling from a single chain")
    if METROPOLIS_RW:
        print("Metropolis RW")
        X, runtime, _, _ = mcmc.chain_rwMetropolis(np.array([4, 1]), h, ban_U,
                                                   num_samples, skip_n_samples,
                                                   L_domain)
        info_str = "INFOSIMU: chain_rwMetropolis, h = " + str(h) + \
                " runtime: " + runtime + " n_samples = " + str(num_samples)+'\n'
    elif ULA:
        print("ULA")
        X, runtime, _ = mcmc.ulaChain(np.array([4, 1]), h, ban_U, ban_gradU,
                                      num_samples, skip_n_samples, L_domain)
        info_str = "INFOSIMU: ULA, h = " + str(h) + \
                " runtime: " + runtime + " n_samples = " + str(num_samples)+'\n'
    elif MALA:
        print("MALA")
        X, runtime, _, _ = mcmc.malaChain(np.array([4, 1]), h, ban_U,
                                          ban_gradU, num_samples,
                                          skip_n_samples, L_domain)
        info_str = "INFOSIMU: MALA, h = " + str(h) + \