stan-dev/stan — pull request #1146 by rtrangucci (2014-11-22 00:33:20+00:00)
Title: Feature/issue 970 mix fun

#### Summary:

Addresses issue #970, with the addition of the binary mixture function

#### Intended Effect:

* Adds a function, log_mix, to simplify the binary mixture model syntax
* fvar version
* var version
* exposed log_mix to parser

#### How to Verify:

* src/test/unit/agrad/fwd/functions/log_mix_test.cpp
* src/test/unit/agrad/rev/functions/log_mix_test.cpp
* src/test/unit/gm/parser/math_functions_test.cpp

The last test will have 83 function signature tests compared to the 82 in develop.

Furthermore, the following model will run correctly in this branch:

```
data {
int N;
vector[N] y;
}
parameters {
real theta;
vector[2] sigmas;
vector[2] means;
}
model {
vector[N] lp_vec;

theta ~ beta(6,4);

sigmas ~ normal(1,2);

means[1] ~ normal(10,1);
means[2] ~ normal(-1,1);

for(i in 1:N)
lp_vec[i] <- log_mix(theta,normal_log(y[i],means[1],sigmas[1]),
normal_log(y[i],means[2],sigmas[2]));
increment_log_prob(lp_vec);
}
```

Given this data from R:

```
set.seed(12345)

N <- 1000
dist_1 <- rnorm(N,mean = 10,sd = 1)
dist_2 <- rnorm(N,mean = -1,sd = 2)

theta <- 0.6

indicator <- rbinom(N,size = 1,prob = theta)
y <- indicator * dist_1 + (1 - indicator) * dist_2

stan_rdump(c('N','y'),file = 'normal_mixture_data.Rdat')
```

#### Side Effects:

None.

#### Documentation:

To-do: Add documentation for the log_mix function

#### Reviewer Suggestions:

Anyone