diff --git a/R-proj/DESCRIPTION b/R-proj/DESCRIPTION index a8e178df9..2eac5b50c 100644 --- a/R-proj/DESCRIPTION +++ b/R-proj/DESCRIPTION @@ -2,9 +2,11 @@ Package: volesti Type: Package License: LGPL-3 Title: Volume Approximation and Sampling of Convex Polytopes -Author: Vissarion Fisikopoulos [aut, cph, cre], - Apostolos Chalkis [cph, aut], - contributors in file inst/AUTHORS +Authors@R: c( + person("Vissarion", "Fisikopoulos", , "vissarion.fisikopoulos@gmail.com", role = c("aut", "cre", "cph"), + comment = c(ORCID = "0000-0002-0780-666X")), + person("Apostolos", "Chalkis", , "tolis.chal@gmail.com", role = c("aut", "cph"), + comment = c(ORCID = "0000-0002-4628-1907"))) Copyright: file inst/COPYRIGHTS Description: Provides an R interface for 'volesti' C++ package. 'volesti' computes estimations of volume of polytopes given by (i) a set of points, (ii) linear inequalities or (iii) Minkowski sum of segments @@ -12,13 +14,13 @@ Description: Provides an R interface for 'volesti' C++ package. 'volesti' comput for sampling, rounding and rotating polytopes. Moreover, 'volesti' provides algorithms for estimating copulas useful in computational finance. Methods implemented in 'volesti' are described in A. Chalkis and V. Fisikopoulos (2022) and references therein. -Version: 1.1.2-7 -Date: 2023-10-18 +Version: 1.1.2-8 +Date: 2024-10-7 Maintainer: Vissarion Fisikopoulos Depends: Rcpp (>= 0.12.17) Imports: methods, stats LinkingTo: Rcpp, RcppEigen, BH Suggests: testthat Encoding: UTF-8 -RoxygenNote: 7.1.1 +RoxygenNote: 7.2.1 BugReports: https://github.com/GeomScale/volesti/issues diff --git a/R-proj/R/RcppExports.R b/R-proj/R/RcppExports.R index ba2c1cd6f..f5c0ed0de 100644 --- a/R-proj/R/RcppExports.R +++ b/R-proj/R/RcppExports.R @@ -42,7 +42,7 @@ copula <- function(r1, r2 = NULL, sigma = NULL, m = NULL, n = NULL, seed = NULL) #' The \eqn{d}-dimensional unit simplex is the set of points \eqn{\vec{x}\in \R^d}, s.t.: \eqn{\sum_i x_i\leq 1}, \eqn{x_i\geq 0}. The \eqn{d}-dimensional canonical simplex is the set of points \eqn{\vec{x}\in \R^d}, s.t.: \eqn{\sum_i x_i = 1}, \eqn{x_i\geq 0}. #' #' @param body A list to request exact uniform sampling from special well known convex bodies through the following input parameters: -#' \itemize{ +#' \describe{ #' \item{\code{type} }{ A string that declares the type of the body for the exact sampling: a) \code{'unit_simplex'} for the unit simplex, b) \code{'canonical_simplex'} for the canonical simplex, c) \code{'hypersphere'} for the boundary of a hypersphere centered at the origin, d) \code{'ball'} for the interior of a hypersphere centered at the origin.} #' \item{\code{dimension} }{ An integer that declares the dimension when exact sampling is enabled for a simplex or a hypersphere.} #' \item{\code{radius} }{ The radius of the \eqn{d}-dimensional hypersphere. The default value is \eqn{1}.} @@ -202,7 +202,7 @@ rounding <- function(P, settings = NULL, seed = NULL) { #' @param P A convex polytope. It is an object from class (a) Hpolytope or (b) Vpolytope or (c) Zonotope or (d) VpolytopeIntersection. #' @param n The number of points that the function is going to sample from the convex polytope. #' @param random_walk Optional. A list that declares the random walk and some related parameters as follows: -#' \itemize{ +#' \describe{ #' \item{\code{walk} }{ A string to declare the random walk: i) \code{'CDHR'} for Coordinate Directions Hit-and-Run, ii) \code{'RDHR'} for Random Directions Hit-and-Run, iii) \code{'BaW'} for Ball Walk, iv) \code{'BiW'} for Billiard walk, v) \code{'BCDHR'} boundary sampling by keeping the extreme points of CDHR or vi) \code{'BRDHR'} boundary sampling by keeping the extreme points of RDHR. The default walk is \code{'BiW'} for the uniform distribution or \code{'CDHR'} for the Gaussian distribution.} #' \item{\code{walk_length} }{ The number of the steps per generated point for the random walk. The default value is 1.} #' \item{\code{nburns} }{ The number of points to burn before start sampling.} @@ -212,7 +212,7 @@ rounding <- function(P, settings = NULL, seed = NULL) { #' \item{\code{seed} }{ A fixed seed for the number generator.} #' } #' @param distribution Optional. A list that declares the target density and some related parameters as follows: -#' \itemize{ +#' \describe{ #' \item{\code{density} }{ A string: (a) \code{'uniform'} for the uniform distribution or b) \code{'gaussian'} for the multidimensional spherical distribution. The default target distribution is uniform.} #' \item{\code{variance} }{ The variance of the multidimensional spherical gaussian. The default value is 1.} #' \item{\code{mode} }{ A \eqn{d}-dimensional numerical vector that declares the mode of the Gaussian distribution. The default choice is the center of the as that one computed by the function \code{inner_ball()}.} @@ -245,7 +245,7 @@ sample_points <- function(P, n, random_walk = NULL, distribution = NULL) { #' #' @param P A convex polytope. It is an object from class a) Hpolytope or b) Vpolytope or c) Zonotope or d) VpolytopeIntersection. #' @param settings Optional. A list that declares which algorithm, random walk and values of parameters to use, as follows: -#' \itemize{ +#' \describe{ #' \item{\code{algorithm} }{ A string to set the algorithm to use: a) \code{'CB'} for CB algorithm, b) \code{'SoB'} for SOB algorithm or b) \code{'CG'} for CG algorithm. The defalut algorithm is \code{'CB'}.} #' \item{\code{error} }{ A numeric value to set the upper bound for the approximation error. The default value is \eqn{1} for SOB algorithm and \eqn{0.1} otherwise.} #' \item{\code{random_walk} }{ A string that declares the random walk method: a) \code{'CDHR'} for Coordinate Directions Hit-and-Run, b) \code{'RDHR'} for Random Directions Hit-and-Run, c) \code{'BaW'} for Ball Walk, or \code{'BiW'} for Billiard walk. For CB and SOB algorithms the default walk is \code{'CDHR'} for H-polytopes and \code{'BiW'} for the other representations. For CG algorithm the default walk is \code{'CDHR'} for H-polytopes and \code{'RDHR'} for the other representations.} diff --git a/R-proj/R/compute_indicators.R b/R-proj/R/compute_indicators.R index b16012e1a..ad17934bd 100644 --- a/R-proj/R/compute_indicators.R +++ b/R-proj/R/compute_indicators.R @@ -1,18 +1,18 @@ #' Compute an indicator for each time period that describes the state of a market. #' -#' Given a matrix that contains row-wise the assets' returns and a sliding window \code{win_length}, this function computes an approximation of the joint distribution (copula, e.g. see \url{https://en.wikipedia.org/wiki/Copula_(probability_theory)}) between portfolios' return and volatility in each time period defined by \code{win_len}. -#' For each copula it computes an indicator: If the indicator is large it corresponds to a crisis period and if it is small it corresponds to a normal period. +#' Given a matrix that contains row-wise the assets' returns and a sliding window \code{win_length}, this function computes an approximation of the joint distribution (copula, e.g. see \url{https://en.wikipedia.org/wiki/Copula_(probability_theory)}) between portfolios' return and volatility in each time period defined by \code{win_len}. +#' For each copula it computes an indicator: If the indicator is large it corresponds to a crisis period and if it is small it corresponds to a normal period. #' In particular, the periods over which the indicator is greater than 1 for more than 60 consecutive sliding windows are warnings and for more than 100 are crisis. The sliding window is shifted by one day. #' #' @param returns A \eqn{d}-dimensional vector that describes the direction of the first family of parallel hyperplanes. #' @param parameters A list to set a parameterization. -#' \itemize{ -#' \item{win_length }{ The length of the sliding window. The default value is 60.} -#' \item{m } { The number of slices for the copula. The default value is 100.} -#' \item{n }{ The number of points to sample. The default value is \eqn{5\cdot 10^5}.} -#' \item{nwarning }{ The number of consecutive indicators larger than 1 required to declare a warning period. The default value is 60.} -#' \item{ncrisis }{ The number of consecutive indicators larger than 1 required to declare a crisis period. The default value is 100.} -#' \item{seed }{ A fixed seed for the number generator.} +#' \describe{ +#' \item{win_length}{The length of the sliding window. The default value is 60.} +#' \item{m}{The number of slices for the copula. The default value is 100.} +#' \item{n}{The number of points to sample. The default value is \eqn{5\cdot 10^5}.} +#' \item{nwarning}{The number of consecutive indicators larger than 1 required to declare a warning period. The default value is 60.} +#' \item{ncrisis}{The number of consecutive indicators larger than 1 required to declare a crisis period. The default value is 100.} +#' \item{seed}{A fixed seed for the number generator.} #' } #' #' @references \cite{L. Cales, A. Chalkis, I.Z. Emiris, V. Fisikopoulos, @@ -20,7 +20,7 @@ #' #' @return A list that contains the indicators and the corresponding vector that label each time period with respect to the market state: a) normal, b) crisis, c) warning. #' -#' @examples +#' @examples #' # simple example on random asset returns #' asset_returns = replicate(10, rnorm(14)) #' market_states_and_indicators = compute_indicators(asset_returns, @@ -34,7 +34,7 @@ #' @importFrom "stats" "cov" #' @importFrom "methods" "new" compute_indicators <- function(returns, parameters = list("win_length" = 60, "m" = 100, "n" = 500000, "nwarning" = 60, "ncrisis" = 100)) { - + win_length = 60 if (!is.null(parameters$win_length)) { win_length = parameters$win_length @@ -59,17 +59,17 @@ compute_indicators <- function(returns, parameters = list("win_length" = 60, "m" if (!is.null(parameters$seed)) { seed = parameters$seed } - + nrows = dim(returns)[1] nassets = dim(returns)[2] wl = win_length-1 - + indicators = c() for (i in 1:(nrows-wl)) { - + Win=i:(i+wl) E = cov(returns[Win,]) - + compRet = rep(1,nassets) for (j in 1:nassets) { for (k in Win) { @@ -77,11 +77,11 @@ compute_indicators <- function(returns, parameters = list("win_length" = 60, "m" } compRet[j] = compRet[j] - 1 } - + cop = copula(r1 = compRet, sigma = E, m = m, n = n, seed = seed) blue_mass = 0 red_mass = 0 - + for (row in 1:m) { for (col in 1:m) { if (row-col<=0.2*m && row-col>=-0.2*m) { @@ -105,7 +105,7 @@ compute_indicators <- function(returns, parameters = list("win_length" = 60, "m" col = rep("normal", N) for (i in 1:N) { - + if(indicators[i]>1 && !set_index){ index = i set_index = TRUE @@ -127,7 +127,7 @@ compute_indicators <- function(returns, parameters = list("win_length" = 60, "m" col[index:i] = "crisis" } } - + return(list("indicators" = indicators, market_states = col)) - + } diff --git a/R-proj/R/gen_rand_hpoly.R b/R-proj/R/gen_rand_hpoly.R index 9e1a4fb40..1df6f3bfe 100644 --- a/R-proj/R/gen_rand_hpoly.R +++ b/R-proj/R/gen_rand_hpoly.R @@ -1,27 +1,27 @@ #' Generator function for random H-polytopes -#' +#' #' This function generates a \eqn{d}-dimensional polytope in H-representation with \eqn{m} facets. We pick \eqn{m} random hyperplanes tangent on the \eqn{d}-dimensional unit hypersphere as facets. -#' +#' #' @param dimension The dimension of the convex polytope. #' @param nfacets The number of the facets. #' @param generator A list that could contain two elements. -#' \itemize{ +#' \describe{ #' \item{constants }{ To declare how to set the constants \eqn{b_i} for each facets: (i) 'sphere', each hyperplane is tangent to the hypersphere of radius 10, (ii) 'uniform' for each \eqn{b_i} the generator picks a uniform number from \eqn{(0,1)}. The defalut value is 'sphere'.} #' \item{seed }{ Optional. A fixed seed for the number generator.} #' } -#' +#' #' @return A polytope class representing a H-polytope. -#' @examples +#' @examples #' # generate a 10-dimensional polytope with 50 facets #' P = gen_rand_hpoly(10, 50) #' @export gen_rand_hpoly <- function(dimension, nfacets, generator = list('constants' = 'sphere')) { - + seed = NULL if (!is.null(generator$seed)) { seed = generator$seed } - + if (is.null(generator$constants)) { kind_gen = 6 } else if (generator$constants == 'sphere'){ @@ -32,14 +32,14 @@ gen_rand_hpoly <- function(dimension, nfacets, generator = list('constants' = 's stop("Wrong generator!") } Vpoly_gen = FALSE - + Mat = poly_gen(kind_gen, Vpoly_gen, FALSE, dimension, nfacets, seed) - + # first column is the vector b b = Mat[, 1] Mat = Mat[, -c(1), drop = FALSE] - + P = Hpolytope(A = Mat, b = b) - + return(P) } diff --git a/R-proj/R/gen_rand_vpoly.R b/R-proj/R/gen_rand_vpoly.R index 3486f2e1f..c9492adfc 100644 --- a/R-proj/R/gen_rand_vpoly.R +++ b/R-proj/R/gen_rand_vpoly.R @@ -1,27 +1,27 @@ #' Generator function for random V-polytopes -#' +#' #' This function generates a \eqn{d}-dimensional polytope in V-representation with \eqn{m} vertices. We pick \eqn{m} random points from the boundary of the \eqn{d}-dimensional unit hypersphere as vertices. -#' +#' #' @param dimension The dimension of the convex polytope. #' @param nvertices The number of the vertices. #' @param generator A list that could contain two elements. -#' \itemize{ +#' \describe{ #' \item{body }{ the body that the generator samples uniformly the vertices from: (i) 'cube' or (ii) 'sphere', the default value is 'sphere'.} #' \item{seed }{ Optional. A fixed seed for the number generator.} #' } -#' +#' #' @return A polytope class representing a V-polytope. -#' @examples +#' @examples #' # generate a 10-dimensional polytope defined as the convex hull of 25 random vertices #' P = gen_rand_vpoly(10, 25) #' @export gen_rand_vpoly <- function(dimension, nvertices, generator = list('body' = 'sphere')) { - + seed = NULL if (!is.null(generator$seed)) { seed = generator$seed } - + if (is.null(generator$body)) { kind_gen = 4 } else if (generator$body == 'cube'){ @@ -31,14 +31,14 @@ gen_rand_vpoly <- function(dimension, nvertices, generator = list('body' = 'sphe } else { stop("Wrong generator!") } - + Mat = poly_gen(kind_gen, TRUE, FALSE, dimension, nvertices, seed) # first column is the vector b b = Mat[, 1] Mat = Mat[, -c(1), drop = FALSE] - + P = Vpolytope(V = Mat) - + return(P) } diff --git a/R-proj/R/gen_rand_zonotope.R b/R-proj/R/gen_rand_zonotope.R index 656f844b0..aa887253d 100644 --- a/R-proj/R/gen_rand_zonotope.R +++ b/R-proj/R/gen_rand_zonotope.R @@ -1,29 +1,29 @@ #' Generator function for zonotopes -#' +#' #' This function generates a random \eqn{d}-dimensional zonotope defined by the Minkowski sum of \eqn{m} \eqn{d}-dimensional segments. #' The function considers \eqn{m} random directions in \eqn{R^d}. There are three strategies to pick the length of each segment: a) it is uniformly sampled from \eqn{[0,100]}, b) it is random from \eqn{\mathcal{N}(50,(50/3)^2)} truncated to \eqn{[0,100]}, c) it is random from \eqn{Exp(1/30)} truncated to \eqn{[0,100]}. -#' +#' #' @param dimension The dimension of the zonotope. #' @param nsegments The number of segments that generate the zonotope. #' @param generator A list that could contain two elements. -#' \itemize{ -#' \item{distribution }{ the distribution to pick the length of each segment from \eqn{[0,100]}: (i) 'uniform', (ii) 'gaussian' or (iii) 'exponential', the default value is 'uniform.} -#' \item {seed }{ Optional. A fixed seed for the number generator.} +#' \describe{ +#' \item{distribution}{The distribution to pick the length of each segment from \eqn{[0,100]}: (i) 'uniform', (ii) 'gaussian' or (iii) 'exponential', the default value is 'uniform.} +#' \item{seed}{Optional. A fixed seed for the number generator.} #' } -#' +#' #' @return A polytope class representing a zonotope. #' -#' @examples +#' @examples #' # generate a 10-dimensional zonotope defined by the Minkowski sum of 20 segments #' P = gen_rand_zonotope(10, 20) #' @export gen_rand_zonotope <- function(dimension, nsegments, generator = list('distribution' = 'uniform')) { - + seed = NULL if (!is.null(generator$seed)) { seed = generator$seed } - + if (is.null(generator$distribution)) { kind_gen = 1 } else if (generator$distribution == 'gaussian') { @@ -35,13 +35,13 @@ gen_rand_zonotope <- function(dimension, nsegments, generator = list('distributi } else { stop("Wrong generator!") } - + Mat = poly_gen(kind_gen, FALSE, TRUE, dimension, nsegments, seed) - + # first column is the vector b b = Mat[, 1] Mat = Mat[, -c(1), drop = FALSE] - + P = Zonotope(G = Mat) return(P) diff --git a/R-proj/R/rotate_polytope.R b/R-proj/R/rotate_polytope.R index 26837e017..bb371e312 100644 --- a/R-proj/R/rotate_polytope.R +++ b/R-proj/R/rotate_polytope.R @@ -1,50 +1,50 @@ #' Apply a random rotation to a convex polytope (H-polytope, V-polytope, zonotope or intersection of two V-polytopes) -#' +#' #' Given a convex H- or V- polytope or a zonotope or an intersection of two V-polytopes as input, this function applies (a) a random rotation or (b) a given rotation by an input matrix \eqn{T}. -#' +#' #' @param P A convex polytope. It is an object from class (a) Hpolytope, (b) Vpolytope, (c) Zonotope, (d) intersection of two V-polytopes. #' @param rotation A list that contains (a) the rotation matrix T and (b) the 'seed' to set a spesific seed for the number generator. -#' +#' #' @return A list that contains the rotated polytope and the matrix \eqn{T} of the linear transformation. #' -#' @details Let \eqn{P} be the given polytope and \eqn{Q} the rotated one and \eqn{T} be the matrix of the linear transformation. +#' @details Let \eqn{P} be the given polytope and \eqn{Q} the rotated one and \eqn{T} be the matrix of the linear transformation. #' \itemize{ -#' \item{If \eqn{P} is in H-representation and \eqn{A} is the matrix that contains the normal vectors of the facets of \eqn{Q} then \eqn{AT} contains the normal vactors of the facets of \eqn{P}.} -#' \item{If \eqn{P} is in V-representation and \eqn{V} is the matrix that contains column-wise the vertices of \eqn{Q} then \eqn{T^TV} contains the vertices of \eqn{P}.} -#' \item{If \eqn{P} is a zonotope and \eqn{G} is the matrix that contains column-wise the generators of \eqn{Q} then \eqn{T^TG} contains the generators of \eqn{P}.} -#' \item{If \eqn{M} is a matrix that contains column-wise points in \eqn{Q} then \eqn{T^TM} contains points in \eqn{P}.} +#' \item If \eqn{P} is in H-representation and \eqn{A} is the matrix that contains the normal vectors of the facets of \eqn{Q} then \eqn{AT} contains the normal vactors of the facets of \eqn{P}. +#' \item If \eqn{P} is in V-representation and \eqn{V} is the matrix that contains column-wise the vertices of \eqn{Q} then \eqn{T^TV} contains the vertices of \eqn{P}. +#' \item If \eqn{P} is a zonotope and \eqn{G} is the matrix that contains column-wise the generators of \eqn{Q} then \eqn{T^TG} contains the generators of \eqn{P}. +#' \item If \eqn{M} is a matrix that contains column-wise points in \eqn{Q} then \eqn{T^TM} contains points in \eqn{P}. #' } #' @examples #' # rotate a H-polytope (2d unit simplex) #' P = gen_simplex(2, 'H') #' poly_matrix_list = rotate_polytope(P) -#' +#' #' # rotate a V-polytope (3d cube) #' P = gen_cube(3, 'V') #' poly_matrix_list = rotate_polytope(P) -#' +#' #' # rotate a 5-dimensional zonotope defined by the Minkowski sum of 15 segments #' Z = gen_rand_zonotope(3, 6) #' poly_matrix_list = rotate_polytope(Z) #' @export rotate_polytope <- function(P, rotation = list()) { - + seed = NULL if (!is.null(rotation$seed)) { seed = rotation$seed } - + if (is.null(rotation$T)) { T = NULL } else { T = rotation$T } - + #call rcpp rotating function Mat = rotating(P, T, seed) - + type = P@type - + if (type == 'Vpolytope') { n = dim(P@V)[2] }else if (type == 'Zonotope') { @@ -52,18 +52,18 @@ rotate_polytope <- function(P, rotation = list()) { } else { n = dim(P@A)[2] } - + m = dim(Mat)[2] - n Tr = Mat[, -c(1:(dim(Mat)[2]-n)), drop = FALSE] Tr = Tr[1:n, 1:n] Mat = t(Mat[, 1:m]) - + # first column is the vector b b = Mat[, 1] - + # remove first column A = Mat[, -c(1), drop = FALSE] - + if (type == 'Vpolytope') { PP = Vpolytope(V = A) }else if (type == 'Zonotope') { diff --git a/R-proj/R/round_polytope.R b/R-proj/R/round_polytope.R index 32b92bd4c..12c2e8c87 100644 --- a/R-proj/R/round_polytope.R +++ b/R-proj/R/round_polytope.R @@ -1,15 +1,15 @@ #' Apply rounding to a convex polytope (H-polytope, V-polytope or a zonotope) -#' +#' #' Given a convex H or V polytope or a zonotope as input this function brings the polytope in rounded position based on minimum volume enclosing ellipsoid of a pointset. -#' +#' #' @param P A convex polytope. It is an object from class (a) Hpolytope or (b) Vpolytope or (c) Zonotope. -#' @param settings Optional. A list to parameterize the method by the random walk. -#' \itemize{ +#' @param settings Optional. A list to parameterize the method by the random walk. +#' \describe{ #' \item{\code{random_walk} }{ The random walk to sample uniformly distributed points: (a) \code{'CDHR'} for Coordinate Directions Hit-and-Run, (b) \code{'RDHR'} for Random Directions Hit-and-Run or (c) \code{'BiW'} for Billiard walk. The default random walk is \code{'CDHR'} for H-polytopes and \code{'BiW'} for the rest of the representations.} #' \item{\code{walk_length} }{ The walk length of the random walk. The default value is \eqn{10 + 10d} for \code{'CDHR'} or \code{'RDHR'} and 2 for \code{'BiW'}.} #' \item{\code{seed} }{ Optional. A fixed seed for the number generator.} #' } -#' +#' #' @return A list with 4 elements: (a) a polytope of the same class as the input polytope class and (b) the element "T" which is the matrix of the inverse linear transformation that is applied on the input polytope, (c) the element "shift" which is the opposite vector of that which has shifted the input polytope, (d) the element "round_value" which is the determinant of the square matrix of the linear transformation that is applied on the input polytope. #' #' @references \cite{I.Z.Emiris and V. Fisikopoulos, @@ -23,33 +23,33 @@ #' b = c(0,0,1) #' P = Hpolytope(A = A, b = b) #' listHpoly = round_polytope(P) -#' +#' #' # rotate a V-polytope (3d unit cube) using Random Directions HnR with step equal to 50 #' P = gen_cube(3, 'V') #' ListVpoly = round_polytope(P) -#' +#' #' # round a 2-dimensional zonotope defined by 6 generators using ball walk #' Z = gen_rand_zonotope(2,6) #' ListZono = round_polytope(Z) #' @export round_polytope <- function(P, settings = list()){ - + seed = NULL if (!is.null(settings$seed)) { seed = settings$seed } - + ret_list = rounding(P, settings, seed) - + #get the matrix that describes the polytope Mat = ret_list$Mat - + # first column is the vector b b = Mat[, 1] - + # remove first column A = Mat[, -c(1), drop = FALSE] - + type = P@type if (type == 'Vpolytope') { PP = list("P" = Vpolytope(V = A), "T" = ret_list$T, "shift" = ret_list$shift, "round_value" = ret_list$round_value) diff --git a/R-proj/R/zonotope_approximation.R b/R-proj/R/zonotope_approximation.R index 523870767..c5c3e6ded 100644 --- a/R-proj/R/zonotope_approximation.R +++ b/R-proj/R/zonotope_approximation.R @@ -1,48 +1,48 @@ #' A function to over-approximate a zonotope with PCA method and to evaluate the approximation by computing a ratio of fitness. -#' +#' #' For the evaluation of the PCA method the exact volume of the approximation body is computed and the volume of the input zonotope is computed by CoolingBodies algorithm. The ratio of fitness is \eqn{R=vol(P) / vol(P_{red})}, where \eqn{P_{red}} is the approximate polytope. -#' +#' #' @param Z A zonotope. #' @param fit_ratio Optional. A boolean parameter to request the computation of the ratio of fitness. #' @param settings Optional. A list that declares the values of the parameters of CB algorithm as follows: -#' \itemize{ +#' \describe{ #' \item{\code{error} }{ A numeric value to set the upper bound for the approximation error. The default value is \eqn{0.1}.} #' \item{\code{walk_length} }{ An integer to set the number of the steps for the random walk. The default value is \eqn{1}.} #' \item{\code{win_len} }{ The length of the sliding window for CB algorithm. The default value is \eqn{250}.} #' \item{\code{hpoly} }{ A boolean parameter to use H-polytopes in MMC of CB algorithm. The default value is \code{TRUE} when the order of the zonotope is \eqn{<5}, otherwise it is \code{FALSE}.} #' \item{\code{seed} }{ Optional. A fixed seed for the number generator.} #' } -#' +#' #' @return A list that contains the approximation body in H-representation and the ratio of fitness -#' +#' #' @references \cite{A.K. Kopetzki and B. Schurmann and M. Althoff, #' \dQuote{Methods for Order Reduction of Zonotopes,} \emph{IEEE Conference on Decision and Control,} 2017.} -#' +#' #' @examples #' # over-approximate a 2-dimensional zonotope with 10 generators and compute the ratio of fitness #' Z = gen_rand_zonotope(2, 8) #' retList = zonotope_approximation(Z = Z) -#' +#' #' @export zonotope_approximation <- function(Z, fit_ratio = FALSE, settings = list('error' = 0.1, 'walk_length' = 1, 'win_len' = 250, 'hpoly' = FALSE)){ - + seed = NULL if (!is.null(settings$seed)) { seed = settings$seed } - + ret_list = zono_approx(Z, fit_ratio, settings, seed) - + Mat = ret_list$Mat - + # first column is the vector b b = Mat[, 1] - + # remove first column A = Mat[, -c(1), drop = FALSE] PP = list("P" = Hpolytope(A = A, b = b), "fit_ratio" = ret_list$fit_ratio) - + return(PP) - + } diff --git a/R-proj/man/compute_indicators.Rd b/R-proj/man/compute_indicators.Rd index 9bee0ab78..3c49f6283 100644 --- a/R-proj/man/compute_indicators.Rd +++ b/R-proj/man/compute_indicators.Rd @@ -13,21 +13,21 @@ compute_indicators( \item{returns}{A \eqn{d}-dimensional vector that describes the direction of the first family of parallel hyperplanes.} \item{parameters}{A list to set a parameterization. -\itemize{ -\item{win_length }{ The length of the sliding window. The default value is 60.} -\item{m } { The number of slices for the copula. The default value is 100.} -\item{n }{ The number of points to sample. The default value is \eqn{5\cdot 10^5}.} -\item{nwarning }{ The number of consecutive indicators larger than 1 required to declare a warning period. The default value is 60.} -\item{ncrisis }{ The number of consecutive indicators larger than 1 required to declare a crisis period. The default value is 100.} -\item{seed }{ A fixed seed for the number generator.} +\describe{ +\item{win_length}{The length of the sliding window. The default value is 60.} +\item{m}{The number of slices for the copula. The default value is 100.} +\item{n}{The number of points to sample. The default value is \eqn{5\cdot 10^5}.} +\item{nwarning}{The number of consecutive indicators larger than 1 required to declare a warning period. The default value is 60.} +\item{ncrisis}{The number of consecutive indicators larger than 1 required to declare a crisis period. The default value is 100.} +\item{seed}{A fixed seed for the number generator.} }} } \value{ A list that contains the indicators and the corresponding vector that label each time period with respect to the market state: a) normal, b) crisis, c) warning. } \description{ -Given a matrix that contains row-wise the assets' returns and a sliding window \code{win_length}, this function computes an approximation of the joint distribution (copula, e.g. see \url{https://en.wikipedia.org/wiki/Copula_(probability_theory)}) between portfolios' return and volatility in each time period defined by \code{win_len}. -For each copula it computes an indicator: If the indicator is large it corresponds to a crisis period and if it is small it corresponds to a normal period. +Given a matrix that contains row-wise the assets' returns and a sliding window \code{win_length}, this function computes an approximation of the joint distribution (copula, e.g. see \url{https://en.wikipedia.org/wiki/Copula_(probability_theory)}) between portfolios' return and volatility in each time period defined by \code{win_len}. +For each copula it computes an indicator: If the indicator is large it corresponds to a crisis period and if it is small it corresponds to a normal period. In particular, the periods over which the indicator is greater than 1 for more than 60 consecutive sliding windows are warnings and for more than 100 are crisis. The sliding window is shifted by one day. } \examples{ diff --git a/R-proj/man/direct_sampling.Rd b/R-proj/man/direct_sampling.Rd index a4259531e..fddd80d04 100644 --- a/R-proj/man/direct_sampling.Rd +++ b/R-proj/man/direct_sampling.Rd @@ -8,7 +8,7 @@ direct_sampling(body, n) } \arguments{ \item{body}{A list to request exact uniform sampling from special well known convex bodies through the following input parameters: -\itemize{ +\describe{ \item{\code{type} }{ A string that declares the type of the body for the exact sampling: a) \code{'unit_simplex'} for the unit simplex, b) \code{'canonical_simplex'} for the canonical simplex, c) \code{'hypersphere'} for the boundary of a hypersphere centered at the origin, d) \code{'ball'} for the interior of a hypersphere centered at the origin.} \item{\code{dimension} }{ An integer that declares the dimension when exact sampling is enabled for a simplex or a hypersphere.} \item{\code{radius} }{ The radius of the \eqn{d}-dimensional hypersphere. The default value is \eqn{1}.} diff --git a/R-proj/man/gen_rand_hpoly.Rd b/R-proj/man/gen_rand_hpoly.Rd index 85dd17571..1a5740f3b 100644 --- a/R-proj/man/gen_rand_hpoly.Rd +++ b/R-proj/man/gen_rand_hpoly.Rd @@ -12,7 +12,7 @@ gen_rand_hpoly(dimension, nfacets, generator = list(constants = "sphere")) \item{nfacets}{The number of the facets.} \item{generator}{A list that could contain two elements. -\itemize{ +\describe{ \item{constants }{ To declare how to set the constants \eqn{b_i} for each facets: (i) 'sphere', each hyperplane is tangent to the hypersphere of radius 10, (ii) 'uniform' for each \eqn{b_i} the generator picks a uniform number from \eqn{(0,1)}. The defalut value is 'sphere'.} \item{seed }{ Optional. A fixed seed for the number generator.} }} diff --git a/R-proj/man/gen_rand_vpoly.Rd b/R-proj/man/gen_rand_vpoly.Rd index e34125a5b..7c6bfbe5e 100644 --- a/R-proj/man/gen_rand_vpoly.Rd +++ b/R-proj/man/gen_rand_vpoly.Rd @@ -12,7 +12,7 @@ gen_rand_vpoly(dimension, nvertices, generator = list(body = "sphere")) \item{nvertices}{The number of the vertices.} \item{generator}{A list that could contain two elements. -\itemize{ +\describe{ \item{body }{ the body that the generator samples uniformly the vertices from: (i) 'cube' or (ii) 'sphere', the default value is 'sphere'.} \item{seed }{ Optional. A fixed seed for the number generator.} }} diff --git a/R-proj/man/gen_rand_zonotope.Rd b/R-proj/man/gen_rand_zonotope.Rd index 41497ef24..683c15f29 100644 --- a/R-proj/man/gen_rand_zonotope.Rd +++ b/R-proj/man/gen_rand_zonotope.Rd @@ -16,9 +16,9 @@ gen_rand_zonotope( \item{nsegments}{The number of segments that generate the zonotope.} \item{generator}{A list that could contain two elements. -\itemize{ -\item{distribution }{ the distribution to pick the length of each segment from \eqn{[0,100]}: (i) 'uniform', (ii) 'gaussian' or (iii) 'exponential', the default value is 'uniform.} -\item {seed }{ Optional. A fixed seed for the number generator.} +\describe{ +\item{distribution}{The distribution to pick the length of each segment from \eqn{[0,100]}: (i) 'uniform', (ii) 'gaussian' or (iii) 'exponential', the default value is 'uniform.} +\item{seed}{Optional. A fixed seed for the number generator.} }} } \value{ diff --git a/R-proj/man/rotate_polytope.Rd b/R-proj/man/rotate_polytope.Rd index 983a2911f..a1e9d4ebb 100644 --- a/R-proj/man/rotate_polytope.Rd +++ b/R-proj/man/rotate_polytope.Rd @@ -18,12 +18,12 @@ A list that contains the rotated polytope and the matrix \eqn{T} of the linear t Given a convex H- or V- polytope or a zonotope or an intersection of two V-polytopes as input, this function applies (a) a random rotation or (b) a given rotation by an input matrix \eqn{T}. } \details{ -Let \eqn{P} be the given polytope and \eqn{Q} the rotated one and \eqn{T} be the matrix of the linear transformation. +Let \eqn{P} be the given polytope and \eqn{Q} the rotated one and \eqn{T} be the matrix of the linear transformation. \itemize{ -\item{If \eqn{P} is in H-representation and \eqn{A} is the matrix that contains the normal vectors of the facets of \eqn{Q} then \eqn{AT} contains the normal vactors of the facets of \eqn{P}.} -\item{If \eqn{P} is in V-representation and \eqn{V} is the matrix that contains column-wise the vertices of \eqn{Q} then \eqn{T^TV} contains the vertices of \eqn{P}.} -\item{If \eqn{P} is a zonotope and \eqn{G} is the matrix that contains column-wise the generators of \eqn{Q} then \eqn{T^TG} contains the generators of \eqn{P}.} -\item{If \eqn{M} is a matrix that contains column-wise points in \eqn{Q} then \eqn{T^TM} contains points in \eqn{P}.} +\item If \eqn{P} is in H-representation and \eqn{A} is the matrix that contains the normal vectors of the facets of \eqn{Q} then \eqn{AT} contains the normal vactors of the facets of \eqn{P}. +\item If \eqn{P} is in V-representation and \eqn{V} is the matrix that contains column-wise the vertices of \eqn{Q} then \eqn{T^TV} contains the vertices of \eqn{P}. +\item If \eqn{P} is a zonotope and \eqn{G} is the matrix that contains column-wise the generators of \eqn{Q} then \eqn{T^TG} contains the generators of \eqn{P}. +\item If \eqn{M} is a matrix that contains column-wise points in \eqn{Q} then \eqn{T^TM} contains points in \eqn{P}. } } \examples{ diff --git a/R-proj/man/round_polytope.Rd b/R-proj/man/round_polytope.Rd index 1e274882a..9688bca84 100644 --- a/R-proj/man/round_polytope.Rd +++ b/R-proj/man/round_polytope.Rd @@ -9,8 +9,8 @@ round_polytope(P, settings = list()) \arguments{ \item{P}{A convex polytope. It is an object from class (a) Hpolytope or (b) Vpolytope or (c) Zonotope.} -\item{settings}{Optional. A list to parameterize the method by the random walk. -\itemize{ +\item{settings}{Optional. A list to parameterize the method by the random walk. +\describe{ \item{\code{random_walk} }{ The random walk to sample uniformly distributed points: (a) \code{'CDHR'} for Coordinate Directions Hit-and-Run, (b) \code{'RDHR'} for Random Directions Hit-and-Run or (c) \code{'BiW'} for Billiard walk. The default random walk is \code{'CDHR'} for H-polytopes and \code{'BiW'} for the rest of the representations.} \item{\code{walk_length} }{ The walk length of the random walk. The default value is \eqn{10 + 10d} for \code{'CDHR'} or \code{'RDHR'} and 2 for \code{'BiW'}.} \item{\code{seed} }{ Optional. A fixed seed for the number generator.} diff --git a/R-proj/man/sample_points.Rd b/R-proj/man/sample_points.Rd index 890d27307..f2ac18a99 100644 --- a/R-proj/man/sample_points.Rd +++ b/R-proj/man/sample_points.Rd @@ -12,7 +12,7 @@ sample_points(P, n, random_walk = NULL, distribution = NULL) \item{n}{The number of points that the function is going to sample from the convex polytope.} \item{random_walk}{Optional. A list that declares the random walk and some related parameters as follows: -\itemize{ +\describe{ \item{\code{walk} }{ A string to declare the random walk: i) \code{'CDHR'} for Coordinate Directions Hit-and-Run, ii) \code{'RDHR'} for Random Directions Hit-and-Run, iii) \code{'BaW'} for Ball Walk, iv) \code{'BiW'} for Billiard walk, v) \code{'BCDHR'} boundary sampling by keeping the extreme points of CDHR or vi) \code{'BRDHR'} boundary sampling by keeping the extreme points of RDHR. The default walk is \code{'BiW'} for the uniform distribution or \code{'CDHR'} for the Gaussian distribution.} \item{\code{walk_length} }{ The number of the steps per generated point for the random walk. The default value is 1.} \item{\code{nburns} }{ The number of points to burn before start sampling.} @@ -23,7 +23,7 @@ sample_points(P, n, random_walk = NULL, distribution = NULL) }} \item{distribution}{Optional. A list that declares the target density and some related parameters as follows: -\itemize{ +\describe{ \item{\code{density} }{ A string: (a) \code{'uniform'} for the uniform distribution or b) \code{'gaussian'} for the multidimensional spherical distribution. The default target distribution is uniform.} \item{\code{variance} }{ The variance of the multidimensional spherical gaussian. The default value is 1.} \item{\code{mode} }{ A \eqn{d}-dimensional numerical vector that declares the mode of the Gaussian distribution. The default choice is the center of the as that one computed by the function \code{inner_ball()}.} diff --git a/R-proj/man/volume.Rd b/R-proj/man/volume.Rd index d1db89515..9698eeee3 100644 --- a/R-proj/man/volume.Rd +++ b/R-proj/man/volume.Rd @@ -10,7 +10,7 @@ volume(P, settings = NULL, rounding = FALSE) \item{P}{A convex polytope. It is an object from class a) Hpolytope or b) Vpolytope or c) Zonotope or d) VpolytopeIntersection.} \item{settings}{Optional. A list that declares which algorithm, random walk and values of parameters to use, as follows: -\itemize{ +\describe{ \item{\code{algorithm} }{ A string to set the algorithm to use: a) \code{'CB'} for CB algorithm, b) \code{'SoB'} for SOB algorithm or b) \code{'CG'} for CG algorithm. The defalut algorithm is \code{'CB'}.} \item{\code{error} }{ A numeric value to set the upper bound for the approximation error. The default value is \eqn{1} for SOB algorithm and \eqn{0.1} otherwise.} \item{\code{random_walk} }{ A string that declares the random walk method: a) \code{'CDHR'} for Coordinate Directions Hit-and-Run, b) \code{'RDHR'} for Random Directions Hit-and-Run, c) \code{'BaW'} for Ball Walk, or \code{'BiW'} for Billiard walk. For CB and SOB algorithms the default walk is \code{'CDHR'} for H-polytopes and \code{'BiW'} for the other representations. For CG algorithm the default walk is \code{'CDHR'} for H-polytopes and \code{'RDHR'} for the other representations.} diff --git a/R-proj/man/zonotope_approximation.Rd b/R-proj/man/zonotope_approximation.Rd index 25510a0c1..0c3343edc 100644 --- a/R-proj/man/zonotope_approximation.Rd +++ b/R-proj/man/zonotope_approximation.Rd @@ -16,7 +16,7 @@ zonotope_approximation( \item{fit_ratio}{Optional. A boolean parameter to request the computation of the ratio of fitness.} \item{settings}{Optional. A list that declares the values of the parameters of CB algorithm as follows: -\itemize{ +\describe{ \item{\code{error} }{ A numeric value to set the upper bound for the approximation error. The default value is \eqn{0.1}.} \item{\code{walk_length} }{ An integer to set the number of the steps for the random walk. The default value is \eqn{1}.} \item{\code{win_len} }{ The length of the sliding window for CB algorithm. The default value is \eqn{250}.} diff --git a/R-proj/src/direct_sampling.cpp b/R-proj/src/direct_sampling.cpp index dee5c72d1..db9ed20da 100644 --- a/R-proj/src/direct_sampling.cpp +++ b/R-proj/src/direct_sampling.cpp @@ -24,7 +24,7 @@ //' The \eqn{d}-dimensional unit simplex is the set of points \eqn{\vec{x}\in \R^d}, s.t.: \eqn{\sum_i x_i\leq 1}, \eqn{x_i\geq 0}. The \eqn{d}-dimensional canonical simplex is the set of points \eqn{\vec{x}\in \R^d}, s.t.: \eqn{\sum_i x_i = 1}, \eqn{x_i\geq 0}. //' //' @param body A list to request exact uniform sampling from special well known convex bodies through the following input parameters: -//' \itemize{ +//' \describe{ //' \item{\code{type} }{ A string that declares the type of the body for the exact sampling: a) \code{'unit_simplex'} for the unit simplex, b) \code{'canonical_simplex'} for the canonical simplex, c) \code{'hypersphere'} for the boundary of a hypersphere centered at the origin, d) \code{'ball'} for the interior of a hypersphere centered at the origin.} //' \item{\code{dimension} }{ An integer that declares the dimension when exact sampling is enabled for a simplex or a hypersphere.} //' \item{\code{radius} }{ The radius of the \eqn{d}-dimensional hypersphere. The default value is \eqn{1}.} diff --git a/R-proj/src/sample_points.cpp b/R-proj/src/sample_points.cpp index f7c48a8d8..a18412f93 100644 --- a/R-proj/src/sample_points.cpp +++ b/R-proj/src/sample_points.cpp @@ -87,7 +87,7 @@ void sample_from_polytope(Polytope &P, RNGType &rng, PointList &randPoints, unsi //' @param P A convex polytope. It is an object from class (a) Hpolytope or (b) Vpolytope or (c) Zonotope or (d) VpolytopeIntersection. //' @param n The number of points that the function is going to sample from the convex polytope. //' @param random_walk Optional. A list that declares the random walk and some related parameters as follows: -//' \itemize{ +//' \describe{ //' \item{\code{walk} }{ A string to declare the random walk: i) \code{'CDHR'} for Coordinate Directions Hit-and-Run, ii) \code{'RDHR'} for Random Directions Hit-and-Run, iii) \code{'BaW'} for Ball Walk, iv) \code{'BiW'} for Billiard walk, v) \code{'BCDHR'} boundary sampling by keeping the extreme points of CDHR or vi) \code{'BRDHR'} boundary sampling by keeping the extreme points of RDHR. The default walk is \code{'BiW'} for the uniform distribution or \code{'CDHR'} for the Gaussian distribution.} //' \item{\code{walk_length} }{ The number of the steps per generated point for the random walk. The default value is 1.} //' \item{\code{nburns} }{ The number of points to burn before start sampling.} @@ -97,7 +97,7 @@ void sample_from_polytope(Polytope &P, RNGType &rng, PointList &randPoints, unsi //' \item{\code{seed} }{ A fixed seed for the number generator.} //' } //' @param distribution Optional. A list that declares the target density and some related parameters as follows: -//' \itemize{ +//' \describe{ //' \item{\code{density} }{ A string: (a) \code{'uniform'} for the uniform distribution or b) \code{'gaussian'} for the multidimensional spherical distribution. The default target distribution is uniform.} //' \item{\code{variance} }{ The variance of the multidimensional spherical gaussian. The default value is 1.} //' \item{\code{mode} }{ A \eqn{d}-dimensional numerical vector that declares the mode of the Gaussian distribution. The default choice is the center of the as that one computed by the function \code{inner_ball()}.} diff --git a/R-proj/src/volume.cpp b/R-proj/src/volume.cpp index b89837b24..9506d9a3e 100644 --- a/R-proj/src/volume.cpp +++ b/R-proj/src/volume.cpp @@ -86,7 +86,7 @@ double generic_volume(Polytope& P, RNGType &rng, unsigned int walk_length, NT e, //' //' @param P A convex polytope. It is an object from class a) Hpolytope or b) Vpolytope or c) Zonotope or d) VpolytopeIntersection. //' @param settings Optional. A list that declares which algorithm, random walk and values of parameters to use, as follows: -//' \itemize{ +//' \describe{ //' \item{\code{algorithm} }{ A string to set the algorithm to use: a) \code{'CB'} for CB algorithm, b) \code{'SoB'} for SOB algorithm or b) \code{'CG'} for CG algorithm. The defalut algorithm is \code{'CB'}.} //' \item{\code{error} }{ A numeric value to set the upper bound for the approximation error. The default value is \eqn{1} for SOB algorithm and \eqn{0.1} otherwise.} //' \item{\code{random_walk} }{ A string that declares the random walk method: a) \code{'CDHR'} for Coordinate Directions Hit-and-Run, b) \code{'RDHR'} for Random Directions Hit-and-Run, c) \code{'BaW'} for Ball Walk, or \code{'BiW'} for Billiard walk. For CB and SOB algorithms the default walk is \code{'CDHR'} for H-polytopes and \code{'BiW'} for the other representations. For CG algorithm the default walk is \code{'CDHR'} for H-polytopes and \code{'RDHR'} for the other representations.} @@ -135,7 +135,7 @@ double volume (Rcpp::Reference P, typedef IntersectionOfVpoly InterVP; typedef Eigen::Matrix VT; typedef Eigen::Matrix MT; - + unsigned int n, walkL, type_num; std::string type = Rcpp::as(P.slot("type")); @@ -164,7 +164,7 @@ double volume (Rcpp::Reference P, bool CG = false, CB = false, cdhr = false, rdhr = false, ball_walk = false, hpoly = false, billiard = false; unsigned int win_len = 4*n*n+500; - + NT e; if (!Rcpp::as(settings).containsElementNamed("algorithm")) { diff --git a/cran_gen/NEWS.md b/cran_gen/NEWS.md index 9b267114b..e4cc5057a 100644 --- a/cran_gen/NEWS.md +++ b/cran_gen/NEWS.md @@ -54,3 +54,7 @@ # volesti 1.1.2-7 - Fix valgrind reporting a realloc(3) call with size = 0 (lpsolve) + +# volesti 1.1.2-8 + +- Fix Wmissing-template-arg-list-after-template-kw error (clang-19) diff --git a/external/LPsolve_src/include/lusol.h b/external/LPsolve_src/include/lusol.h index 56e9b9e93..644dcff9d 100644 --- a/external/LPsolve_src/include/lusol.h +++ b/external/LPsolve_src/include/lusol.h @@ -1,4 +1,5 @@ // Copyright(c) 2016-2018 Kjell Konis . +// Copyright(c) 2024 Vissarion Fisikopoulos . // Version: 5.5.2.0-17 // Description: The lpSolveAPI package provides an R interface to 'lp_solve', // a Mixed Integer Linear Programming (MILP) solver with support for pure @@ -32,7 +33,7 @@ #else #define LUSOL_MALLOC(bytesize) malloc(bytesize) #define LUSOL_CALLOC(count, recsize) calloc(count, recsize) - #define LUSOL_REALLOC(ptr, bytesize) realloc((void *) ptr, bytesize) + #define LUSOL_REALLOC(ptr, bytesize) realloc((void *) ptr, (unsigned int)bytesize) #define LUSOL_FREE(ptr) {free(ptr); ptr=NULL;} #endif diff --git a/include/sampling/random_point_generators.hpp b/include/sampling/random_point_generators.hpp index d2e5bcf37..020187758 100644 --- a/include/sampling/random_point_generators.hpp +++ b/include/sampling/random_point_generators.hpp @@ -36,7 +36,7 @@ struct RandomPointGenerator Walk walk(P, p, rng, parameters); for (unsigned int i=0; i