template<class Type>
struct univariate {
Type x, n, mu, sd;
Type operator() (Type u) {
Type ans = 0;
ans +=
dnorm(u, Type(0.), Type(1.),
true);
ans += x * log(p) + (n - x) * log(1. - p);
ans = exp(ans);
return ans;
}
};
template<class Type>
Type x = input[0], n = input[1];
Type mu = input[2], sd = input[3];
univariate<Type> f = {x, n, mu, sd};
Type a = -5, b = 5;
return res;
}
REGISTER_ATOMIC(GaussBinomial)
template<class Type>
Type objective_function<Type>::operator() ()
{
Type sd = exp(logsd);
Type ans = 0;
Type tiny = 0.0;
for(int i=0; i < x.size(); i++)
PARALLEL_REGION {
input << x(i), n(i), mu(i), sd;
ans -= log( GaussBinomial(input)[0] + tiny );
}
return ans;
}
License: