Skip to content

Commit

Permalink
Various improvements (tests, documentation, names) (#61)
Browse files Browse the repository at this point in the history
* modify c++ tests

* improve input variable names in R functions

* check for negative error parameter in both interfaces

* update Rd files

* update compute_indicators

* update rotating R functions

* update sample_points.cpp

* update volume.cpp and Rd files

* update zonotope_approximation functons and the Rd files

* fix examples, tests, documentation errors

* update rounding and fix a bug in sample_points gaussian sampling, update Rd files, fix cran errors

* add the option to set seed for the random polytope generator

* fix cran check errors

* add boundary sampling in R interface

* add references in rounding functions and fix bug in vol.cpp
  • Loading branch information
TolisChal authored Feb 27, 2020
1 parent 2b8a871 commit ca784f5
Show file tree
Hide file tree
Showing 40 changed files with 783 additions and 659 deletions.
2 changes: 1 addition & 1 deletion R-proj/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export(gen_rand_zonotope)
export(gen_simplex)
export(gen_skinny_cube)
export(inner_ball)
export(rand_rotate)
export(rotate_polytope)
export(round_polytope)
export(sample_points)
export(volume)
Expand Down
129 changes: 64 additions & 65 deletions R-proj/R/RcppExports.R

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions R-proj/R/compute_indicators.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
#' @param win_length Optional. The length of the sliding window. The default value is 60.
#' @param m Optional. The number of slices for the copula. The default value is 100.
#' @param n Optional. The number of points to sample. The default value is \eqn{5\cdot 10^5}.
#' @param nwarning Optional. The number of consecutive indicators larger than 1 required to declare a warning period. The default value is 60.
#' @param ncrisis Optional. The number of consecutive indicators larger than 1 required to declare a crisis period. The default value is 100.
#'
#' @references \cite{L. Cales, A. Chalkis, I.Z. Emiris, V. Fisikopoulos,
#' \dQuote{Practical volume computation of structured convex bodies, and an application to modeling portfolio dependencies and financial crises,} \emph{Proc. of Symposium on Computational Geometry, Budapest, Hungary,} 2018.}
#'
#' @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.
#'
#' @export
compute_indicators <- function(returns, win_length = NULL, m = NULL, n = NULL) {
compute_indicators <- function(returns, win_length = NULL, m = NULL, n = NULL, nwarning = NULL, ncrisis = NULL) {

if (is.null(win_length)) win_length = 60
if (is.null(m)) m = 100
if (is.null(n)) n = 500000
if (is.null(nwarning)) nwarning = 60
if (is.null(ncrisis)) ncrisis = 100

nrows = dim(returns)[1]
nassets = dim(returns)[2]
Expand Down Expand Up @@ -72,9 +76,9 @@ compute_indicators <- function(returns, win_length = NULL, m = NULL, n = NULL) {
set_index = TRUE
} else if (indicators[i]<1) {
if(set_index){
if(i-index+1>30 && i-index+1<60){
if(i-index+1 > nwarning && i-index+1 < ncrisis){
col[index:i] = "warning"
} else if(i-index+1>60) {
} else if(i-index+1 > ncrisis) {
col[index:i] = "crisis"
}
}
Expand Down
5 changes: 3 additions & 2 deletions R-proj/R/gen_rand_hpoly.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
#'
#' @param dimension The dimension of the convex polytope.
#' @param nfacets The number of the facets.
#' @param seed Optional. A fixed seed for the generator.
#'
#' @return A polytope class representing a H-polytope.
#' @examples
#' # generate a 10-dimensional polytope with 50 facets
#' P = gen_rand_hpoly(10, 50)
#' @export
gen_rand_hpoly <- function(dimension, nfacets) {
gen_rand_hpoly <- function(dimension, nfacets, seed = NULL) {

kind_gen = 6
Vpoly_gen = FALSE

Mat = poly_gen(kind_gen, Vpoly_gen, FALSE, dimension, nfacets)
Mat = poly_gen(kind_gen, Vpoly_gen, FALSE, dimension, nfacets, seed)

# first column is the vector b
b = Mat[,1]
Expand Down
5 changes: 3 additions & 2 deletions R-proj/R/gen_rand_vpoly.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#' @param dimension The dimension of the convex polytope.
#' @param nvertices The number of the vertices.
#' @param generator The body that the generator samples uniformly the vertices from: (a) 'cube' or (b) 'sphere'.
#' @param seed Optional. A fixed seed for the generator.
#'
#' @return A polytope class representing a V-polytope.
#' @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 = NULL) {
gen_rand_vpoly <- function(dimension, nvertices, generator = NULL, seed = NULL) {

kind_gen = 4

Expand All @@ -23,7 +24,7 @@ gen_rand_vpoly <- function(dimension, nvertices, generator = NULL) {
}
}

Mat = poly_gen(kind_gen, TRUE, FALSE, dimension, nvertices)
Mat = poly_gen(kind_gen, TRUE, FALSE, dimension, nvertices, seed)

# first column is the vector b
b = Mat[,1]
Expand Down
7 changes: 4 additions & 3 deletions R-proj/R/gen_rand_zonotope.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#' @param dimension The dimension of the zonotope.
#' @param nsegments The number of segments that generate the zonotope.
#' @param generator The distribution to pick the length of each segment from \eqn{[0,100]}: (a) 'uniform', (b) 'gaussian' or (c) 'exponential'.
#'
#' @param seed Optional. A fixed seed for the generator.
#'
#' @return A polytope class representing a zonotope.
#'
#' @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 = NULL) {
gen_rand_zonotope <- function(dimension, nsegments, generator = NULL, seed = NULL) {

kind_gen = 1

Expand All @@ -26,7 +27,7 @@ gen_rand_zonotope <- function(dimension, nsegments, generator = NULL) {
}
}

Mat = poly_gen(kind_gen, FALSE, TRUE, dimension, nsegments)
Mat = poly_gen(kind_gen, FALSE, TRUE, dimension, nsegments, seed)

# first column is the vector b
b = Mat[,1]
Expand Down
19 changes: 10 additions & 9 deletions R-proj/R/rand_rotate.R → R-proj/R/rotate_polytope.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#' Apply a random rotation to a convex polytope (H-polytope, V-polytope or a zonotope)
#' 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 as input this function applies a random rotation.
#' 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 or (b) Vpolytope or (c) Zonotope.
#' @param P A convex polytope. It is an object from class (a) Hpolytope, (b) Vpolytope, (c) Zonotope, (d) intersection of two V-polytopes.
#' @param T Optional. A \eqn{d\times d} rotation matrix.
#'
#' @return A list that contains the rotated polytope and the matrix of the linear transformation.
#' @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.
#' \itemize{
Expand All @@ -16,20 +17,20 @@
#' @examples
#' # rotate a H-polytope (2d unit simplex)
#' P = gen_simplex(2,'H')
#' poly_matrix_list = rand_rotate(P)
#' poly_matrix_list = rotate_polytope(P)
#'
#' # rotate a V-polytope (3d cube)
#' P = gen_cube(3, 'V')
#' poly_matrix_list = rand_rotate(P)
#' 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 = rand_rotate(Z)
#' poly_matrix_list = rotate_polytope(Z)
#' @export
rand_rotate <- function(P){
rotate_polytope <- function(P, T = NULL){

#call rcpp rotating function
Mat = rotating(P)
Mat = rotating(P, T)

n = P$dimension
m=dim(Mat)[2]-n
Expand Down
18 changes: 7 additions & 11 deletions R-proj/R/round_polytope.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
#' Given a convex H or V polytope or a zonotope as input this functionbrings the polytope in well 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 random_walk Optional. 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 d) \code{'BiW'} for Billiard walk. The default walk is \code{'CDHR'} for H-polytope and \code{'BiW'} for the other representations.
#' @param walk_length Optional. The number of the steps for the random walk. The default value is \eqn{1} for \code{'BiW'} and \eqn{\lfloor 10 + d/10\rfloor} otherwise.
#' @param parameters Optional. A list for the parameters of the methods:
#' \itemize{
#' \item{\code{BW_rad} }{ The radius for the ball walk.}
#' \item{\code{diameter} }{ The diameter of the polytope. It is used to set the maximum length of the trajectory in billiard walk.}
#' }
#'
#' @return A list with 2 elements: (a) a polytope of the same class as the input polytope class and (b) the element "round_value" which is the determinant of the square matrix of the linear transformation that was applied on the polytope that is given as input.
#'
#' @references \cite{Michael J. Todd and E. Alper Yildirim,
#' \dQuote{On Khachiyan’s Algorithm for the Computation of Minimum Volume Enclosing Ellipsoids,} \emph{Discrete Applied Mathematics,} 2007.}
#'
#' @examples
#' # rotate a H-polytope (2d unit simplex)
#' A = matrix(c(-1,0,0,-1,1,1), ncol=2, nrow=3, byrow=TRUE)
Expand All @@ -22,15 +18,15 @@
#'
#' # 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, random_walk = 'RDHR', walk_length = 50)
#' 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, random_walk = 'BW')
#' ListZono = round_polytope(Z)
#' @export
round_polytope <- function(P, random_walk = NULL, walk_length = NULL, parameters = NULL){
round_polytope <- function(P){

ret_list = rounding(P, random_walk, walk_length, parameters)
ret_list = rounding(P)

#get the matrix that describes the polytope
Mat = ret_list$Mat
Expand Down
42 changes: 20 additions & 22 deletions R-proj/R/zonotope_approximation.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,37 @@
#'
#' @param Z A zonotope.
#' @param fit_ratio Optional. A boolean parameter to request the computation of the ratio of fitness.
#' @param walk_length Optional. The number of the steps for the random walk. The default value is \eqn{\lfloor 10 + d/10\rfloor} for SequenceOfBalls and \eqn{1} for CoolingGaussian.
#' @param error Optional. Declare the upper bound for the approximation error. The default value is \eqn{1} for SequenceOfBalls and \eqn{0.1} for CoolingGaussian.
#' @param inner_ball Optional. A \eqn{d+1} vector that contains an inner ball. The first \eqn{d} coordinates corresponds to the center and the last one to the radius of the ball. If it is not given then for H-polytopes the Chebychev ball is computed, for V-polytopes \eqn{d+1} vertices are picked randomly and the Chebychev ball of the defined simplex is computed. For a zonotope that is defined by the Minkowski sum of \eqn{m} segments we compute the maximal \eqn{r} s.t.: \eqn{re_i\in Z} for all \eqn{i=1,\dots ,d}, then the ball centered at the origin with radius \eqn{r/\sqrt{d}} is an inscribed ball.
#' @param random_walk Optional. 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 or c) \code{'BW'} for Ball Walk. The default walk is \code{'CDHR'}.
#' @param rounding Optional. A boolean parameter for rounding. The default value is \code{FALSE}.
#' @param parameters Optional. A list for the parameters of the algorithms:
#' @param algo_parameters Optional. A list that declares the values of the parameters of CB algorithm as follows:
#' \itemize{
#' \item{\code{Window} }{ The length of the sliding window for CG algorithm. The default value is \eqn{500+4dimension^2}.}
#' \item{\code{BW_rad} }{ The radius for the ball walk. The default value is \eqn{4r/dimension}, where \eqn{r} is the radius of the inscribed ball of the polytope.}
#' \item{\code{ub} }{ The lower bound for the ratios in MMC in BAN algorithm. The default value is \eqn{0.1}.}
#' \item{\code{lb} }{ The upper bound for the ratios in MMC in BAN algorithm. The default value is \eqn{0.15}.}
#' \item{\code{N} }{ An integer that controls the number of points \eqn{\nu N} generated in each convex body in annealing schedule.}
#' \item{\code{nu} }{ The degrees of freedom for the t-student distribution in t-tests in BAN algorithm. The default value is \eqn{10}.}
#' \item{\code{alpha} }{ The significance level for the t-tests in BAN algorithm. The default values is 0.2.}
#' \item{\code{prob} }{ The probability is used for the empirical confidence interval in ratio estimation of BAN algorithm. The default value is \eqn{0.75}.}
#' \item{\code{hpoly} }{ A boolean parameter to use H-polytopes in MMC of BAN algorithm. The default value is \code{FALSE}.}
#' \item{\code{minmaxW} }{ A boolean parameter to use the sliding window with a minmax values stopping criterion.}
#' \item{\code{L} }{The maximum length of the billiard trajectory.}
#' \item{\code{error} }{ A numeric value to set the upper bound for the approximation error. The default value is \eqn{1} for \code{'SOB'} 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. The default walk is \code{'CDHR'} for H-polytopes and \code{'BiW'} for the other representations.}
#' \item{\code{walk_length} }{ An integer to set the number of the steps for the random walk. The default value is \eqn{\lfloor 10 + d/10\rfloor} for \code{'SOB'} and \eqn{1} otherwise.}
#' \item{\code{rounding} }{ A boolean parameter for rounding. The default value is \code{FALSE}.}
#' \item{\code{inner_ball} }{ A \eqn{d+1} numeric vector that contains an inner ball. The first \eqn{d} coordinates corresponds to the center and the last one to the radius of the ball. If it is not given then for H-polytopes the Chebychev ball is computed, for V-polytopes \eqn{d+1} vertices are picked randomly and the Chebychev ball of the defined simplex is computed. For a zonotope that is defined by the Minkowski sum of \eqn{m} segments we compute the maximal \eqn{r} s.t.: \eqn{re_i\in Z} for all \eqn{i=1,\dots ,d}, then the ball centered at the origin with radius \eqn{r/\sqrt{d}} is an inscribed ball.}
#' \item{\code{len_win} }{ The length of the sliding window for CG algorithm. The default value is \eqn{500+4dimension^2}.}
#' \item{\code{BW_rad} }{ The radius for the ball walk. The default value is \eqn{4r/dimension}, where \eqn{r} is the radius of the inscribed ball of the polytope.}
#' \item{\code{ub} }{ The lower bound for the ratios in MMC in CB algorithm. The default value is \eqn{0.1}.}
#' \item{\code{lb} }{ The upper bound for the ratios in MMC in CB algorithm. The default value is \eqn{0.15}.}
#' \item{\code{N} }{ An integer that controls the number of points \eqn{\nu N} generated in each convex body in annealing schedule of algorithm CB.}
#' \item{\code{nu} }{ The degrees of freedom for the t-student distribution in t-tests in CB algorithm. The default value is \eqn{10}.}
#' \item{\code{alpha} }{ The significance level for the t-tests in CB algorithm. The default values is 0.2.}
#' \item{\code{prob} }{ The probability is used for the empirical confidence interval in ratio estimation of CB algorithm. The default value is \eqn{0.75}.}
#' \item{\code{hpoly} }{ A boolean parameter to use H-polytopes in MMC of CB algorithm. The default value is \code{FALSE}.}
#' \item{\code{minmaxW} }{ A boolean parameter to use the sliding window with a minmax values as a stopping criterion.}
#' \item{\code{L} }{The maximum length of the billiard trajectory.}
#' }
#'
#' @return A list that contains the approximation body in H-representation and the ratio of fitness
#'
#' @examples
#' # over-approximate a 2-dimensional zonotope with 10 generators and compute the ratio of fitness
#' Z = GenZonotope(2,10)
#' Z = gen_rand_zonotope(2,10)
#' retList = zonotope_approximation(Z = Z, fit_ratio = TRUE)
#'
#' @export
zonotope_approximation <- function(Z, fit_ratio = NULL, walk_length = NULL, error = NULL,
inner_ball = NULL, random_walk = NULL, rounding = NULL,
parameters = NULL){
zonotope_approximation <- function(Z, fit_ratio = NULL, algo_parameters = NULL){

ret_list = zono_approx(Z, fit_ratio, walk_length, error, inner_ball, random_walk, rounding, parameters)
ret_list = zono_approx(Z, fit_ratio, algo_parameters)

Mat = ret_list$Mat

Expand Down
7 changes: 6 additions & 1 deletion R-proj/man/compute_indicators.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion R-proj/man/gen_rand_hpoly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion R-proj/man/gen_rand_vpoly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion R-proj/man/gen_rand_zonotope.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions R-proj/man/poly_gen.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ca784f5

Please sign in to comment.