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
|
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# A copy of the GNU General Public License is available at
# ../../COPYING
################################################################################
# FUNCTION: COLUMN CUMULATIVE SUMS:
# colCumsums Computes sample cumulated sums by column
# colCumsums,matrix S3 default method (for matrix objects)
# colCumsums,timeSeries S3 method for timeSeries objects
# FUNCTION: COLUMN CUMULATIVE MAXIMA:
# colCummaxs Computes cumulated maximum values
# colCummaxs,matrix S3 default method (for matrix objects)
# colCummaxs,timeSeries S3 method for timeSeries objects
# FUNCTION: COLUMN CUMULATIVE MAXIMA:
# colCummins Computes cumulated maximum values
# colCummins,matrix S3 default method (for matrix objects)
# colCummins,timeSeries S3 method for timeSeries objects
# FUNCTION: COLUMN CUMULATIVE MINIMA:
# colCumprods Computes cumulated product values
# colCumprods,matrix S3 default method (for matrix objects)
# colCumprods,timeSeries S3 method for timeSeries objects
# FUNCTION: COLUMN CUMULATIVE RETURNS:
# colCumreturns Computes cumulated product values
# colCumreturns,matrix S3 default method (for matrix objects)
# colCumreturns,timeSeries S3 method for timeSeries objects
################################################################################
# ------------------------------------------------------------------------------
setMethod("colCumsums", "matrix", function(x, na.rm = FALSE, ...)
{
if (na.rm)
x <- na.omit(x)
ans <- apply(x, 2, cumsum, ...)
# special treatment when x has one row because apply returns a vector
if (NROW(x) == 1)
ans <- matrix(ans, nrow = 1, dimnames = dimnames(x))
ans
})
# ------------------------------------------------------------------------------
setMethod("colCumsums", "timeSeries", function(x, na.rm = FALSE, ...)
setDataPart(x, callGeneric(getDataPart(x), na.rm = na.rm, ...)))
# ------------------------------------------------------------------------------
setMethod("colCummaxs", "matrix", function(x, na.rm = FALSE, ...)
{
if (na.rm)
x <- na.omit(x)
ans <- apply(x, 2, cummax, ...)
# special treatment when x has one row because apply returns a vector
if (NROW(x) == 1)
ans <- matrix(ans, nrow = 1, dimnames = dimnames(x))
ans
})
# ------------------------------------------------------------------------------
setMethod("colCummaxs", "timeSeries", function(x, na.rm = FALSE, ...)
setDataPart(x, callGeneric(getDataPart(x), na.rm = na.rm, ...)))
# ------------------------------------------------------------------------------
setMethod("colCummins", "matrix", function(x, na.rm = FALSE, ...)
{
if (na.rm)
x <- na.omit(x)
ans <- apply(x, 2, cummin, ...)
# special treatment when x has one row because apply returns a vector
if (NROW(x) == 1)
ans <- matrix(ans, nrow = 1, dimnames = dimnames(x))
ans
})
# ------------------------------------------------------------------------------
setMethod("colCummins", "timeSeries", function(x, na.rm = FALSE, ...)
setDataPart(x, callGeneric(getDataPart(x), na.rm = na.rm, ...)))
# ------------------------------------------------------------------------------
setMethod("colCumprods", "matrix", function(x, na.rm = FALSE, ...)
{
if (na.rm)
x <- na.omit(x)
ans <- apply(x, 2, cumprod, ...)
# special treatment when x has one row because apply returns a vector
if (NROW(x) == 1)
ans <- matrix(ans, nrow = 1, dimnames = dimnames(x))
ans
})
# ------------------------------------------------------------------------------
setMethod("colCumprods", "timeSeries", function(x, na.rm = FALSE, ...)
setDataPart(x, callGeneric(getDataPart(x), na.rm = na.rm, ...)))
# ------------------------------------------------------------------------------
setMethod("colCumreturns", "matrix",
function(x, method = c("geometric", "simple"), na.rm = FALSE, ...)
{
# A function implemented by Diethelm Wuertz and Yohan Chalabi
# Description:
# Cumulates Returns from a stream of returns
# Arguments:
# x : a matrix object
# method : generate geometric or simple returns,
# default "geometric".
# FUNCTION:
# Handle Missing Values:
if (na.rm) x <- na.omit(x, ...)
method <- match.arg(method)
# Return Value
switch(method,
"geometric" = colCumsums(x),
"simple" = colCumprods(1+x) - 1)
})
# ------------------------------------------------------------------------------
setMethod("colCumreturns", "timeSeries",
function(x, method = c("geometric", "simple"), na.rm = FALSE, ...)
{
# A function implemented by Diethelm Wuertz and Yohan Chalabi
# Description:
# Cumulates Returns from a stream of returns
# Arguments:
# x : a timeSeries object
# method : generate geometric or simple returns,
# default "geometric".
# FUNCTION:
# Handle Missing Values:
if (na.rm) x <- na.omit(x, ...)
method <- match.arg(method)
# Return Value
switch(method,
"geometric" = colCumsums(x),
"simple" = colCumprods(1+x) - 1)
})
################################################################################
|