|
| 1 | +# Integration tests for VEGAS + MonteCarlo/MCMC integral methods. |
| 2 | +import torch |
| 3 | +from integrators import MonteCarlo, MCMC, setup |
| 4 | +from maps import Vegas, Linear |
| 5 | +from utils import set_seed, get_device |
| 6 | +import torch.utils.benchmark as benchmark |
| 7 | +# set_seed(42) |
| 8 | +setup() |
| 9 | +device = get_device() |
| 10 | +# device = torch.device("cpu") |
| 11 | + |
| 12 | + |
| 13 | +def sharp_peak(x, f, dim=4): |
| 14 | + f.zero_() |
| 15 | + for d in range(dim): |
| 16 | + f[:, 0] += (x[:, d] - 0.5) ** 2 |
| 17 | + f[:, 0] *= -200 |
| 18 | + f[:, 0].exp_() |
| 19 | + return f[:, 0] |
| 20 | + |
| 21 | + |
| 22 | +def sharp_integrands(x, f, dim=4): |
| 23 | + f.zero_() |
| 24 | + for d in range(dim): |
| 25 | + f[:, 0] += (x[:, d] - 0.5) ** 2 |
| 26 | + f[:, 0] *= -200 |
| 27 | + f[:, 0].exp_() |
| 28 | + f[:, 1] = f[:, 0] * x[:, 0] |
| 29 | + f[:, 2] = f[:, 0] * x[:, 0] ** 2 |
| 30 | + return f.mean(dim=-1) |
| 31 | + |
| 32 | + |
| 33 | +def func(x, f): |
| 34 | + f[:, 0] = torch.log(x[:, 0]) / torch.sqrt(x[:, 0]) |
| 35 | + return f[:, 0] |
| 36 | + |
| 37 | + |
| 38 | +ninc = 1000 |
| 39 | +n_eval = 50000 |
| 40 | +n_batch = 10000 |
| 41 | +n_therm = 10 |
| 42 | + |
| 43 | +print("\nCalculate the integral log(x)/x^0.5 in the bounds [0, 1]") |
| 44 | + |
| 45 | +print("train VEGAS map") |
| 46 | +vegas_map = Vegas([(0, 1)], device=device, ninc=ninc) |
| 47 | +vegas_map.train(20000, func, epoch=10, alpha=0.5, multigpu=True) |
| 48 | + |
| 49 | +vegas_integrator = MonteCarlo( |
| 50 | + maps=vegas_map, |
| 51 | + neval=1000000, |
| 52 | + nbatch=n_batch, |
| 53 | + device=device, |
| 54 | +) |
| 55 | +res = vegas_integrator(func, multigpu=True) |
| 56 | +result=benchmark.Timer( |
| 57 | + stmt="vegas_integrator(func, multigpu=True)", |
| 58 | + globals=globals() |
| 59 | +) |
| 60 | +print(result.timeit(10)) |
| 61 | +print("VEGAS Integral results: ", res) |
| 62 | + |
| 63 | +vegasmcmc_integrator = MCMC( |
| 64 | + maps=vegas_map, |
| 65 | + neval=1000000, |
| 66 | + nbatch=n_batch, |
| 67 | + nburnin=n_therm, |
| 68 | + device=device, |
| 69 | +) |
| 70 | +res = vegasmcmc_integrator(func, mix_rate=0.5, multigpu=True) |
| 71 | +result=benchmark.Timer( |
| 72 | + stmt="vegasmcmc_integrator(func, mix_rate=0.5, multigpu=True)", |
| 73 | + globals=globals() |
| 74 | +) |
| 75 | +print(result.timeit(10)) |
| 76 | +print("VEGAS-MCMC Integral results: ", res) |
| 77 | +print(type(res)) |
| 78 | + |
| 79 | +# Start Monte Carlo integration, including plain-MC, MCMC, vegas, and vegas-MCMC |
| 80 | +print("\nCalculate the integral [h(X), x1 * h(X), x1^2 * h(X)] in the bounds [0, 1]^4") |
| 81 | +print("h(X) = exp(-200 * (x1^2 + x2^2 + x3^2 + x4^2))") |
| 82 | + |
| 83 | +bounds = [(0, 1)] * 4 |
| 84 | +vegas_map = Vegas(bounds, device=device, ninc=ninc) |
| 85 | +print("train VEGAS map for h(X)...") |
| 86 | +vegas_map.train(20000, sharp_peak, epoch=10, alpha=0.5, multigpu=True) |
| 87 | +# print(vegas_map.extract_grid()) |
| 88 | + |
| 89 | +print("VEGAS Integral results:") |
| 90 | +vegas_integrator = MonteCarlo( |
| 91 | + maps=vegas_map, |
| 92 | + neval=n_eval, |
| 93 | + nbatch=n_batch, |
| 94 | + device=device, |
| 95 | +) |
| 96 | +res = vegas_integrator(sharp_integrands, f_dim=3, multigpu=True) |
| 97 | +print( |
| 98 | + " I[0] =", |
| 99 | + res[0], |
| 100 | + " I[1] =", |
| 101 | + res[1], |
| 102 | + " I[2] =", |
| 103 | + res[2], |
| 104 | + " I[1]/I[0] =", |
| 105 | + res[1] / res[0], |
| 106 | +) |
| 107 | +print(type(res)) |
| 108 | +print(type(res[0])) |
| 109 | +print(res[0].sum_neval) |
| 110 | +print(res[0].itn_results) |
| 111 | +print(res[0].nitn) |
| 112 | + |
| 113 | + |
| 114 | +print("VEGAS-MCMC Integral results:") |
| 115 | +vegasmcmc_integrator = MCMC( |
| 116 | + maps=vegas_map, |
| 117 | + neval=n_eval, |
| 118 | + nbatch=n_batch, |
| 119 | + nburnin=n_therm, |
| 120 | + device=device, |
| 121 | +) |
| 122 | +res = vegasmcmc_integrator(sharp_integrands, f_dim=3, mix_rate=0.5, multigpu=True) |
| 123 | +print( |
| 124 | + " I[0] =", |
| 125 | + res[0], |
| 126 | + " I[1] =", |
| 127 | + res[1], |
| 128 | + " I[2] =", |
| 129 | + res[2], |
| 130 | + " I[1]/I[0] =", |
| 131 | + res[1] / res[0], |
| 132 | +) |
0 commit comments