View source: R/expected_value.R
expected_value | R Documentation |
This function takes the name of a probability density/mass function as an argument and creates a function to compute the expected value.
expected_value(f, parameters, support, g = identity, routine = NULL, ...)
f |
a character with the probability density/mass function name. The
function must be availble in the |
parameters |
a list with the input parameters for the distribution. |
support |
a list with the following entries:
|
g |
a given function |
routine |
a character specifying the integration routine.
|
... |
further arguments for the integration routine. |
the expected value of the specified distribution.
Jaime Mosquera GutiƩrrez, jmosquerag@unal.edu.co
Other distributions utilities:
cum_hazard_fun()
,
hazard_fun()
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
)
#----------------------------------------------------------------------------
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.