-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSDistribution_ShiftedLoglogistic.R
192 lines (172 loc) · 6.73 KB
/
SDistribution_ShiftedLoglogistic.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
# nolint start
#' @name ShiftedLoglogistic
#' @template SDist
#' @templateVar ClassName ShiftedLoglogistic
#' @templateVar DistName Shifted Log-Logistic
#' @templateVar uses in survival analysis for its non-monotonic hazard as well as in economics, a generalised variant of [Loglogistic]
#' @templateVar params shape, \eqn{\beta}, scale, \eqn{\alpha}, and location, \eqn{\gamma},
#' @templateVar pdfpmf pdf
#' @templateVar pdfpmfeq \deqn{f(x) = (\beta/\alpha)((x-\gamma)/\alpha)^{\beta-1}(1 + ((x-\gamma)/\alpha)^\beta)^{-2}}
#' @templateVar paramsupport \eqn{\alpha, \beta > 0} and \eqn{\gamma >= 0}
#' @templateVar distsupport the non-negative Reals
#' @templateVar default scale = 1, shape = 1, location = 0
# nolint end
#' @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_ratescale
#' @template param_location
#' @template param_shape
#' @template field_packages
#'
#' @family continuous distributions
#' @family univariate distributions
#'
#' @export
ShiftedLoglogistic <- R6Class("ShiftedLoglogistic",
inherit = SDistribution, lock_objects = F,
public = list(
# Public fields
name = "ShiftedLoglogistic",
short_name = "ShiftLLogis",
description = "Shifted Loglogistic Probability Distribution.",
alias = "SLL",
packages = "pracma",
# Public methods
# initialize
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function(scale = NULL, shape = NULL, location = NULL,
rate = NULL, decorators = NULL) {
super$initialize(
decorators = decorators,
support = Interval$new(-1, Inf, type = "[)"),
type = Reals$new()
)
},
# 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(...) {
location <- unlist(self$getParameterValue("location"))
scale <- unlist(self$getParameterValue("scale"))
shape <- unlist(self$getParameterValue("shape"))
return(location + ((scale / shape) * (((pi * shape) / (sin(pi * shape))) - 1)))
},
#' @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") {
location <- unlist(self$getParameterValue("location"))
scale <- unlist(self$getParameterValue("scale"))
shape <- unlist(self$getParameterValue("shape"))
return(location + ((scale / shape) * ((((1 - shape) / (1 + shape))^shape) - 1)))
},
#' @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("location"))
},
#' @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"))
shapi <- pi * unlist(self$getParameterValue("shape"))
return((scale^2 / shape^2) * ((2 * shapi / sin(2 * shapi)) - ((shapi / sin(shapi))^2)))
},
#' @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)
}
),
active = list(
#' @field properties
#' Returns distribution properties, including skewness type and symmetry.
properties = function() {
prop <- super$properties
shape <- self$getParameterValue("shape")
location <- self$getParameterValue("location")
scale <- self$getParameterValue("scale")
prop$support <- if (shape == 0) {
Reals$new()
} else if (shape < 0) {
prop$support <- Interval$new(-Inf, location - scale / shape,
type = "(]")
} else {
prop$support <- Interval$new(location - scale / shape, Inf,
type = "[)")
}
prop
}
),
private = list(
# dpqr
.pdf = function(x, log = FALSE) {
location <- self$getParameterValue("location")
shape <- self$getParameterValue("shape")
scale <- self$getParameterValue("scale")
if (checkmate::testList(location)) {
return(C_ShiftedLoglogisticPdf(x, unlist(location), unlist(shape), unlist(scale), log))
} else {
return(as.numeric(C_ShiftedLoglogisticPdf(x, location, shape, scale, log)))
}
},
.cdf = function(x, lower.tail = TRUE, log.p = FALSE) {
location <- self$getParameterValue("location")
shape <- self$getParameterValue("shape")
scale <- self$getParameterValue("scale")
if (checkmate::testList(location)) {
return(C_ShiftedLoglogisticCdf(x, unlist(location), unlist(shape), unlist(scale),
lower.tail, log.p))
} else {
return(as.numeric(C_ShiftedLoglogisticCdf(x, location, shape, scale, lower.tail, log.p)))
}
},
.quantile = function(p, lower.tail = TRUE, log.p = FALSE) {
location <- self$getParameterValue("location")
shape <- self$getParameterValue("shape")
scale <- self$getParameterValue("scale")
if (checkmate::testList(location)) {
return(C_ShiftedLoglogisticQuantile(p, unlist(location), unlist(shape), unlist(scale),
lower.tail, log.p))
} else {
return(as.numeric(C_ShiftedLoglogisticQuantile(p, location, shape, scale, lower.tail,
log.p)))
}
},
.rand = function(n) {
self$quantile(runif(n))
},
# traits
.traits = list(valueSupport = "continuous", variateForm = "univariate")
)
)
.distr6$distributions <- rbind(
.distr6$distributions,
data.table::data.table(
ShortName = "ShiftLLogis", ClassName = "ShiftedLoglogistic",
Type = "\u211D+", ValueSupport = "continuous",
VariateForm = "univariate",
Package = "-", Tags = "", Alias = "SLL"
)
)