Subpackages

Distributions

Auxiliary functions

trilearn.auxiliary_functions.gen_prec_mat(graph, a)[source]
trilearn.auxiliary_functions.get_marg_counts(full_data, subset)[source]

Returns a contingency table in dictionary form.

Parameters:
  • data (np.array) – The data in n x p form.

  • subset (list) – The subset of interest

trilearn.auxiliary_functions.group_trajectories_by_setting(trajlist)[source]
trilearn.auxiliary_functions.is_pos_def(x)[source]
trilearn.auxiliary_functions.l1_loss(m1, m2)[source]

L1 loss.

Parameters:
  • m1 (Numpy array) – A matrix

  • m1 – A matrix

Returns:

float

trilearn.auxiliary_functions.l2_loss(m1, m2)[source]

L2 loss between m1 and m2.

Parameters:
  • m1 (Numpy array) – A matrix

  • m1 – A matrix

Returns:

float

trilearn.auxiliary_functions.plot_graph_traj_statistics(graph_traj, write_to_file=False)[source]
trilearn.auxiliary_functions.plot_heatmap(heatmap, cbar=False, annot=False, xticklabels=1, yticklabels=1)[source]
trilearn.auxiliary_functions.plot_matrix(m, filename, extension, title='Adjmat')[source]

Plots a 2-dim numpy array as heatmap. :param m: matrix to plot. :type m: numpy array

trilearn.auxiliary_functions.plot_multiple_traj_statistics(trajs, burnin_end, write_to_file=False, annot=False, output_directory='./', file_extension='eps')[source]
trilearn.auxiliary_functions.random_element_from_coll(A)[source]
trilearn.auxiliary_functions.random_subset(A)[source]

Draws a random subset of elements in a list, inclding the empty set.

Parameters:

A (list) –

Returns:

Subset of A.

Return type:

set

trilearn.auxiliary_functions.read_all_trajectories_in_dir(directory)[source]
trilearn.auxiliary_functions.sample_classification_datasets(mus, covmats, n_samples_in_each_class)[source]
trilearn.auxiliary_functions.spc1(true_graph, est_graph)[source]

Takes 2 adjacency matrices.

trilearn.auxiliary_functions.tpr(true_graph, est_graph)[source]

Calculates the True positive rate of an estimated adjacency matrix.

Graph predictive classification

P. Green & A. Thomas MH-sampler

trilearn.mh_greenthomas.sample_trajectories_ggm_parallel(dataframe, n_samples, randomize=[1000], D=None, delta=1.0, reps=1, output_directory='.', **args)[source]
trilearn.mh_greenthomas.sample_trajectories_ggm_to_file(dataframe, n_samples, randomize=[1000], D=None, delta=1.0, reps=1, output_directory='.', **args)[source]
trilearn.mh_greenthomas.sample_trajectories_loglin_parallel(dataframe, n_samples, randomize=[1000], pseudo_obs=[1.0], reps=1, output_directory='.', **args)[source]
trilearn.mh_greenthomas.sample_trajectories_loglin_to_file(dataframe, n_samples, randomize=[1000], pseudo_obs=[1.0], reps=1, output_directory='.', **args)[source]
trilearn.mh_greenthomas.sample_trajectory(n_samples, randomize, sd)[source]
trilearn.mh_greenthomas.sample_trajectory_ggm(dataframe, n_samples, randomize=1000, D=None, delta=1.0, cache={}, **args)[source]
trilearn.mh_greenthomas.sample_trajectory_loglin(dataframe, n_samples, pseudo_obs=1.0, randomize=1000, cache={}, **args)[source]
trilearn.mh_greenthomas.sample_trajectory_uniform(n_samples, randomize=100, graph_size=5, cache={}, **args)[source]
trilearn.mh_greenthomas.trajectory_to_file(n_samples, randomize, seqdist, dir='.', reseed=False)[source]

Writes the trajectory of graphs generated by particle Gibbs to file.

Parameters:
  • seq_dist (SequentialJTDistributions) – the distribution to be sampled from

  • filename_prefix (string) – prefix to the filename

Returns:

Markov chain of underlying graphs of the junction trees sampled by pgibbs.

Return type:

Trajectory

trilearn.mh_greenthomas.trajectory_to_queue(n_samples, randomize, seqdist, queue, reseed=False)[source]

Writes the trajectory of graphs generated by particle Gibbs to file.

