Skip to content

Commit

Permalink
version 0.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Staniak authored and cran-robot committed Jun 6, 2018
0 parents commit cff88c3
Show file tree
Hide file tree
Showing 58 changed files with 2,689 additions and 0 deletions.
23 changes: 23 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,23 @@
Package: kendallRandomWalks
Title: Simulate and Visualize Kendall Random Walks and Related
Distributions
Version: 0.9.2
Authors@R: person("Mateusz", "Staniak", email = "mateusz.staniak@math.uni.wroc.pl", role = c("aut", "cre"))
Description: Kendall random walks are a continuous-space Markov chains generated
by the Kendall generalized convolution. This package provides tools
for simulating these random walks and studying distributions
related to them. For more information about Kendall random walks see Jasiulis-Gołdyn (2014) <arXiv:1412.0220>.
Depends: R (>= 3.3)
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Imports: ggplot2, dplyr, tidyr, EnvStats, tibble, nleqslv
RoxygenNote: 6.0.1
Suggests: knitr, rmarkdown, testthat, covr
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2018-06-05 18:33:35 UTC; mtst
Author: Mateusz Staniak [aut, cre]
Maintainer: Mateusz Staniak <mateusz.staniak@math.uni.wroc.pl>
Repository: CRAN
Date/Publication: 2018-06-06 11:17:55 UTC
2 changes: 2 additions & 0 deletions LICENSE
@@ -0,0 +1,2 @@
YEAR: 2018
COPYRIGHT HOLDER: Mateusz Staniak
57 changes: 57 additions & 0 deletions MD5
@@ -0,0 +1,57 @@
802161c45338e0f65405afc5c1e31b88 *DESCRIPTION
127cbfaba59733e6a2c6a1cb068efef0 *LICENSE
bf40ddb040e1475489436cc818624dbd *NAMESPACE
d2b472e46a573de5a3285e5a9ddcc53c *NEWS.md
321cb795d29c29dd544278a672d66d3a *R/barrier_crossing.R
44742a93c96bea8c1e66ebddf3ee3b11 *R/exact_ladder_moments.R
0c0ea4b64e63a12cf1c77d1e192a3586 *R/kendallRandomPackage.R
342bab9561ad2b935f5ef3d3363b1951 *R/simulations_kendall_rw.R
0cdf1ddbbe6db456b3e2761647dbc7cb *R/stable_kendall_distribution.R
ecc63136d6a466f61e1ed491dad20450 *R/stable_kendall_fit.R
b6b60ec945f8a1a226a2bcb0d375a493 *README.md
b84cc913d6ce0d3aaec243382c8dfe9d *build/vignette.rds
a48fdecfe2262ed77d8b61e89c783509 *inst/doc/behaviour.R
7161eac5f4c752b0251bead321523c98 *inst/doc/behaviour.Rmd
95472fdbb4e06f604fc52498a3335f38 *inst/doc/behaviour.html
57b6d03ded5319bbee71626929038290 *inst/doc/kendall_rws.R
a2ba97d109c7e774099396657baf7dee *inst/doc/kendall_rws.Rmd
11a135a403e922a11064872a6a20344e *inst/doc/kendall_rws.html
90cfad0d498cc81140a99dcad617506f *man/Qn.Rd
bac14a58926029a0fea03be2cf7e4b67 *man/U.Rd
dce1d5e75d456212d0000537beb067d8 *man/Z.Rd
1bcbad8ce48ea83faf4981ed33c5f6a9 *man/dkend.Rd
7296860b96d294cfa90a5ddb143e2911 *man/estimate_stable_alpha.Rd
43a4e3c0349028a7207ff90add1f1ed4 *man/fit_kendall.Rd
daf8f31ad99cf97685028d8a8e0dec70 *man/fit_separate.Rd
1e078f529dd4b789c0c4e20b0652a1c8 *man/fit_stable_alpha.Rd
dc478a9804d04e57d5bcbc1b479ce038 *man/full_loglik_gradient.Rd
e966dbd9c7ff22c98413cc5fe52e73f0 *man/full_minus_loglik.Rd
846d3b0a92c23421e9ed338b9f2aa1a3 *man/g_function.Rd
f0682dab3c6e09294d0cd304504fe201 *man/g_function_single.Rd
ee19dae30b9c959f00384f52fd5d4fc5 *man/kendallRandomWalks.Rd
e75c117dd539f486a67685b7f4d8d4d9 *man/kendall_loglik.Rd
741d52e764454a56e6256747236e1220 *man/ladder_height.Rd
00a5d7bd2e162f4dfcde322d8c790fdc *man/ladder_moment.Rd
2a28176cce226256b3ebfb0d38285036 *man/ladder_moment_pmf.Rd
3387928db5e95dac979483e734955cde *man/mutate_kendall_rw.Rd
7266e4a1ff17b60c0ba629a1fe93a026 *man/pkend.Rd
472d9b2fdd3a2ae9e110c8b24924c59f *man/pkendSym.Rd
654edc804cc98fa738f73cd7c25daf28 *man/plot.kendall_barrier_crossing.Rd
f561f98fdea9e12c299cbc836b32d2f1 *man/plot.kendall_fit.Rd
490a4121b7c9eb59a3cf4a46e536288d *man/plot.kendall_simulation.Rd
a89f67567cf8a3c7e8066700c8fb2e5a *man/print.kendall_barrier_crossing.Rd
8f0bcbc88a8135114238114a78888895 *man/print.kendall_simulation.Rd
4f1a7c340be93276c09ad4f942643629 *man/qkend.Rd
1ce650196a895a90eaf4bf0dfe2ca00b *man/qkendSym.Rd
ed3c5ae983d3eb2dfc2683d8757ae478 *man/rkend.Rd
56d91d1a29b9af683a57f03ed9a08fe1 *man/simulateOneTrajectory.Rd
3154f95637a38863cb5786ae6da69b3d *man/simulate_kendall_rw.Rd
d8c7c8c7a35a36d36178bae6c9f7aa90 *man/summarise_kendall_rw.Rd
867c1a92807ac63f896b3832bc347a55 *man/transform_kendall_rw.Rd
1ccf23728997c0389f54ddc50a4ed5da *tests/testthat.R
467ab774ad60f93ebb7667bd63db79fc *tests/testthat/test_barriers.R
a48ae04e756695fa1c0c30d42c222541 *tests/testthat/test_dists.R
4a31b49bed12cd9b0b7ef16aabab43e6 *tests/testthat/test_fit.R
991a9cfa7116d3b333f6289e2e971dee *tests/testthat/test_simulations.R
7161eac5f4c752b0251bead321523c98 *vignettes/behaviour.Rmd
a2ba97d109c7e774099396657baf7dee *vignettes/kendall_rws.Rmd
35 changes: 35 additions & 0 deletions NAMESPACE
@@ -0,0 +1,35 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,kendall_barrier_crossing)
S3method(plot,kendall_fit)
S3method(plot,kendall_simulation)
S3method(print,kendall_barrier_crossing)
S3method(print,kendall_simulation)
export(dkend)
export(estimate_stable_alpha)
export(fit_kendall)
export(fit_separate)
export(fit_stable_alpha)
export(full_loglik_gradient)
export(full_minus_loglik)
export(g_function)
export(g_function_single)
export(ladder_height)
export(ladder_moment)
export(ladder_moment_pmf)
export(mutate_kendall_rw)
export(pkend)
export(pkendSym)
export(qkend)
export(qkendSym)
export(rkend)
export(simulate_kendall_rw)
export(summarise_kendall_rw)
export(transform_kendall_rw)
importFrom(dplyr,n)
importFrom(stats,integrate)
importFrom(stats,optim)
importFrom(stats,optimize)
importFrom(stats,quantile)
importFrom(stats,rgamma)
importFrom(stats,sd)
21 changes: 21 additions & 0 deletions NEWS.md
@@ -0,0 +1,21 @@
# kendallRandomPackage 0.9.2

