lundi 14 août 2017

How to add random weights to list in Python?

Let's say I have a list of countries that I am investing in:

Angola  Croatia  Denmark  Germany ...

How can I randomize weights to these countries that all add up to 100%, essentially - I am looking to run an optimization test on my investments:

Angola  Croatia  Denmark  Germany ...
1.3%    3.8%     4.6%     7.5%    ... (sum equals 100%)

The logic behind randomization is quite simple, but I was wondering if there is an easy way to do it? My current logic being, assign a random number to all of them, take the sum of all randoms, take the number assigned divided by total.

How would I calculate these weights properly and add a new row with these weights using Pandas DataFrame? How would I set up a for-all statement?




Aucun commentaire:

Enregistrer un commentaire