-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSDistribution_Weibull.R
239 lines (218 loc) · 7.67 KB
/
SDistribution_Weibull.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
228
229
230
231
232
233
234
235
236
237
238
239
#' @name Weibull
#' @template SDist
#' @templateVar ClassName Weibull
#' @templateVar DistName Weibull
#' @templateVar uses in survival analysis as it satisfies both PH and AFT requirements
#' @templateVar params shape, \eqn{\alpha}, and scale, \eqn{\beta},
#' @templateVar pdfpmf pdf
#' @templateVar pdfpmfeq \deqn{f(x) = (\alpha/\beta)(x/\beta)^{\alpha-1}exp(-x/\beta)^\alpha}
#' @templateVar paramsupport \eqn{\alpha, \beta > 0}
#' @templateVar distsupport the Positive Reals
#' @templateVar default shape = 1, scale = 1
#'
#' @template class_distribution
#' @template field_alias
#' @template method_mode
#' @template method_entropy
#' @template method_kurtosis
#' @template method_pgf
#' @template method_mgfcf
#' @template method_setParameterValue
#' @template param_decorators
#' @template param_shape
#' @template param_scale
#' @template field_packages
#'
#' @family continuous distributions
#' @family univariate distributions
#'
#' @export
Weibull <- R6Class("Weibull",
inherit = SDistribution, lock_objects = F,
public = list(
# Public fields
name = "Weibull",
short_name = "Weibull",
description = "Weibull Probability Distribution.",
alias = "WB, Weib",
packages = "stats",
# Public methods
# initialize
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
#' @param altscale `(numeric(1))`\cr
#' Alternative scale parameter, if given then `scale` is ignored.
#' `altscale = scale^-shape`.
initialize = function(shape = NULL, scale = NULL, altscale = NULL, decorators = NULL) {
super$initialize(
decorators = decorators,
support = PosReals$new(zero = T),
type = PosReals$new(zero = T)
)
},
# stats
#' @description
#' The arithmetic mean of a (discrete) probability distribution X is the expectation
#' \deqn{E_X(X) = \sum p_X(x)*x}
#' with an integration analogue for continuous distributions.
#' @param ... Unused.
mean = function(...) {
unlist(self$getParameterValue("scale")) *
gamma(1 + 1 / unlist(self$getParameterValue("shape")))
},
#' @description
#' The mode of a probability distribution is the point at which the pdf is
#' a local maximum, a distribution can be unimodal (one maximum) or multimodal (several
#' maxima).
mode = function(which = "all") {
scale <- unlist(self$getParameterValue("scale"))
shape <- unlist(self$getParameterValue("shape"))
mode <- numeric(length(scale))
mode[shape > 1] <- scale[shape > 1] *
((shape[shape > 1] - 1) / shape[shape > 1])^(1 / shape[shape > 1]) # nolint
return(mode)
},
#' @description
#' Returns the median of the distribution. If an analytical expression is available
#' returns distribution median, otherwise if symmetric returns `self$mean`, otherwise
#' returns `self$quantile(0.5)`.
median = function() {
unlist(self$getParameterValue("scale")) *
(log(2)^(1 / unlist(self$getParameterValue("shape")))) # nolint
},
#' @description
#' The variance of a distribution is defined by the formula
#' \deqn{var_X = E[X^2] - E[X]^2}
#' where \eqn{E_X} is the expectation of distribution X. If the distribution is multivariate the
#' covariance matrix is returned.
#' @param ... Unused.
variance = function(...) {
scale <- unlist(self$getParameterValue("scale"))
shape <- unlist(self$getParameterValue("shape"))
return(scale^2 * (gamma(1 + 2 / shape) - gamma(1 + 1 / shape)^2))
},
#' @description
#' The skewness of a distribution is defined by the third standardised moment,
#' \deqn{sk_X = E_X[\frac{x - \mu}{\sigma}^3]}{sk_X = E_X[((x - \mu)/\sigma)^3]}
#' where \eqn{E_X} is the expectation of distribution X, \eqn{\mu} is the mean of the
#' distribution and \eqn{\sigma} is the standard deviation of the distribution.
#' @param ... Unused.
skewness = function(...) {
scale <- unlist(self$getParameterValue("scale"))
shape <- unlist(self$getParameterValue("shape"))
mu <- self$mean()
sigma <- self$stdev()
return(((gamma(1 + 3 / shape) * (scale^3)) - (3 * mu * sigma^2) - (mu^3)) / (sigma^3))
},
#' @description
#' The kurtosis of a distribution is defined by the fourth standardised moment,
#' \deqn{k_X = E_X[\frac{x - \mu}{\sigma}^4]}{k_X = E_X[((x - \mu)/\sigma)^4]}
#' where \eqn{E_X} is the expectation of distribution X, \eqn{\mu} is the mean of the
#' distribution and \eqn{\sigma} is the standard deviation of the distribution.
#' Excess Kurtosis is Kurtosis - 3.
#' @param ... Unused.
kurtosis = function(excess = TRUE, ...) {
skew <- self$skewness()
scale <- unlist(self$getParameterValue("scale"))
shape <- unlist(self$getParameterValue("shape"))
mu <- self$mean()
sigma <- self$stdev()
kur <- (((scale^4) * gamma(1 + 4 / shape)) - (4 * skew * (sigma^3) * mu) -
(6 * (sigma^2) * (mu^2)) - (mu^4)) / (sigma^4)
if (excess) {
return(kur - 3)
} else {
return(kur)
}
},
#' @description
#' The entropy of a (discrete) distribution is defined by
#' \deqn{- \sum (f_X)log(f_X)}
#' where \eqn{f_X} is the pdf of distribution X, with an integration analogue for
#' continuous distributions.
#' @param ... Unused.
entropy = function(base = 2, ...) {
scale <- unlist(self$getParameterValue("scale"))
shape <- unlist(self$getParameterValue("shape"))
return(-digamma(1) * (1 - 1 / shape) + log(scale / shape, base) + 1)
},
#' @description The probability generating function is defined by
#' \deqn{pgf_X(z) = E_X[exp(z^x)]}
#' where X is the distribution and \eqn{E_X} is the expectation of the distribution X.
#' @param ... Unused.
pgf = function(z, ...) {
return(NaN)
}
),
private = list(
# dpqr
.pdf = function(x, log = FALSE) {
shape <- self$getParameterValue("shape")
scale <- self$getParameterValue("scale")
call_C_base_pdqr(
fun = "dweibull",
x = x,
args = list(
shape = unlist(shape),
scale = unlist(scale)
),
log = log,
vec = test_list(shape)
)
},
.cdf = function(x, lower.tail = TRUE, log.p = FALSE) {
shape <- self$getParameterValue("shape")
scale <- self$getParameterValue("scale")
call_C_base_pdqr(
fun = "pweibull",
x = x,
args = list(
shape = unlist(shape),
scale = unlist(scale)
),
lower.tail = lower.tail,
log = log.p,
vec = test_list(shape)
)
},
.quantile = function(p, lower.tail = TRUE, log.p = FALSE) {
shape <- self$getParameterValue("shape")
scale <- self$getParameterValue("scale")
call_C_base_pdqr(
fun = "qweibull",
x = p,
args = list(
shape = unlist(shape),
scale = unlist(scale)
),
lower.tail = lower.tail,
log = log.p,
vec = test_list(shape)
)
},
.rand = function(n) {
shape <- self$getParameterValue("shape")
scale <- self$getParameterValue("scale")
call_C_base_pdqr(
fun = "rweibull",
x = n,
args = list(
shape = unlist(shape),
scale = unlist(scale)
),
vec = test_list(shape)
)
},
# traits
.traits = list(valueSupport = "continuous", variateForm = "univariate")
)
)
.distr6$distributions <- rbind(
.distr6$distributions,
data.table::data.table(
ShortName = "Weibull", ClassName = "Weibull",
Type = "\u211D+", ValueSupport = "continuous",
VariateForm = "univariate",
Package = "stats", Tags = "", Alias = "WB, Weib"
)
)