41 for(
int i = 0; i < n; ++i){
42 if(k(i) > 0.5){ ord(++at) = i; }
45 for(
int i = n - 1; i >= 0; --i){
46 if(k(i) < 0.5){ ord(--at) = i; }
56 Type eps = std::numeric_limits<double>::epsilon();
57 return (Type(1.0) - eps) * (u - Type(0.5)) + Type(0.5);
77 Type eps = Type(4.0) * std::numeric_limits<double>::epsilon();
78 return (Type(1.0) - eps) * (u - Type(0.5)) + Type(0.5);
99 Type nx = CppAD::CondExpLt(n - x, Type(1e-12), Type(1e-12), n - x);
100 return Type(1.0) - pbeta(p, x + Type(1.0), nx);
119 return cdf_lower * log(F) + cdf_upper * log(Type(1.0) - F);
140 Type cdf_lower, Type cdf_upper){
145 return cdf_lower * log(F) + cdf_upper * log(G);
160 data_indicator<vector<Type>, Type> keep,
161 int do_osa,
int give_log,
int do_cdf = 0)
167 vector<Type> p_x = p / (sum(p) + Type(1e-300));
170 vector<Type> k = keep;
182 vector<Type> cl = keep.cdf_lower;
183 vector<Type> cu = keep.cdf_upper;
185 x = x(o); p_x = p_x(o); k = k(o);
186 if(do_cdf){ cl = cl(o); cu = cu(o); }
187 Type nUnused = asDouble(sum(x));
190 for(
int i = 0; i < x.size(); ++i){
191 if(i != (x.size() - 1)){
194 vector<Type> x2(2), p2(2);
196 x2(1) = nUnused - x(i);
202 Type eps_clamp = std::numeric_limits<double>::epsilon();
203 p2(0) = CppAD::CondExpGt(p2(0), Type(1) - eps_clamp, Type(1) - eps_clamp, p2(0));
204 p2(1) = Type(1) - p2(0);
205 logres += k(i) * dmultinom(x2, p2,
true);
208 if(do_cdf) cdf =
osa_pbinom(x(i), nUnused, p2(0));
218 logres += k(i) * Type(0);
224 logres += cl(i) * log(cdf);
225 logres += cu(i) * log(Type(1) - cdf);
229 logres = dmultinom(x, p_x,
true);
231 return give_log ? logres : exp(logres);
253 data_indicator<vector<Type>, Type> keep,
254 int do_osa,
int give_log)
258 vector<Type> k = keep;
260 obs = obs(o); alpha = alpha(o); k = k(o);
261 int dim = obs.size();
262 Type alp_sum = alpha.sum();
263 Type obs_sum = asDouble(obs.sum());
264 for(
int a = 0; a < dim - 1; ++a){
269 vector<Type> obs_a(2), alphas_a(2);
270 obs_a(0) = obs(a); obs_a(1) = obs_sum;
271 alphas_a(0) = alpha(a); alphas_a(1) = alp_sum;
277 return give_log ? ll : exp(ll);
Type osa_pbinom(Type x, Type n, Type p)
Conditional binomial CDF P(X <= x), X ~ Binomial(n, p), at fractional x.
Type ddirmultinom_osa(vector< Type > obs, vector< Type > alpha, data_indicator< vector< Type >, Type > keep, int do_osa, int give_log)
Keep-aware Dirichlet-multinomial log-density via conditional beta-binomials.
Type dmultinom_osa(vector< Type > x, vector< Type > p, data_indicator< vector< Type >, Type > keep, int do_osa, int give_log, int do_cdf=0)
Keep-aware multinomial log-density via conditional binomials.
Type osa_norm_cdf_terms(Type x, Type mu, Type sd, Type cdf_lower, Type cdf_upper)
One-step-ahead CDF terms for a normal observation.
Type osa_trunc_norm_cdf_terms(Type x, Type mu, Type sd, Type cdf_lower, Type cdf_upper)
One-step-ahead CDF terms for a normal left-truncated at zero.
Type osa_squeeze_cdf(Type u)
Map a CDF onto the open interval before its logarithm is taken.
vector< int > osa_order(vector< Type > k)
Type ddirmultinom(vector< Type > obs, vector< Type > alpha, int do_log)