File: CopulaeClass.R

package info (click to toggle)
fcopulae 2110.78-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 952 kB
  • ctags: 57
  • sloc: fortran: 366; ansic: 43; makefile: 13
file content (267 lines) | stat: -rw-r--r-- 7,927 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

# 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

# Copyrights (C)
# for this R-port:
#   1999 - 2007, Diethelm Wuertz, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   info@rmetrics.org
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# FUNCTION:                 COPULA SPECIFICATION:
#  fCOPULA                   S4 class representation
#  show                      S4 print method for copula specification
# FUNCTION:                 FRECHET COPULA:
#  pfrechetCopula            Computes Frechet copula probability
# FUNCTION:                 SPEARMAN'S RHO:
#  .copulaRho                Spearman's rho by integration for "ANY" copula
################################################################################

# moved to zzz.R
# setRmetricsOptions(.counter = NA)


################################################################################
# Specifying and creating copula objects


setClass("fCOPULA",
    # Copula Representation:
    representation(
        call = "call",
        copula = "character",
        param = "list",
        title = "character",
        description = "character")
)


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


setMethod("show", "fCOPULA",
    function(object)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Print and Summary method for fCOPULA

    # Source:
    #   This function copies code from base:print.htest

    # FUNCTION:

    # Unlike print the argument for show is 'object'.
    x = object

    # Title:
    cat("\nTitle:\n ", x@title, "\n", sep = "")

    # Call:
    cat("\nCall:\n ")
    cat(paste(deparse(x@call), sep = "\n", collapse = "\n"), "\n", sep = "")

    # Copula Type:
    cat("\nCopula:\n ", x@copula, "\n", sep = "")

    # Model Parameter:
    if (length(x@param) != 0) {
        cat("\nModel Parameter(s):\n ")
        print(unlist(x@param), quote = FALSE)
    }

    # Description:
    cat("\nDescription:\n ", x@description, sep = "")
    cat("\n\n")

    # Return Value:
    invisible(object)
})


################################################################################
# Frechet Copulae:


pfrechetCopula =
function(u = 0.5, v = u, type = c("m", "pi", "w"),
output = c("vector", "list"))
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes Frechet copula probability

    # Arguments:
    #   u, v - two numeric values or vectors of the same length at
    #       which the copula will be computed. If 'u' is a list then the
    #       the '$x' and '$y' elements will be used as 'u' and 'v'.
    #       If 'u' is a two column matrix then the first column will
    #       be used as 'u' and the the second as 'v'.
    #   type - the type of the Frechet copula. A character
    #       string selected from: "m", "pi", or "w".
    #   output - a character string specifying how the output should
    #       be formatted. By default a vector of the same length as
    #       'u' and 'v'. If specified as "list" then 'u' and 'v' are
    #       expected to span a two-dimensional grid as outputted by the
    #       function 'grid2d' and the function returns a list with
    #       elements '$x', 'y', and 'z' which can be directly used
    #       for example by 2D plotting functions.

    # Examples:
    #   persp(pfrechetCopula(u=grid2d(), output="list", type = "m"))
    #   persp(pfrechetCopula(u=grid2d(), output="list", type = "pi"))
    #   persp(pfrechetCopula(u=grid2d(), output="list", type = "w"))

    # FUNCTION:

    # Match Arguments:
    type = type[1] # Allow for "psp" ... # type = match.arg(type)
    output = match.arg(output)

    # Settings:
    if (is.list(u)) {
        v = u[[2]]
        u = u[[1]]
    }
    if (is.matrix(u)) {
        v = u[, 1]
        u = u[, 2]
    }

    # Compute Copula Probability:
    if (type == "m") {
        # C(u,v) = min(u,v)
        C.uv = apply(cbind(u, v), 1, min)
    }
    if (type == "pi") {
        # C(u, v) = u * v
        C.uv = u * v
    }
    if (type == "w") {
        # C(u,v) = max(u+v-1, 0)
        C.uv = apply(cbind(X = u+v-1, Y = rep(0, length = length(u))), 1, max)
    }
    if (type == "psp") {
        # C(u,v) = u*v/(u+v-u*v)
        C.uv = u*v/(u+v-u*v)
    }

    # Add Control:
    attr(C.uv, "control") <- unlist(list(type = type))

    # As List ?
    if (output == "list") {
        N = sqrt(length(u))
        x = u[1:N]
        y = matrix(v, ncol = N)[1, ]
        C.uv = list(x = x, y = y,  z = matrix(C.uv, ncol = N))
    }

    # Return Value:
    C.uv
}


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


.copulaRho =
function(rho = NULL, alpha = NULL, param = NULL,
family = c("elliptical", "archm", "ev", "archmax"),
type = NULL, error = 1e-3, ...)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Spearman's rho by integration for "ANY" copula

    # Notes:
    #   pellipticalCopula(u, v, rho,   param, type, output, border)
    #   parchmCopula     (u, v, alpha,        type, output, alternative)
    #   pevCopula        (u, v,        param, type, output, alternative)
    #   parchmaxCopula   (u, v,        param, type, output )

    # Examples:
    #   .copulaRho(rho = 0.5, family = "elliptical", type = "norm")
    #   .copulaRho(alpha = 1, family = "archm", type = "1")
    #   .copulaRho(param = 2, family = "ev", type = "galambos")

    # FUNCTION:

    # Match Arguments:
    family = match.arg(family)

    # Type:
    if (is.null(type)) {
        family = "elliptical"
        type = "norm"
    } else {
        type = as.character(type)
    }

    # 2D Function to be integrated:
    rho <<- rho
    alpha <<- alpha
    param <<- param
    type <<- type
    if (family == "elliptical") {
        dCopulaRho <- function(x, y) {
            C = pellipticalCopula(x, y, rho = rho, param = param, type = type)
            12 * (C - x*y )
        }
    } else if (family == "archm") {
        if (is.null(alpha)) alpha <<- archmParam(type)$param
        check = archmCheck(alpha, type)
        dCopulaRho <- function(x, y) {
            C = parchmCopula(x, y, alpha = alpha, type = type)
            12 * (C - x*y )
        }
    } else if (family == "ev") {
        dCopulaRho <- function(x, y) {
            C = pevCopula(x, y, param = param, type = type)
            12 * (C - x*y )
        }
    }
    # else if (family == "archmax") {
    #     dCopulaRho <- function(x, y) {
    #         C = parchmaxCopula(x, y, param = param, type = type)
    #         12 * (C - x*y )
    #     }
    # }

    # Integrate:
    ans = integrate2d(dCopulaRho, error = error)
    Rho = ans$value
    error = ans$error

    # Result:
    control = list(rho = rho, alpha = alpha, param = param,
        family = family, type = type, error = signif(error, 3))
    attr(Rho, "control") <- unlist(control)

    # Return Value:
    Rho
}


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