samedi 25 avril 2020

Generating Random Non Integer Numbers with Constraints Python

I've been trying to add constraints for my random non integer number generation. I am working on a portfolio optimization process and looking to constraint weights of random generetad portfolios.

I managed to put constraints somehow but I could not limit the sum of weights to 100% while keeping each weight between its own threshold.

#Create Random Weighted Portfolios for 5 Assets

num_assets = 5
num_portfolios = 1000
p_weights = []

for portfolio in range(num_portfolios):

    w1 = random.uniform(0.5, 0.7)
    w2 = random.uniform(0.05, 0.3)
    w3 = random.uniform(0.05, 0.3)
    w4 = random.uniform(0.05, 0.5)
    w5 = random.uniform(0.03, 0.15)

    k = w1 + w2 + w3 + w4 + w5 

    #Sum of Asset weights is 100%

    if k = 1:
        weights = (w1, w2, w3, w4, w5)
    else

    #weights = weights/np.sum(weights)
    p_weights.append(weights)



w_data_matrix =  np.asmatrix(p_weights)
print(w_data_matrix)





Aucun commentaire:

Enregistrer un commentaire