File: dist-gh.R

package info (click to toggle)
fbasics 4041.97-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: ansic: 740; makefile: 14
file content (181 lines) | stat: -rw-r--r-- 5,285 bytes parent folder | download
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

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA


################################################################################
# FUNCTION:             DESCRIPTION:
#  dgh                   Returns density for generalized hyperbolic DF
#  pgh                   Returns probability for generalized hyperbolic DF
#  qgh                   Returns quantiles for generalized hyperbolic DF
#  rgh                   Returns random variates for generalized hyperbolic DF
################################################################################


dgh <-
function(x, alpha = 1, beta = 0, delta = 1, mu = 0, lambda = -1/2, log = FALSE)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns density for the generalized hyperbolic distribution

    # FUNCTION:

    # Parameters:
    if (length(alpha) == 4) {
       mu = alpha[4]
       delta = alpha[3]
       beta = alpha[2]
       alpha = alpha[1]
    } 
    
    # Checks:
    if (alpha <= 0) stop("alpha must be greater than zero")
    if (delta <= 0) stop("delta must be greater than zero")
    if (abs(beta) >= alpha) stop("abs value of beta must be less than alpha")

    # Density:
    arg = delta*sqrt(alpha^2-beta^2)
    a = (lambda/2)*log(alpha^2-beta^2) - (
        log(sqrt(2*pi)) + (lambda-0.5)*log(alpha) + lambda*log(delta) +
        log(besselK(arg, lambda, expon.scaled = TRUE)) - arg )
    f = ((lambda-0.5)/2)*log(delta^2+(x - mu)^2)

    # Use exponential scaled form to prevent from overflows:
    arg = alpha * sqrt(delta^2+(x-mu)^2)
    k = log(besselK(arg, lambda-0.5, expon.scaled = TRUE)) - arg
    e = beta*(x-mu)

    # Put all together:
    ans = a + f + k + e
    if(!log) ans = exp(ans)

    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


pgh <-
function(q, alpha = 1, beta = 0, delta = 1, mu = 0, lambda = -1/2)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns probability for the generalized hyperbolic distribution

    # FUNCTION:

    # Checks:
    if (alpha <= 0) stop("alpha must be greater than zero")
    if (delta <= 0) stop("delta must be greater than zero")
    if (abs(beta) >= alpha) stop("abs value of beta must be less than alpha")

    # Probability:
    ans = NULL
    for (Q in q) {
        Integral = integrate(dgh, -Inf, Q, stop.on.error = FALSE,
            alpha = alpha, beta = beta, delta = delta, mu = mu,
            lambda = lambda)
        ans = c(ans, as.numeric(unlist(Integral)[1]))
    }

    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


qgh <-
function(p, alpha = 1, beta = 0, delta = 1, mu = 0, lambda = -1/2)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns quantiles for the generalized hyperbolic distribution

    # FUNCTION:

    # Checks:
    if (alpha <= 0) stop("alpha must be greater than zero")
    if (delta <= 0) stop("delta must be greater than zero")
    if (abs(beta) >= alpha) stop("abs value of beta must be less than alpha")

    # Internal Function:
    .froot <- function(x, alpha, beta, delta, mu, lambda, p)
    {
        pgh(q = x, alpha = alpha, beta = beta, delta = delta,
            mu = mu, lambda = lambda) - p
    }

    # Quantiles:
    result = NULL
    for (pp in p) {
        lower = -1
        upper = +1
        counter = 0
        iteration = NA
        while (is.na(iteration)) {
            iteration = .unirootNA(f = .froot, interval = c(lower,
                upper), alpha = alpha, beta = beta, delta = delta,
                mu = mu, lambda = lambda, p = pp)
            counter = counter + 1
            lower = lower - 2^counter
            upper = upper + 2^counter
        }
        result = c(result, iteration)
    }

    # Return Value:
    result
}


# ------------------------------------------------------------------------------


rgh <-
function(n, alpha = 1, beta = 0, delta = 1, mu = 0, lambda = -1/2)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns random variates for the generalized hyperbolic distribution

    # FUNCTION:

    # Checks:
    if (alpha <= 0) stop("alpha must be greater than zero")
    if (delta <= 0) stop("delta must be greater than zero")
    if (abs(beta) >= alpha) stop("abs value of beta must be less than alpha")

    # Settings:
    theta = c(lambda, alpha, beta, delta, mu)

    # Random Numbers:
    ans = .rghyp(n, theta)

    # Return Value:
    ans
}


################################################################################