* Functions for transformations and summaries of simulations of Kendall random walks
(`summarise_kendall_rw` and `mutate_kendall_rw`).

# kendallRandomPackage 0.9.1

* Numerical computation of G function (so also Williamson transform)
* Exact distribution of first ladder moments

# kendallRandomPackage 0.9.0

* Generic function for printing and plotting simulation objects
* Barrier crossing simulations and visualizations

# kendallRandomPackage 0.0.0.9000

* Added a `NEWS.md` file to track changes to the package.



125 changes: 125 additions & 0 deletions R/barrier_crossing.R
@@ -0,0 +1,125 @@
#' Estimate the distribution of first ladder moment for given level
#'
#' NA is returned if the level wasn't crossed. Printing the resulting object
#' will give summary of the estimated distribution and information whether
#' level wasn't crossed in some simulations. This information can be used to
#' pick the right trajectory length for the given level.
#'
#' @param simulations kendall_simulation object
#' @param level Positive numeric
#'
#' @return tibble
#'
#' @importFrom dplyr n
#'
#' @export
#'
#' @examples {
#' kendall_rw <- simulate_kendall_rw(100, 100, runif, 0.5)
#' estim_ladder <- ladder_moment(kendall_rw, 1000)
#' estim_ladder
#' }
#'

ladder_moment <- function(simulations, level) {
if(level < 0) stop("Level must be positive")
sim <- id <- sim_id <- NULL
kendall_rw <- simulations$simulation
kendall_rw <- dplyr::group_by(kendall_rw, sim_id)
kendall_rw <- dplyr::mutate(kendall_rw, id = 1:n())
all_sims <- dplyr::ungroup(dplyr::distinct(kendall_rw, sim_id))
kendall_rw <- dplyr::filter(kendall_rw, sim > level)
kendall_rw <- dplyr::summarise(kendall_rw, ladder_moment = min(id))
kendall_rw <- dplyr::ungroup(kendall_rw)
all_sims <- dplyr::left_join(all_sims, kendall_rw, by = "sim_id")

class(all_sims) <- c("kendall_barrier_crossing", class(all_sims))
all_sims
}


