/
s4-ParetoBernsteinFunction.R
193 lines (185 loc) · 5.65 KB
/
s4-ParetoBernsteinFunction.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#' Class for Pareto Bernstein functions
#'
#' @slot alpha The index \eqn{\alpha}
#' @slot x0 The cutoff point \eqn{x_0}
#'
#' @description
#' For the Pareto-jump compound Poisson process with index \eqn{0 < \alpha < 1}
#' and cutoff point \eqn{x0}, the corresponding Bernstein function is
#' \deqn{
#' \psi(x)
#' = 1 - e^{-x x_0} + (x_0 x)^\alpha \Gamma(1-\alpha, x_0 x) ,
#' x>0 .
#' }
#'
#' @details
#' For this Bernstein function, the higher-order alternating iterated forward
#' differences are known in closed form but cannot be evaluated numerically
#' without the danger of loss of significance. But we can use numerical
#' integration (here: [stats::integrate()]) to approximate it with the following
#' representation:
#' \deqn{
#' {(-1)}^{k-1} \Delta^k \psi(x)
#' = \int_{x_0}^\infty e^{-ux} (1-e^{-u})^k
#' \alpha \frac{{x_0}^\alpha}{t^{1+\alpha}} du,
#' x>0, k>0 .
#' }
#'
#' The Pareto Bernstein function has the Lévy density \eqn{\nu}:
#' \deqn{
#' \nu(du)
#' = \alpha \frac{x_0^\alpha}{u^{\alpha + 1}}, \quad u > x_0 .
#' }
#'
#' The Pareto Bernstein function, in combination with a linear Bernstein
#' function can be used to approximate the Bernstein function of an
#' \eqn{\alpha}-stable subordinator, see Sec. 5.3 of
#' \insertCite{Fernandez2015a}{rmo}.
#'
#' @references
#' \insertAllCited{}
#'
#' @seealso [getLevyDensity()], [calcIterativeDifference()],
#' [calcShockArrivalIntensities()], [calcExShockArrivalIntensities()],
#' [calcExShockSizeArrivalIntensities()], [calcMDCMGeneratorMatrix()],
#' [rextmo()], [rpextmo()]
#'
#' @docType class
#' @name ParetoBernsteinFunction-class
#' @rdname ParetoBernsteinFunction-class
#' @aliases ParetoBernsteinFunction
#' @include s4-BernsteinFunction.R s4-LevyBernsteinFunction.R
#' @family Bernstein function classes
#' @family Levy Bernstein function classes
#' @family Algebraic Bernstein function classes
#' @export ParetoBernsteinFunction
#' @examples
#' # Create an object of class ParetoBernsteinFunction
#' ParetoBernsteinFunction()
#' ParetoBernsteinFunction(alpha = 0.2, x0 = 1e-2)
#'
#' # Create a Lévy density
#' bf <- ParetoBernsteinFunction(alpha = 0.7, x0 = 1e-2)
#' levy_density <- getLevyDensity(bf)
#' integrate(
#' function(x) pmin(1, x) * levy_density(x),
#' lower = attr(levy_density, "lower"),
#' upper = attr(levy_density, "upper")
#' )
#'
#' # Evaluate the Bernstein function
#' bf <- ParetoBernsteinFunction(alpha = 0.3, x0 = 1)
#' calcIterativeDifference(bf, 1:5)
#'
#' # Calculate shock-arrival intensities
#' bf <- ParetoBernsteinFunction(alpha = 0.8, x0 = 1e-2)
#' calcShockArrivalIntensities(bf, 3)
#' calcShockArrivalIntensities(bf, 3, tolerance = 1e-4)
#'
#' # Calculate exchangeable shock-arrival intensities
#' bf <- ParetoBernsteinFunction(alpha = 0.4, x0 = 1e-2)
#' calcExShockArrivalIntensities(bf, 3)
#' calcExShockArrivalIntensities(bf, 3, tolerance = 1e-4)
#'
#' # Calculate exchangeable shock-size arrival intensities
#' bf <- ParetoBernsteinFunction(alpha = 0.2, x0 = 1e-2)
#' calcExShockSizeArrivalIntensities(bf, 3)
#' calcExShockSizeArrivalIntensities(bf, 3, tolerance = 1e-4)
#'
#' # Calculate the Markov generator
#' bf <- ParetoBernsteinFunction(alpha = 0.6, x0 = 1e-2)
#' calcMDCMGeneratorMatrix(bf, 3)
#' calcMDCMGeneratorMatrix(bf, 3, tolerance = 1e-4)
ParetoBernsteinFunction <- setClass("ParetoBernsteinFunction", # nolint
contains = "LevyBernsteinFunction",
slots = c(alpha = "numeric", x0 = "numeric")
)
#' @rdname hidden_aliases
#'
#' @inheritParams methods::initialize
#' @param alpha Positive number between zero and one (bounds excl.).
#' @param x0 Positive number.
setMethod(
"initialize", "ParetoBernsteinFunction",
function(.Object, alpha, x0) { # nolint
if (!(missing(alpha) || missing(x0))) {
.Object@alpha <- alpha # nolint
.Object@x0 <- x0 # nolint
validObject(.Object)
}
invisible(.Object)
}
)
#' @include error.R
#' @importFrom checkmate qtest
setValidity(
"ParetoBernsteinFunction",
function(object) {
if (!qtest(object@alpha, "N1(0,1)")) {
return(error_msg_domain("alpha", "N1(0,1)"))
}
if (!qtest(object@x0, "N1(0,)")) {
return(error_msg_domain("x0", "N1(0,)"))
}
invisible(TRUE)
}
)
#' @rdname hidden_aliases
#'
#' @inheritParams methods::show
#'
#' @export
setMethod( # nocov start
"show", "ParetoBernsteinFunction",
function(object) {
cat(sprintf("An object of class %s\n", classLabel(class(object))))
if (isTRUE(validObject(object, test = TRUE))) {
cat(sprintf("- alpha: %s\n", format(object@alpha)))
cat(sprintf("- x0: %s\n", format(object@x0)))
} else {
cat("\t (invalid or not initialized)\n")
}
invisible(NULL)
}
) # nocov end
#' @rdname hidden_aliases
#'
#' @inheritParams getLevyDensity
#'
#' @include s4-getLevyDensity.R
#' @export
setMethod(
"getLevyDensity", "ParetoBernsteinFunction",
function(object) {
structure(
function(x) {
object@alpha * (object@x0 / x)^(object@alpha) / x
},
lower = object@x0, upper = Inf, type = "continuous"
)
}
)
#' @rdname hidden_aliases
#'
#' @inheritParams calcValue
#'
#' @include s4-calcValue.R
#' @importFrom checkmate assert qassert check_numeric check_complex
#' @importFrom stats pgamma
#' @export
setMethod(
"calcValue", "ParetoBernsteinFunction",
function(object, x, cscale = 1, ...) {
assert(
combine = "or",
check_numeric(x, min.len = 1L, any.missing = FALSE),
check_complex(x, min.len = 1L, any.missing = FALSE)
)
qassert(Re(x), "N+[0,)")
qassert(cscale, "N1(0,)")
x <- x * cscale
1 - exp(-object@x0 * x) + (object@x0 * x)^(object@alpha) *
pgamma(object@x0 * x, 1 - object@alpha, lower.tail = FALSE) *
gamma(1 - object@alpha)
}
)