expsamplr: Single and Two Variable Expectation

Description Usage Arguments Details Value Examples

View source: R/expsamplr.R

Description

This function implements one and two variable rejection sampling to find expected values for one and two dimensional pdfs.

Usage

1
expsamplr(f, g = NULL)

Arguments

f

The pdf you wish to use to find the expectation of. For 2D probability density functions, the argument must be a vector of the two parameters of the pdf.

g

The function of random variables x and y for 2D that you wish to find the expected value of.

Details

For one dimensional variables, this function uses samplr to find E[X]

For two dimensional random variables, this function uses samplr to find E[g(X,Y)], where X and Y are found using samplr(f, 10000, twod = TRUE).

Value

For one dimensional pdfs the ouput is E[X] and for two dimensional pdfs the ouput is E[g(X,Y)].

Examples

 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
41
42
43
One dimensional

f <- function(x) {
  ifelse(0 < x & x < 1, 2*x, 0)
}
expsamplr(f)

f <- function(x) {
  ifelse(0 < x & x < 2, 1/2*x, 0)
}
expsamplr(f)

f <- function(x) {
  ifelse(0 < x & x < 6.2832, 1/2/pi*(sin(x) + 1), 0)
}
expsamplr(f)

Two dimensional

f <- function(z) {
  x <- z[1]
  y <- z[2]
  ifelse(0 <= x & x <= 1 & 0 <= y & y <= 1, x + y, 0)
}
g <- function(z) {
  x <- z[1]
  y <- z[2]
  x*y
}
expx <- function(z) {
  x <- z[1]
  y <- z[2]
  x
}
expy <- function(z) {
  x <- z[1]
  y <- z[2]
  y
}
expsamplr(f,f)
expsamplr(f,g)
expsamplr(f,expx)
expsamplr(f,expy)

bubj/samplr documentation built on May 28, 2019, 7:14 p.m.