Skip to content

Commit 240adc2

Browse files
authored
Merge pull request #34 from numericalEFT/pchou
add 3 examples
2 parents 6d94497 + 1cca1bd commit 240adc2

File tree

4 files changed

+178
-131
lines changed

4 files changed

+178
-131
lines changed

examples/example_1.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Example 1: Unit Circle and Half-Sphere Integrands Comparison
2+
3+
import torch
4+
from MCintegration import MonteCarlo, MarkovChainMonteCarlo, Vegas, set_seed, get_device
5+
6+
set_seed(42)
7+
device = get_device()
8+
9+
10+
def unit_circle_integrand(x, f):
11+
f[:, 0] = (x[:, 0] ** 2 + x[:, 1] ** 2 < 1).double()
12+
return f[:, 0]
13+
14+
15+
def half_sphere_integrand(x, f):
16+
f[:, 0] = torch.clamp(1 - (x[:, 0] ** 2 + x[:, 1] ** 2), min=0) * 2
17+
return f[:, 0]
18+
19+
20+
dim = 2
21+
bounds = [(-1, 1), (-1, 1)]
22+
n_eval = 6400000
23+
batch_size = 10000
24+
n_therm = 20
25+
26+
vegas_map = Vegas(dim, device=device, ninc=10)
27+
28+
# Monte Carlo and MCMC for Unit Circle
29+
mc_integrator = MonteCarlo(
30+
f=unit_circle_integrand, bounds=bounds, batch_size=batch_size
31+
)
32+
mcmc_integrator = MarkovChainMonteCarlo(
33+
f=unit_circle_integrand, bounds=bounds, batch_size=batch_size, nburnin=n_therm
34+
)
35+
36+
print("Unit Circle Integration Results:")
37+
print("Plain MC:", mc_integrator(n_eval))
38+
print("MCMC:", mcmc_integrator(n_eval, mix_rate=0.5))
39+
40+
# Train VEGAS map for Unit Circle
41+
vegas_map.adaptive_training(batch_size, unit_circle_integrand, alpha=0.5)
42+
vegas_integrator = MonteCarlo(
43+
bounds, f=unit_circle_integrand, maps=vegas_map, batch_size=batch_size
44+
)
45+
vegasmcmc_integrator = MarkovChainMonteCarlo(
46+
bounds,
47+
f=unit_circle_integrand,
48+
maps=vegas_map,
49+
batch_size=batch_size,
50+
nburnin=n_therm,
51+
)
52+
53+
print("VEGAS:", vegas_integrator(n_eval))
54+
print("VEGAS-MCMC:", vegasmcmc_integrator(n_eval, mix_rate=0.5))
55+
56+
# Monte Carlo and MCMC for Half-Sphere
57+
mc_integrator.f = half_sphere_integrand
58+
mcmc_integrator.f = half_sphere_integrand
59+
60+
print("\nHalf-Sphere Integration Results:")
61+
print("Plain MC:", mc_integrator(n_eval))
62+
print("MCMC:", mcmc_integrator(n_eval, mix_rate=0.5))
63+
64+
vegas_map.make_uniform()
65+
vegas_map.adaptive_training(batch_size, half_sphere_integrand, epoch=10, alpha=0.5)
66+
vegas_integrator.f = half_sphere_integrand
67+
vegasmcmc_integrator.f = half_sphere_integrand
68+
69+
print("VEGAS:", vegas_integrator(n_eval))
70+
print("VEGAS-MCMC:", vegasmcmc_integrator(n_eval, mix_rate=0.5))

