55 for(
int i = 0; i < n; ++i){
56 if(k(i) > 0.5){ ord(++at) = i; }
59 for(
int i = n - 1; i >= 0; --i){
60 if(k(i) < 0.5){ ord(--at) = i; }
70 Type eps = std::numeric_limits<double>::epsilon();
71 return (Type(1.0) - eps) * (u - Type(0.5)) + Type(0.5);
91 Type eps = Type(4.0) * std::numeric_limits<double>::epsilon();
92 return (Type(1.0) - eps) * (u - Type(0.5)) + Type(0.5);
113 Type nx = CppAD::CondExpLt(n - x, Type(1e-12), Type(1e-12), n - x);
114 return Type(1.0) - pbeta(p, x + Type(1.0), nx);
133 return cdf_lower * log(F) + cdf_upper * log(Type(1.0) - F);
154 Type cdf_lower, Type cdf_upper){
159 return cdf_lower * log(F) + cdf_upper * log(G);
174 data_indicator<vector<Type>, Type> keep,
175 int do_osa,
int give_log,
int do_cdf = 0)
181 vector<Type> p_x = p / (sum(p) + Type(1e-300));
184 vector<Type> k = keep;
196 vector<Type> cl = keep.cdf_lower;
197 vector<Type> cu = keep.cdf_upper;
199 x = x(o); p_x = p_x(o); k = k(o);
200 if(do_cdf){ cl = cl(o); cu = cu(o); }
201 Type nUnused = asDouble(sum(x));
204 for(
int i = 0; i < x.size(); ++i){
205 if(i != (x.size() - 1)){
208 vector<Type> x2(2), p2(2);
210 x2(1) = nUnused - x(i);
216 Type eps_clamp = std::numeric_limits<double>::epsilon();
217 p2(0) = CppAD::CondExpGt(p2(0), Type(1) - eps_clamp, Type(1) - eps_clamp, p2(0));
218 p2(1) = Type(1) - p2(0);
219 logres += k(i) * dmultinom(x2, p2,
true);
222 if(do_cdf) cdf =
osa_pbinom(x(i), nUnused, p2(0));
232 logres += k(i) * Type(0);
238 logres += cl(i) * log(cdf);
239 logres += cu(i) * log(Type(1) - cdf);
243 logres = dmultinom(x, p_x,
true);
245 return give_log ? logres : exp(logres);
267 data_indicator<vector<Type>, Type> keep,
268 int do_osa,
int give_log)
272 vector<Type> k = keep;
274 obs = obs(o); alpha = alpha(o); k = k(o);
275 int dim = obs.size();
276 Type alp_sum = alpha.sum();
277 Type obs_sum = asDouble(obs.sum());
278 for(
int a = 0; a < dim - 1; ++a){
283 vector<Type> obs_a(2), alphas_a(2);
284 obs_a(0) = obs(a); obs_a(1) = obs_sum;
285 alphas_a(0) = alpha(a); alphas_a(1) = alp_sum;
291 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)