Learn R Programming

rportfolios (version 1.0-1)

random.general: Random general portfolio

Description

This function generates a general random portfolio of n investments with k long or short positiobs, The probability that a a non-zero investment weight is positive is p. The maximum absolute exposure for any investment is x.u. The default value is $1$.

Usage

random.general(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

An $n \times 1$ numeric vector of investment weights for the equal weighted portfolio.

Details

If $ k < n$ the function random.general 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
###
x.long <- random.general( 30, p=1.0 )
###
### long only portfolio of 30 investments with 10 non zero positions
###
y.long <- random.general( 30, 10, p=1.0 )
###
### short only portfolio of 30 investments with 30 non zero positions
###
x.short <- random.general( 30, p=0.0 )
###
### short only portfolio of 30 investments with 10 non zero positions
###
y.short <- random.general( 30, 10, p=1.0 )
###
### long short portfolio of 30 investments with 30 non zero positions
###
x.long.short <- random.general( 30, p=0.5 )
###
### long short portfolio of 30 investments with 10 non zero positions
###
y.long.short <- random.general( 30, 10, p=0.5 )
###
### long bias portfolio of 30 investments with 30 non zero positions
###
x.long.bias <- random.general( 30, p=0.7 )
###
### long bias portfolio of 30 investments with 10 non zero positions
###
y.long.bias <- random.general( 30, 10, p=0.7 )
###
### short bias portfolio of 30 investments with 30 non zero positions
###
x.short.bias <- random.general( 30, p=0.3 )
###
### short bias portfolio of 30 investments with 10 non zero positions
###
y.short.bias <- random.general( 30, 10, p=0.3 )

Run the code above in your browser using DataLab