Learning a CTBN
One of the main features of this library is the possibility to learn a CTBN.
- More precisely what can be learned is :
The dependency graph of a CTBN
The CIMs of a CTBN
(The variables and their labels from a sample)
Tools to extract data from samples are necessary. This is the role of class pyagrum.ctbn.Trajectory and function pyagrum.ctbn.CTBNFromData().
- Before introducing the algorithms, here are the following definitions :
\(M_{xx'|u}\) is the number of time a variable
Xgo from a statexto a statex', conditioned by an instance of its parentsu. It is filled using samples.\(M_{x|u}\) is the number of time
Xgoes to statex.\(T_{x|u}\) is the time spent in state
x, conditioned by an instance of its parentsu.\(M_{xx'|y,u}\) and \(T_{x|y,u}\) are the same but with another conditioning variable
Yin statey.
Those can be stored in pyagrum.Tensor.
Being conditioned by an instance means that the extracted data comes from time intervals where conditioning variables take specific values.
Learning parameters : learning the CIMs
Goal : finding the \(q_{i,j|u}\) (i.e \(q_{x|u}\) and \(q_{x \rightarrow x'|u}\)) coefficients.
Idea : \(q_{x|u}\) = \(\frac{M_{x|u}}{T_{x|u}}\); \(P_X(x\rightarrow x') = \frac{M_{x \rightarrow x'|u}}{M_{x|u}} = \frac{q_{x \rightarrow x'|u}}{q_{x|u}}\) Then \(q_{x \rightarrow x'|u} = \frac{M_{x \rightarrow x'|u}}{T_{x|u}}\)
Learning the graph
To learn the graph of a CTBN (ie the dependence between variables) we use the CTPC algorithm from Bregoli et al. [BSS20] (and using Nodelman et al. [NSK03]). The independence test used is based on Fisher and chi2 tests to compare exponential distributions.
- class pyagrum.ctbn.Learner(source)
Class used to learn a CTBN (independence between variables and CIMs) using samples.
- Parameters:
source (
str|dict[int,list[tuple[float,str,str]]]) – Path to the csv file containing the samples(trajectories). Or directly the trajectories in a python dict.
Scientific notes
Learner learns the conditional intensity matrices (see pyagrum.ctbn.CIM) and the graph of a pyagrum.ctbn.CTBN from
fully observed trajectories.
Parameters. For a variable \(X\) with parents \(U\), the sufficient statistics of the trajectories are the time \(T[x \mid u]\) spent in \(x\) while \(U = u\), and the number \(M[x, x' \mid u]\) of transitions from \(x\) to \(x'\) while \(U = u\), summed over all the trajectories [1]. The maximum likelihood estimates are
(no prior; a state never visited while \(U = u\) keeps rates 0).
Structure. The graph is learned by CTPC [2], a PC-like algorithm: starting from the complete directed graph, for each \(X\) and for growing sizes \(b\), the arc \(Y \rightarrow X\) is removed as soon as \(X\) is found independent of \(Y\) given a subset \(S\) of size \(b\) of the other current parents. As the dependence of a continuous-time process is not symmetric and follows time, no orientation step is needed, and cycles are allowed. \(X\) is declared independent of \(Y\) given \(S\) unless one of two tests rejects it:
the time to transition test compares the exit rates without and with \(Y\): under independence, \(F = \hat q_{x \mid s} / \hat q_{x \mid y,s}\) follows a Fisher law with \((r_1, r_2)\) degrees of freedom, \(r_1 = M[x \mid y, s]\), \(r_2 = M[x \mid s]\). It rejects when, for some \(x\), \(y\) and \(s\), \(F\) is out of the interval between the \(\alpha_F/2\) and \(1 - \alpha_F/2\) quantiles, with \(\alpha_F\) =
pyagrum.config['ctbn', 'fSignificance'](0.05); a configuration without any exit observed (\(r_1 = 0\) or \(r_2 = 0\)) brings no evidence and is skipped;the state to state test (only when \(X\) has at least 3 values, otherwise the target of a transition is known) compares the transition counts with a two-sample chi-square statistic
\[\chi^2 = \sum_{x' \neq x} \frac{(K\, M[x,x' \mid y,s] - L\, M[x,x' \mid s])^2}{M[x,x' \mid y,s] + M[x,x' \mid s]}, \quad K = \sqrt{\frac{M[x \mid s]}{M[x \mid y,s]}}, \quad L = \frac{1}{K},\]It rejects when, for some \(x\), \(y\) and \(s\), \(\chi^2\) exceeds the \(1 - \alpha_{\chi^2}\) quantile of a \(\chi^2\) law with \(|\mathrm{Val}(X)| - 1\) degrees of freedom, with \(\alpha_{\chi^2}\) =
pyagrum.config['ctbn', 'chi2Significance'](0.05).
Each level applies to one elementary test (one per state and configuration of the parents), without correction for multiple testing.
Implementation limit. The independence tests are computed on the variables and values seen in the data; every variable of a template CTBN must appear in the data.
Methods
Learns the parameters of |
|
Learns a CTBN, using the CTPC (continuous-time PC) algorithm. |
Reads trajectories from a csv file. |
|
Constructs a CTBN and add the corresponding variables found in the trajectories. |
|
Computes a CIM (Conditional Intensity Matrix) using stats from a trajectory. |
- class pyagrum.ctbn.Trajectory(source, ctbn=None)
Tools to extract useful informations from a trajectory. It is used for parameters/graph learning. It can be created from a trajectory (a dict of trajectories) or from a file that contains one.
- Parameters:
source (
str|dict[int,list[tuple[float,str,str]]]) – The path to a csv file containing the samples or the dict of trajectories itself.ctbn (
CTBN) – To link the variables’s name in the trajectory to their pyAgrum variable. If not given, a new CTBN is created with the variables and labels found in the trajectory. (warning : if the trajectory is short, all of the variables may not be found correctly).
- data
The samples.
- Type:
dict[int,list[tuple[float,str,str]]]
- timeHorizon
The time length of the first trajectory (each trajectory is read up to its own end).
- Type:
float
Methods
Computes the CIMs of the variables in |
|
Computes time spent and number of transitions values of |
|
Computes time spent and number of transitions values of |
|
Fills the tensors given. |
|
Fills the tensors given. |
- class pyagrum.ctbn.Stats(trajectory, X, Y, par)
Stores all tensors used for learning.
- Parameters:
trajectory (
Trajectory) – Samples used to find stats.X (
str) – Name of the variable to study.Y (
str) – Name of the variable used for conditioning variableX.par (
list[str]) – List of conditioning variables ofX.
- Mxy
Tensor containing the number of transitions the variable
Xdoes from any of its states for any instance of its parents and variable``Y``.- Type:
- Mx
Tensor containing the number of transitions the variable
Xdoes from any of its states for any instance of its parents.- Type:
- Tx
Tensor containing the time spent by
Xto transition from a state to another for any instance of its parents.- Type:
- Txy
Tensor containing the time spent by
Xto transition from a state to another for any instance of its parents and ofY.- Type:
- Qx
Conditional Intensity Matrix(CIM) of
X.- Type:
- QxY
Conditional Intensity Matrix(CIM) of
Xthat includes the conditioning variableY.- Type:
- class pyagrum.ctbn.StatsIndepTest.FChi2Test(tr)
Bases:
IndepTestThis class use 2 independence tests : Fisher Test (F-test) and chi2 Test. To test independence between 2 variables, we first consider them independent. There is independence until one of the 2 tests (F and chi2) contradict the independence hypothesis. If the hyopothesis is not rejected, the variables are considered independent.
The F test is two-sided, with the significance level
pyagrum.config['ctbn', 'fSignificance']; the chi2 test usespyagrum.config['ctbn', 'chi2Significance']. Both levels are read when the object is created.- Parameters:
tr (
Trajectory) – Samples used to extract stats.
- addVariables(X, Y, U)
Saves variables
XandYand the conditioning setU, and generates stats to be used in statistical tests.- Parameters:
X (
str) – Name of the variable.Y (
str) – Name of the variable to test independence from, not inU.U (
list[str]) – List of conditioning variables.
- computeChi2()
Compute chi2-test value for every instance of the variables.
- Returns:
chi2-test value.
- Return type:
- computeF()
Compute F-test value for every instance of the variables (the test reads only the diagonal, the exit rates).
- Returns:
F-test value.
- Return type:
- getMxxGivenU(M, Y)
- Parameters:
M (
pyagrum.Tensor) – A matrix M_{x, x’ | y, U}, for some instantiation U of the conditioning set and y of a specific parent.Y (
str) – A parent.
- Returns:
The tensor M_{x, x’ | U} by summing over all values of y.
- Return type:
- nullStateToStateTransitionHypothesisChi2(X, Y, _)
Decides if the null state to state transition hypothesis is rejected using chi2-test.
- Parameters:
X (
str) – A random variable.Y (
str) – A parent ofX._ (
list[str]) – A subset of the parents ofXthat does not containY._
- Returns:
False if
Xis not independent ofYgiven the conditioning setU.- Return type:
bool
- nullTimeToTransitionHypothesisF(X, Y, _)
Decides if the null time to transition hypothesis is rejected using F-test.
- Parameters:
X (
str) – A random variable.Y (
str) – A parent ofX._ (
list[str]) – A subset of the parents ofXthat does not containY._
- Returns:
False if
Xis not independent ofYgiven the conditioning setU.- Return type:
bool
- testIndep(X, Y, U)
- Parameters:
X (
str) – Name of the variable.Y (
str) – Name of the variable to test independence from, not inU.U (
list[str]) – List of conditioning variables.
- Returns:
true if
Xis independent toYgivenU, otherwise false.- Return type:
bool
- class pyagrum.ctbn.StatsIndepTest.IndepTest
Bases:
objectMother class used to test independance between 2 variables knowing some other parents.
- abstractmethod testIndep(X, Y, U)
- Parameters:
X (
str) – Head of the arc we want to test.Y (
str) – Tail of the arc we want to test.U (
list[str]) – Known parents.
- Return type:
bool
- class pyagrum.ctbn.StatsIndepTest.Oracle(ctbn)
Bases:
IndepTestOracle’s testing tools.
- Parameters:
ctbn (
CTBN)
- testIndep(X, Y, U)
- Parameters:
X (
str) – Head of the arc we want to test.Y (
str) – Tail of the arc we want to test.U (
list[str]) – Known parents.
- Returns:
False if there is an arc from Y to X knowing U, True otherwise.
- Return type:
bool
- pyagrum.ctbn.StatsIndepTest.sqrtTensor(tensor)
Applies sqrt function to all values inside the tensor.
- Parameters:
tensor (
pyagrum.Tensor) – tensor to play sqrt to.- Returns:
sqrt of tensor.
- Return type:
Bibliography for CTBN
Alessandro Bregoli, Marco Scutari, and Fabio Stella. Constraint-based learning for continuous-time Bayesian networks. In Manfred Jaeger and Thomas Dyhre Nielsen, editors, Proceedings of the 10th International Conference on Probabilistic Graphical Models, volume 138 of Proceedings of Machine Learning Research, 41–52. PMLR, 23–25 Sep 2020. URL: https://proceedings.mlr.press/v138/bregoli20a.html.
Uri Nodelman, Christian R. Shelton, and Daphne Koller. Learning continuous time Bayesian networks. In Proceedings of the Nineteenth Conference on Uncertainty in Artificial Intelligence, UAI'03, 451–458. San Francisco, CA, USA, 2003. Morgan Kaufmann Publishers Inc. URL: https://arxiv.org/abs/1212.2498.