70#include <unordered_map>
78 template < GUM_Numeric GUM_SCALAR >
80 std::string_view dirPath,
81 std::string_view csvBaseName,
84 const std::unordered_set< std::string >& atemporalVars,
85 const std::vector< std::string >& missingSymbols,
98 namespace fs = std::filesystem;
99 const fs::path firstCSV = fs::path{dirPath} / (std::string{csvBaseName} +
"1.csv");
100 std::ifstream is(firstCSV, std::ifstream::in);
105 GUM_ERROR(
IOError,
"Empty trajectory file (no header row): " << firstCSV.string())
106 const std::vector< std::string >& header = parser.
current();
113 "atemporal variable '" << aname <<
"' not found in the CSV header")
118 template < GUM_Numeric GUM_SCALAR >
120 std::string_view dirPath,
121 std::string_view csvBaseName,
124 const std::vector< std::string >& missingSymbols,
143 template < GUM_Numeric GUM_SCALAR >
145 std::string_view dirPath,
146 std::string_view csvBaseName,
149 const std::vector< std::string >& missingSymbols) {
158 template < GUM_Numeric GUM_SCALAR >
160 std::string_view dirPath,
161 std::string_view csvBaseName,
164 const BayesNet< GUM_SCALAR >& bn,
165 const std::unordered_set< std::string >& atemporalVars,
166 const std::vector< std::string >& missingSymbols) :
177 for (
const NodeId node: bn.nodes())
184 "atemporal variable '" << aname <<
"' not found in the schema BN")
189 template < GUM_Numeric GUM_SCALAR >
198 template < GUM_Numeric GUM_SCALAR >
212 KTBN< GUM_SCALAR > best;
214 double bestScore = 0.0;
216 std::vector< std::pair< Size, double > > scores;
217 std::vector< std::pair< std::string, std::string > >
224 std::unique_ptr< KTBNLearner< GUM_SCALAR > > learnerPtr
233 : std::make_unique< KTBNLearner< GUM_SCALAR > >(
_dirPath_,
249 "the trajectories contain missing values. Neither aGrUM's structure learning "
250 "nor the cross-k order score can evaluate an incomplete window. Call "
251 "ignoreMissingSymbols() to learn and score on the fully observed data only "
252 "(see its warning: dropping skews the selection towards larger k).")
259 for (
const Size len: learner.nbRows())
261 logN = std::log2(nbObs);
267 KTBN< GUM_SCALAR > candidate = learner.learnKTBN();
276 scores.emplace_back(k, score);
277 if (
bestK == 0 || score > bestScore) {
279 best = std::move(candidate);
284 ? learner.latentVariables()
285 : std::vector< std::pair< std::string, std::string > >{};
299 template < GUM_Numeric GUM_SCALAR >
305 "bestK() is undefined: call learnKTBN() before querying the selected order.")
309 template < GUM_Numeric GUM_SCALAR >
310 const std::vector< std::pair< std::string, std::string > >&
318 template < GUM_Numeric GUM_SCALAR >
319 const std::vector< std::pair< Size, double > >&
325 "scorePerCandidateK() is undefined: call learnKTBN() before querying the per-k scores.")
333 template < GUM_Numeric GUM_SCALAR >
338 template < GUM_Numeric GUM_SCALAR >
367 template < GUM_Numeric GUM_SCALAR >
368 std::vector< std::tuple< std::string, std::string, std::string > >
370 std::vector< std::tuple< std::string, std::string, std::string > > vals;
371 const auto add = [&](std::string k, std::string v, std::string c =
"") {
372 vals.emplace_back(std::move(k), std::move(v), std::move(c));
375 const auto arcs = [](
const std::set< std::pair< std::string, std::string > >& s) {
378 for (
const auto& [a, b]: s) {
379 if (!first) r +=
", ";
385 const auto names = [](
const std::set< std::string >& s) {
388 for (
const auto& n: s) {
389 if (!first) r +=
", ";
396 const auto kernelArcs = [](
const std::set< std::tuple< std::string, std::string, int > >& s) {
399 for (
const auto& [a, b, lag]: s) {
400 if (!first) r +=
", ";
402 r += a +
"->" + b +
" (lag " + std::to_string(lag) +
")";
407 add(
"Candidate orders", std::to_string(
_kMin_) +
".." + std::to_string(
_kMax_));
408 add(
"Selected k",
_bestK_ == 0 ?
"not learned yet" : std::to_string(
_bestK_));
409 add(
"Base variables", std::to_string(
_baseNames_.size()));
418 add(
"Algorithm",
"Greedy Thick Thinning");
421 add(
"Algorithm",
"Greedy Hill Climbing");
424 add(
"Algorithm",
"Extended Greedy Hill Climbing");
427 add(
"Algorithm",
"Local Search with Tabu List");
428 add(
"Tabu list size", std::to_string(
_tabuSize_));
431 default : add(
"Algorithm",
"(other)");
break;
439 add(
"Correction",
"No correction");
450 add(
"Structure score",
"Log2Likelihood");
453 default : add(
"Structure score",
"(other)");
break;
493 template < GUM_Numeric GUM_SCALAR >
496 const auto st =
state();
497 std::size_t maxkey = 0;
498 for (
const auto& t: st)
499 maxkey = std::max(maxkey, std::get< 0 >(t).length());
502 for (
const auto& t: st) {
503 s += std::format(
"{:<{}} : {}", std::get< 0 >(t), maxkey, std::get< 1 >(t));
504 if (!std::get< 2 >(t).empty()) s += std::format(
" ({})", std::get< 2 >(t));
514 template < GUM_Numeric GUM_SCALAR >
520 template < GUM_Numeric GUM_SCALAR >
526 template < GUM_Numeric GUM_SCALAR >
532 template < GUM_Numeric GUM_SCALAR >
538 template < GUM_Numeric GUM_SCALAR >
544 template < GUM_Numeric GUM_SCALAR >
550 template < GUM_Numeric GUM_SCALAR >
559 template < GUM_Numeric GUM_SCALAR >
565 template < GUM_Numeric GUM_SCALAR >
571 template < GUM_Numeric GUM_SCALAR >
581 template < GUM_Numeric GUM_SCALAR >
588 template < GUM_Numeric GUM_SCALAR >
597 template < GUM_Numeric GUM_SCALAR >
603 template < GUM_Numeric GUM_SCALAR >
610 template < GUM_Numeric GUM_SCALAR >
617 template < GUM_Numeric GUM_SCALAR >
624 template < GUM_Numeric GUM_SCALAR >
634 template < GUM_Numeric GUM_SCALAR >
640 template < GUM_Numeric GUM_SCALAR >
646 template < GUM_Numeric GUM_SCALAR >
652 template < GUM_Numeric GUM_SCALAR >
662 template < GUM_Numeric GUM_SCALAR >
668 template < GUM_Numeric GUM_SCALAR >
674 template < GUM_Numeric GUM_SCALAR >
684 template < GUM_Numeric GUM_SCALAR >
689 template < GUM_Numeric GUM_SCALAR >
701 template < GUM_Numeric GUM_SCALAR >
704 std::string_view headNode) {
705 const auto [tailBase, tailSlice] =
_determineNode_(std::string{tailNode});
706 const auto [headBase, headSlice] =
_determineNode_(std::string{headNode});
718 template < GUM_Numeric GUM_SCALAR >
722 std::string_view headBase,
731 template < GUM_Numeric GUM_SCALAR >
734 std::string_view headNode) {
735 const auto [tailBase, tailSlice] =
_determineNode_(std::string{tailNode});
736 const auto [headBase, headSlice] =
_determineNode_(std::string{headNode});
745 template < GUM_Numeric GUM_SCALAR >
749 std::string_view headBase,
758 template < GUM_Numeric GUM_SCALAR >
761 std::string_view headNode) {
762 const auto [tailBase, tailSlice] =
_determineNode_(std::string{tailNode});
763 const auto [headBase, headSlice] =
_determineNode_(std::string{headNode});
776 template < GUM_Numeric GUM_SCALAR >
780 std::string_view headBase,
789 template < GUM_Numeric GUM_SCALAR >
792 std::string_view headNode) {
793 const auto [tailBase, tailSlice] =
_determineNode_(std::string{tailNode});
794 const auto [headBase, headSlice] =
_determineNode_(std::string{headNode});
804 template < GUM_Numeric GUM_SCALAR >
808 std::string_view headBase,
821 template < GUM_Numeric GUM_SCALAR >
825 std::string_view headBase) {
832 template < GUM_Numeric GUM_SCALAR >
836 std::string_view headBase) {
843 template < GUM_Numeric GUM_SCALAR >
847 std::string_view headBase) {
857 template < GUM_Numeric GUM_SCALAR >
861 std::string_view headBase) {
868 template < GUM_Numeric GUM_SCALAR >
871 std::string_view headBase) {
883 template < GUM_Numeric GUM_SCALAR >
886 std::string_view headBase) {
895 template < GUM_Numeric GUM_SCALAR >
898 std::string_view headBase) {
907 template < GUM_Numeric GUM_SCALAR >
910 std::string_view headBase) {
917 template < GUM_Numeric GUM_SCALAR >
925 template < GUM_Numeric GUM_SCALAR >
935 template < GUM_Numeric GUM_SCALAR >
943 template < GUM_Numeric GUM_SCALAR >
953 template < GUM_Numeric GUM_SCALAR >
961 template < GUM_Numeric GUM_SCALAR >
971 template < GUM_Numeric GUM_SCALAR >
979 template < GUM_Numeric GUM_SCALAR >
989 template < GUM_Numeric GUM_SCALAR >
993 std::string_view headBase,
1002 template < GUM_Numeric GUM_SCALAR >
1005 std::string_view head) {
1006 const auto [tailBase, tailSlice] =
_determineNode_(std::string{tail});
1007 const auto [headBase, headSlice] =
_determineNode_(std::string{head});
1012 std::string first{tail}, second{head};
1013 if (second < first) std::swap(first, second);
1023 template < GUM_Numeric GUM_SCALAR >
1027 std::string_view headBase,
1036 template < GUM_Numeric GUM_SCALAR >
1039 std::string_view head) {
1040 const auto [tailBase, tailSlice] =
_determineNode_(std::string{tail});
1041 const auto [headBase, headSlice] =
_determineNode_(std::string{head});
1046 std::string first{tail}, second{head};
1047 if (second < first) std::swap(first, second);
1053 template < GUM_Numeric GUM_SCALAR >
1060 template < GUM_Numeric GUM_SCALAR >
1067 template < GUM_Numeric GUM_SCALAR >
1074 template < GUM_Numeric GUM_SCALAR >
1085 template < GUM_Numeric GUM_SCALAR >
1086 const std::unordered_set< std::string >&
1091 template < GUM_Numeric GUM_SCALAR >
1100 template < GUM_Numeric GUM_SCALAR >
1104 "unknown base variable '" << base <<
"': it is not one of the data columns")
1110 "atemporal variable '" << base <<
"' cannot be given a time slice (got " << slice
1111 <<
"): use KTBN::ATEMPORAL")
1119 "negative time slice " << slice <<
" for '" << base
1120 <<
"': slices start at 0 (use KTBN::ATEMPORAL to address an "
1121 "atemporal variable)")
1122 if (slice >=
static_cast< int >(
_kMax_))
1124 "time slice " << slice <<
" for '" << base
1125 <<
"' is out of range: it must be < kMax (" <<
_kMax_ <<
")")
1128 template < GUM_Numeric GUM_SCALAR >
1130 std::string_view headBase,
1135 for (
const std::string_view base: {tailBase, headBase})
1142 if (lag >=
static_cast< int >(
_kMax_))
1144 "kernel lag " << lag <<
" is out of range: it must be < kMax (" <<
_kMax_
1145 <<
"), so some candidate k can place the tail at slice k-1-lag >= 0")
1152 template < GUM_Numeric GUM_SCALAR >
1158 template < GUM_Numeric GUM_SCALAR >
1197 template < GUM_Numeric GUM_SCALAR >
1203 bool scoreBased =
false;
1254 template < GUM_Numeric GUM_SCALAR >
1257 const int ik =
static_cast< int >(k);
1261 auto fits = [&](
const std::string& node) {
return _determineNode_(node).second < ik; };
1279 const int tailSlice = ik - 1 - lag;
1280 if (tailSlice >= 0) learner.
addForbiddenArc(tailBase, tailSlice, headBase, ik - 1);
1283 const int tailSlice = ik - 1 - lag;
1284 if (tailSlice >= 0) learner.
addMandatoryArc(tailBase, tailSlice, headBase, ik - 1);
1305 template < GUM_Numeric GUM_SCALAR >
1306 template <
typename PerInstance,
typename PerNodeFinal >
1308 PerInstance perInstance,
1309 PerNodeFinal perNodeFinal)
const {
1310 namespace fs = std::filesystem;
1311 const int k =
static_cast< int >(net.k());
1319 const Tensor< GUM_SCALAR >* cpt;
1321 std::vector< std::string > dimName;
1322 std::vector< std::pair< std::string, int > > dimNode;
1324 std::unordered_map< Size, double > counts;
1327 auto makeEval = [&](
const Tensor< GUM_SCALAR >& cpt) {
1331 e.selfDom = cpt.variable(0).domainSize();
1332 e.dimName.reserve(cpt.nbrDim());
1333 e.dimNode.reserve(cpt.nbrDim());
1334 for (
Idx d = 0; d < cpt.nbrDim(); ++d) {
1335 std::string name = cpt.variable(d).name();
1337 e.dimName.push_back(std::move(name));
1344 const auto& temporalBases = net.temporalVarNames();
1345 std::vector< std::vector< NodeEval > > temporalEvals(k);
1346 for (
int slice = 0; slice < k; ++slice) {
1347 temporalEvals[slice].reserve(temporalBases.size());
1348 for (
const auto& base: temporalBases)
1349 temporalEvals[slice].push_back(makeEval(net.cpt(base, slice)));
1353 std::vector< NodeEval > atemporalEvals;
1354 atemporalEvals.reserve(net.atemporalVarNames().size());
1355 for (
const auto& base: net.atemporalVarNames())
1361 std::vector< std::vector< std::string > > window(k);
1379 std::unordered_map< std::string, std::size_t > colOf;
1380 std::unordered_map< std::string, std::string > atempVal;
1382 std::ifstream pis(file, std::ifstream::in);
1386 const auto& header = pre.
current();
1387 for (std::size_t c = 0; c < header.size(); ++c)
1388 colOf[header[c]] = c;
1390 const std::size_t nbAtemp = net.atemporalVarNames().size();
1391 while (atempVal.size() < nbAtemp && pre.
next()) {
1392 const auto& row = pre.
current();
1393 for (
const auto& c: net.atemporalVarNames()) {
1394 if (atempVal.contains(c))
continue;
1395 const std::string& tok = row[colOf.at(c)];
1396 if (!missing.contains(tok)) atempVal[c] = tok;
1401 std::ifstream is(file, std::ifstream::in);
1413 auto labelOf = [&](
const std::string& varBase,
1416 int scoredSlice) ->
const std::string* {
1419 const auto it = atempVal.find(varBase);
1420 return (it == atempVal.end()) ? nullptr : &it->second;
1424 const int varTime = scoredTime - (scoredSlice - varSlice);
1426 const std::string& tok = window[varTime % k][colOf.at(varBase)];
1427 return missing.contains(tok) ? nullptr : &tok;
1431 while (parser.
next()) {
1432 const auto& cur = parser.
current();
1433 window[t % k].assign(cur.begin(), cur.end());
1437 const int childSlice = (t < k - 1) ? t : (k - 1);
1438 for (NodeEval& e: temporalEvals[childSlice]) {
1442 bool complete =
true;
1443 for (std::size_t d = 0; d < e.dimName.size(); ++d) {
1444 const auto& [vbase, vslice] = e.dimNode[d];
1445 const std::string* lbl = labelOf(vbase, vslice, t, childSlice);
1446 if (lbl ==
nullptr) {
1450 e.inst.chgVal(e.dimName[d], *lbl);
1452 if (complete) perInstance(e);
1462 for (NodeEval& e: atemporalEvals) {
1463 bool complete =
true;
1464 for (std::size_t d = 0; d < e.dimName.size(); ++d) {
1465 const auto it = atempVal.find(e.dimNode[d].first);
1466 if (it == atempVal.end()) {
1470 e.inst.chgVal(e.dimName[d], it->second);
1472 if (complete) perInstance(e);
1478 for (
auto& slice: temporalEvals)
1479 for (NodeEval& e: slice)
1481 for (NodeEval& e: atemporalEvals)
1485 template < GUM_Numeric GUM_SCALAR >
1492 [&logL](
auto& e) { logL += std::log2((*e.cpt)[e.inst]); },
1497 template < GUM_Numeric GUM_SCALAR >
1505 double logL = 0.0, penalty = 0.0;
1509 logL += std::log2((*e.cpt)[e.inst]);
1512 for (
Idx d = 1; d < e.cpt->nbrDim(); ++d)
1513 idx = idx * e.cpt->variable(d).domainSize() + e.inst.val(e.cpt->variable(d));
1514 e.counts[idx] += 1.0;
1516 [
this, &penalty](
auto& e) {
1517 for (
const auto& [cfg, n]: e.counts)
1518 penalty +=
_ctable_.log2Cnr(e.selfDom, n);
1520 return logL - penalty;
1523 template < GUM_Numeric GUM_SCALAR >
1530 for (
const auto& [base, slice]: net.nodes()) {
1531 double cell = net.variable(base, slice).domainSize() - 1.0;
1532 for (
const auto& [pbase, pslice]: net.parents(base, slice))
1533 cell *= net.variable(pbase, pslice).domainSize();
1539 template < GUM_Numeric GUM_SCALAR >
1541 double logN)
const {
Class for fast parsing of CSV file (never more than one line in application memory).
A k-TBN learner that also selects the order k from the data.
Class representing a Bayesian network.
Exception : input/output problem.
Class for assigning/browsing values to tuples of discrete variables.
Instantiation()
Default constructor: creates an empty tuple.
Exception: at least one argument passed to a function is not what was expected.
static constexpr int ATEMPORAL
Conventional time-slice value denoting an atemporal (static) variable.
Error: The database contains some missing values.
Exception : operation not allowed.
Class for fast parsing of CSV file (never more than one line in application memory).
bool next()
gets the next line of the csv stream and parses it
const std::vector< std::string > & current() const
returns the current parsed line
@ EXTENDED_GREEDY_HILL_CLIMBING
@ LOCAL_SEARCH_WITH_TABU_LIST
void _checkBaseIsTemporal_(std::string_view base, std::string_view context) const
Throw InvalidArgument unless base is a known temporal base. context completes "cannot appear in <cont...
void _checkArcTemporallyFeasible_(std::string_view tail, std::string_view head, std::string_view action) const
Reject an arc the k-TBN definition can never contain, so eraseForbiddenArc and addMandatoryArc both f...
static std::unordered_set< std::string > _scanConstantColumns_(std::string_view dirPath, std::string_view csvBaseName, Size nbSamples, const std::vector< std::string > &missingSymbols)
Scans every trajectory and returns the base names classified atemporal: those whose value never chang...
static void _checkMinimalOrder_(Size order, std::string_view label)
Throw InvalidArgument unless order is at least 2, label naming the offending parameter ("k" for the f...
static double defaultPriorWeight_()
static Size defaultTabuListSize_()
std::string _encode_(std::string_view base, int slice) const
(base, slice) -> engine name ("A[1]" / atemporal engine name). Pure function, shared by every learner...
std::pair< std::string, int > _determineNode_(const std::string &name) const
engine name -> (base, slice); atemporal names map to KTBN::ATEMPORAL. Shared by every learner; only t...
static Size defaultTabuNbDecrease_()
Learns a k-TBN (order k + structure + parameters) from trajectory CSVs.
std::string toString() const
Human-readable summary of the recorded configuration (candidate order range, algorithm / score / corr...
KTBNAdaptiveLearner< GUM_SCALAR > & addForbiddenArcAllSlices(std::string_view tailBase, std::string_view headBase) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
bool _induceTypes_
whether numeric columns are retyped (see KTBNLearner); unused (and forced false) when a schema BN is ...
std::set< std::string > _noParentNodes_
root nodes (no parents), as engine names
KTBNAdaptiveLearner< GUM_SCALAR > & allowArcAdditions(bool allow=true) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
std::vector< std::string > _missingSymbols_
symbols in the CSVs to interpret as missing values
std::unordered_set< std::string > _atemporalVars_
base names of the atemporal (static) variables
bool _allowAdditions_
recorded graph-change permissions / indegree cap
void _verifyKernelArc_(std::string_view tailBase, std::string_view headBase, int lag) const
Throw InvalidArgument unless tailBase and headBase are known, temporal base variables (a kernel-relat...
KTBNAdaptiveLearner< GUM_SCALAR > & useOrderScoreBIC()
Select k by BIC (the default): keep the k whose learned model maximises .
std::unique_ptr< BayesNet< GUM_SCALAR > > _prior_bn_
optional variable-schema BN (set by the BN constructor): when present, each per-k KTBNLearner is buil...
Size _bestK_
k selected by the last learnKTBN() call, or 0 as a sentinel while no learning has happened yet....
std::string _dirPath_
directory holding the trajectory CSV files
Size _kMax_
largest order to explore (candidates are kMin..kMax)
KTBNAdaptiveLearner< GUM_SCALAR > & addMandatoryArc(std::string_view tailNode, std::string_view headNode) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
bool _isKnownBase_(std::string_view base) const override
whether base is one of this learner's variables; the base names read at construction,...
KTBNAdaptiveLearner< GUM_SCALAR > & eraseForbiddenArcAllSlices(std::string_view tailBase, std::string_view headBase) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & useNMLCorrection() override
void _applyConstraints_(KTBNLearner< GUM_SCALAR > &learner, Size k) const
Replay the recorded structural constraints onto learner (built for order k). Engine-name constraints ...
std::vector< std::pair< std::string, std::string > > _bestLatentVariables_
latent-variable arcs (engine-name pairs) reported by the winning candidate's MIIC run,...
IBNLearner::BNLearnerPriorType _prior_
recorded prior and its weight
double _orderSelectionScore_(const KTBN< GUM_SCALAR > &net, double logN) const
The cross-k order-selection score of net under the recorded orderScore criterion, given logN = log2 o...
const std::vector< std::pair< std::string, std::string > > & latentVariables() const
Engine-name (tail, head) pairs of arcs the selected model's MIIC run flagged as hiding a latent varia...
std::set< std::string > _noChildrenNodes_
leaf nodes (no children), as engine names
double _priorWeight_
always written by the useXxxPrior() methods, which read learning.priorWeight when no weight is given
KTBNAdaptiveLearner< GUM_SCALAR > & useGreedyHillClimbing() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
double _fNMLScore_(const KTBN< GUM_SCALAR > &net) const
fNML order score of net over the recorded trajectories: , with the node's domain size and the count...
KTBNAdaptiveLearner< GUM_SCALAR > & eraseForbiddenIntraSliceArc(std::string_view tailBase, std::string_view headBase) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
void _forEachScoredNode_(const KTBN< GUM_SCALAR > &net, PerInstance perInstance, PerNodeFinal perNodeFinal) const
Stream every scored template-node instance of net over the recorded trajectories, driving both the li...
~KTBNAdaptiveLearner()
Constructor — the candidate orders are kMin..kMax.
KTBNAdaptiveLearner< GUM_SCALAR > & eraseMandatoryArc(std::string_view tailNode, std::string_view headNode) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
IBNLearner::AlgoType _algo_
recorded structure-learning algorithm ; it is initialized from the configuration, see IBNLearner::pre...
KTBNAdaptiveLearner< GUM_SCALAR > & addMandatoryKernelArc(std::string_view tailBase, int lag, std::string_view headBase)
Force an arc from tailBase, lag slices before the kernel, to headBase in the kernel....
OrderScoreType _orderScore_
recorded cross-k order-selection criterion (the outer score used by learnKTBN() to pick the best k; i...
Size kMax() const
Largest order explored (the kMax argument of the constructor).
KTBNAdaptiveLearner< GUM_SCALAR > & useLocalSearchWithTabuList(Size tabu_size, Size nb_decrease) override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNAdaptiveLearner< GUM_SCALAR > & allowArcDeletions(bool allow=true) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & erasePossibleEdge(std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner(std::string_view dirPath, std::string_view csvBaseName, Size nbSamples, Size kMax, const std::unordered_set< std::string > &atemporalVars, const std::vector< std::string > &missingSymbols={"?"}, bool induceTypes=true)
Constructor — the candidate orders are kMin..kMax.
bool _ignoreMissingSymbols_
whether incomplete rows/instances are dropped (see ignoreMissingSymbols())
KTBNAdaptiveLearner< GUM_SCALAR > & addNoChildrenNode(std::string_view base, int slice) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & addPossibleEdge(std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
std::set< std::pair< std::string, std::string > > _forbiddenArcsAllSlices_
forbidden all-slices arcs, as (tailBase, headBase) pairs
KTBNAdaptiveLearner< GUM_SCALAR > & addForbiddenIntraSliceArc(std::string_view tailBase, std::string_view headBase) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
std::set< std::pair< std::string, std::string > > _mandatoryArcs_
mandatory arcs, as (tail, head) engine-name pairs
std::set< std::pair< std::string, std::string > > _possibleEdges_
MIIC candidate edges, as (tail, head) engine-name pairs.
const std::vector< std::pair< Size, double > > & scorePerCandidateK() const
Per-candidate cross-k scores from the last learnKTBN() call, as (k, score) pairs for k = kMin....
const std::unordered_set< std::string > & _atemporalVarNames_() const override
atemporal base names for IKTBNLearner's shared encode/_determineNode_; the set recorded at constructi...
IBNLearner::ScoreType _score_
recorded per-k structure score (the inner score, replayed on each candidate)
KTBNAdaptiveLearner< GUM_SCALAR > & useScoreBDeu() override
std::set< std::pair< std::string, std::string > > _forbiddenArcs_
forbidden arcs, as (tail, head) engine-name pairs
std::set< std::pair< std::string, std::string > > _forbiddenIntraSliceArcs_
forbidden intra-slice arcs, as (tailBase, headBase) pairs
KTBNAdaptiveLearner< GUM_SCALAR > & addForbiddenArc(std::string_view tailNode, std::string_view headNode) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
Size bestK() const
Order k selected by the last learnKTBN() call.
bool isIgnoringMissingSymbols() const
Whether incomplete rows and instances are dropped. False by default.
Size _nbSamples_
number of CSV files to read
KTBNAdaptiveLearner< GUM_SCALAR > & useScoreMDL() override
double _log2Likelihood_(const KTBN< GUM_SCALAR > &net) const
Factorized log2-likelihood of net over the recorded trajectories, comparable across candidate k (the ...
void useScorefNML() override
KTBNAdaptiveLearner< GUM_SCALAR > & eraseForbiddenKernelArc(std::string_view tailBase, int lag, std::string_view headBase)
Undo a previous addForbiddenKernelArc (same (tailBase, lag, headBase) triple).
KTBNAdaptiveLearner< GUM_SCALAR > & useScoreAIC() override
void _applyConfig_(KTBNLearner< GUM_SCALAR > &learner) const
Apply the recorded score / algorithm / correction / prior onto a freshly-built learner....
KTBNAdaptiveLearner< GUM_SCALAR > & eraseMandatoryKernelArc(std::string_view tailBase, int lag, std::string_view headBase)
Undo a previous addMandatoryKernelArc (same (tailBase, lag, headBase) triple).
KTBNAdaptiveLearner< GUM_SCALAR > & useGreedyThickThinning() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNAdaptiveLearner< GUM_SCALAR > & useExtendedGreedyHillClimbing() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
Size _kMin_
smallest order worth exploring (candidates are kMin..kMax). Starts at 2 and is recomputed by recomput...
std::set< std::tuple< std::string, std::string, int > > _mandatoryKernelArcs_
mandatory kernel-relative arcs, same shape as forbiddenKernelArcs
log_2 where the penalty replaces BIC s KTBNAdaptiveLearner< GUM_SCALAR > & useOrderScorefNML()
by a sum of per-node, per-parent-configuration multinomial parametric complexities (regret) – data-de...
KTBNAdaptiveLearner< GUM_SCALAR > & eraseForbiddenArc(std::string_view tailNode, std::string_view headNode) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & useSmoothingPrior(double weight) override
the default weight (key priorWeight of the section learning)
void _recomputeKMin_()
Recompute kMin from scratch: max(2, 1 + the largest concrete slice named by any recorded forbidden/ma...
KTBNAdaptiveLearner< GUM_SCALAR > & useMDLCorrection() override
std::vector< std::pair< Size, double > > _scorePerCandidateK_
per-candidate (k, order-score) pairs from the last learnKTBN() run, in ascending k....
VariableLog2ParamComplexity _ctable_
cache of log2 of the multinomial parametric complexity C^r_n, used by the fNML order penalty....
KTBNAdaptiveLearner< GUM_SCALAR > & useSmoothingPrior()
the default weight (key priorWeight of the section learning)
double _countParameters_(const KTBN< GUM_SCALAR > &net) const
Number of free parameters of net's template: summed over every template node (all initial slices,...
KTBNAdaptiveLearner< GUM_SCALAR > & allowArcReversals(bool allow=true) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
std::vector< std::tuple< std::string, std::string, std::string > > state() const
The recorded configuration as (key, value, comment) tuples (mirrors KTBNLearner::state()); toString()...
KTBNAdaptiveLearner< GUM_SCALAR > & useLocalSearchWithTabuList()
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNAdaptiveLearner< GUM_SCALAR > & usePC() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBN< GUM_SCALAR > learnKTBN() override
Learns the best k in [kMin, kMax] together with the structure and the CPTs: one KTBNLearner is built ...
KTBNAdaptiveLearner< GUM_SCALAR > & useScoreLog2Likelihood() override
std::unordered_set< std::string > _baseNames_
all base variable names (temporal + atemporal), read from the first trajectory CSV header at construc...
KTBNAdaptiveLearner< GUM_SCALAR > & eraseNoParentNode(std::string_view base, int slice) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & ignoreMissingSymbols(bool ignore=true)
Learn and score on the fully observed data only, dropping every row and every scoring instance that c...
void _raiseKMinForSlice_(int slice)
Raise kMin, if needed, so that kMin > slice: a smaller candidate would silently drop a constraint nam...
KTBNAdaptiveLearner< GUM_SCALAR > & useOrderScoreAIC()
Select k by AIC: keep the k whose learned model maximises (a lighter, sample-size-independent comple...
void _verifyBase_(std::string_view base, int slice) const
Throw InvalidArgument unless base is a known base variable and slice is valid for it: base must be in...
std::string _csvBaseName_
stem of each trajectory file name
Size _tabuSize_
tabu-list parameters (meaningful when the algorithm is LOCAL_SEARCH_WITH_TABU_LIST)....
static std::unordered_set< std::string > _inferAtemporalVars_(std::string_view dirPath, std::string_view csvBaseName, Size nbSamples, Size kMax, const std::vector< std::string > &missingSymbols)
checks kMax >= 2 and nbSamples >= 1, then delegates to the shared IKTBNLearner::scanConstantColumns()...
KTBNAdaptiveLearner< GUM_SCALAR > & setMaxIndegree(Size max_indegree) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & useScoreBD() override
KTBNAdaptiveLearner< GUM_SCALAR > & addNoParentNode(std::string_view base, int slice) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & useScoreBIC() override
CorrectedMutualInformation::KModeTypes _correction_
recorded MIIC correction
KTBNAdaptiveLearner< GUM_SCALAR > & addForbiddenKernelArc(std::string_view tailBase, int lag, std::string_view headBase)
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
KTBNAdaptiveLearner< GUM_SCALAR > & useNoCorrection() override
KTBNAdaptiveLearner< GUM_SCALAR > & useMIIC() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNAdaptiveLearner< GUM_SCALAR > & eraseNoChildrenNode(std::string_view base, int slice) override
Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at sli...
std::set< std::tuple< std::string, std::string, int > > _forbiddenKernelArcs_
forbidden kernel-relative arcs, as (tailBase, headBase, lag) triples: tailBase at slice k-1-lag -> he...
KTBNAdaptiveLearner< GUM_SCALAR > & useFCI() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
std::string checkScorePriorCompatibility() const
Warning string if the recorded score and prior are incompatible, empty otherwise. Data-free: it evalu...
Learns a k-TBN (structure and/or parameters) from trajectory CSVs.
KTBNLearner< GUM_SCALAR > & useNMLCorrection() override
Engine-name pairs (tail, head) of arcs flagged as hiding a latent variable by MIIC (merged from all i...
KTBNLearner< GUM_SCALAR > & useGreedyThickThinning() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNLearner< GUM_SCALAR > & addForbiddenArc(std::string_view tailNode, std::string_view headNode) override
Forbid tailNode from ever parenting headNode (engine names, e.g. "X[1]", "C").
KTBNLearner< GUM_SCALAR > & usePC() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNLearner< GUM_SCALAR > & useScoreAIC() override
Returns a warning string if the current score and prior are incompatible, empty string otherwise.
KTBNLearner< GUM_SCALAR > & useGreedyHillClimbing() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNLearner< GUM_SCALAR > & useScoreLog2Likelihood() override
Returns a warning string if the current score and prior are incompatible, empty string otherwise.
KTBNLearner< GUM_SCALAR > & addForbiddenArcAllSlices(std::string_view tailBase, std::string_view headBase) override
Forbid tailBase -> headBase at every causally-possible slice pair (every lag, not just matching slice...
KTBNLearner< GUM_SCALAR > & useScoreBD() override
Returns a warning string if the current score and prior are incompatible, empty string otherwise.
KTBNLearner< GUM_SCALAR > & allowArcReversals(bool allow=true) override
Allow or forbid arc reversals during structure search.
KTBNLearner< GUM_SCALAR > & addNoChildrenNode(std::string_view base, int slice) override
Declare a single (base, slice) node as a leaf (no children).
KTBNLearner< GUM_SCALAR > & allowArcDeletions(bool allow=true) override
Allow or forbid arc deletions during structure search.
KTBNLearner< GUM_SCALAR > & useExtendedGreedyHillClimbing() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNLearner< GUM_SCALAR > & useNoCorrection() override
Engine-name pairs (tail, head) of arcs flagged as hiding a latent variable by MIIC (merged from all i...
KTBNLearner< GUM_SCALAR > & allowArcAdditions(bool allow=true) override
Allow or forbid arc additions during structure search.
KTBNLearner< GUM_SCALAR > & useSmoothingPrior(double weight) override
the default weight (key priorWeight of the section learning)
KTBNLearner< GUM_SCALAR > & useScoreBIC() override
Returns a warning string if the current score and prior are incompatible, empty string otherwise.
KTBNLearner< GUM_SCALAR > & useMIIC() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
void useScorefNML() override
Returns a warning string if the current score and prior are incompatible, empty string otherwise.
KTBNLearner< GUM_SCALAR > & useScoreMDL() override
Returns a warning string if the current score and prior are incompatible, empty string otherwise.
KTBNLearner< GUM_SCALAR > & addMandatoryArc(std::string_view tailNode, std::string_view headNode) override
Force tailNode to be a parent of headNode (engine names, e.g. "X[1]", "C").
KTBNLearner< GUM_SCALAR > & setMaxIndegree(Size max_indegree) override
Cap the number of parents of any single node.
KTBNLearner< GUM_SCALAR > & addForbiddenIntraSliceArc(std::string_view tailBase, std::string_view headBase) override
Forbid tailBase -> headBase at every intra-slice position (i.e. tailBase[t] -> headBase[t] for all t ...
KTBNLearner< GUM_SCALAR > & addPossibleEdge(std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice) override
Add a candidate edge for MIIC (only edges explicitly listed are explored).
KTBNLearner< GUM_SCALAR > & useLocalSearchWithTabuList(Size tabu_size, Size nb_decrease) override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
KTBNLearner< GUM_SCALAR > & addNoParentNode(std::string_view base, int slice) override
Declare a single (base, slice) node as a root (no parents).
KTBNLearner< GUM_SCALAR > & useFCI() override
the default parameters (keys tabuListSize, tabuNbDecrease of the section learning)
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Header files of gum::Instantiation.
include the inlined functions if necessary
the base class for all a priori
the class for computing AIC scores
the class for computing Bayesian Dirichlet (BD) log2 scores
the class for computing BDeu scores
the class for computing BIC scores
the class for computing Log2-likelihood scores
the class for computing MDL scores
the class for computing fNML scores