#πŸ”’ My algorithm on normalization of asset allocations are returning complex numbers :(

10 messages Β· Page 1 of 1 (latest)

storm nacelle
#

Quick Background:

So there are 5 randomly generated numbers from 0 to 1, and we want to normalize them, meaning the sum of the 5 numbers should be equal to 1.

My algorithm is concerned with asset allocations and there are 3 constraints that should be implemented in the normalization. Denote the following:

y - contains the list of asset allocations in the current period
x - contains the list of asset allocations in the previous period
H - set of assets to be Held in the current period

  • this means that the current allocation for an asset in this set should be equal to the previous allocation
    B - set of assets to be Bought in the current period
  • this means that the current allocation for an asset in this set should be more than or equal to the previous asset allocation
    S - set of assets to be Sold in the current period
  • this means that the current allocation for an asset in this set should be less than or equal to the previous asset allocation

Simplified code:
https://paste.pythondiscord.com/6ZKA

Concern:

The code above will normalize an initialized asset allocations. However, I have noticed that the normalized allocations sometimes do not follow the three constraints. I addressed this problem using the following algorithm:

  1. Don't include the H assets in the "sum_x" value in the Normalize function and subtract them to the "totalCapital" value.
  2. Continue the normalization procedure for the B assets and S assets
  3. If the asset index is in H then its previous allocation will be copied into the current allocation.
  4. The allocation for the asset indeces in B and S will be the result of the normalization procedure in Step 2.

This algorithm however only enforces the Hold constraint successfully, and not the Buy and Sell constraints. Is there a way to normalize with some terms having a minimum and maximum value?

high cypressBOT
#

@storm nacelle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

storm nacelle
#

With regards to the complex numbers, I have a large data that I want to apply this algorithm, however, the normalized results are showing complex numbers, which isn't supposed to happen (since every asset allocation should only be between 0 and 1 inclusive).

dusk topaz
#

!e

import random
asset_index = [0, 1, 2, 3, 4] #each number refers to an asset
x = [0.15, 0.25, 0.1, 0, 0.5]
y = []
totalCapital= 1 #Refers to the total capital that can be invested; a 1 means 100%
for i in asset_index:
    y[i] = 0
H = [0, 3]
B = [4]
S = [1, 2]
for i in asset_index: #initialize the asset allocations
    if i in H:
        y[i] = x[i] #enforces the hold constraint
    if i in B:
        y[i] = random.uniform(x[i],1)
    if i in S:
        y[i] = random.uniform(0,x[i])

def Normalize(x):
    xNew = []
    sum_x = 0
    for i in range(0,len(x)):
        sum_x = sum_x + x[i]
    xNew = (x[i]/sum_x)*totalCapital
    return xNew

print(Normalize(y))
high cypressBOT
#

@dusk topaz :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 7, in <module>
003 |     y[i] = 0
004 |     ~^^^
005 | IndexError: list assignment index out of range
dusk topaz
#

!e

import random
asset_index = [0, 1, 2, 3, 4] #each number refers to an asset
x = [0.15, 0.25, 0.1, 0, 0.5]
y = [0] * len(asset_index)
totalCapital= 1 #Refers to the total capital that can be invested; a 1 means 100%
# for i in asset_index:
#     y[i] = 0
H = [0, 3]
B = [4]
S = [1, 2]
for i in asset_index: #initialize the asset allocations
    if i in H:
        y[i] = x[i] #enforces the hold constraint
    if i in B:
        y[i] = random.uniform(x[i],1)
    if i in S:
        y[i] = random.uniform(0,x[i])

def Normalize(x):
    xNew = []
    sum_x = 0
    for i in range(0,len(x)):
        sum_x = sum_x + x[i]
    xNew = (x[i]/sum_x)*totalCapital
    return xNew

print(Normalize(y))
high cypressBOT
#

@dusk topaz :white_check_mark: Your 3.12 eval job has completed with return code 0.

0.6114857981885584
dusk topaz
#

seems to work?

high cypressBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.