/
PH.R
65 lines (53 loc) · 1.74 KB
/
PH.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#' @rdname generator_functions
#' @order 2
#'
#' @title Continuous phase-type distribution
#'
#' @usage PH(subint_mat = NULL, init_probs = NULL)
#'
#' @examples
#'
#' ##===========================##
#' ## For continuous univariate ##
#' ##===========================##
#'
#' subintensity_matrix <- matrix(c(-1.5, 1.5, 0,
#' 0, -1, 1,
#' 0, 0, -0.5),
#' ncol = 3,
#' byrow = TRUE)
#' PH(subintensity_matrix)
#'
#' #---
#'
#' subintensity_matrix <- matrix(c(-1.5, 1.5, 0,
#' 0, -1, 1,
#' 0, 0, -0.5),
#' ncol = 3,
#' byrow = TRUE)
#' initial_probabilities <- c(0.9, 0.1, 0)
#' PH(subintensity_matrix, initial_probabilities)
#'
#' @export
PH <- function(subint_mat = NULL, init_probs = NULL) {
lst_check <- check_phase_type(subint_mat, init_probs)
subint_mat <- lst_check$subint_mat
init_probs <- lst_check$init_probs
#############
# Check the conditions necessary for continuous phase-type distribution
#############
if (length(which(diag(subint_mat) < 0)) == length(init_probs)) {
# check if any rowsums are positive
if (any((rowSums(subint_mat)-.Machine$double.eps^0.25)>1e-14)){
stop('The row sums of the subintensity matrix should be non-positive.')
} else {
value <- list(subint_mat = subint_mat,
init_probs = init_probs,
defect = 1 - sum(init_probs))
attr(value, "class") <- "cont_phase_type"
return(value)
}
} else {
stop('All the diagonal values of the sub-intensity matrix should be negative.')
}
}