Expected value of a given function for any distribution
Source:R/expected_value.R
expected_value.Rd
This function takes the name of a probability density/mass function as an argument and creates a function to compute the expected value.
Arguments
- f
a character with the probability density/mass function name. The function must be availble in the
R
environment using the usual nomenclature (d
prefix before the name).- parameters
a list with the input parameters for the distribution.
- support
a list with the following entries:
interval
: a two dimensional atomic vector indicating the set of possible values of a random variable having the distribution specified iny_dist
.type
: character indicating if distribution has adiscrete
or acontinous
random variable.
- g
a given function \(g(x)\). If
g = identity
, then \(g(x) = x\) and this is actually the mean of the distribution.- routine
a character specifying the integration routine.
integrate
andgauss_quad
are available for continuous distributions, andsummate
for discrete ones. Custom routines can be defined but they must be compatible with theintegration
API.- ...
further arguments for the integration routine.
See also
Other distributions utilities:
cum_hazard_fun()
,
hazard_fun()
Author
Jaime Mosquera Gutiérrez, jmosquerag@unal.edu.co
Examples
library(EstimationTools)
#----------------------------------------------------------------------------
# Example 1: mean of X ~ N(2, 1) using 'integrate' under the hood.
support <- list(interval=c(-Inf, Inf), type = "continuous")
expected_value(
f = "dnorm",
parameters = list(mean = 2, sd = 1),
support = support
)
#> [1] 2
# Equivalent to
expected_value(
f = "dnorm",
parameters = list(mean = 2, sd = 1),
support = support,
g = identity,
routine = "integrate"
)
#> [1] 2
# Example 1: mean of X ~ N(22, 1)
# 'integrate' fails because the mean is 22.
expected_value(
f = "dnorm",
parameters = list(mean = 22, sd = 1),
support = support
)
#> [1] 9.111217e-08
# Let's compute with Monte Carlo integration
expected_value(
f = "dnorm",
parameters = list(mean = 22, sd = 1),
support = support,
routine = "monte-carlo"
)
#> [1] 22.00126
# Compute Monte Carlo integration with more samples
# \donttest{
expected_value(
f = "dnorm",
parameters = list(mean = 22, sd = 1),
support = support,
routine = "monte-carlo",
n = 1e8
)
#> [1] 22.00005
# }
#----------------------------------------------------------------------------