Learning k-TBNs

pyagrum.ktbn.KTBNDatabaseGenerator samples trajectory CSV databases from a k-TBN, for learning experiments.

class pyagrum.ktbn.KTBNDatabaseGenerator(kdbn)

KTBNDatabaseGenerator generates a database of trajectories from a pyagrum.ktbn.KTBN, one CSV file per trajectory. Unlike a plain Bayesian network, a k-TBN describes a stochastic process, so its database is a set of trajectories rather than a flat table of i.i.d. rows: each CSV file has one column per base variable and T data rows, an atemporal variable keeping the same value across all T rows.

Sampling follows the same forward (ancestral) sampling principle as pyAgrum’s Bayesian-network database generator, but never unrolls the k-TBN: it keeps only the k-slice template and slides it forward in time, so memory stays independent of the number of trajectories and their length.

Examples

>>> import pyagrum.ktbn as ktbn
>>> kdbn = ...                                          # a k-TBN
>>> gen = ktbn.KTBNDatabaseGenerator(kdbn)
>>> # 1000 trajectories of length 10 -> out_dir/traj1.csv ... out_dir/traj1000.csv
>>> ll = gen.drawSamples(1000, 10, "out_dir", "traj")
KTBNDatabaseGenerator(kdbn) -> KTBNDatabaseGenerator
Parameters:
  • kdbn (pyagrum.ktbn.KTBN) – the k-TBN to sample from (only its k-slice template is copied; the k-TBN itself is not retained)

Parameters:

kdbn (KTBN)

DiscretizedLabelMode_INTERVAL = 0
DiscretizedLabelMode_MEDIAN = 1
DiscretizedLabelMode_RANDOM = 2
VarOrderMode_ANTI_TOPOLOGICAL = 2
VarOrderMode_RANDOM = 0
VarOrderMode_TOPOLOGICAL = 1
drawSamples(*args)

Generate trajectories, writing one CSV file per trajectory into dirPath. File names are csvBaseName followed by the 1-based trajectory index and .csv (e.g. traj1.csv, traj2.csv, …). Either every trajectory shares the same horizon, or each trajectory gets its own (pass a list of horizons instead of a sample count and a single horizon).

Examples

>>> gen.drawSamples(100, 10, "out_dir", "traj")            # 100 trajectories, length 10
>>> gen.drawSamples([8, 10, 12], "out_dir", "traj")        # 3 trajectories, own lengths
Parameters:
  • nbSamples (int) – the number of trajectories to generate (fixed-length form)

  • nbTimeSlices (int or list[int]) – the horizon shared by every trajectory (fixed-length form), or a list giving each trajectory’s own horizon (variable-length form); every value must be >= k

  • dirPath (str) – directory to write the CSV files into

  • csvBaseName (str) – stem for each file name (index and .csv appended)

  • mode (pyagrum.ktbn.KTBNDatabaseGenerator.VarOrderMode) – the column order of the base variables (default: RANDOM)

  • useLabels (bool) – render values as variable labels rather than modality indices (default True)

  • csvSeparator (str) – the column separator (must not contain a newline)

Returns:

the log2-likelihood of each generated trajectory

Return type:

tuple[float]

Raises:

pyagrum.OperationNotAllowed – if a horizon is smaller than k

nbVars()
Returns:

the number of base variable columns

Return type:

int

setDiscretizedLabelModeInterval()

Set discretized-variable label rendering to the interval label, e.g. "[min,max[".

Return type:

None

setDiscretizedLabelModeMedian()

Set discretized-variable label rendering to the (deterministic) interval median.

Return type:

None

setDiscretizedLabelModeRandom()

Set discretized-variable label rendering to a uniform random draw within the interval (the default; each labelled export then differs).

Return type:

None

pyagrum.ktbn.KTBNLearner learns a k-TBN’s structure and/or parameters from trajectory CSVs, at a fixed order k.

class pyagrum.ktbn.KTBNLearner(*args)

KTBNLearner learns a pyagrum.ktbn.KTBN (structure and/or parameters), at a fixed order k, from a set of trajectory CSV files (one file per trajectory, as produced by pyagrum.ktbn.KTBNDatabaseGenerator: one column per base variable, one row per time step).

