[Returnanalytics-commits] r2705 - in pkg/FactorAnalytics: R data man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Aug 2 23:34:21 CEST 2013


Author: chenyian
Date: 2013-08-02 23:34:21 +0200 (Fri, 02 Aug 2013)
New Revision: 2705

Added:
   pkg/FactorAnalytics/R/rCornishFisher.R
   pkg/FactorAnalytics/data/CommonFactors.RData
   pkg/FactorAnalytics/man/CommonFactors.Rd
   pkg/FactorAnalytics/man/rCornishFisher.Rd
Log:
1. add CommonFactors.RData
2. add rCornishFisher.R which generates random variables from Cornish-Fisher distribution. 

Added: pkg/FactorAnalytics/R/rCornishFisher.R
===================================================================
--- pkg/FactorAnalytics/R/rCornishFisher.R	                        (rev 0)
+++ pkg/FactorAnalytics/R/rCornishFisher.R	2013-08-02 21:34:21 UTC (rev 2705)
@@ -0,0 +1,85 @@
+#' Functions for Cornish-Fisher density, CDF, random number simulation and
+#' quantile.
+#'
+#'@aliases rCornishFisher 
+#'@aliases dCornishFisher
+#'@aliases qCornishFisher
+#'@aliases pCornishFisher
+#'
+#'
+#'@description 
+#' \itemize{
+#' \item \code{rCornishFisher} simulate observations based on
+#' Cornish-Fisher quantile expansion given mean, standard
+#' deviation, skewness and excess kurtosis.
+#' \item \code{dCornishFisher} Computes Cornish-Fisher density
+#' from two term Edgeworth expansion given mean, standard
+#' deviation, skewness and excess kurtosis.
+#' \item \code{pCornishFisher} Computes Cornish-Fisher CDF from
+#' two term Edgeworth expansion given mean, standard
+#' deviation, skewness and excess kurtosis.
+#' \item \code{qCornishFisher} Computes Cornish-Fisher quantiles
+#' from two term Edgeworth expansion given mean, standard
+#' deviation, skewness and excess kurtosis.
+#'}
+#'
+#'@param n scalar, number of simulated values in rCornishFisher. Sample length in
+#' density,distribution,quantile function.
+#' @param sigma scalar, standard deviation.
+#' @param skew scalar, skewness.
+#' @param ekurt scalar, excess kurtosis.
+#' @param seed set seed here. Default is \code{NULL}.
+#' @param x,q vector of standardized quantiles. See detail.
+#' @param p vector of probabilities.
+#' 
+#' @return n simulated values from Cornish-Fisher distribution.
+#' @author Eric Zivot and Yi-An Chen.
+#' @references 
+#' \itemize{
+#' \item A.DasGupta, "Asymptotic Theory of Statistics and
+#' Probability", Springer Science+Business Media,LLC 2008
+#' \item   Thomas A.Severini, "Likelihood Methods in Statistics",
+#'  Oxford University Press, 2000 
+#'  }
+#'  @export
+#'  
+#'  @details CDF(q) = Pr(sqrt(n)*(x_bar-mu)/sigma < q)
+#'  
+#'  @examples
+#'  # generate 1000 observation from Cornish-Fisher distribution
+#' rc <- rCornishFisher(1000,1,0,5)
+#'hist(rc,breaks=100,freq=FALSE,main="simulation of Cornish Fisher Distribution",
+#'     xlim=c(-10,10))
+#'lines(seq(-10,10,0.1),dnorm(seq(-10,10,0.1),mean=0,sd=1),col=2)
+#' # compare with standard normal curve
+#'
+#' # example from A.dasGupta p.188 exponential example
+#' # x is iid exp(1) distribution, sample size = 5
+#' # then x_bar is Gamma(shape=5,scale=1/5) distribution
+#' q <- c(0,0.4,1,2)
+#' # exact cdf
+#' pgamma(q/sqrt(5)+1,shape=5,scale=1/5)
+#' # use CLT
+#' pnorm(q)
+#' # use edgeworth expansion
+#' pCornishFisher(q,n=5,skew=2,ekurt=6)
+#' 
+
+
+
+rCornishFisher <-
+function(n, sigma, skew, ekurt, seed=NULL) {
+## inputs:
+## n          scalar, number of simulated values
+## sigma      scalar, standard deviation
+## skew       scalar, skewness
+## ekurt      scalar, excess kurtosis
+## outputs:
+## n simulated values from Cornish-Fisher distribution
+if (!is.null(seed)) set.seed(seed)
+zc = rnorm(n)
+z.cf = zc  + (((zc^2 - 1) * skew)/6) + (((zc^3 - 3 * zc) *
+      ekurt)/24) - ((((2 * zc^3) - 5 * zc) * skew^2)/36)
+ans = sigma*z.cf
+ans
+}

Added: pkg/FactorAnalytics/data/CommonFactors.RData
===================================================================
(Binary files differ)


