Random portfolio simulation using dirichlet distribution with weight boundaries

Viewed 54

I'm currently stuck at a problem which is on random portfolio simulation, however I'm struggling to generate these portfolios that fit into a certain constraints: the code I have is below:

import numpy as np 
from scipy import stats

# n is number of simulation, and width is number of assets
n ,width = 1000000, 38
bound = [0.02, 0.04]

np.random.seed(5) # Set seed
random_weights = np.random.dirichlet(np.ones(width), size = n)

# alphas = np.ones((width,))
# random_weights = np.abs(stats.dirichlet.rvs(alphas, size=n))


# Select only rows that meet weight condition:
cond1 = np.all(bound[0] <= random_weights, axis=1)
cond2 = np.all(random_weights <= bound[1], axis=1)
valid_rows = cond1*cond2
new_weights = random_weights[valid_rows, :]

new_weights end up being empty

I have also tried:

weights = np.random.random((n, width))
weights_sum = weights.sum(axis=1)
weights_sum = np.reshape(weights_sum, (n, 1))
# Standardise these weights so that they sum to 1
random_weights = weights / weights_sum

cond1 = np.all(bound[0] <= random_weights, axis=1)
cond2 = np.all(random_weights <= bound[1], axis=1)
valid_rows = cond1*cond2
new_weights = random_weights[valid_rows, :]

new_weights still ends up being empty

Would you be advise what a possible solution is and why that might be the case?

1 Answers
Related