Bernoulli: (Weighted) MLE of Bernoulli Distribution

Description Usage Arguments Value Author(s) Examples

View source: R/Bernoulli.R

Description

Bernoulli distribution is characterized by the following probability mass function,

f(x;p) = p^x (1-p)^{1-x}

where the domain is x \in \lbrace 0,1 \rbrace with proportion parameter p \in [0,1].

Usage

1
Bernoulli(x, weight = NULL)

Arguments

x

a length-n vector of values \lbrace 0, 1 \rbrace.

weight

a length-n weight vector. If set as NULL, it gives an equal weight, leading to standard MLE.

Value

a named list containing (weighted) MLE of

p

proportion parameter p.

Author(s)

Kisung You

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
#  generate data with p=0.5
x = sample(0:1, 50, replace=TRUE, prob=c(0.5,0.5))

#  fit unweighted
Bernoulli(x)

## Not run: 
# put random weights to see effect of weights
niter = 1000
ndata = 200

# generate data as above and fit unweighted MLE
x    = sample(0:1, ndata, replace=TRUE, prob=c(0.5,0.5))
xmle = Bernoulli(x)

# iterate
vec.p = rep(0,niter)
for (i in 1:niter){
  # random weight
  ww = abs(stats::rnorm(ndata))
  
  # fit
  MLE = Bernoulli(x, weight=ww)
  vec.p[i] = MLE$p
  if ((i%%10) == 0){
    print(paste0(" iteration ",i,"/",niter," complete.."))
  }
}

# distribution of weighted estimates + standard MLE
opar <- par(no.readonly=TRUE)
hist(vec.p, main="proportion 'p'")
abline(v=xmle$p, lwd=3, col="red")
par(opar)

## End(Not run) 

kyoustat/T4mle documentation built on March 26, 2020, 12:09 a.m.