Parameters:
  • seq_dist (SequentialJTDistributions) – the distribution to be sampled from

  • filename_prefix (string) – prefix to the filename

Returns:

Markov chain of underlying graphs of the junction trees sampled by pgibbs.

Return type:

Trajectory

Particle Gibbs

trilearn.pgibbs.sample_trajectories_ggm(dataframe, n_particles, n_samples, D=None, delta=1.0, alphas=[0.5], betas=[0.5], radii=[None], reset_cache=True, reps=1, **args)[source]
trilearn.pgibbs.sample_trajectories_ggm_parallel(dataframe, n_particles, n_samples, D=None, delta=1.0, alphas=[0.5], betas=[0.5], radii=[None], reset_cache=True, reps=1, **args)[source]
trilearn.pgibbs.sample_trajectories_ggm_to_file(dataframe, n_particles, n_samples, D=None, delta=1.0, alphas=[0.5], betas=[0.5], radii=[None], reset_cache=True, reps=1, output_directory='.', output_filename='trajectory.json', **args)[source]
trilearn.pgibbs.sample_trajectories_loglin(dataframe, n_particles, n_samples, pseudo_observations=[1.0], alphas=[0.5], betas=[0.5], radii=[None], reset_cache=True, reps=1, **args)[source]
trilearn.pgibbs.sample_trajectories_loglin_parallel(dataframe, n_particles, n_samples, pseudo_observations=[1.0], alphas=[0.5], betas=[0.5], radii=[None], reset_cache=True, reps=1, output_directory='.', **args)[source]
trilearn.pgibbs.sample_trajectories_loglin_to_file(dataframe, n_particles, n_samples, pseudo_observations=[1.0], alphas=[0.5], betas=[0.5], radii=[None], reset_cache=True, reps=1, output_directory='.', output_filename='trajectory.json', **args)[source]
trilearn.pgibbs.sample_trajectory(smc_N, alpha, beta, radius, n_samples, seq_dist, jt_traj=None, debug=False, reset_cache=True)[source]

A particle Gibbs implementation for approximating distributions over junction trees.

Parameters:
  • smc_N (int) – Number of particles in SMC in each Gibbs iteration

  • n_samples (int) – Number of Gibbs iterations (samples)

  • alpha (float) – sparsity parameter for the Christmas tree algorithm

  • beta (float) – sparsity parameter for the Christmas tree algorithm

  • radius (float) – defines the radius within which ned nodes are selected

  • seq_dist (SequentialJTDistributions) – the distribution to be sampled from

Returns:

Markov chain of the underlying graphs of the junction trees sampled by pgibbs.

Return type:

Trajectory

trilearn.pgibbs.sample_trajectory_ggm(dataframe, n_particles, n_samples, D=None, delta=1.0, alpha=0.5, beta=0.5, radius=None, reset_cache=True, **args)[source]

Particle Gibbs for approximating distributions over Gaussian graphical models.

Parameters:
  • n_particles (int) – Number of particles in SMC in each Gibbs iteration

  • n_samples (int) – Number of Gibbs iterations (samples)

  • alpha (float) – sparsity parameter for the Christmas tree algorithm

  • beta (float) – sparsity parameter for the Christmas tree algorithm

  • radius (float) – defines the radius within which ned nodes are selected

  • dataframe (np.matrix) – row matrix of data

  • D (np.matrix) – matrix parameter for the hyper inverse wishart prior

  • delta (float) – degrees of freedom for the hyper inverse wishart prior

  • cache (dict) – cache for clique likelihoods

Returns:

Markov chain of the underlying graphs of the junction trees sampled by pgibbs.

Return type:

Trajectory

trilearn.pgibbs.sample_trajectory_loglin(dataframe, n_particles, n_samples, pseudo_obs=1.0, alpha=0.5, beta=0.5, radius=None, reset_cache=True, **args)[source]
trilearn.pgibbs.trajectory_to_file(n_particles, n_samples, alpha, beta, radius, seqdist, node_labels, reset_cache=True, dir='.', output_filename='trajectory.csv', reseed=False)[source]

Writes the trajectory of graphs generated by particle Gibbs to file.

Parameters:
  • n_particles (int) – Number of particles in SMC in each Gibbs iteration

  • n_samples (int) – Number of Gibbs iterations (samples)

  • alpha (float) – sparsity parameter for the Christmas tree algorithm

  • beta (float) – sparsity parameter for the Christmas tree algorithm

  • radius (float) – defines the radius within which ned nodes are selected

  • seq_dist (SequentialJTDistributions) – the distribution to be sampled from

  • filename_prefix (string) – prefix to the filename

