Skip to content

Commit

Permalink
Improvements on cran tests and R interface (#83)
Browse files Browse the repository at this point in the history
* fix windows errors in cran checks

* update Rd files, fix bugs in direct_sampling and boundary sampling

* improve R tests, update description file and fix seeding in cooling hpoly

* re order template classes in volume functions

* remove skiping for 32-bit in R tests

* fix declaration of random walks for uniform sampling

* fix the default value of the ball walk radius, fix zonotope c++ test

* fix c++ tests

* fix c++ tests

* fix memory leak in cg algorithm. update R examples and tests

* remove unused comments

* update dicumentation and Rd files

* fix error in lpsolve, seeding in vpolytopeintersection and c++ tests

* fix vpolytope-intersection c++ test
  • Loading branch information
TolisChal authored Jun 1, 2020
1 parent 4507209 commit ac30224
Show file tree
Hide file tree
Showing 87 changed files with 588 additions and 537 deletions.
2 changes: 1 addition & 1 deletion R-proj/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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.
Version: 1.1.0
Date: 2020-01-23
Date: 2020-05-29
Maintainer: Vissarion Fisikopoulos <[email protected]>
Depends: Rcpp (>= 0.12.17)
Imports: methods, stats
Expand Down
2 changes: 1 addition & 1 deletion R-proj/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export(volume)
export(zonotope_approximation)
exportPattern("^[[:alpha:]]+")
importFrom("methods","new")
importFrom("stats", "cov")
importFrom("stats","cov")
importFrom("utils","read.csv")
importFrom(Rcpp,evalCpp)
importFrom(Rcpp,loadModule)
Expand Down
103 changes: 52 additions & 51 deletions R-proj/R/RcppExports.R

Large diffs are not rendered by default.

26 changes: 19 additions & 7 deletions R-proj/R/compute_indicators.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' 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 W, this function computes an approximation of the joint distribution (copula) between portfolios' return and volatility in each time period implied by W.
#' For each copula it computes an indicator: large value corresponds to a crisis period and a small value to a normal period.
#' The periods over which the indicator is greater than 1 for more than 60 consecutives sliding windows are warnings and for more than 100 are crisis. The sliding window is shifted by one day.
#' 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 win_length Optional. The length of the sliding window. The default value is 60.
Expand All @@ -17,6 +17,11 @@
#'
#' @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
#' # simple example on random asset returns
#' asset_returns = replicate(10, rnorm(14))
#' market_states_and_indicators = compute_indicators(asset_returns, 10, 10, 10000, 2, 3)
#'
#' @export
compute_indicators <- function(returns, win_length = NULL, m = NULL, n = NULL, nwarning = NULL, ncrisis = NULL, seed = NULL) {

Expand Down Expand Up @@ -77,15 +82,22 @@ compute_indicators <- function(returns, win_length = NULL, m = NULL, n = NULL, n
set_index = TRUE
} else if (indicators[i]<1) {
if(set_index){
if(i-index+1 > nwarning && i-index+1 < ncrisis){
col[index:i] = "warning"
} else if(i-index+1 > ncrisis) {
col[index:i] = "crisis"
if(i-index > nwarning-1 && i-index <= ncrisis-1){
col[index:(i-1)] = "warning"
} else if(i-index > ncrisis-1) {
col[index:(i-1)] = "crisis"
}
}
set_index = FALSE
}
}
if(set_index){
if(N-index+1 > nwarning-1 && N-index+1 <= ncrisis-1){
col[index:i] = "warning"
} else if(N-index+1 > ncrisis-1) {
col[index:i] = "crisis"
}
}

return(list("indicators" = indicators, market_states = col))

Expand Down
4 changes: 3 additions & 1 deletion R-proj/R/file_to_polytope.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' function to get an ine or an ext file and returns the corresponding polytope
#'
#' For an ine file it generates the corresponding H-polytope. For an ext file it generates the corresponding V-polytope or zonotope.
#' For an ".ine" file it generates the corresponding H-polytope. For an ".ext" file it generates the corresponding V-polytope or zonotope.
#' For more details on those file formats see \url{https://github.com/GeomScale/volume_approximation/blob/develop/doc/cpp_interface.md#polytope-input}.
#'
#' @param path A string that containes the path to an ine or a ext file. The ine file desrcibes a H-polytope and ext file describes a V-polytope or a zonotope.
#' @param zonotope A boolean parameter. It has to be TRUE when the path leads to an .ext file that describes a zonotope.
Expand All @@ -16,6 +17,7 @@
#' @importFrom Rcpp evalCpp
#' @importFrom Rcpp loadModule
#' @importFrom "utils" "read.csv"
#' @importFrom "stats" "cov"
#' @importFrom "methods" "new"
#' @exportPattern "^[[:alpha:]]+"
file_to_polytope <- function(path, zonotope){
Expand Down
4 changes: 2 additions & 2 deletions R-proj/R/gen_cross.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Generator function for cross polytopes
#'
#' This function can be used to generate the \eqn{d}-dimensional cross polytope in H- or V-representation.
#' This function generates the \eqn{d}-dimensional cross polytope in H- or V-representation.
#'
#' @param dimension The dimension of the cross polytope.
#' @param representation A string to declare the representation. It has to be \code{'H'} for H-representation or \code{'V'} for V-representation.
#'
#' @return A polytope class representing a cross polytope in H- or V-representation.
#' @examples
#' # generate a 10-dimensional cross polytope in H-representation
#' P = gen_cross(10, 'H')
#' P = gen_cross(5, 'H')
#'
#' # generate a 15-dimension cross polytope in V-representation
#' P = gen_cross(15, 'V')
Expand Down
4 changes: 2 additions & 2 deletions R-proj/R/gen_cube.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Generator function for hypercubes
#'
#' This function can be used to generate the \eqn{d}-dimensional unit hypercube \eqn{[-1,1]^d} in H- or V-representation.
#' This function generates the \eqn{d}-dimensional unit hypercube \eqn{[-1,1]^d} in H- or V-representation.
#'
#' @param dimension The dimension of the hypercube
#' @param representation A string to declare the representation. It has to be \code{'H'} for H-representation or \code{'V'} for V-representation.
Expand All @@ -11,7 +11,7 @@
#' P = gen_cube(10, 'H')
#'
#' # generate a 15-dimension hypercube in V-representation
#' P = gen_cube(15, 'V')
#' P = gen_cube(5, 'V')
#' @export
gen_cube <- function(dimension, representation) {

Expand Down
2 changes: 1 addition & 1 deletion R-proj/R/gen_prod_simplex.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Generator function for product of simplices
#'
#' This function can be used to generate a \eqn{2d}-dimensional polytope that is defined as the product of two \eqn{d}-dimensional unit simplices in H-representation.
#' This function generates a \eqn{2d}-dimensional polytope that is defined as the product of two \eqn{d}-dimensional unit simplices in H-representation.
#'
#' @param dimension The dimension of the simplices.
#'
Expand Down
2 changes: 1 addition & 1 deletion R-proj/R/gen_rand_hpoly.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Generator function for random H-polytopes
#'
#' This function can be used to generate 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.
#' 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.
Expand Down
2 changes: 1 addition & 1 deletion R-proj/R/gen_rand_vpoly.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Generator function for random V-polytopes
#'
#' This function can be used to generate 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.
#' 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.
Expand Down
3 changes: 2 additions & 1 deletion R-proj/R/gen_rand_zonotope.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Generator function for zonotopes
#'
#' This function can be used to generate a random \eqn{d}-dimensional zonotope defined by the Minkowski sum of \eqn{m} \eqn{d}-dimensional segments. We consider \eqn{m} random directions in \eqn{R^d} and for each direction we pick a random length in \eqn{[(,\sqrt{d}]} in order to define \eqn{m} segments.
#' 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.
Expand Down
2 changes: 1 addition & 1 deletion R-proj/R/gen_simplex.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Generator function for simplices
#'
#' This function can be used to generate the \eqn{d}-dimensional unit simplex in H- or V-representation.
#' This function generates the \eqn{d}-dimensional unit simplex in H- or V-representation.
#'
#' @param dimension The dimension of the unit simplex.
#' @param representation A string to declare the representation. It has to be \code{'H'} for H-representation or \code{'V'} for V-representation.
Expand Down
2 changes: 1 addition & 1 deletion R-proj/R/gen_skinny_cube.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Generator function for skinny hypercubes
#'
#' This function can be used to generate a \eqn{d}-dimensional skinny hypercube in H-representation.
#' This function generates a \eqn{d}-dimensional skinny hypercube \eqn{[-1,1]^{d-1}\times [-100,100]}.
#'
#' @param dimension The dimension of the skinny hypercube.
#'
Expand Down
6 changes: 4 additions & 2 deletions R-proj/R/round_polytope.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#' 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 functionbrings the polytope in well rounded position based on minimum volume enclosing ellipsoid of a pointset.
#' 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 seed Optional. A fixed seed for the number generator.
#'
#' @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.
#' @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,
#' \dQuote{Practical polytope volume approximation,} \emph{ACM Trans. Math. Soft.,} 2018.},
#' @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.}
#'
Expand Down
19 changes: 11 additions & 8 deletions R-proj/R/zonotope_approximation.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#' 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 (BAN). The ratio of fitness is \eqn{R=}.
#' 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{
#' \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{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{win_len} }{ The length of the sliding window for CG algorithm. The default value is \eqn{500+4dimension^2}.}
#' \item{\code{hpoly} }{ A boolean parameter to use H-polytopes in MMC of CB algorithm. The default value is \code{FALSE}.}
#' \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{200}.}
#' \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}.}
#' }
#' @param 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
#'
#' @examples
#' @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,10)
#' retList = zonotope_approximation(Z = Z, fit_ratio = TRUE)
#' Z = gen_rand_zonotope(2,8)
#' retList = zonotope_approximation(Z = Z)
#'
#' @export
zonotope_approximation <- function(Z, fit_ratio = NULL, settings = NULL, seed = NULL){
Expand Down
8 changes: 5 additions & 3 deletions R-proj/man/Hpolytope.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
\title{An \code{R} class to represent H-polytopes.}

\description{
A H-polytope is a convex polytope defined by a set of linear inequalities or equivalently a \eqn{d}-dimensional H-polytope with \eqn{m} facets is defined by a \eqn{m\times d} matrix A and a \eqn{m}-dimensional vector b, s.t.: \eqn{Ax\leq b}.
A H-polytope is a convex polytope defined by a set of linear inequalities or equivalently a \eqn{d}-dimensional H-polytope with \eqn{m} facets is defined by a \eqn{m\times d} matrix A and a \eqn{m}-dimensional vector b, s.t.: \eqn{P=\{x\ |\ Ax\leq b\} }.
}
\section{Fields}{
\itemize{
Expand All @@ -13,5 +13,7 @@ A H-polytope is a convex polytope defined by a set of linear inequalities or equ

\item{\code{type} }{ An integer that declares the representation of the polytope. For H-representation the default value is 1.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.
}}}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}}
8 changes: 5 additions & 3 deletions R-proj/man/Rcpp_Hpolytope.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
An \code{Rcpp} class to represent H-polytopes, exposed to \code{R} via modules.
}
\description{
A H-polytope is a convex polytope defined by a set of linear inequalities or equivalently a \eqn{d}-dimensional H-polytope with \eqn{m} facets is defined by a \eqn{m\times d} matrix A and a \eqn{m}-dimensional vector b, s.t.: \eqn{Ax\leq b}.
A H-polytope is a convex polytope defined by a set of linear inequalities or equivalently a \eqn{d}-dimensional H-polytope with \eqn{m} facets is defined by a \eqn{m\times d} matrix A and a \eqn{m}-dimensional vector b, s.t.: \eqn{P=\{ Ax\leq b \} }.
}
\details{
\describe{
Expand All @@ -20,7 +20,9 @@ A H-polytope is a convex polytope defined by a set of linear inequalities or equ

\item{\code{type} }{ An integer that declares the representation of the polytope. For H-representation the default value is 1.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}
}

\keyword{internal}
5 changes: 4 additions & 1 deletion R-proj/man/Rcpp_Vpolytope.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ A V-polytope is defined as the convex hull of \eqn{m} \eqn{d}-dimensional points

\item{\code{type} }{ An integer that declares the representation of the polytope. For V-representation the default value is 2.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}
}
\keyword{internal}
5 changes: 4 additions & 1 deletion R-proj/man/Rcpp_VpolytopeIntersection.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ An intersection of two V-polytopes, \eqn{P_1}, \eqn{P_2}, is defined by the inte

\item{\code{type} }{ An integer that declares the representation of the polytope. For these kinf of polytopes the default value is 4.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}
}
\keyword{internal}
5 changes: 4 additions & 1 deletion R-proj/man/Rcpp_Zonotope.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ A zonotope is a convex polytope defined by the Minkowski sum of \eqn{m} \eqn{d}-

\item{\code{type} }{ An integer that declares the representation of the polytope. For zonotopes the default value is 3.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}
}
\keyword{internal}
4 changes: 3 additions & 1 deletion R-proj/man/Vpolytope.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@

