Ejemplo n.º 1
0
def test_divonne():
    print_header('Divonne')
    print_results(
        'Divonne',
        pycuba.Divonne(Integrand,
                       NDIM,
                       mineval=MINEVAL,
                       maxeval=MAXEVAL,
                       key1=KEY1,
                       key2=KEY2,
                       key3=KEY3,
                       maxpass=MAXPASS,
                       border=BORDER,
                       maxchisq=MAXCHISQ,
                       mindeviation=MINDEVIATION,
                       ldxgiven=LDXGIVEN,
                       verbose=2))
Ejemplo n.º 2
0
def proceed_err(nidim, nods, nt):
    r_vec = [nt, nidim]
    p_integ = PyMikor()
    fof = Integrand('FCN', nidim)
    fof.normalize_a(2, 600)
    exact_res = fof.exact_oscillatory()
    v_integ = vegas.Integrator(nidim * [[0, 1]])
    v_result = []
    v_result.append(v_integ(fof.oscillatory_fcn, nitn=10, neval=1e3).mean)
    v_result.append(v_integ(fof.oscillatory_fcn, nitn=10, neval=1e5).mean)

    global ave, ucc
    ave = fof.a
    ucc = fof.u

    def IntegCuba(ndim, xx, ncomp, ff, userdata):
        # x, y, z = [xx[i] for i in range(ndim.contents.value)]
        nnn = ndim.contents.value
        sma = 0.
        for i in range(nnn):
            sma += ave[i] * xx[i]
        ff[0] = math.cos(2. * math.pi * ucc[0] + sma)

        # res = 1.
        # for i in range(nnn):
        #    res *= 1./(1./math.pow(ave[i], 2) + math.pow(xx[i] - ucc[i], 2))
        #ff[0] = res
        return 0

    NDIM = nidim
    NCOMP = 1

    NNEW = 1000
    NMIN = 2
    FLATNESS = 50.

    KEY1 = 47
    KEY2 = 1
    KEY3 = 1
    MAXPASS = 5
    BORDER = 0.
    MAXCHISQ = 10.
    MINDEVIATION = .25
    NGIVEN = 0
    LDXGIVEN = NDIM
    NEXTRA = 0
    MINEVAL = 0
    MAXEVAL = 50000

    KEY = 0

    def print_header(name):
        print('-------------------- %s test -------------------' % name)

    def print_results(name, results):
        keys = ['nregions', 'neval', 'fail']
        text = ["%s %d" % (k, results[k]) for k in keys if k in results]
        print("%s RESULT:\t" % name.upper() + "\t".join(text))
        for comp in results['results']:
            print("%s RESULT:\t" % name.upper() +
                  "%(integral).8f +- %(error).8f\tp = %(prob).3f\n" % comp)

    # print_header('Vegas')
    vrs = pycuba.Vegas(IntegCuba, NDIM, verbose=0)
    # print_results('Vegas', vrs)
    v_result.append(vrs['results'][0]['integral'])

    # print_header('Divonne')
    cub = pycuba.Divonne(IntegCuba,
                         NDIM,
                         mineval=MINEVAL,
                         maxeval=MAXEVAL,
                         key1=KEY1,
                         key2=KEY2,
                         key3=KEY3,
                         maxpass=MAXPASS,
                         border=BORDER,
                         maxchisq=MAXCHISQ,
                         mindeviation=MINDEVIATION,
                         ldxgiven=LDXGIVEN,
                         verbose=0)
    # print_results('Divonne', cub)
    v_result.append(cub['results'][0]['integral'])

    for v in v_result:
        v = math.fabs((v - exact_res) / exact_res)
        r_vec.append(format_num(v))

    for tab_prime in nods:
        p_integ.set_values(1, nidim, tab_prime, 1, sigma=2)
        p_result = p_integ(fof.oscillatory_fcn)  # , eps=1e-4
        p_rel_res = math.fabs((p_result - exact_res) / exact_res)
        r_vec.append(format_num(p_rel_res))

    with open('data_oscillatory_a_norm1.csv', 'a', newline='') as file:
        wr = csv.writer(file, delimiter=',', quoting=csv.QUOTE_ALL)
        wr.writerow(r_vec)

    del p_integ
