I have the following problem:
I have to do a capping of an index and I have to redistribute proportionally the extra weight of the capped security to the other security based on the uncapped weight.
My code works till the 2nd interaction, where it doesn′t redistribute the weight.
Somebody can help me?
Thanks
Here the section of the code:
###Calculation of market value and market cap######
log.info("Calculating marketValue and marketWeight")
df['marketValue'] = df['price'] * df['volume'] * df['iwf']
total = df['marketValue'].sum()
df['marketWeight'] = df['marketValue']/total
###Capping starting here###
log.info("Do capping")
###Creation of 'dat' df with only the column of market weight, that is needed for the capping####
dat = df.loc[:, ['marketWeight']]
###while loop##
while dat['marketWeight'].max() > 0.3:
dat['newWeight'] = dat.marketWeight - 0.30
dat['newWeight'] = np.where(dat.newWeight < 0, 0, dat.newWeight)
all_weights = dat.newWeight.sum()
avg_to_apply_to_remaining = all_weights / sum(dat.newWeight == 0)
dat['marketWeight'] = np.where(dat.marketWeight < 0.30, dat.marketWeight + avg_to_apply_to_remaining, 0.30)
###end of the while loop###
question from:
https://stackoverflow.com/questions/66054102/applying-proportional-redistribution-of-weight-in-a-capping 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…