SCM

SCM Repository

[rmetrics] View of /pkg/timeSeries/R/fin-cumulated.R
ViewVC logotype

View of /pkg/timeSeries/R/fin-cumulated.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4158 - (download) (annotate)
Sun May 17 14:09:03 2009 UTC (9 years, 9 months ago) by wuertz
File size: 2751 byte(s)
NEW FILE ORDERING CHECKED IN ...
#
#  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:                 DESCRIPTION:
#  cumulated                 Computes cumulated series from financial returns
#  cumulated.default         Computes cumulated series, default method
################################################################################


cumulated <-
function(x, ...)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes cumulated series from financial returns

    # Return Value:
    UseMethod("cumulated")
}


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


cumulated.default <-
function(x, method = c("continuous", "discrete", "compound", "simple"),
    percentage = FALSE, ...)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes cumulated series from financial returns
    #   supports 'matrix' and 'timeSeries'.

    # Arguments:
    #   x - data object containing ordered price observations
    #   method - "continuous == "compound" and "discrete" == "simple"

    # Example:
    #   X = as.timeSeries(data(msft.dat))[1:10, "Close"]; X = X/series(X)[1, 1]
    #   x = returns(X, "continuous"); x;  X; cumulated(x, "continuous")
    #   x = returns(X, "discrete"); x;  X; cumulated(x, "discrete")

    # Note:
    #   To make it conform with PortfolioAnalytics:
    #   "compound" == "continuous", and "simple" == "discrete"

    # FUNCTION:

    # Settings:
    method = match.arg(method)

    # Handle Missing Values:
    # if (na.rm) x = na.omit(x, ...)

    # Transform data:
    if (percentage) x = x/100
    positions = time(x)

    # Calculate Returns:
    # ... colCumsums and colCumprods are generic functions with
    #     methods for 'matrix' and 'timeSeries'.
    if(method == "geometric") {
        ans = colCumsums(x)
    }
    if(method == "compound" || method == "continuous") {
        ans = exp(colCumsums(x))
    }
    if(method == "simple" || method == "discrete") {
        ans = colCumprods(1+x)
    }

    # Return Value:
    ans
}


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


R-Forge@R-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge