Ejemplo n.º 1
0
// Calculate log posterior by processing the timeline
// Assumption: one edge at a time
// [[Rcpp::export(".estimate_alpha_core")]]
SEXP estimate_alpha_core(SEXP a,
                         SEXP b,
                         SEXP c,
                         SEXP d) {
    //std::cout << "Reached here 0\n";
    NumericVector Sum_m_k(a);
    NumericMatrix n_tk(b);
    NumericVector m_t(c);
    NumericVector center_k(d);
    double first_term = 0;
    //std::cout << center_k.size() << "\n";
    for (unsigned long k = 0; k < center_k.size(); ++k) {
        first_term += Sum_m_k.at(k) * log(center_k.at(k));
    }
    //std::cout << "Reached here 1\n";
    unsigned long K = center_k.size();
    unsigned long T = n_tk.nrow();
    auto f =[&] (double alpha) {
        double second_term = 0;
        #pragma omp parallel for \
        default(shared)        \
        reduction(+:second_term)
        for (unsigned long t = 0; t < T; ++t) {
            double upper = 0;
            double lower = 0;
            for (unsigned long k = 0; k < K; ++k) {
                lower += n_tk.at(t,k) * pow(center_k.at(k),alpha);
                upper += m_t.at(t) * n_tk.at(t,k) * pow(center_k.at(k),alpha) * log(center_k.at(k));
            }
            second_term += upper / lower;
        }
        return(first_term - second_term);
    };
    double alpha = my_zeroin(-2, 2, f, DBL_EPSILON, 500);
    //std::cout << "Reached here 2\n";
    // calculate standard deviation of alpha
    double var   = 0; // variance
    #pragma omp parallel for \
    default(shared)        \
    reduction(+:var)
      for (unsigned long t = 0; t < T; ++t) {
        double u_u = 0;
        double v_v = 0;
        double u_v = 0;
        for (unsigned long k = 0; k < K; ++k) {
            u_u += n_tk.at(t,k) * pow(center_k.at(k),alpha) * pow(log(center_k.at(k)),2);
            u_v += n_tk.at(t,k) * pow(center_k.at(k),alpha) * log(center_k.at(k));
            v_v += n_tk.at(t,k) * pow(center_k.at(k),alpha);
        }
        var += m_t.at(t) * (u_u * v_v - pow(u_v,2)) / pow(v_v,2);
      }
      
    return Rcpp::List::create(Rcpp::Named("alpha")    = alpha,
                              Rcpp::Named("variance") = 1/ var);
}
Ejemplo n.º 2
0
    Obj operator()(const std::pair<T,U>& v) const
    {
        Obj list = NEW_PLIST(T_PLIST_DENSE, 2);
        SET_LEN_PLIST(list, 2);

        GAP_maker<T> m_t;
        SET_ELM_PLIST(list, 1, m_t(v.first));
        CHANGED_BAG(list);

        GAP_maker<U> m_u;
        SET_ELM_PLIST(list, 2, m_u(v.second));
        CHANGED_BAG(list);

        return list;
    }
Ejemplo n.º 3
0
 template <typename MeasureType> measure(bool, MeasureType &&m) {
     static_assert(std::is_move_constructible<MeasureType>::value, "This measure is not MoveConstructible");
     static_assert(has_accumulate<MCSignType, MeasureType>::value, " This measure has no accumulate method !");
     static_assert(has_collect_result<MeasureType>::value, " This measure has no collect_results method !");
     using m_t = std14::decay_t<MeasureType>;
     m_t *p = new m_t(std::forward<MeasureType>(m));
     impl_ = std::shared_ptr<m_t>(p);
     clone_ = [p]() {
         return measure{true, m_t(*p)};
     };
     accumulate_ = [p](MCSignType const &x) {
         p->accumulate(x);
     };
     count_ = 0;
     collect_results_ = [p](mpi::communicator const &c) {
         p->collect_results(c);
     };
     h5_r = make_h5_read(p);
     h5_w = make_h5_write(p);
 }