Caution
Section omitted to comply with the Honor Code
Bayesian Statistics: Mixture Models
Oren Bochman
Mixture Models, Homework Honors
Section omitted to comply with the Honor Code
---
title : 'Homework Sim mixture of exponential distributions - M1L2HW6'
subtitle : 'Bayesian Statistics: Mixture Models'
categories:
- Bayesian Statistics
keywords:
- Mixture Models
- Homework Honors
---
::::: {.content-visible unless-profile="HC"}
::: {.callout-caution}
Section omitted to comply with the Honor Code
:::
:::::
::::: {.content-hidden unless-profile="HC"}
::: {.callout-note}
### Instructions
- Modify code to Generate n observations from a mixture of two Gaussian # distributions into code to sample 100 random numbers from a mixture of 4 exponential distributions with means 1, 4, 7 and 10 and weights 0.3, 0.25, 0.25 and 0.2, respectively.
- Use these sample to approximate the mean and variance of the mixture.
:::
```{r}
#| label: C3-L01-Ex6-1
set.seed(238) # reproducibility
n <- 100 # the sample size
w <- c(0.3, 0.25, 0.25, 0.2) # weights
lambda <- c(1, 4, 7 , 10) # means
rates <- 1 / lambda # inverses of the means
ac <- sample (1:length(w), n, replace=T, prob =w) # smaple the active component
x <- rexp(n,rate = rates[ac]) # sample from the exponential distribution
```
next we use the samples to estimate the mean and variance of the mixture
```{r}
#| label: C3-L01-Ex6-2
(sample_mean=mean(x))
(sample_variance=var(x))
```
:::::