expected_value: Expected value of a given function for any distribution

View source: R/expected_value.R

expected_valueR Documentation

Expected value of a given function for any distribution

Description

[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.

Author(s)

Jaime Mosquera GutiƩrrez, jmosquerag@unal.edu.co

See Also

Other distributions utilities: cum_hazard_fun(), hazard_fun()

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
)

# Equivalent to
expected_value(
  f = "dnorm",
  parameters = list(mean = 2, sd = 1),
  support = support,
  g = identity,
  routine = "integrate"
)

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

# Let's compute with Monte Carlo integration
expected_value(
  f = "dnorm",
  parameters = list(mean = 22, sd = 1),
  support = support,
  routine = "monte-carlo"
)

# Compute Monte Carlo integration with more samples

expected_value(
  f = "dnorm",
  parameters = list(mean = 22, sd = 1),
  support = support,
  routine = "monte-carlo",
  n = 1e8
)


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


Jaimemosg/EstimationTools documentation built on Oct. 23, 2023, 10 a.m.