Returns:

Markov chain of underlying graphs of the junction trees sampled by pgibbs.

Return type:

Trajectory

trilearn.pgibbs.trajectory_to_queue(n_particles, n_samples, alpha, beta, radius, seqdist, queue, reset_cache=True, reseed=False)[source]

Writes the trajectory of graphs generated by particle Gibbs to file.

Parameters:
  • n_particles (int) – Number of particles in SMC in each Gibbs iteration

  • n_samples (int) – Number of Gibbs iterations (samples)

  • alpha (float) – sparsity parameter for the Christmas tree algorithm

  • beta (float) – sparsity parameter for the Christmas tree algorithm

  • radius (float) – defines the radius within which ned nodes are selected

  • seq_dist (SequentialJTDistributions) – the distribution to be sampled from

  • filename_prefix (string) – prefix to the filename

Returns:

Markov chain of underlying graphs of the junction trees sampled by pgibbs.

Return type:

Trajectory

Stochastic set process

trilearn.set_process.backward_order_neigh_log_prob(from_order, to_order, radius, maxradius)[source]

Probability of generating order from_order from the larger order to_order under the restriction that no hole greater than radius is created.

trilearn.set_process.backward_order_neigh_set(from_order, radius, maxradius)[source]

Returns the list of nodes that can be removed from from_order (the greater order).

trilearn.set_process.backward_perm_traj_sample(p, radius)[source]

Samples a permutation tajectory with maximum p indices.

trilearn.set_process.gen_backward_order_neigh(from_order, radius, maxradius)[source]

Returns: A permutation wit one less element than fromm_order

trilearn.set_process.gen_order_neigh(from_order, radius, total_set)[source]
Returns a list with one more element than from_order

such that the new element is within the radius and belongs to total_set.

Parameters:
  • from_order (list) – list of elements

  • radius (int) – specifies the radius within which the new element can be taken

  • total_set (list) – the full set of elements

Returns:

numpy array

trilearn.set_process.order_neigh_log_prob(from_order, to_order, radius, total_set)[source]

Since the perm trajectory is Markovian, the ratio becomes only the probability.

trilearn.set_process.order_neigh_set(current_order, radius, total_set)[source]

The set of neighbors of current_order with maximal distance radius (of total_set current_order).

SMC

Sequential Monte Carlo sampler for junction tree distributions.

trilearn.smc.approximate(N, alpha, beta, radius, seq_dist, debug=False, neig_set_cache={})[source]

Sequential Monte Carlo for junction trees using the christmas tree algorithm as proposal kernel.

Parameters:
  • N (int) – number

  • alpha (float) – sparsity parameter for the Christmas tree algorithm

  • beta (float) – sparsity parameter for the Christmas tree algorithm

  • radius (float) – defines the radius within which ned nodes are selected

  • seqdist (SequentialJTDistributions) – the distribution to be sampled from

Returns:

(new_trees, log_w)

References:

trilearn.smc.approximate_cond(N, alpha, beta, radius, seq_dist, T_cond, perm_cond, debug=False, neig_set_cache={})[source]

SMC an junction trees conditioned on the trajectories T_cond and perm_cond.

trilearn.smc.est_dec_max_clique_size(order, n_particles, alpha=0.5, beta=0.5, n_smc_estimates=1, debug=False)[source]
trilearn.smc.est_log_norm_consts(order, n_particles, sequential_distribution, alpha=0.5, beta=0.5, n_smc_estimates=1, debug=False)[source]
trilearn.smc.est_n_dec_graphs(order, n_particles, alpha=0.5, beta=0.5, n_smc_estimates=1, debug=False)[source]
trilearn.smc.get_smc_trajs(Is)[source]

This method is made for visualizing the collapsing in SMC.

trilearn.smc.get_traj(n, i, Is)[source]
trilearn.smc.smc_approximate_ggm(N, alpha, beta, radius, X, D, delta)[source]
trilearn.smc.smc_ggm_graphs(N, alpha, beta, radius, X, D, delta)[source]
trilearn.smc.uniform_dec_maxl_clique_size_samples(order, n_particles, alpha=0.5, beta=0.5, debug=False)[source]
trilearn.smc.uniform_dec_samples(order, n_particles, alpha=0.5, beta=0.5, debug=False)[source]