Calculate probability density function of a mixture of univariate normal distributions.

dnormix(x, lambda, mu, sigma, log = FALSE)

Arguments

x

`numeric` - Vector of values at which to evaluate the density

lambda

`numeric` - Vector of mixing proportions of each normal component

mu

`numeric` - Vector of means of each normal component

sigma

`numeric` - Vector of standard deviations of each normal component

log

`logical` - Logical value indicating whether to return log density (TRUE) or density (FALSE)

Value

`numeric`; Vector of densities or log densities

Examples

dnormix(-5:5, c(0.7, 0.3), c(0, 3), c(1, 1))
#>  [1] 1.040704e-06 9.368116e-05 3.102296e-03 3.779412e-02 1.694197e-01
#>  [6] 2.805892e-01 1.855768e-01 1.103849e-01 1.227850e-01 7.268490e-02
#> [11] 1.619833e-02

# visualize
p <- function(x) {dnormix(x, c(0.7, 0.3), c(0, 3), c(1, 1))}
lp <- function(x) {dnormix(x, c(0.7, 0.3), c(0, 3), c(1, 1), log=TRUE)}
par(mfrow=c(1, 2))
curve(p, -5, 5)
curve(lp, -5, 5)

par(mfrow=c(1, 1))

# supports fast numerical integration without underflow/overflow
integrate(p, -Inf, Inf) # equals 1
#> 1 with absolute error < 9.5e-06
integrate(function(x){- p(x) * lp(x)}, -Inf, Inf) # entropy
#> 1.879838 with absolute error < 0.00015