Caution
Section omitted to comply with the Honor Code
Bayesian Statistics: Mixture Models
Oren Bochman
Mixture Models, Homework
Section omitted to comply with the Honor Code
---
title : 'Homework on simulating from a Poisson Mixture Model - M1L2HW4'
subtitle : 'Bayesian Statistics: Mixture Models'
categories:
- Bayesian Statistics
keywords:
- Mixture Models
- Homework
---
::::: {.content-visible unless-profile="HC"}
::: {.callout-caution}
Section omitted to comply with the Honor Code
:::
:::::
::::: {.content-hidden unless-profile="HC"}
::: {.callout-note}
### Instructions
```r
# Generate n observations from a mixture of two Gaussian
# distributions
n = 50 # Size of the sample to be generated
w = c(0.6, 0.4) # Weights
mu = c(0, 5) # Means
sigma = c(1, 2) # Standard deviations
cc = sample(1:2, n, replace=T, prob=w)
x = rnorm(n, mu[cc], sigma[cc])
# Plot f(x) along with the observations
# just sampled
xx = seq(-5, 12, length=200)
yy = w[1]*dnorm(xx, mu[1], sigma[1]) +
w[2]*dnorm(xx, mu[2], sigma[2])
par(mar=c(4,4,1,1)+0.1)
plot(xx, yy, type="l", ylab="Density", xlab="x", las=1, lwd=2)
points(x, y=rep(0,n), pch=1)
```
- Modify the code above to sample 200 random numbers from a mixture of 3 Poisson distributions with means 1, 2 and 6 and weights 0.7, 0.2 and 0.1, respectively, and
- generate a barplot with the empirical frequencies of all the integers included in the sample.
:::
> Enter code to sample 200 random numbers from a mixture of 3 Poisson distributions with means 1, 2 and 6 and weights 0.7, 0.2 and 0.1, respectively
```{r}
#| label: lst-poisson-mix
# Generate n observations from a mixture of three poisson distributions
set.seed(452) #<1>
n = 200 # <2>
w = c(0.7,0.2,0.1) # <3>
lambda = c(1,2,6) # <4>
ac = sample(1:3,n, replace=T, prob=w) # <5>
x = rpois(n,lambda=lambda[ac]) # <6>
```
1. Ensure simulation is reproducible
2. Set sample size to 50
3. Set weights for the Poisson distributions
4. Set the means for the Poisson distributions
5. Sample the active component for each observation in the sample
6. simulate an ac mixture component n times
>Plot the empirical distribution
```{r}
#| label: fig-poisson-mix-1
#| fig-cap: "Empirical distribution for Poisson mixture"
empfreq = table(x)/n
par(mar=c(4,4,1,1) + 0.1)
#barplot(empfreq)
barplot(empfreq,xlab="counts",ylab="probability",main="Empirical distribution for Poisson mixture")
```
:::::