Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

python - Applying proportional redistribution of weight in a capping

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...