条件性的 For 循环

huangapple go评论54阅读模式
英文:

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 = [&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;] 

portfolio_weights = []
found=0
count=0
while count&lt;5:
    weights = np.random.random_sample(len(tickers))
    weights = np.round(weights / np.sum(weights),3)
    if sum(ar * weights) &gt; 0.8:
        portfolio_weights.append(weights)
        count+=1
print(&quot;obtained output is :&quot;,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 = [&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;] 

portfolio_weights = []
found=0
count=0
tries=0

while count&lt;5:
    weights = np.random.random_sample(len(tickers))
    weights = np.round(weights / np.sum(weights),3)
    tries+=1
    if sum(ar * weights) &gt; 0.8:
        portfolio_weights.append(weights)
        count+=1
print(&quot;obtained output is :&quot;,portfolio_weights)
print(&quot;number of tries it took to yeild result :&quot;,tries)

</pre>

if this helps leaving a upvote is appreciated.

huangapple
  • 本文由 发表于 2023年3月1日 10:22:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定