Property changes on: pkg/FactorAnalytics/data/CommonFactors.RData
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/FactorAnalytics/man/CommonFactors.Rd
===================================================================
--- pkg/FactorAnalytics/man/CommonFactors.Rd	                        (rev 0)
+++ pkg/FactorAnalytics/man/CommonFactors.Rd	2013-08-02 21:34:21 UTC (rev 2705)
@@ -0,0 +1,41 @@
+\name{CommonFactors}
+\alias{CommonFactors}
+\alias{factors}
+\alias{factors.Q}
+\docType{data}
+\title{
+Factor set of several commonly used factors
+}
+\description{
+10 monthly and quarterly common factors xts data from 1997-01-31 to 2013-07-31. 
+\itemize{
+\item SP500 is SP500 returns from FRED,
+\item GS10TR US Treasury 10y yields total returns from the yeild of the 10 year constant maturity from FRED,
+\item USD.Index Trade Weighted U.S. Dollar Index: Major Currencies - TWEXMMTH. from FRED
+\item Term.Spread Yield spread of Merrill Lynch High-Yield Corporate Master II Index minus 10-year Treasury. from FRED
+\item TED.Spread 3-Month Treasury Bill: Secondary Market Rate(TB3MS) - 3-Month Eurodollar Deposit Rate (London) (MED)3. from FRED.
+\item DJUBSTR DJUBS Commodities index.
+\item dVIX the first difference of the end-of-month value of the CBOE Volatility Index (VIX).
+\item OILPRICE ""OILPRICE" from FRED.
+\item TB3MS 3-Month Treasury Bill: Secondary Market Rate(TB3MS) from FRED
+}
+
+}
+\usage{data(CommonFactors)}
+\format{
+  A data frame with 0 observations on the following 2 variables.
+  \describe{
+    \item{\code{x}}{a numeric vector}
+    \item{\code{y}}{a numeric vector}
+  }
+}
+\source{
+\itemize{
+\item FRED
+\item http://www.djindexes.com/mdsidx/downloads/xlspages/ubsci_public/DJUBS_full_hist.xls
+\item http://www.cboe.com/publish/ScheduledTask/MktData/datahouse/vixarchive.xls
+}
+}
+
+
+

Added: pkg/FactorAnalytics/man/rCornishFisher.Rd
===================================================================
--- pkg/FactorAnalytics/man/rCornishFisher.Rd	                        (rev 0)
+++ pkg/FactorAnalytics/man/rCornishFisher.Rd	2013-08-02 21:34:21 UTC (rev 2705)
@@ -0,0 +1,77 @@
+\name{rCornishFisher}
+\alias{dCornishFisher}
+\alias{pCornishFisher}
+\alias{qCornishFisher}
+\alias{rCornishFisher}
+\title{Functions for Cornish-Fisher density, CDF, random number simulation and
+quantile.}
+\usage{
+  rCornishFisher(n, sigma, skew, ekurt, seed = NULL)
+}
+\arguments{
+  \item{n}{scalar, number of simulated values in
+  rCornishFisher. Sample length in
+  density,distribution,quantile function.}
+
+  \item{sigma}{scalar, standard deviation.}
+
+  \item{skew}{scalar, skewness.}
+
+  \item{ekurt}{scalar, excess kurtosis.}
+
+  \item{seed}{set seed here. Default is \code{NULL}.}
+
+  \item{x,q}{vector of standardized quantiles. See detail.}
+
+  \item{p}{vector of probabilities.}
+}
+\value{
+  n simulated values from Cornish-Fisher distribution.
+}
+\description{
+  \itemize{ \item \code{rCornishFisher} simulate
+  observations based on Cornish-Fisher quantile expansion
+  given mean, standard deviation, skewness and excess
+  kurtosis. \item \code{dCornishFisher} Computes
+  Cornish-Fisher density from two term Edgeworth expansion
+  given mean, standard deviation, skewness and excess
+  kurtosis. \item \code{pCornishFisher} Computes
+  Cornish-Fisher CDF from two term Edgeworth expansion
+  given mean, standard deviation, skewness and excess
+  kurtosis. \item \code{qCornishFisher} Computes
+  Cornish-Fisher quantiles from two term Edgeworth
+  expansion given mean, standard deviation, skewness and
+  excess kurtosis. }
+}
+\details{
+  CDF(q) = Pr(sqrt(n)*(x_bar-mu)/sigma < q)
+}
+\examples{
+# generate 1000 observation from Cornish-Fisher distribution
+rc <- rCornishFisher(1000,1,0,5)
+hist(rc,breaks=100,freq=FALSE,main="simulation of Cornish Fisher Distribution",
+    xlim=c(-10,10))
+lines(seq(-10,10,0.1),dnorm(seq(-10,10,0.1),mean=0,sd=1),col=2)
+# compare with standard normal curve
+
+# example from A.dasGupta p.188 exponential example
+# x is iid exp(1) distribution, sample size = 5
+# then x_bar is Gamma(shape=5,scale=1/5) distribution
+q <- c(0,0.4,1,2)
+# exact cdf
+pgamma(q/sqrt(5)+1,shape=5,scale=1/5)
+# use CLT
+pnorm(q)
+# use edgeworth expansion
+pCornishFisher(q,n=5,skew=2,ekurt=6)
+}
+\author{
+  Eric Zivot and Yi-An Chen.
+}
+\references{
+  \itemize{ \item A.DasGupta, "Asymptotic Theory of
+  Statistics and Probability", Springer Science+Business
+  Media,LLC 2008 \item Thomas A.Severini, "Likelihood
+  Methods in Statistics", Oxford University Press, 2000 }
+}
+



More information about the Returnanalytics-commits mailing list