examples/example_2.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Example 2: Sharp Peak Integrand in Higher Dimensions
2+
3+
import torch
4+
from MCintegration import MonteCarlo, MarkovChainMonteCarlo, Vegas, set_seed, get_device
5+
6+
set_seed(42)
7+
device = get_device()
8+
9+
10+
def sharp_integrands(x, f):
11+
f[:, 0] = torch.sum((x - 0.5) ** 2, dim=-1)
12+
f[:, 0] *= -200
13+
f[:, 0].exp_()
14+
f[:, 1] = f[:, 0] * x[:, 0]
15+
f[:, 2] = f[:, 0] * x[:, 0] ** 2
16+
return f.mean(dim=-1)
17+
18+
19+
dim = 4
20+
bounds = [(0, 1)] * dim
21+
n_eval = 500000
22+
batch_size = 10000
23+
n_therm = 20
24+
25+
vegas_map = Vegas(dim, device=device, ninc=1000)
26+
27+
# Plain MC and MCMC
28+
mc_integrator = MonteCarlo(
29+
f=sharp_integrands, f_dim=3, bounds=bounds, batch_size=batch_size
30+
)
31+
mcmc_integrator = MarkovChainMonteCarlo(
32+
f=sharp_integrands, f_dim=3, bounds=bounds, batch_size=batch_size, nburnin=n_therm
33+
)
34+
35+
print("Sharp Peak Integration Results:")
36+
print("Plain MC:", mc_integrator(n_eval))
37+
print("MCMC:", mcmc_integrator(n_eval, mix_rate=0.5))
38+
39+
# Train VEGAS map
40+
vegas_map.adaptive_training(batch_size, sharp_integrands, f_dim=3, epoch=10, alpha=2.0)
41+
vegas_integrator = MonteCarlo(
42+
bounds, f=sharp_integrands, f_dim=3, maps=vegas_map, batch_size=batch_size
43+
)
44+
vegasmcmc_integrator = MarkovChainMonteCarlo(
45+
bounds,
46+
f=sharp_integrands,
47+
f_dim=3,
48+
maps=vegas_map,
49+
batch_size=batch_size,
50+
nburnin=n_therm,
51+
)
52+
53+
print("VEGAS:", vegas_integrator(n_eval))
54+
print("VEGAS-MCMC:", vegasmcmc_integrator(n_eval, mix_rate=0.5))

examples/example_3.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Example 3: Integration of log(x)/sqrt(x) using VEGAS
2+
3+
import torch
4+
from MCintegration import MonteCarlo, MarkovChainMonteCarlo
5+
from MCintegration import Vegas, set_seed, get_device
6+
7+
set_seed(42)
8+
device = get_device()
9+
10+
11+
def func(x, f):
12+
f[:, 0] = torch.log(x[:, 0]) / torch.sqrt(x[:, 0])
13+
return f[:, 0]
14+
15+
16+
dim = 1
17+
bounds = [[0, 1]] * dim
18+
n_eval = 500000
19+
batch_size = 10000
20+
alpha = 2.0
21+
ninc = 1000
22+
n_therm = 20
23+
24+
vegas_map = Vegas(dim, device=device, ninc=ninc)
25+
26+
# Train VEGAS map
27+
print("Training VEGAS map for log(x)/sqrt(x)... \n")
28+
vegas_map.adaptive_training(batch_size, func, epoch=10, alpha=alpha)
29+
30+
print("Integration Results for log(x)/sqrt(x):")
31+
32+
33+
# Plain MC Integration
34+
mc_integrator = MonteCarlo(bounds, func, batch_size=batch_size)
35+
print("Plain MC Integral Result:", mc_integrator(n_eval))
36+
37+
# MCMC Integration
38+
mcmc_integrator = MarkovChainMonteCarlo(
39+
bounds, func, batch_size=batch_size, nburnin=n_therm
40+
)
41+
print("MCMC Integral Result:", mcmc_integrator(n_eval, mix_rate=0.5))
42+
43+
# Perform VEGAS integration
44+
vegas_integrator = MonteCarlo(bounds, func, maps=vegas_map, batch_size=batch_size)
45+
res = vegas_integrator(n_eval)
46+
47+
print("VEGAS Integral Result:", res)
48+
49+
# VEGAS-MCMC Integration
50+
vegasmcmc_integrator = MarkovChainMonteCarlo(
51+
bounds, func, maps=vegas_map, batch_size=batch_size, nburnin=n_therm
52+
)
53+
res_vegasmcmc = vegasmcmc_integrator(n_eval, mix_rate=0.5)
54+
print("VEGAS-MCMC Integral Result:", res_vegasmcmc)

examples/vegas_test.py

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)