-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSDistribution_StudentTNoncentral.R
165 lines (152 loc) · 4.85 KB
/
SDistribution_StudentTNoncentral.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
# nolint start
#' @name StudentTNoncentral
#' @author Jordan Deenichin
#' @template SDist
#' @templateVar ClassName StudentTNoncentral
#' @templateVar DistName Noncentral Student's T
#' @templateVar uses to estimate the mean of populations with unknown variance from a small sample size, as well as in t-testing for difference of means and regression analysis
#' @templateVar params degrees of freedom, \eqn{\nu} and location, \eqn{\lambda},
#' @templateVar pdfpmf pdf
#' @templateVar pdfpmfeq \deqn{f(x) = (\nu^{\nu/2}exp(-(\nu\lambda^2)/(2(x^2+\nu)))/(\sqrt{\pi} \Gamma(\nu/2) 2^{(\nu-1)/2} (x^2+\nu)^{(\nu+1)/2}))\int_{0}^{\infty} y^\nu exp(-1/2(y-x\lambda/\sqrt{x^2+\nu})^2)}
#' @templateVar paramsupport \eqn{\nu > 0}, \eqn{\lambda \epsilon R}
#' @templateVar distsupport the Reals
#' @templateVar default df = 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 param_decorators
#' @template param_df
#' @template param_location
#' @template field_packages
#'
#' @family continuous distributions
#' @family univariate distributions
#'
#' @export
StudentTNoncentral <- R6Class("StudentTNoncentral",
inherit = SDistribution, lock_objects = F,
public = list(
# Public fields
name = "StudentTNoncentral",
short_name = "TNS",
description = "Non-central Student's T Probability Distribution.",
alias = "STNC",
packages = "stats",
# Public methods
# initialize
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function(df = NULL, location = NULL, decorators = NULL) {
super$initialize(
decorators = decorators,
support = Reals$new(),
symmetry = "sym",
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(...) {
df <- unlist(self$getParameterValue("df"))
location <- unlist(self$getParameterValue("location"))
mean <- rep(NaN, length(location))
mean[df > 1] <- location[df > 1] * sqrt(df[df > 1] / 2) *
gamma((df[df > 1] - 1) / 2) / gamma(df[df > 1] / 2)
return(mean)
},
#' @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(...) {
df <- unlist(self$getParameterValue("df"))
mu <- unlist(self$getParameterValue("location"))
var <- rep(NaN, length(mu))
var[df > 2] <- df[df > 2] * (1 + mu[df > 2]^2) / (df[df > 2] - 2) -
(mu[df > 2]^2 * df[df > 2] / 2) * (gamma((df[df > 2] - 1) / 2) / gamma(df[df > 2] / 2))^2
return(var)
}
),
private = list(
# dpqr
.pdf = function(x, log = FALSE) {
df <- self$getParameterValue("df")
ncp <- self$getParameterValue("location")
call_C_base_pdqr(
fun = "dt",
x = x,
args = list(
df = unlist(df),
ncp = unlist(ncp)
),
log = log,
vec = test_list(df)
)
},
.cdf = function(x, lower.tail = TRUE, log.p = FALSE) {
df <- self$getParameterValue("df")
ncp <- self$getParameterValue("location")
call_C_base_pdqr(
fun = "pt",
x = x,
args = list(
df = unlist(df),
ncp = unlist(ncp)
),
lower.tail = lower.tail,
log = log.p,
vec = test_list(df)
)
},
.quantile = function(p, lower.tail = TRUE, log.p = FALSE) {
df <- self$getParameterValue("df")
ncp <- self$getParameterValue("location")
call_C_base_pdqr(
fun = "qt",
x = p,
args = list(
df = unlist(df),
ncp = unlist(ncp)
),
lower.tail = lower.tail,
log = log.p,
vec = test_list(df)
)
},
.rand = function(n) {
df <- self$getParameterValue("df")
ncp <- self$getParameterValue("location")
call_C_base_pdqr(
fun = "rt",
x = n,
args = list(
df = unlist(df),
ncp = unlist(ncp)
),
vec = test_list(df)
)
},
# traits
.traits = list(valueSupport = "continuous", variateForm = "univariate")
)
)
.distr6$distributions <- rbind(
.distr6$distributions,
data.table::data.table(
ShortName = "TNC", ClassName = "StudentTNoncentral",
Type = "\u211D", ValueSupport = "continuous",
VariateForm = "univariate",
Package = "stats", Tags = "", Alias = "STNC"
)
)