The function to be integrated has its own datatype, the GSL::Monte::Function class.
GSL::Munte::Function.alloc(proc, dim, params)GSL::Munte::Function.alloc(proc, dim)ex:
proc_f = Proc.new { |x, dim, params|
a = params[0]; b = params[1]; c = params[2]
if dim != 2; raise("dim != 2"); end
a*x[0]*x[0] + b*x[0]*x[1] + c*x[1]*x[1]
}
dim = 2
mf = Monte::Function.alloc(proc_f, dim)
mf.set_params([3, 2, 1]) GSL::Munte::Function#set(proc, dim, params)GSL::Munte::Function#set(proc, dim)GSL::Munte::Function#set(proc)GSL::Munte::Function#set_proc(proc)GSL::Munte::Function#set_proc(proc, dim)GSL::Munte::Function#set_params(params)GSL::Munte::Function#paramsGSL::Munte::Function#evalGSL::Munte::Function#callGSL::Monte::Plain.alloc(dim)GSL::Monte::Plain#initGSL::Monte::Miser.alloc(dim)GSL::Monte::Miser#initGSL::Monte::Vegas.alloc(dim)GSL::Monte::Vegas#initGSL:Monte::Function#integrate(xl, xu, dim, calls, rng, s)GSL:Monte::Function#integrate(xl, xu, dim, calls, s)GSL:Monte::Function#integrate(xl, xu, calls, rng, s)GSL:Monte::Function#integrate(xl, xu, calls, s)This method performs Monte-Carlo integration of the function self using the algorithm s, over the dim-dimensional hypercubic region defined by the lower and upper limits in the arrays xl and xu, each of size dim. The integration uses a fixed number of function calls calls. The argument rng is a random number generator (optional). If it is not given, a new generator is created internally and freed when the calculation finishes.
See sample scripts sample/monte*.rb for more details.
GSL::Monte::Miser#estimate_fracGSL::Monte::Miser#estimate_frac=GSL::Monte::Miser#min_callsGSL::Monte::Miser#min_calls=GSL::Monte::Miser#min_call_per_bisectionGSL::Monte::Miser#min_calls_per_bisection=GSL::Monte::Miser#alphaGSL::Monte::Miser#alpha=GSL::Monte::Miser#ditherGSL::Monte::Miser#dither=GSL::Monte::Vegas#alphaGSL::Monte::Vegas#resultGSL::Monte::Vegas#sigmaGSL::Monte::Vegas#chisqGSL::Monte::Vegas#runvalGSL::Monte::Vegas#iterationsGSL::Monte::Vegas#iterations=GSL::Monte::Vegas#alphaGSL::Monte::Vegas#alpha=GSL::Monte::Vegas#stageGSL::Monte::Vegas#stage=GSL::Monte::Vegas#modeGSL::Monte::Vegas#mode=GSL::Monte::Vegas#verboseGSL::Monte::Vegas#verbose=GSL::Monte::Miser#params_getGSL::Monte::Miser::Params class.GSL::Monte::Miser#params_set(params)GSL::Monte::Miser::Params class params.GSL::Monte::Miser::ParamsGSL::Monte::Miser::Params#estimate_fracGSL::Monte::Miser::Params#estimate_frac=GSL::Monte::Miser::Params#min_callsGSL::Monte::Miser::Params#min_calls=GSL::Monte::Miser::Params#min_calls_per_bisectionGSL::Monte::Miser::Params#min_calls_per_bisection=GSL::Monte::Miser::Params#alphaGSL::Monte::Miser::Params#alpha=GSL::Monte::Miser::Params#ditherGSL::Monte::Miser::Params#dither=This parameter introduces a random fractional variation of size dither into each bisection, which can be used to break the symmetry of integrands which are concentrated near the exact center of the hypercubic integration region. The default value of dither is zero, so no variation is introduced. If needed, a typical value of dither is 0.1.
GSL::Monte::Vegas#params_getGSL::Monte::Vegas::Params class.GSL::Monte::Vegas#params_set(params)GSL::Monte::Vegas::Params class params.GSL::Monte::Vegas::ParamsGSL::Monte::Vegas::Params#alphaGSL::Monte::Vegas::Params#alpha=GSL::Monte::Vegas::Params#iterationsGSL::Monte::Vegas::Params#iterations=GSL::Monte::Vegas::Params#stageGSL::Monte::Vegas::Params#stage=GSL::Monte::Vegas::Params#modeGSL::Monte::Vegas::Params#mode=GSL::VEGAS::MODE_IMPORTANCE, GSL::VEGAS::MODE_STRATIFIED, GSL::VEGAS::MODE_IMPORTANCE_ONLY. This determines whether VEGAS will use importance sampling or stratified sampling, or whether it can pick on its own. In low dimensions VEGAS uses strict stratified sampling (more precisely, stratified sampling is chosen if there are fewer than 2 bins per box).GSL::Monte::Vegas::Params#verboseGSL::Monte::Vegas::Params#verbose=#!/usr/bin/env ruby
require("gsl")
include GSL::Monte
include Math
proc_f = Proc.new { |k, dim, params|
pi = Math::PI
a = 1.0/(pi*pi*pi)
a/(1.0 - cos(k[0])*cos(k[1])*cos(k[2]))
}
def display_results(title, result, error)
exact = 1.3932039296856768591842462603255
diff = result - exact
printf("%s ==================\n", title);
printf("result = % .6f\n", result);
printf("sigma = % .6f\n", error);
printf("exact = % .6f\n", exact);
printf("error = % .6f = %.1g sigma\n", diff, diff.abs/error)
end
dim = 3
xl = Vector.alloc(0, 0, 0)
xu = Vector.alloc(PI, PI, PI)
G = Monte::Function.alloc(proc_f, dim)
calls = 500000
r = GSL::Rng.alloc(Rng::DEFAULT)
plain = Monte::Plain.alloc(dim)
result, error = G.integrate(xl, xu, dim, calls, r, plain)
display_results("plain", result, error)
miser = Monte::Miser.alloc(dim)
result, error = G.integrate(xl, xu, dim, calls, r, miser)
display_results("miser", result, error)
vegas = Monte::Vegas.alloc(dim)
result, error = G.integrate(xl, xu, dim, 10000, r, vegas)
display_results("vegas warm-up", result, error)
puts("converging...");
begin
result, error = G.integrate(xl, xu, dim, calls/5, r, vegas)
printf("result = % .6f sigma = % .6f chisq/dof = %.1f\n",
result, error, vegas.chisq)
end while (vegas.chisq-1.0).abs > 0.5
display_results("vegas final", result, error)