R/ractive.test.R

ractive.test <- function( m, x.b, x.g, k=length(x.b), segments=NULL, 
    max.iter=2000, eps=0.001 )
{
###
### This function generates m long short random portfolios with n investments where
### gross notional exposure is x.t.long + and x.t.short and the net notional
### exposure is x.t.long - x.t.short.  Results are returned as a matrix.
###
### Arguments
### m = a positive integer value for the number of portfolios to be generated
### x.b = a numeric vector with the benchmark investment weights
### x.g = a numeric value for the gross notional amount
### k = a positive integer value for the number of non-zero weights in the long short portfolio
### segments = a vector or list of vectors that defines the portfolio segments
### max.iter = a positive integer value for the maximum iterations in the rejection method
### eps = a positive numeric value for the acceptance rejection method based on gross notional exposure
###
### private function
###
    by.case <- function( case, benchmark, gross.notional, size, theseSegments,
        iterations, epsilon )
    {
        return( random.active.test( x.b=benchmark, x.g=gross.notional, 
            k=size, segments=theseSegments, max.iter=iterations, eps=epsilon ) )
    }
    results <- lapply( 1:m, by.case, x.b, x.g, k, segments, max.iter, eps )
###
### separate the investment weights and iterations into a matrix and vector
###
    n <- length( x.b )
    xmatrix <- matrix( 0, nrow=m, ncol=n )
    iters <- rep( 0, m )
    for ( case in 1:m ) {
        thisResult <- results[[case]]
        iters[case] <- thisResult$iter
        xmatrix[case,] <- thisResult$x
    }
###
### create a new result list
###
    result <- list( xmatrix=xmatrix, iters=iters )
    return( result )
}

Try the rportfolios package in your browser

Any scripts or data that you put into this service are public.

rportfolios documentation built on May 2, 2019, 3:40 p.m.