Skip to contents

[Experimental]

This function takes the name of a probability density/mass function as an argument and creates a function to compute the expected value.

Usage

expected_value(f, parameters, support, g = identity, routine = NULL, ...)

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 in y_dist.

  • type: character indicating if distribution has a discrete or a continous 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 and gauss_quad are available for continuous distributions, and summate for discrete ones. Custom routines can be defined but they must be compatible with the integration API.

...

further arguments for the integration routine.

Value

the expected value of the specified distribution.

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
# }

#----------------------------------------------------------------------------