from kameleon_rks.experiments.tools import store_samples

result_fname = os.path.splitext(os.path.basename(__file__))[0] + ".txt"

if __name__ == '__main__':
    import time
    
    from kameleon_rks.examples.plotting import visualise_pairwise_marginals
    from kameleon_rks.proposals.Metropolis import AdaptiveIndependentMetropolis
    from kameleon_rks.samplers.mini_pmc import mini_pmc
    from kameleon_rks.tools.log import Log
    import numpy as np
    
    from smc2.sv_models import SVoneSP500Model
    
    logger = Log.get_logger()


    def get_AdaptiveIndependentMetropolis_instance(D, target_log_pdf):
        gamma2 = 0.1
        
#         # run 1 (initial parameters from Ingmar's pilot runs)
#         proposal_mu = np.array([ -0.17973298, 0.11796741, 0.86733172, 0.52834129, -3.32354247])
#         proposal_var = np.array([  0.01932092, 0.02373668, 0.02096583, 0.1566503 , 0.46316933])
#         proposal_L_C = np.diag(np.sqrt(proposal_var))
#         proposal_L_C *= 2
#         # resuts:
#         # mean: array([ 0.08820961, -0.03436691,  0.7178945,   0.54124475, -3.77499049])
#         # var: array([ 0.01514925,  0.01407534,  0.056966,    0.37502436,  0.5887545])
#         # np.mean(var): 0.201
#         # np.linalg.norm(mean): 3.882
예제 #2
0
import time

from kameleon_rks.tools.log import Log
import numpy as np

logger = Log.get_logger()


def mini_mcmc(transition_kernel,
              start,
              num_iter,
              D,
              recompute_log_pdf=False,
              time_budget=None):
    # MCMC results
    samples = np.zeros((num_iter, D)) + np.nan
    proposals = np.zeros((num_iter, D)) + np.nan
    accepted = np.zeros(num_iter) + np.nan
    acc_prob = np.zeros(num_iter) + np.nan
    log_pdf = np.zeros(num_iter) + np.nan
    step_sizes = np.zeros(num_iter) + np.nan

    # timings for output and time limit
    times = np.zeros(num_iter)
    last_time_printed = time.time()

    # for adaptive transition kernels
    avg_accept = 0.

    # init MCMC (first iteration)
    current = start
예제 #3
0
    if False:
        plt.scatter(samples.T[0],
                    samples.T[1],
                    c='r',
                    marker='*',
                    zorder=4,
                    s=5)
        #    fig.suptitle("%s - importance resampled" %  (sampler_is.__class__.__name__,))
        plt.show()
        plt.scatter(unadj_samp.T[0],
                    unadj_samp.T[1],
                    c=logw - logsumexp(logw),
                    cmap=plt.get_cmap('Blues'),
                    alpha=0.5,
                    zorder=2)  #)visualize_scatter_2d()
        #    plt.suptitle("%s - unadjusted Langevin" %  (sampler_is.__class__.__name__,))
        #    plt.scatter(mcmc_samps[0].T[0], mcmc_samps[0].T[1], c='b',marker='*')
        plt.show()
    Log.get_logger().info('====' + str(sampler_mh.step_size) + ' ' +
                          str(mcmc_samps[2].mean()) + '====')
    #the following two should be 0 ideally
    #mmd_w = pk.estimateMMD(Z,samples)
    #mmd_unw = pk.estimateMMD(Z,unadj_samp)
    Log.get_logger().info(
        'estimated weighted mse moments %.2f, res per gen %.2f unadj %.2f, mcmc %.2f\n weight/unw. (good if <1):%.2f\n is/mcmc %2f'
        % (mom_w, mom_gen, mom_unadj, mom_mcmc, mom_w / mom_unadj,
           mom_w / mom_mcmc))

#    plt.show()
예제 #4
0
    
    D = mdl.dim
    pop_size=50
    target_log_pdf = mdl.get_logpdf_closure()

    samplers = [
#                get_StaticMetropolis_instance(D, target_log_pdf),
#                get_AdaptiveMetropolis_instance(D, target_log_pdf),
#                get_OracleKameleon_instance(D, target_log_pdf),
#                get_Kameleon_instance(D, target_log_pdf),
#                get_StaticLangevin_instance(D, target_log_pdf, target_grad),
#                get_AM_5(D, target_log_pdf),
                get_AM_1(D, target_log_pdf),
#                get_AM_2(D, target_log_pdf),
#                get_AM_05(D, target_log_pdf),
                ]
    
    for sampler in samplers:
        print(sampler.__class__)
        start = mdl.rvs(pop_size)
        num_iter = 5000
        
        samples, log_target_densities, times = mini_pmc(sampler, start, num_iter, pop_size)
        mom_samp = np.array([(samples**i).mean(0) for i in range(1,4)])
        
        visualize_scatter_2d(samples[:,:2])
        Log.get_logger().info('===='+str(sampler.step_size)+'====')

    plt.show()