\item{\code{type} }{ An integer that declares the representation of the polytope. For V-representation the default value is 2.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}}
8 changes: 5 additions & 3 deletions R-proj/man/VpolytopeIntersection.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
\title{An \code{R} class to represent the intersection of two V-polytopes.}

\description{
An intersection of two V-polytopes, \eqn{P_1}, \eqn{P_2}, is defined by the intersection of the two coresponding convex hulls.
An intersection of two V-polytopes, \eqn{P_1}, \eqn{P_2}, is defined by the intersection of the two corresponding convex hulls.
}
\section{Fields}{
\itemize{
\item{\code{V1} }{ The numerical matrix that contains the vertices of \eqn{P_1} row-wise.}

\item{\code{V2} }{ The numerical matrix that contains the vertices of \eqn{P_2} row-wise.}

\item{\code{type} }{ An integer that declares the representation of the polytope. For these kinf of polytopes the default value is 4.}
\item{\code{type} }{ An integer that declares the representation of the polytope. For this polytope the default value is 4.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}}
4 changes: 3 additions & 1 deletion R-proj/man/Zonotope.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ A zonotope is a convex polytope defined by the Minkowski sum of \eqn{m} \eqn{d}-

\item{\code{type} }{ An integer that declares the representation of the polytope. For zonotopes the default value is 3.}

\item{\code{dimension} }{ An integer that declares the dimension of the polytope.}
\item{\code{dimension} }{ The dimension of the polytope.}

\item{\code{volume} }{ The volume of the polytope, if it is known.}
}}
Loading

0 comments on commit ac30224

Please sign in to comment.