2 #define DEFM_MEAT_HPP 1
9 size_t nrow = Array_.nrow();
10 size_t ncol = Array_.ncol();
12 std::vector< double >
res;
18 res.push_back(
static_cast<double>(nrow));
19 res.push_back(
static_cast<double>(ncol));
23 for (
size_t i = 0u;
i < (nrow - 1); ++
i)
24 for (
size_t j = 0u;
j < ncol; ++
j)
25 res.push_back(Array_(
i,
j));
31 #define DEFM_RANGES(a) \
32 size_t start_i = start_end[a * 2u];\
33 size_t end_i = start_end[a * 2u + 1u];\
34 size_t nobs_i = end_i - start_i + 1u;
36 #define DEFM_LOOP_ARRAYS(a) \
37 for (size_t a = 0u; a < (nobs_i - M_order); ++a)
40 std::vector< double > par,
44 size_t model_num = 0u;
45 size_t n_entry = M_order * Y_ncol;
46 auto idx = this->get_arrays2support();
48 for (
size_t i = 0u;
i < N; ++
i)
60 last_array = this->sample(idx->at(model_num++), par);
61 for (
size_t y = 0u; y < Y_ncol; ++y)
62 *(y_out + n_entry++) = last_array(M_order, y,
false);
71 DEFMArray tmp_array(M_order + 1u, Y_ncol);
72 for (
size_t t_i = 1u; t_i < (M_order + 1u); ++t_i)
73 for (
size_t t_j = 0u; t_j < Y_ncol; ++t_j)
74 tmp_array(t_i - 1u, t_j) = last_array(t_i, t_j);
79 &tmp_array, X, (start_i + proc_n), X_ncol, ID_length,
90 last_array = this->sample(tmp_array, par);
91 for (
size_t y = 0u; y < Y_ncol; ++y)
92 *(y_out + n_entry++) = last_array(M_order, y,
false);
102 n_entry += M_order * Y_ncol;
118 ) : column_major(column_major) {
124 ID_shared = std::make_shared< std::vector<int> >(id_length);
125 Y_shared = std::make_shared< std::vector<int> >(id_length * y_ncol);
126 X_shared = std::make_shared< std::vector<double> >(id_length * x_ncol);
128 for (
size_t i = 0u;
i < id_length; ++
i)
129 ID_shared->at(
i) = *(
id +
i);
131 for (
size_t i = 0u;
i < (id_length * y_ncol); ++
i)
132 Y_shared->at(
i) = *(y +
i);
134 for (
size_t i = 0u;
i < (id_length * x_ncol); ++
i)
135 X_shared->at(
i) = *(x +
i);
137 ID = &ID_shared->at(0u);
138 Y = &Y_shared->at(0u);
139 X = &X_shared->at(0u);
150 ID_length = id_length;
153 Y_length = y_ncol * id_length;
156 X_length = x_ncol * id_length;
161 this->rengine =
new std::mt19937();
162 this->delete_rengine =
true;
166 this->add_hasher(kgen);
169 start_end.reserve(id_length);
170 start_end.push_back(0);
174 for (
size_t row = 1u; row < id_length; ++row)
178 if (*(
id + row) != *(
id + row - 1u))
182 start_end.push_back(row - 1u);
186 size_t n_rows_i = (row - 1u) - start_end[N++ * 2u] + 1;
187 if (n_rows_i < (M_order + 1u))
188 throw std::length_error(
189 "Obs. id: " + std::to_string(*(
id + row - 1u)) +
" (row " +
190 std::to_string(row) +
") has fewer rows (" +
191 std::to_string(n_rows_i) +
") than those needed (" +
192 std::to_string(M_order + 1) +
") for the Markov Model."
196 start_end.push_back(row);
202 start_end.push_back(id_length - 1u);
207 for (
auto i = 0u;
i < Y_ncol; ++
i)
208 Y_names.emplace_back(std::string(
"y") + std::to_string(
i));
210 for (
auto i = 0u;
i < X_ncol; ++
i)
211 X_names.emplace_back(std::string(
"X") + std::to_string(
i));
225 std::function<size_t(
size_t,
size_t,
size_t,
size_t)> element_access;
227 if (this->column_major)
230 element_access = [](
size_t i,
size_t j,
size_t nrow, size_t) ->
size_t {
236 element_access = [](
size_t i,
size_t j, size_t,
size_t ncol) ->
size_t {
243 for (
size_t i = 0u;
i < N; ++
i)
247 size_t start_i = start_end[
i * 2u];
248 size_t end_i = start_end[
i * 2u + 1u];
249 size_t nobs_i = end_i - start_i + 1u;
253 for (
size_t n_proc = 0u; n_proc < (nobs_i - M_order); ++n_proc)
260 &array, X, (start_i + n_proc), X_ncol, ID_length,
267 for (
size_t k = 0u; k < Y_ncol; ++k)
268 for (
size_t o = 0u; o < (M_order + 1u); ++o)
270 array(o, k) = *(Y + element_access(
271 start_i + n_proc + o,
278 model_ord.push_back( this->add_array(array,
true) );
328 std::vector< size_t > idx
332 for (
const auto &
i : idx)
334 throw std::range_error(
"The -idx- for motif accounting is out of range.");
336 barry::FreqTable<int> ans;
337 std::vector<int> array(idx.size() * (M_order + 1));
339 for (
size_t i = 0u;
i < N; ++
i)
351 for (
size_t o = 0u; o < (M_order + 1u); ++o)
353 array[nele++] = *(Y + k * ID_length + start_i + proc_n + o);
355 ans.add(array,
nullptr);
366 const std::vector< double > & par,
372 std::vector< double >
res(ID_length, std::nan(
""));
374 for (
size_t i = 0u;
i < N; ++
i)
387 &array, X, (start_i + n_proc), X_ncol, ID_length,
394 for (
size_t k = 0u; k < Y_ncol; ++k)
395 for (
size_t o = 0u; o < (M_order + 1u); ++o)
396 array(o, k) = *(Y + k * ID_length + start_i + n_proc + o);
398 double p_1 = this->conditional_prob(array, par, i_, j_);
399 res[M_order + start_i + n_proc] = std::log(p_1/(1.0 - p_1));
411 std::vector< std::string > Y_names_,
412 std::vector< std::string > X_names_
416 if (Y_names_.size() != Y_ncol)
417 throw std::length_error(
"The length of Y_names_ doesn't match the number of dependent variables.");
419 if (X_names_.size() != X_ncol)
420 throw std::length_error(
"The length of X_names_ doesn't match the number of dependent variables.");
450 std::vector< bool >
res(0u);
451 auto * counterss = DEFMModel::get_counters();
453 res.push_back(counterss->operator[](
i).data.is_motif);
464 #undef DEFM_LOOP_ARRAYS
Data class used to store arbitrary size_t or double vectors.
Data class for DEFM arrays.
std::vector< bool > is_motif()
size_t get_n_covars() const
DEFM(int *id, int *y, double *x, size_t id_length, size_t y_ncol, size_t x_ncol, size_t m_order, bool copy_data=true, bool column_major=true)
const int * get_Y() const
size_t get_m_order() const
void set_names(std::vector< std::string > Y_names_, std::vector< std::string > X_names_)
size_t get_n_rows() const
const int * get_ID() const
const double * get_X() const
bool get_column_major() const noexcept
std::vector< double > logodds(const std::vector< double > &par, size_t i, size_t j)
const std::vector< std::string > & get_Y_names() const
const std::vector< std::string > & get_X_names() const
void simulate(std::vector< double > par, int *y_out)
barry::FreqTable< int > motif_census(std::vector< size_t > idx)
Data_Type &&counter_ data(std::move(counter_.data))
Data_Type &&counter_ noexcept
std::vector< double > keygen_defm(const DEFMArray &Array_, DEFMCounterData *data)
#define DEFM_LOOP_ARRAYS(a)
barry::BArrayDense< int, DEFMData > DEFMArray
void rules_markov_fixed(DEFMRules *rules, size_t markov_order)
Number of edges.