英文:
Conditional For Loop
问题
我试图在Python中创建一个for循环,用于生成一定数量的投资组合的随机权重。然而,我希望确保我对某些股票的最低曝光不低于一定比例。例如,我希望确保股票B和C的权重至少占投资组合的80%。也就是说,我需要生成一堆随机权重的投资组合,以确保股票B和C的权重至少占投资组合的80%。
我的代码如下,但似乎不能真正生成权重大于80%的随机投资组合:
import numpy as np
ar = np.array([0, 1, 1, 0])
number_of_portfolios = 5
tickers = ['A', 'B', 'C', 'D']
portfolio_weights = []
for portfolio in range(number_of_portfolios):
weights = np.random.random_sample(len(tickers))
weights = np.round((weights / np.sum(weights)), 3)
if sum(ar * weights) > 0.8:
portfolio_weights.append(weights)
谢谢!
我期望得到5个不同权重的投资组合,其中股票B和C的权重至少占80%。
英文:
I'm trying to create a for loop in python that will generate random numbers (weights) for a specific number of portfolios. However, I want to make sure I have a minimum number of exposure to some tickers. For example I want to make sure that tickers B and C will account for at least 80% of the portfolio. That is I need to have a bunch of random weights generated for the portfolio such that tickers B & C will account for at least 80% of the portfolio.
My code is below, but it doesn't seem to really generate random portfolios with weights > 80%
ar = np.array([0,1,1,0])
number_of_portfolios = 5
tickers = ['A','B','C','D']
portfolio_weights = []
for portfolio in range (number_of_portfolios):
weights = np.random.random_sample(len(tickers))
weights = np.round((weights / np.sum(weights)),3)
if sum(ar * weights) > 0.8:
portfolio_weights.append(weights)
Thank you!
I was expecting to get 5 differently weighted portfolios with tickers C and C accounting for at least 80% of the portfolio.
答案1
得分: 0
以下是翻译好的部分:
"there is a little issue with your code which is it belive that it yeild output every time. but it actually won't.
to be more precise the probability of getting 0.8 or more for 2 variables is severely biased with very less probability. so you can repeatedly call the guess random to obtain the output. i believe it takes on average 70-92 guesses, on worst case it can go upto 130+ tries.
hope my explaination is clear.
try this code:
import numpy as np
ar = np.array([0,1,1,0])
number_of_portfolios = 5
tickers = ['A','B','C','D']
portfolio_weights = []
found=0
count=0
while count<5:
weights = np.random.random_sample(len(tickers))
weights = np.round weights / np.sum(weights),3)
if sum(ar * weights) > 0.8:
portfolio_weights.append(weights)
count+=1
print("obtained output is :",portfolio_weights)
"
"if you are interested on knowing how many tries it takes on average run this to see the number of tries.
import numpy as np
ar = np.array([0,1,1,0])
number_of_portfolios = 5
tickers = ['A','B','C','D']
portfolio_weights = []
found=0
count=0
tries=0
while count<5:
weights = np.random.random_sample(len(tickers))
weights = np.round(weights / np.sum(weights),3)
tries+=1
if sum(ar * weights) > 0.8:
portfolio_weights.append(weights)
count+=1
print("obtained output is :",portfolio_weights)
print("number of tries it took to yeild result :",tries)
"
"if this helps leaving a upvote is appreciated."
英文:
there is a little issue with your code which is it belive that it yeild output every time. but it actually won't.
to be more precise the probability of getting 0.8 or more for 2 variables is severely biased with very less probability. so you can repeatedly call the guess random to obtain the output. i believe it takes on average 70-92 guesses, on worst case it can go upto 130+ tries.
hope my explaination is clear.
try this code:
<p>
import numpy as np
ar = np.array([0,1,1,0])
number_of_portfolios = 5
tickers = ['A','B','C','D']
portfolio_weights = []
found=0
count=0
while count<5:
weights = np.random.random_sample(len(tickers))
weights = np.round(weights / np.sum(weights),3)
if sum(ar * weights) > 0.8:
portfolio_weights.append(weights)
count+=1
print("obtained output is :",portfolio_weights)
</pre>
if you are interested on knowing how many tries it takes on average run this to see the number of tries.
<p>
import numpy as np
ar = np.array([0,1,1,0])
number_of_portfolios = 5
tickers = ['A','B','C','D']
portfolio_weights = []
found=0
count=0
tries=0
while count<5:
weights = np.random.random_sample(len(tickers))
weights = np.round(weights / np.sum(weights),3)
tries+=1
if sum(ar * weights) > 0.8:
portfolio_weights.append(weights)
count+=1
print("obtained output is :",portfolio_weights)
print("number of tries it took to yeild result :",tries)
</pre>
if this helps leaving a upvote is appreciated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论