Check that a given weight vector and transition matrix satisfy several conditions to determine if they form a valid graph. It verifies that the matrix is square, the dimensions match the length of the weight vector, the weight vector elements and matrix elements lie within the interval [0, 1], and that the weight vector sums to 1. Additionally, it checks the matrix is properly normalised, with all diagonal elements equal to 0 and each row summing to 1.
Usage
is_graph_valid(
hyp_weight,
trans_matrix,
sum_to_one_constraint = TRUE,
tolerance = sqrt(.Machine$double.eps)
)Arguments
- hyp_weight
A numeric vector representing the weights of the nodes (hypotheses) in the graph. All elements must be in the interval [0, 1] and the vector must sum to 1.
- trans_matrix
A numeric matrix representing the transition matrix between nodes. All elements must be in the interval [0, 1], with diagonal elements equal to 0, and each row must sum to 1.
- sum_to_one_constraint
A logical indicating whether to allow graphs where both the transition matrix rows are not constrained to sum-to-one, for example in a fixed sequence. Defaults to
TRUE.- tolerance
numeric >= 0. The tolerance when evaluating the sum-to-one constraints of the hypothesis weights and transition matrix rows. The default value is close to
1.5e-8- i.e.sqrt(.Machine$double.eps)(the standard R definition of "practically equal", as used bybase::all.equal()).
Value
A logical value: TRUE if the graph is valid, otherwise FALSE.
The function issues warnings if any of the validity checks fail.
Details
The function performs the following checks to determine graph validity:
trans_matrixis a square matrix.the length of
hyp_weightmatches the number of rows and columns intrans_matrix.all diagonal elements of
trans_matrixare 0.all elements of
hyp_weightandtrans_matrixlie in the interval [0, 1].the sum of
hyp_weightequals 1.each row of
trans_matrixsums to 1.
If any of these conditions are not satisfied, the function returns FALSE
and issues the corresponding warning message.