예제 #5
0
    target_grad = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=True)

    samplers = [
#                get_StaticMetropolis_instance(D, target_log_pdf),
#                get_AdaptiveMetropolis_instance(D, target_log_pdf),
#                get_OracleKameleon_instance(D, target_log_pdf),
#                get_Kameleon_instance(D, target_log_pdf),
#                get_StaticLangevin_instance(D, target_log_pdf, target_grad),
                get_AM_5(D, target_log_pdf),
                get_AM_1(D, target_log_pdf),
                get_AM_2(D, target_log_pdf),
                get_AM_05(D, target_log_pdf),
                ]
    
    for sampler in samplers:
        start = np.zeros(D)
        num_iter = 1000
        
        samples, log_target_densities, times = mini_pmc(sampler, start, num_iter, pop_size)
        mom_samp = np.array([(samples**i).mean(0) for i in range(1,4)])
        
        visualize_scatter_2d(samples)
        Log.get_logger().info('===='+str(sampler.step_size)+'====')
        #the following two should be 0 ideally
        Log.get_logger().info(np.mean((esj(samples,pop_size)-1)**2)) 
        Log.get_logger().info(np.mean(((mom_samp / moments)-1)**2))
        plt.suptitle("%s" % \
                     (sampler.__class__.__name__,))
    plt.show()

예제 #6
0
    D = mdl.dim
    pop_size = 50
    target_log_pdf = mdl.get_logpdf_closure()

    samplers = [
        #                get_StaticMetropolis_instance(D, target_log_pdf),
        #                get_AdaptiveMetropolis_instance(D, target_log_pdf),
        #                get_OracleKameleon_instance(D, target_log_pdf),
        #                get_Kameleon_instance(D, target_log_pdf),
        #                get_StaticLangevin_instance(D, target_log_pdf, target_grad),
        #                get_AM_5(D, target_log_pdf),
        get_AM_1(D, target_log_pdf),
        #                get_AM_2(D, target_log_pdf),
        #                get_AM_05(D, target_log_pdf),
    ]

    for sampler in samplers:
        print(sampler.__class__)
        start = mdl.rvs(pop_size)
        num_iter = 5000

        samples, log_target_densities, times = mini_pmc(
            sampler, start, num_iter, pop_size)
        mom_samp = np.array([(samples**i).mean(0) for i in range(1, 4)])

        visualize_scatter_2d(samples[:, :2])
        Log.get_logger().info('====' + str(sampler.step_size) + '====')

    plt.show()
예제 #7
0
    samples, log_target_densities, unadj_samp, unadj_log_target, logw, unw_samptimes = mini_rb_pmc(sampler_is, start, num_iter, pop_size, D, time_budget=100000)
    mom_gen = np.mean((np.array([(samples**i).mean(0) for i in range(1, max_moment)]) - moments)**2)    
    mcmc_samps = mini_mcmc(sampler_mh, start, num_iter, D)
    
    #the weights we get back are not Rao-Blackwellized, which is what we do now.
    #beware: this only works if the proposal is not adapted during sampling!!
    #logw = logsumexp(np.array([sampler_is.proposal_log_pdf(i, unadj_samp) for i in unadj_samp]), 0)
    
    res_idx = system_res(range(len(logw)), logw, resampled_size=10*len(logw))
    samples = unadj_samp[res_idx]
    mom_unadj = np.mean((np.array([(unadj_samp**i).mean(0) for i in range(1, max_moment)]) - moments)**2)
    mom_w = np.mean((np.array([(unadj_samp**i * exp(logw - logsumexp(logw))[:,np.newaxis]).sum(0) for i in range(1, max_moment)]) -moments)**2)
    mom_mcmc = np.mean((np.array([(mcmc_samps[0]**i).mean(0) for i in range(1, max_moment)]) - moments)**2)
    
    if False:
        plt.scatter(samples.T[0], samples.T[1], c='r', marker='*', zorder=4, s=5)
    #    fig.suptitle("%s - importance resampled" %  (sampler_is.__class__.__name__,))
        plt.show()
        plt.scatter(unadj_samp.T[0], unadj_samp.T[1], c = logw - logsumexp(logw), cmap = plt.get_cmap('Blues'), alpha=0.5, zorder=2) #)visualize_scatter_2d()
    #    plt.suptitle("%s - unadjusted Langevin" %  (sampler_is.__class__.__name__,))
    #    plt.scatter(mcmc_samps[0].T[0], mcmc_samps[0].T[1], c='b',marker='*')
        plt.show()
    Log.get_logger().info('===='+str(sampler_mh.step_size)+' '+str(mcmc_samps[2].mean())+'====')
    #the following two should be 0 ideally
    #mmd_w = pk.estimateMMD(Z,samples)
    #mmd_unw = pk.estimateMMD(Z,unadj_samp)
    Log.get_logger().info('estimated weighted mse moments %.2f, res per gen %.2f unadj %.2f, mcmc %.2f\n weight/unw. (good if <1):%.2f\n is/mcmc %2f' % (mom_w, mom_gen, mom_unadj,mom_mcmc, mom_w/mom_unadj, mom_w/mom_mcmc))

#    plt.show()