Ejemplo n.º 1
0
    ]

    sampler_is = get_StaticLangevin(
        D, target_log_pdf,
        target_grad)  #get_AdaptiveLangevin(D, target_log_pdf, target_grad)
    sampler_mh = get_StaticLangevin(
        D, target_log_pdf, target_grad
    )  #get_AdaptiveLangevin(D, target_log_pdf, target_grad, prec=True, step_size=1.)
    start = np.zeros(D)
    num_iter = 100

    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)
    mdl = SVoneSP500Model()
    target_log_pdf = mdl.get_logpdf_closure()

    # number of particles used for integrating out the latent variables
    mdl.mdl_param.NX = mdl.mdl_param.NX * 1

    D = mdl.dim

    # from initial runs
    initial_mean = np.array(
        [0.19404095, -0.14017837, 0.35465807, -0.22049461, -4.53669311])
    start = initial_mean

    sampler = get_StaticMetropolis_instance(D, target_log_pdf)
    samples, proposals, accepted, acc_prob, log_pdf, times, step_sizes = mini_mcmc(
        sampler, start, num_iter, D)

    logger.info("Storing results under %s" % result_fname)
    store_samples(samples, result_fname)

    mean = np.mean(samples, axis=0)
    var = np.var(samples, axis=0)
    print "mean:", repr(mean)
    print "var:", repr(var)
    print "np.mean(var): %.3f" % np.mean(var)
    print "np.linalg.norm(mean): %.3f" % np.linalg.norm(mean)
    print "min ESS: %.3f" % min_ess(samples)

    if False:
        import matplotlib.pyplot as plt
        visualise_trace_2d(samples, log_pdf, accepted, step_sizes)
    num_iter = 20000
    
    mdl = SVoneSP500Model()
    target_log_pdf = mdl.get_logpdf_closure()

    # number of particles used for integrating out the latent variables
    mdl.mdl_param.NX = mdl.mdl_param.NX * 1
    
    D = mdl.dim
    
    # from initial runs
    initial_mean = np.array([ 0.19404095, -0.14017837, 0.35465807, -0.22049461, -4.53669311])
    start = initial_mean

    sampler = get_StaticMetropolis_instance(D, target_log_pdf)
    samples, proposals, accepted, acc_prob, log_pdf, times, step_sizes = mini_mcmc(sampler, start, num_iter, D)

    logger.info("Storing results under %s" % result_fname)
    store_samples(samples, result_fname)
    
    mean = np.mean(samples, axis=0)
    var = np.var(samples, axis=0)
    print "mean:", repr(mean)
    print "var:", repr(var)
    print "np.mean(var): %.3f" % np.mean(var)
    print "np.linalg.norm(mean): %.3f" % np.linalg.norm(mean)
    print "min ESS: %.3f" % min_ess(samples)
        
    if False:
        import matplotlib.pyplot as plt
        visualise_trace_2d(samples, log_pdf, accepted, step_sizes)
Ejemplo n.º 4
0
    target_log_pdf = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=False)
    target_grad = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=True)

    samplers = [
#                
                
                ]
    
    sampler_is = get_StaticLangevin(D, target_log_pdf, target_grad)#get_AdaptiveLangevin(D, target_log_pdf, target_grad)
    sampler_mh = get_StaticLangevin(D, target_log_pdf, target_grad)#get_AdaptiveLangevin(D, target_log_pdf, target_grad, prec=True, step_size=1.)
    start = np.zeros(D)
    num_iter = 100
    
    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()