Internally, k-TBN learning is reduced to three ordinary Bayesian-network learning problems, solved with the same score/algorithm/prior machinery as pyagrum.BNLearner, then glued back together: the transition table (sliding windows of width k) learns the repeating kernel; the initial table (the first k-1 time steps of each trajectory) learns the initial slices; the atemporal table (one row per trajectory) learns the arcs between atemporal variables. Temporal ordering and no-backward-arc constraints are applied automatically.

Examples

>>> import pyagrum.ktbn as ktbn
>>> # atemporal variables inferred from the data
>>> learner = ktbn.KTBNLearner("trajs/", "traj", 500, 2)
>>> learner.useScoreBIC().useGreedyHillClimbing()
>>> model = learner.learnKTBN()
>>>
>>> # or state the classification explicitly (pass an actual `set`, not a list)
>>> learner2 = ktbn.KTBNLearner("trajs/", "traj", 500, 2, {"C", "D"})
KTBNLearner(dirPath, csvBaseName, nbSamples, k, atemporalVars, missingSymbols=[‘?’], induceTypes=True, ignoreMissingSymbols=False) -> KTBNLearner

Structure-learning constructor with the temporal/atemporal classification supplied explicitly.

Parameters:
  • dirPath (str) – directory holding the trajectory CSV files

  • csvBaseName (str) – stem of each file name (1-based index and .csv appended, e.g. "traj" -> traj1.csv, traj2.csv, …)

  • nbSamples (int) – number of CSV files to read

  • k (int) – order of the k-TBN; must be >= 2

  • atemporalVars (set[str]) – base names of the atemporal (static) variables; every other name found in the CSV header is temporal. Must be an actual Python set (a list/tuple at this position instead selects the atemporal-inferring overload)

  • missingSymbols (list[str]) – symbols in the CSVs interpreted as missing values

  • induceTypes (bool) – retype all-numeric columns as integer/range/continuous instead of plain labels

  • ignoreMissingSymbols (bool) – drop a row carrying a missing symbol instead of refusing the whole database (see nbDroppedRows() for the bias this introduces)

KTBNLearner(dirPath, csvBaseName, nbSamples, k, missingSymbols=[‘?’], induceTypes=True, ignoreMissingSymbols=False) -> KTBNLearner

Same, with the temporal/atemporal classification inferred instead: a base variable is atemporal iff its value never changes across the rows of any single trajectory (a heuristic – prefer the explicit form when the classification is already known).

KTBNLearner(dirPath, csvBaseName, nbSamples, k, bn, atemporalVars=set(), missingSymbols=[‘?’], ignoreMissingSymbols=False) -> KTBNLearner

Variable-schema constructor: types and domains are supplied via a reference pyagrum.BayesNet (one node per base variable, bare names, arcs ignored) instead of inferred from the CSVs. Use this when a variable’s full domain is not guaranteed to appear in the first trajectory – notably atemporal variables, which only ever show one value per trajectory.

Parameters:
  • bn (pyagrum.BayesNet) – a BayesNet with one node per base variable, providing the variable types and domains

addForbiddenArc(*args)

Forbid an arc from ever appearing in the learned structure.

Parameters:
  • tailNode (str) – engine names (e.g. "X[1]", "C") of the two endpoints

  • headNode (str) – engine names (e.g. "X[1]", "C") of the two endpoints

  • tailBase (str) – alternatively, base names of the two endpoints (used together with the slices below)

  • headBase (str) – alternatively, base names of the two endpoints (used together with the slices below)

  • tailSlice (int) – slices of the endpoints (pyagrum.ktbn.KTBN.ATEMPORAL for static)

  • headSlice (int) – slices of the endpoints (pyagrum.ktbn.KTBN.ATEMPORAL for static)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

addForbiddenArcAllSlices(tailBase, headBase)

Forbid tailBase -> headBase at every causally-possible slice pair (every lag): tailBase can never be an ancestor of headBase in the learned k-TBN.

