-
Notifications
You must be signed in to change notification settings - Fork 2
/
s4-InverseGaussianBernsteinFunction.R
227 lines (218 loc) · 6.5 KB
/
s4-InverseGaussianBernsteinFunction.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#' Class for Inverse Gaussian Bernstein function
#'
#' @slot eta The distribution parameter (drift of the
#' underlying Gaussian process).
#'
#' @description
#' For the inverse Gaussian Lévy subordinator with \eqn{\eta > 0},
#' the corresponding Bernstein function is the function
#' \deqn{
#' \psi(x) = \sqrt{2x + \eta^2} - \eta, x>0.
#' }
#'
#' @details
#' For the inverse Gaussian Bernstein function, the higher-order alternating
#' iterated forward differences are not known in closed-form, 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_0^\infty e^{-ux} (1-e^{-u})^k \frac{1}{\sqrt{2\pi}
#' u^{3/2}} e^{-\frac{1}{2}\eta^2 u} du, x>0, k>0.
#' }
#'
#' This Bernstein function can be found on p. 309 in \insertCite{Mai2017a}{rmo}.
#' Furthermore it is a transformation of no. 2 in the list of complete
#' Bernstein functions in Chp. 16 of \insertCite{Schilling2012a}{rmo}.
#'
#' The inverse Gaussian Bernstein function has the Lévy density \eqn{\nu}:
#' \deqn{
#' \nu(du)
#' = \frac{1}{\sqrt{2 \pi u^3}} \operatorname{e}^{-\frac{1}{2} \eta^2 u} ,
#' \quad u > 0 ,
#' }
#'
#' and it has the Stieltjes density \eqn{\sigma}:
#' \deqn{
#' \sigma(du)
#' = \frac{
#' \sin(\pi / 2)
#' }{
#' \pi
#' } \cdot \frac{
#' \sqrt{2 x - \eta^2}
#' }{
#' x
#' } ,
#' \quad u > \eta^2 / 2 .
#' }
#'
#' @references
#' \insertAllCited{}
#'
#' @seealso [getLevyDensity()], [getStieltjesDensity()],
#' [calcIterativeDifference()], [calcShockArrivalIntensities()],
#' [calcExShockArrivalIntensities()], [calcExShockSizeArrivalIntensities()],
#' [calcMDCMGeneratorMatrix()], [rextmo()], [rpextmo()]
#'
#' @docType class
#' @name InverseGaussianBernsteinFunction-class
#' @rdname InverseGaussianBernsteinFunction-class
#' @aliases InverseGaussianBernsteinFunction
#' @include s4-BernsteinFunction.R s4-CompleteBernsteinFunction.R
#' @family Bernstein function classes
#' @family Levy Bernstein function classes
#' @family Complete Bernstein function classes
#' @family Algebraic Bernstein function classes
#' @export InverseGaussianBernsteinFunction
#' @examples
#' # Create an object of class InverseGaussianBernsteinFunction
#' InverseGaussianBernsteinFunction()
#' InverseGaussianBernsteinFunction(eta = 0.3)
#'
#' # Create a Lévy density
#' bf <- InverseGaussianBernsteinFunction(eta = 0.7)
#' levy_density <- getLevyDensity(bf)
#' integrate(
#' function(x) pmin(1, x) * levy_density(x),
#' lower = attr(levy_density, "lower"),
#' upper = attr(levy_density, "upper")
#' )
#'
#' # Create a Stieltjes density
#' bf <- InverseGaussianBernsteinFunction(eta = 0.5)
#' stieltjes_density <- getStieltjesDensity(bf)
#' integrate(
#' function(x) 1/(1 + x) * stieltjes_density(x),
#' lower = attr(stieltjes_density, "lower"),
#' upper = attr(stieltjes_density, "upper")
#' )
#'
#' # Evaluate the Bernstein function
#' bf <- InverseGaussianBernsteinFunction(eta = 0.3)
#' calcIterativeDifference(bf, 1:5)
#'
#' # Calculate shock-arrival intensities
#' bf <- InverseGaussianBernsteinFunction(eta = 0.8)
#' calcShockArrivalIntensities(bf, 3)
#' calcShockArrivalIntensities(bf, 3, method = "stieltjes")
#' calcShockArrivalIntensities(bf, 3, tolerance = 1e-4)
#'
#' # Calculate exchangeable shock-arrival intensities
#' bf <- InverseGaussianBernsteinFunction(eta = 0.4)
#' calcExShockArrivalIntensities(bf, 3)
#' calcExShockArrivalIntensities(bf, 3, method = "stieltjes")
#' calcExShockArrivalIntensities(bf, 3, tolerance = 1e-4)
#'
#' # Calculate exchangeable shock-size arrival intensities
#' bf <- InverseGaussianBernsteinFunction(eta = 0.2)
#' calcExShockSizeArrivalIntensities(bf, 3)
#' calcExShockSizeArrivalIntensities(bf, 3, method = "stieltjes")
#' calcExShockSizeArrivalIntensities(bf, 3, tolerance = 1e-4)
#'
#' # Calculate the Markov generator
#' bf <- InverseGaussianBernsteinFunction(eta = 0.6)
#' calcMDCMGeneratorMatrix(bf, 3)
#' calcMDCMGeneratorMatrix(bf, 3, method = "stieltjes")
#' calcMDCMGeneratorMatrix(bf, 3, tolerance = 1e-4)
InverseGaussianBernsteinFunction <- setClass("InverseGaussianBernsteinFunction", # nolint
contains = "CompleteBernsteinFunction",
slots = c(eta = "numeric")
)
#' @rdname hidden_aliases
#'
#' @inheritParams methods::initialize
#' @param eta Non-negative number.
setMethod(
"initialize", "InverseGaussianBernsteinFunction",
function(.Object, eta) { # nolint
if (!missing(eta)) {
.Object@eta <- eta # nolint
validObject(.Object)
}
invisible(.Object)
}
)
#' @include error.R
#' @importFrom checkmate qtest
setValidity(
"InverseGaussianBernsteinFunction",
function(object) {
if (!qtest(object@eta, "N1(0,)")) {
return(error_msg_domain("eta", "N1(0,)"))
}
invisible(TRUE)
}
)
#' @rdname hidden_aliases
#'
#' @inheritParams methods::show
#'
#' @export
setMethod( # nocov start
"show", "InverseGaussianBernsteinFunction",
function(object) {
cat(sprintf("An object of class %s\n", classLabel(class(object))))
if (isTRUE(validObject(object, test = TRUE))) {
cat(sprintf("- eta: %s\n", format(object@eta)))
} else {
cat("\t (invalid or not initialized)\n")
}
invisible(NULL)
}
) # nocov end
#' @rdname hidden_aliases
#'
#' @inheritParams getLevyDensity
#'
#' @include s4-getLevyDensity.R
#' @export
setMethod(
"getLevyDensity", "InverseGaussianBernsteinFunction",
function(object) {
structure(
function(x) {
1 / sqrt(2 * pi * x^3) * exp(-0.5 * object@eta^2 * x)
},
lower = 0, upper = Inf, type = "continuous"
)
}
)
#' @rdname hidden_aliases
#'
#' @inheritParams getStieltjesDensity
#'
#' @include s4-getStieltjesDensity.R
#' @export
setMethod(
"getStieltjesDensity", "InverseGaussianBernsteinFunction",
function(object) {
structure(
function(x) {
sin(pi / 2) / pi * sqrt(2 * x - object@eta^2) / x
},
lower = object@eta^2 / 2, upper = Inf, type = "continuous"
)
}
)
#' @rdname hidden_aliases
#'
#' @inheritParams calcValue
#'
#' @include s4-calcValue.R
#' @importFrom checkmate assert qassert check_numeric check_complex
#' @export
setMethod(
"calcValue", "InverseGaussianBernsteinFunction",
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
sqrt(2 * x + object@eta^2) - object@eta
}
)