Description Usage Arguments Value Examples
Numerical integration using adaptive quadrature
1 2 3 | double_integrate(fn, x1_lims, x2_lims)
triple_integrate(fn, x1_lims, x2_lims, x3_lims)
|
fn |
A function that takes two or three numeric arguments (for double_integrate or triple_integrate, respectively) and which returns a single number |
x1_lims |
A length-two numeric vector giving the lower and upper bound of integration (e.g. c(0, 100)) |
x2_lims |
A length-two numeric vector giving the lower and upper bound of integration (e.g. c(0, 2*pi)) |
x3_lims |
A length-two numeric vector giving the lower and upper bound of integration (e.g. c(-Inf, Inf)) |
A single number, representing the numeric integral calculated using adaptive quadrature.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # Area of a cylinder!
# Radius 2, height 5
perimeter_of_circle <- function(r) {
2*pi*r
}
double_integrate(function(r, height) perimeter_of_circle(r),
x1_lims = c(0, 2),
x2_lims = c(0, 5))
# Joint density of three standard normal variables
d_x1x2x3 <- function(x1, x2, x3) {
dnorm(x1)*dnorm(x2)*dnorm(x3)
}
triple_integrate(d_x1x2x3, c(-Inf, Inf), c(-Inf, Inf), c(-Inf, Inf))
# Probability that sum of three squared standard normal variables
is less than 9
supported_density <- function(k, x1, x2, x3) {
sum_squares <- sum(c(x1, x2, x3)^2)
if (sum_squares <= k) {
dnorm(x1)*dnorm(x2)*dnorm(x3)
} else {
0
}
}
triple_integrate(fn = function(x1, x2, x3) {
supported_density(k = 9, x1, x2, x3)
},
x1_lims = c(-Inf, Inf),
x2_lims = c(-Inf, Inf),
x3_lims = c(-Inf, Inf))
# Compare to the analytic result (Chi-squared distribution)
pchisq(9, df = 3)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.