Parameters:
  • tailBase (str) – base names of the two variables

  • headBase (str) – base names of the two variables

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

addForbiddenIntraSliceArc(tailBase, headBase)

Forbid tailBase -> headBase at every intra-slice position (i.e. for every slice t, tailBase[t] -> headBase[t]).

Parameters:
  • tailBase (str) – base names of the two temporal variables

  • headBase (str) – base names of the two temporal variables

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

Raises:

pyagrum.InvalidArgument – if either endpoint is unknown or atemporal

addMandatoryArc(*args)

Force an arc to be part of the learned structure.

Parameters:
  • tailNode (str) – engine names of the two endpoints

  • headNode (str) – engine names of the two endpoints

  • tailBase (str) – alternatively, base names of the two endpoints (used together with the slices below)

  • headBase (str) – alternatively, base names of the two endpoints (used together with the slices below)

  • tailSlice (int) – slices of the endpoints (pyagrum.ktbn.KTBN.ATEMPORAL for static)

  • headSlice (int) – slices of the endpoints (pyagrum.ktbn.KTBN.ATEMPORAL for static)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

addNoChildrenNode(*args)

Declare a node as a leaf (forbid it from having any child).

Parameters:
  • base (str) – base name of the node (used together with slice)

  • slice (int) – slice of the node (pyagrum.ktbn.KTBN.ATEMPORAL for a static node)

  • name (str) – alternatively, the node’s engine name

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

addNoParentNode(*args)

Declare a node as a root (forbid it from having any parent).

Parameters:
  • base (str) – base name of the node (used together with slice)

  • slice (int) – slice of the node (pyagrum.ktbn.KTBN.ATEMPORAL for a static node)

  • name (str) – alternatively, the node’s engine name (e.g. "X[2]", "C")

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

addPossibleEdge(*args)

Add a candidate edge for MIIC: once at least one edge has been listed, only explicitly listed edges are explored by the structure search.

Parameters:
  • tail (str) – engine names of the two endpoints

  • head (str) – engine names of the two endpoints

  • tailBase (str) – alternatively, base names of the two endpoints (used together with the slices below)

  • headBase (str) – alternatively, base names of the two endpoints (used together with the slices below)

  • tailSlice (int) – slices of the endpoints

  • headSlice (int) – slices of the endpoints

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

allowArcAdditions(allow=True)

Allow or forbid arc additions during structure search.

Parameters:

allow (bool) – whether to allow arc additions (default True)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

allowArcDeletions(allow=True)

Allow or forbid arc deletions during structure search.

Parameters:

allow (bool) – whether to allow arc deletions (default True)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

allowArcReversals(allow=True)

Allow or forbid arc reversals during structure search.

Parameters:

allow (bool) – whether to allow arc reversals (default True)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

checkScorePriorCompatibility()
Returns:

a warning message if the current score and prior are incompatible, an empty string otherwise

Return type:

str

copyState(learner)

Copy all score/algorithm/prior/constraint settings from another KTBNLearner (does not copy the database).

Parameters:

learner (pyagrum.ktbn.KTBNLearner) – the learner to copy settings from

Return type:

None

domainSize(base)
Parameters:

base (str) – a base variable name (e.g. "X"); an engine name (e.g. "X[1]") is also accepted

Returns:

the domain size of base

Return type:

int

domainSizes()
Returns:

domain sizes of the base variables, in the same order as names()

Return type:

tuple[int]

eraseForbiddenArc(*args)