Ejemplo n.º 3
0
    print_header('Vegas')
    print_results('Vegas', pycuba.Vegas(Integrand, NDIM, verbose=2))

    print_header('Suave')
    print_results(
        'Suave',
        pycuba.Suave(Integrand, NDIM, NNEW, NMIN, FLATNESS, verbose=2 | 4))

    print_header('Divonne')
    print_results(
        'Divonne',
        pycuba.Divonne(Integrand,
                       NDIM,
                       mineval=MINEVAL,
                       maxeval=MAXEVAL,
                       key1=KEY1,
                       key2=KEY2,
                       key3=KEY3,
                       maxpass=MAXPASS,
                       border=BORDER,
                       maxchisq=MAXCHISQ,
                       mindeviation=MINDEVIATION,
                       ldxgiven=LDXGIVEN,
                       verbose=2))

    print_header('Cuhre')
    print_results('Cuhre', pycuba.Cuhre(Integrand,
                                        NDIM,
                                        key=KEY,
                                        verbose=2 | 4))
Ejemplo n.º 4
0
def run_cuba(**config):
	NDIM = config['ndim']
	loglikelihood = config['loglikelihood']
	def Integrand(ndim, xx, ncomp, ff, userdata):
		ff[0] = exp(loglikelihood([xx[i] for i in range(ndim.contents.value)]))
		return 0
	NCOMP = 1

	NNEW = 1000
	FLATNESS = 25.

	KEY1 = 47
	KEY2 = 1
	KEY3 = 1
	MAXPASS = 5
	BORDER = 0.
	MAXCHISQ = 10.
	MINDEVIATION = .25
	NGIVEN = 0
	LDXGIVEN = NDIM
	NEXTRA = 0
	MINEVAL = 100
	MAXEVAL = 2000000
	epsrel=0.5
	epsabs=1e-300
	
	KEY = 0
	
	commonargs = dict(
		mineval=MINEVAL, maxeval=MAXEVAL,
		epsrel=epsrel, epsabs=epsabs, 
		seed = config['seed'] + 1, # 0 stands for random
	)
	
	def print_results(name, results):
		keys = ['nregions', 'neval', 'fail']
		keys = list(filter(results.has_key, keys))
		text = ["%s %d" % (k, results[k]) for k in keys]
		print ("%s RESULT:\t" % name.upper() + "\t".join(text))
		for comp in results['results']:
			print ("%s RESULT:\t" % name.upper() + "%(integral).8f +- %(error).8f\tp = %(prob).3f\n" % comp)
	starttime = time.time()
	if config['cuba_algorithm'] == 'Vegas':
		results = pycuba.Vegas(Integrand, NDIM, verbose=2, **commonargs)
	elif config['cuba_algorithm'] == 'Suave':
		results = pycuba.Suave(Integrand, NDIM, NNEW, FLATNESS, verbose=2 | 4, **commonargs)
	elif config['cuba_algorithm'] == 'Divonne':
		results = pycuba.Divonne(Integrand, NDIM,
			key1=KEY1, key2=KEY2, key3=KEY3, maxpass=MAXPASS,
			border=BORDER, maxchisq=MAXCHISQ, mindeviation=MINDEVIATION,
			ldxgiven=LDXGIVEN, verbose=2, **commonargs)
	elif config['cuba_algorithm'] == 'Cuhre':
		results = pycuba.Cuhre(Integrand, NDIM, key=KEY, verbose=2 | 4, **commonargs)
  	else:
  		assert False, 'Unknown cuba algorithm "%s"!' % config['cuba_algorithm']
  	duration = time.time() - starttime
	print_results(config['cuba_algorithm'], results)
	Z = results['results'][0]['integral']
	Zerr = results['results'][0]['error']

	return dict(
		Z_computed = float(log(abs(Z) + 1e-300)),
		Z_computed_err = float(log(abs(Z+Zerr) + 1e-300) - log(abs(Z) + 1e-300)),
		failed=results['fail'] != 0 and Z >= 0 and Zerr >= 0,
		duration = duration,
	)