Zero inflated negative binomial distribution

x = seq(0, 15)
y = dnbinom(x, 8, 0.6)
z = 0.2*c(1,rep(0,length(x)-1)) + (1-0.2)*y
par(mfrow=c(2,1))
par(mar=c(4,4,2,2)+0.1)
barplot(y, names.arg=x, las=1, xlab = "x", ylab="Probability", 
        border=NA, main="Negative Binomial")
par(mar=c(4,4,1,1)+0.1)
barplot(z, names.arg=x, las=1, xlab = "x", ylab="Probability", 
        border=NA, main="Zero-inflated Negative Binomial")

Zero-inflated log Gaussian distribution

x = seq(-2, 15, length=1000)
y = plnorm(x, 1.5, 0.5)
z = 0.3*as.numeric(x>=0) + (1-0.3)*y
par(mar=c(4,4,1,1)+0.1)
plot(x, y, type="l", las=1, lty=2, xlab="x", 
     ylab="Cumulative distribution Function", lwd=2)
lines(x, z, lty=1, lwd=2)
legend(4, 0.45, c("Zero infla. log Gaussian","log Gaussian"), 
     lty=c(1,2), bty="n", lwd=c(2,2))

Consider a zero-inflated mixture that involves a point mass at \(0\) with weight \(0.3\) and an exponential distribution of mean \(1\) and weight \(0.7\). What is the mean of this mixture?

x = seq(-2, 15, length=1000)
y = dexp(x, rate=1)
z = 0.3*as.numeric(x>=0) + (1-0.3)*y
par(mar=c(4,4,1,1)+0.1)
plot(x, y, type="l", las=1, lty=2, xlab="x", 
     ylab="Cumulative distribution Function", lwd=2)
lines(x, z, lty=1, lwd=2)
legend(4, 0.45, c("Zero-inflated Exponential","Exponential"), 
     lty=c(1,2), bty="n", lwd=c(2,2))

mean(z)
## [1] 0.3057554

Consider a zero-inflated mixture that involves a point mass at \(0\) with weight \(0.2\), a Gamma distribution with mean \(1\), variance \(2\) and weight \(0.5\), and another Gamma distribution with mean \(2\), variance \(4\) and weight \(0.3\). What is the variance of this mixture?

x = seq(-2, 15, length=1000)
a = dgamma(x, 0.5, 0.5)
b = dgamma(x, 1, 0.5)
c = as.numeric(x>=0) 
z = 0.5*a+0.3*b+0.2*c
par(mar=c(4,4,1,1)+0.1)
plot(x, z, type="l", las=1, lty=2, xlab="x", 
     ylab="Mixture Function", lwd=2)

# legend(4, 0.45, c("Zero infla. log Gaussian","log Gaussian"), lty=c(1,2), bty="n", lwd=c(2,2))

Recall that \[Var(X) = E(X^2) - [E(X)]^2\] and that expectations are linear functionals. In the videos, we showed that \[E(X^2) = \sum_{k=1}^K w_k \left[Var_{g_k}(X) + [E_{g_k}(X)]^2 \right]\] where \(g_k\) is the \(k\)th component of the mixture

expected_z = 0.5*(1) + 0.3*(2)
expected_z2 = (.3)*(4+(2)^2)+(0.5)*(2+(1)^2)
var_z=expected_z2-expected_z^2
var_z
## [1] 2.69