Undo a previous addForbiddenArc(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

eraseForbiddenArcAllSlices(tailBase, headBase)

Undo a previous addForbiddenArcAllSlices(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

Parameters:
  • tailBase (str)

  • headBase (str)

eraseForbiddenIntraSliceArc(tailBase, headBase)

Undo a previous addForbiddenIntraSliceArc(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

Parameters:
  • tailBase (str)

  • headBase (str)

eraseMandatoryArc(*args)

Undo a previous addMandatoryArc(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

eraseNoChildrenNode(*args)

Undo a previous addNoChildrenNode(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

eraseNoParentNode(*args)

Undo a previous addNoParentNode(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

erasePossibleEdge(*args)

Undo a previous addPossibleEdge(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

hasMissingValues()
Returns:

always False: incomplete rows are dropped by construction (see nbDroppedRows() to learn whether the CSVs actually had any)

Return type:

bool

isConstraintBased()
Returns:

True if the current structure-learning algorithm is constraint-based (MIIC)

Return type:

bool

isIgnoringMissingSymbols()
Returns:

True if incomplete rows are dropped rather than rejected outright

Return type:

bool

isScoreBased()
Returns:

True if the current structure-learning algorithm is score-based (BIC, AIC, …)

Return type:

bool

k()
Returns:

the order k of the k-TBN being learned

Return type:

int

latentVariables()
Returns:

(tail, head) engine-name pairs of arcs MIIC flagged as hiding a latent variable (merged from the three internal learners); empty when the algorithm is not MIIC

Return type:

tuple[tuple[str, str]]

learnKTBN()

Learn the k-TBN’s structure and CPTs.

Returns:

the learned k-TBN

Return type:

pyagrum.ktbn.KTBN

learnParameters(structure, takeIntoAccountScore=True)

Learn only the CPTs, using the arc structure of an already-known k-TBN.

Parameters:
  • structure (pyagrum.ktbn.KTBN) – a k-TBN with the same base variables (names and domains) used to construct this learner; a mismatch raises at learn time

  • takeIntoAccountScore (bool) – whether to use the recorded score/prior when estimating parameters (default True)

Returns:

a new k-TBN with structure’s arcs and freshly learned CPTs

Return type:

pyagrum.ktbn.KTBN

names()
Returns:

base names (no slice suffix), one per base variable, in the original CSV header order

Return type:

tuple[str]

nbCols()
Returns:

the number of base variable columns (temporal + atemporal)

Return type:

int

nbDroppedRows()

Number of rows dropped from the internal databases because they carried a missing symbol.

Warning

Dropping rows inflates the log-likelihood computed on the result, and does so more for larger k (a larger k spans more rows per window, so a single missing value costs more of them). Prefer complete trajectories whenever comparing likelihoods across models or orders.

Returns:

the number of dropped rows, summed across the three internal tables

Return type:

int

nbRows()
Returns:

the number of time steps in each trajectory CSV (one entry per sample, in load order); the raw trajectory length, not the transition table’s sliding-window row count

Return type:

tuple[int]

nbSamples()
Returns:

the number of trajectory CSV files loaded

Return type:

int

setMaxIndegree(max_indegree)

Cap the number of parents of any single node.

Parameters:

max_indegree (int) – the maximum in-degree

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

state()
Returns:

the settings, as (key, value, comment) tuples

Return type:

tuple[tuple[str, str, str]]

toString()
Returns:

a human-readable summary of the learner’s current configuration

Return type:

str

useExtendedGreedyHillClimbing()

Use greedy hill-climbing extended with arc-reversal moves.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useFCI()

Use the constraint-based FCI algorithm for structure search.

The significance threshold is the key alpha of the section learning of the configuration.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useGreedyHillClimbing()

Use greedy hill-climbing for structure search.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useGreedyThickThinning()

Use greedy thick-thinning for structure search.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useLocalSearchWithTabuList(tabu_size=None, nb_decrease=None)

Use local search with a tabu list.

Parameters:
  • tabu_size (int, optional) – the tabu list size. By default, it is pyagrum.config[‘learning’, ‘tabuListSize’] (100).

  • nb_decrease (int, optional) – the number of non-improving moves tolerated before stopping. By default, it is pyagrum.config[‘learning’, ‘tabuNbDecrease’] (2).

Returns:

the learner itself, so that we can chain the calls.

Return type:

pyagrum.ktbn.KTBNLearner

useMDLCorrection()

Use the MDL correction for MIIC’s independence tests.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useMIIC()

Use the constraint-based MIIC algorithm for structure search.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useNMLCorrection()

Use the NML correction for MIIC’s independence tests.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useNoCorrection()

Disable correction for MIIC’s independence tests.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

usePC()

Use the constraint-based PC algorithm for structure search.

The significance threshold is the key alpha of the section learning of the configuration.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useScoreAIC()

Use the AIC score for structure learning.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useScoreBD()

Use the BD score for structure learning.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useScoreBDeu()

Use the BDeu score for structure learning.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useScoreBIC()

Use the BIC score for structure learning.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useScoreLog2Likelihood()

Use the raw log2-likelihood score for structure learning.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useScoreMDL()

Use the MDL score for structure learning.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNLearner

useScorefNML()

Use the fNML score for structure learning.

Return type:

None

useSmoothingPrior(weight=None)

Use a Laplace/BDeu-style smoothing prior.

Parameters:

weight (float, optional) – the prior weight, which must not be negative. By default, it is pyagrum.config[‘learning’, ‘priorWeight’] (1.0).

Returns:

the learner itself, so that we can chain the calls.

Return type:

pyagrum.ktbn.KTBNLearner

Raises:

pyagrum.OutOfBounds – if weight is negative.

pyagrum.ktbn.KTBNAdaptiveLearner additionally selects the order k itself, by exploring every candidate in a range and keeping the best one according to a cross-k model-selection criterion.

class pyagrum.ktbn.KTBNAdaptiveLearner(*args)

KTBNAdaptiveLearner is the order-selecting counterpart of pyagrum.ktbn.KTBNLearner: instead of being given the order k, it explores every candidate k in [kMin, kMax] (kMin starts at 2 and is raised automatically by structural constraints naming a concrete slice), learns one k-TBN per candidate with an internal pyagrum.ktbn.KTBNLearner, and keeps the one with the best cross-k model-selection score (BIC by default).

It exposes the same configuration interface as KTBNLearner (score, algorithm, prior, structural constraints – see that class’s docstrings, valid here too), except settings are only recorded and replayed on each per-k learner at learnKTBN() time; a constraint whose slice does not fit a given candidate is silently skipped for that candidate only.

Examples

>>> import pyagrum.ktbn as ktbn
>>> learner = ktbn.KTBNAdaptiveLearner("trajs/", "traj", 500, kMax=4)
>>> learner.useScoreBIC().useGreedyHillClimbing()
>>> model = learner.learnKTBN()
>>> learner.bestK()
>>> learner.scorePerCandidateK()
KTBNAdaptiveLearner(dirPath, csvBaseName, nbSamples, kMax, atemporalVars, missingSymbols=[‘?’], induceTypes=True) -> KTBNAdaptiveLearner
Parameters:
  • dirPath (str) – directory holding the trajectory CSV files

  • csvBaseName (str) – stem of each file name

  • nbSamples (int) – number of CSV files to read

  • kMax (int) – largest order to explore; must be >= 2

  • atemporalVars (set[str]) – base names of the atemporal variables (must be an actual Python set)

  • missingSymbols (list[str]) – symbols in the CSVs interpreted as missing values

  • induceTypes (bool) – retype all-numeric columns

KTBNAdaptiveLearner(dirPath, csvBaseName, nbSamples, kMax, missingSymbols=[‘?’], induceTypes=True) -> KTBNAdaptiveLearner

Same, with the atemporal classification inferred from the data instead of supplied explicitly (see the equivalent KTBNLearner constructor).

KTBNAdaptiveLearner(dirPath, csvBaseName, nbSamples, kMax, bn, atemporalVars=set(), missingSymbols=[‘?’]) -> KTBNAdaptiveLearner

Variable-schema constructor: types and domains supplied via a reference pyagrum.BayesNet instead of inferred from the CSVs (see the equivalent KTBNLearner constructor).

OrderScoreType_AIC = 1
OrderScoreType_BIC = 0
OrderScoreType_fNML = 2
addForbiddenArc(*args)
Return type:

KTBNAdaptiveLearner

addForbiddenArcAllSlices(tailBase, headBase)
Parameters:
  • tailBase (str)

  • headBase (str)

Return type:

KTBNAdaptiveLearner

addForbiddenIntraSliceArc(tailBase, headBase)
Parameters:
  • tailBase (str)

  • headBase (str)

Return type:

KTBNAdaptiveLearner

addForbiddenKernelArc(tailBase, lag, headBase)

Forbid an arc from tailBase, lag slices before the kernel, to headBase in the kernel: tailBase at slice k-1-lag -> headBase at slice k-1, for whichever k is selected. Unlike a plain (base,slice) constraint, the slice moves with the candidate, so it must be expressed relative to the kernel. Adaptive-only: the fixed-k pyagrum.ktbn.KTBNLearner has no moving kernel slice to anchor it to.

Parameters:
  • tailBase (str) – base names of the two temporal variables

  • headBase (str) – base names of the two temporal variables

  • lag (int) – the lag before the kernel slice, in [0, kMax)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

addMandatoryArc(*args)
Return type:

KTBNAdaptiveLearner

addMandatoryKernelArc(tailBase, lag, headBase)

Force an arc from tailBase, lag slices before the kernel, to headBase in the kernel. See addForbiddenKernelArc() for the kernel-relative convention.

Parameters:
  • tailBase (str) – base names of the two temporal variables

  • headBase (str) – base names of the two temporal variables

  • lag (int) – the lag before the kernel slice, in [0, kMax)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

addNoChildrenNode(*args)
Return type:

KTBNAdaptiveLearner

addNoParentNode(*args)
Return type:

KTBNAdaptiveLearner

addPossibleEdge(*args)
Return type:

KTBNAdaptiveLearner

allowArcAdditions(allow=True)
Parameters:

allow (bool)

Return type:

KTBNAdaptiveLearner

allowArcDeletions(allow=True)
Parameters:

allow (bool)

Return type:

KTBNAdaptiveLearner

allowArcReversals(allow=True)
Parameters:

allow (bool)

Return type:

KTBNAdaptiveLearner

bestK()
Returns:

the order k selected by the last learnKTBN() call

Return type:

int

Raises:

pyagrum.OperationNotAllowed – if learnKTBN has not run yet

checkScorePriorCompatibility()

Data-free check: evaluates the recorded (score, prior) pair as pyagrum.ktbn.KTBNLearner would, so an incompatible combination can be caught before learnKTBN reads any trajectory.

Returns:

a warning message if the current score and prior are incompatible, an empty string otherwise

Return type:

str

eraseForbiddenArc(*args)
Return type:

KTBNAdaptiveLearner

eraseForbiddenArcAllSlices(tailBase, headBase)
Parameters:
  • tailBase (str)

  • headBase (str)

Return type:

KTBNAdaptiveLearner

eraseForbiddenIntraSliceArc(tailBase, headBase)
Parameters:
  • tailBase (str)

  • headBase (str)

Return type:

KTBNAdaptiveLearner

eraseForbiddenKernelArc(tailBase, lag, headBase)

Undo a previous addForbiddenKernelArc(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

Parameters:
  • tailBase (str)

  • lag (int)

  • headBase (str)

eraseMandatoryArc(*args)
Return type:

KTBNAdaptiveLearner

eraseMandatoryKernelArc(tailBase, lag, headBase)

Undo a previous addMandatoryKernelArc(), same arguments.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

Parameters:
  • tailBase (str)

  • lag (int)

  • headBase (str)

eraseNoChildrenNode(*args)
Return type:

KTBNAdaptiveLearner

eraseNoParentNode(*args)
Return type:

KTBNAdaptiveLearner

erasePossibleEdge(*args)
Return type:

KTBNAdaptiveLearner

ignoreMissingSymbols(ignore=True)

Learn and score on the fully observed data only, dropping every row and scoring instance that carries a missing symbol.

Warning

This inflates the log-likelihood, and inflates it more for larger k – so it biases the very order selection this class performs. Inspect scorePerCandidateK() rather than trusting bestK() alone when missing values are frequent.

Parameters:

ignore (bool) – whether to drop incomplete rows/instances (default True)

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

isIgnoringMissingSymbols()
Returns:

True if incomplete rows/instances are dropped (default False)

Return type:

bool

kMax()
Returns:

the largest order explored (the kMax constructor argument)

Return type:

int

latentVariables()
Returns:

(tail, head) engine-name pairs of arcs the selected model’s MIIC run flagged as hiding a latent variable; empty when the recorded algorithm is not MIIC or none were found

Return type:

tuple[tuple[str, str]]

Raises:

pyagrum.OperationNotAllowed – if learnKTBN has not run yet

learnKTBN()

Learn the best k in [kMin, kMax] together with the structure and the CPTs: one internal pyagrum.ktbn.KTBNLearner is built and run per candidate, the recorded configuration is replayed on each, and the k-TBN with the best cross-k score is returned.

Returns:

the learned k-TBN, at the selected order

Return type:

pyagrum.ktbn.KTBN

scorePerCandidateK()
Returns:

(k, score) pairs for k = kMin..kMax in ascending k order, from the last learnKTBN call – the values order selection compared to pick bestK() (higher is better; the argmax is bestK)

Return type:

tuple[tuple[int, float]]

Raises:

pyagrum.OperationNotAllowed – if learnKTBN has not run yet

setMaxIndegree(max_indegree)
Parameters:

max_indegree (int)

Return type:

KTBNAdaptiveLearner

state()
Returns:

the recorded configuration, as (key, value, comment) tuples

Return type:

tuple[tuple[str, str, str]]

toString()
Returns:

a human-readable summary of the recorded configuration (candidate order range, algorithm/score/correction/prior, structural constraints), plus the selected k once learnKTBN has run

Return type:

str

useExtendedGreedyHillClimbing()
Return type:

KTBNAdaptiveLearner

useFCI()
Return type:

KTBNAdaptiveLearner

useGreedyHillClimbing()
Return type:

KTBNAdaptiveLearner

useGreedyThickThinning()
Return type:

KTBNAdaptiveLearner

useLocalSearchWithTabuList(tabu_size=None, nb_decrease=None)

Use local search with a tabu list.

Parameters:
  • tabu_size (int, optional) – the tabu list size. By default, it is pyagrum.config[‘learning’, ‘tabuListSize’] (100).

  • nb_decrease (int, optional) – the number of non-improving moves tolerated before stopping. By default, it is pyagrum.config[‘learning’, ‘tabuNbDecrease’] (2).

Returns:

the learner itself, so that we can chain the calls.

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

useMDLCorrection()
Return type:

KTBNAdaptiveLearner

useMIIC()
Return type:

KTBNAdaptiveLearner

useNMLCorrection()
Return type:

KTBNAdaptiveLearner

useNoCorrection()
Return type:

KTBNAdaptiveLearner

useOrderScoreAIC()

Select the best k by AIC: a lighter, sample-size-independent complexity penalty than BIC.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

useOrderScoreBIC()

Select the best k by BIC (the default): the candidate maximising log2-likelihood minus half the parameter count times log2(sample size).

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

useOrderScorefNML()

Select the best k by fNML (factorized Normalized Maximum Likelihood): a data-dependent penalty (unlike BIC/AIC), matching aGrUM’s ScorefNML.

Returns:

self, for chaining

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

usePC()
Return type:

KTBNAdaptiveLearner

useScoreAIC()
Return type:

KTBNAdaptiveLearner

useScoreBD()
Return type:

KTBNAdaptiveLearner

useScoreBDeu()
Return type:

KTBNAdaptiveLearner

useScoreBIC()
Return type:

KTBNAdaptiveLearner

useScoreLog2Likelihood()
Return type:

KTBNAdaptiveLearner

useScoreMDL()
Return type:

KTBNAdaptiveLearner

useScorefNML()
Return type:

None

useSmoothingPrior(weight=None)

Use a Laplace/BDeu-style smoothing prior.

Parameters:

weight (float, optional) – the prior weight, which must not be negative. By default, it is pyagrum.config[‘learning’, ‘priorWeight’] (1.0).

Returns:

the learner itself, so that we can chain the calls.

Return type:

pyagrum.ktbn.KTBNAdaptiveLearner

Raises:

pyagrum.OutOfBounds – if weight is negative.