#' Estimate the distribution of first ladder height for given level
#'
#' NA is returned if the level wasn't crossed. Printing the resulting object
#' will give summary of the estimated distribution and information whether
#' level wasn't crossed in some simulations. This information can be used to
#' pick the right trajectory length for the given level.
#'
#' @param simulations kendall_simulation object
#' @param level Positive numeric
#'
#' @return tibble
#'
#' @importFrom dplyr n
#'
#' @export
#'
#' @examples {
#' kendall_rw <- simulate_kendall_rw(100, 100, runif, 0.5)
#' estim_ladder <- ladder_height(kendall_rw, 1000)
#' estim_ladder
#' }
#'

ladder_height <- function(simulations, level) {
if(level < 0) stop("Level must be positive")
sim <- id <- sim_id <- NULL
kendall_rw <- simulations$simulation
kendall_rw <- dplyr::group_by(kendall_rw, sim_id)
kendall_rw <- dplyr::mutate(kendall_rw, id = 1:n())
all_sims <- dplyr::ungroup(dplyr::distinct(kendall_rw, sim_id))
kendall_rw <- dplyr::filter(kendall_rw, sim > level)
kendall_rw <- dplyr::summarise(kendall_rw, ladder_moment = min(sim))
kendall_rw <- dplyr::ungroup(kendall_rw)
all_sims <- dplyr::left_join(all_sims, kendall_rw, by = "sim_id")

class(all_sims) <- c("kendall_barrier_crossing", class(all_sims))
all_sims
}

#' Generic function for printing result of ladder_moment function
#'
#' @param x kendall_barrier_crossing object
#' @param ... Additional arguments
#'
#' @return invisible x
#'
#' @importFrom stats quantile sd
#'
#' @export
#'

print.kendall_barrier_crossing <- function(x, ...) {
quantiles <- quantile(x$ladder_moment, na.rm = T,
probs = seq(0, 1, by = 0.1))
labels <- names(quantiles)
cat("Mean of the distribution: ", mean(x$ladder_moment, na.rm = T), "\n")
cat("Standard deviation of the distribution: ", sd(x$ladder_moment, na.rm = T), "\n")
cat("Number of observations: ", max(x$sim_id), "\n")
cat("Times the level was not crossed: ", sum(!is.finite(x$ladder_moment)), "\n")
cat("Quantiles of the distribution: \n")
print(quantiles)
invisible(x)
}


#' Generic function for plotting results of ladder_moment function.
#'
#' @param x kendall_barrier_crossing object
#' @param ... Additional arguments
#'
#' @return ggplot2
#'
#' @export
#'

