Learn R Programming

rportfolios (version 1.0-1)

random.general.test: Random general portfolio

Description

This function generates a general random portfolio of n investments with k long and short positions. The probability that a non-zero investment weight is positive is p. The maximum absolute exposure for any investment is x.u. The default value is 1. The function is used to evaluate the performance of the portfolio generation algorithm.

Usage

random.general.test(n = 2, k = n, segments = NULL, p = 0.5, x.u = 1)

Arguments

n
A positive integer value for the number of investments in the portfolio
k
A positive integer value for the number of non-zero positions
segments
A vector or list of vectors that defines the portfolio segments
p
A positive numeric value for the probability that an investment weight is positive
x.u
A positive numeric value for the maximum absolute exposure to an investment

Value

A list with two named components.

Details

If $ k < n$ the function random.general.test is recursively called with n set equal to k to obtain a $k \times 1$ vector of non-zero long and short weights. The R function sample is used to generate a simple random sample without replacement of k values from the integers $1,2, \ldots ,n$. These are the subscripts into an $n \times 1$ zero vector to assign the k non-zero weights. This vector is returned.

If $ k = n $, the R function rbinom is used to generate a vector of plus and minus ones corresponding to the long and short positions. The R function runif is used to generate uniformly distributed values between 0 and 1. These are scaled by x.u and then multiplied by the signs. The sum of the investment weights is not restricted.

Examples

Run this code
###
### long only portfolio of 30 investments with 30 non-zero positions
###
result.x.long <- random.general.test( 30, p=1.0 )
###
### long only portfolio of 30 investments with 10 non-zero positions
###
result.y.long <- random.general.test( 30, 10, p=1.0 )
###
### short only portfolio of 30 investments with 30 non-zero positions
###
result.x.short <- random.general.test( 30, p=0.0 )
###
### short only portfolio of 30 investments with 10 non-zero positions
###
result.y.short <- random.general.test( 30,10,  p=0.0 )
###
### long short portfolio of 30 investments with 30 non-zero positions
###
result.x.long.short <- random.general.test( 30, p=0.5 )
###
### long short portfolio of 30 investments with 10 non-zero positions
###
result.y.long.short <- random.general.test( 30, 10, p=0.5 )
###
### long bias portfolio of 30 investments with 30 non-zero positions
###
result.x.long.bias <- random.general.test( 30, p=0.7 )
###
### long bias portfolio of 30 investments with 10 non-zero positions
###
result.y.long.bias <- random.general.test( 30, 10, p=0.7 )
###
### short bias portfolio of 30 investments with 30 non-zero positions
###
result.x.short.bias <- random.general.test( 30, p=0.3 )
###
### short bias portfolio of 30 investments with 10 non-zero positions
###
result.y.short.bias <- random.general.test( 30, 10, p=0.3 )

Run the code above in your browser using DataLab