plot.kendall_barrier_crossing <- function(x, ...) {
mean_value <- mean(x$ladder_moment, na.rm = TRUE)
ggplot2::ggplot(x, ggplot2::aes_string(x = 'ladder_moment')) +
ggplot2::geom_histogram() +
ggplot2::geom_vline(xintercept = mean_value) +
ggplot2::theme_bw() +
ggplot2::xlab("First ladder moments") +
ggplot2::ylab("Count")
# dodac tu ecdf
}
86 changes: 86 additions & 0 deletions R/exact_ladder_moments.R
@@ -0,0 +1,86 @@
#' Function G(t) - Williamson transform taken at point 1/t.
#'
#' This function return the whole "integrate" object, so precision of the approximation
#' can be checked.
#'
#' @param t Argument to the function.
#' @param alpha Value of the alpha parameter.
#' @param density Density function of the step distribution.
#'
#' @return Object of class "integrate"
#'
#' @importFrom stats integrate
#'
#' @examples
#' g_function_single(5, 0.26, dnorm)
#'
#' @export
#'

g_function_single <- function(t, alpha, density) {
force(t)
under_integral <- function(x) {
(1 - abs(x/t)^(alpha))*density(x)
}
if(t != 0) {
integrate(under_integral, lower = -abs(t), upper = abs(t))
} else {
fun <- function(x) x
integrate(fun, lower = -1, upper = 1)
}
}


#' Function G(t) - Williamson transform taken at point 1/t.
#'
#' This function return only approximated values. To check their precisions use
#' g_function_single function with an argument of length 1.
#'
#' @param t Argument to the function.
#' @param alpha Value of the alpha parameter.
#' @param density Density function of the step distribution.
#'
#' @return Object of class "integrate"
#'
#' @importFrom stats integrate
#'
#' @export
#'
#' @examples
#' g_function(1:5, 0.75, dnorm)
#'
#'

g_function <- function(t, alpha, density) {
sapply(t, function(x) g_function_single(x, alpha, density)$value)
}


#' Distribution of the first ladder moment.
#'
#' @param n Argument to the PDF.
#' @param level Level a to be crossed.
#' @param alpha Alpha parameter of Kendall random walk.
#' @param step_cdf CDF of the step distribution.
#' @param step_pdf PDF of the step distribution.
#'
#' @return Value of PMF of the distribution of first ladder moment
#'
#' @export
#'
#' @examples
#' prob <- ladder_moment_pmf(10, 1000, 0.5, pnorm, dnorm)
#' prob
#'
#'

ladder_moment_pmf <- function(n, level, alpha, step_cdf, step_pdf) {
Ga <- g_function(level, alpha, step_pdf)
Fa <- step_cdf(level)
Ha <- 2*Fa - 1 - Ga
A <- 1 + Ha/((2*Ga - 1)^2) - Ga/(2*Ga - 1)
B <- Ha/((1 - Ga)*(2*Ga - 1))
C <- Ga/(2*Ga - 1) - (Ha*Ga)/(((2*Ga - 1)^2)*(1 - Ga))

A*(2^(-n)) + B*n*((1 - Ga)^2)*(Ga^(n - 1)) + C*(1 - Ga)*(Ga^(n - 1))
}
29 changes: 29 additions & 0 deletions R/kendallRandomPackage.R
@@ -0,0 +1,29 @@
#' kendallRandomWalks: explore and visualize Kendall random walks.
#'
#' Kendall random walks are Markov processes generated by the Kendall convolution.
#' This package helps simulate and visualize these random walks and associated
#' distributions. It also provides function to fit these distributions to data.
#'
#' @section Important functions:
#' \code{\link{simulate_kendall_rw}} simulates Kendall random walks.
#'
#' \code{\link{transform_kendall_rw}} applies scaling and shift to simulated Kendall r.w-s.
#'
#' \code{\link{ladder_moment}} estimates the distribution of first ladder moment
#' by simulating Kendall random walks.
#'
#' \code{\link{ladder_height}} estimates the distribution of first ladder height
#' by simulating Kendall random walks.
#'
#' \code{\link{ladder_moment_pmf}} computes the PMF of the distribution of first
#' ladder moment.
#'
#' \code{\link{g_function}} Finds the value of G(t) numerically.
#'
#' \code{\link{pkend}}, \code{\link{dkend}}, \code{\link{qkend}}, \code{\link{rkend}}
#' give CDF, PDF, quantile function and random numbers from stable Kendall distribution.
#'
#'
#' @docType package
#' @name kendallRandomWalks
NULL

0 comments on commit cff88c3

Please sign in to comment.