A couple of days ago, I wanted to invest a certain amount into a basket of stocks. But, I encountered a dilemma on how much to invest in individual stocks. I aim to maintain a target allocation ratio for each stock to have a balanced portfolio.
Consider the example that you have a portfolio of 3 stocks viz. INFOSYS, TCS and WIPRO. Your current investment in each of these stocks is ₹30, ₹50, and ₹20 respectively (currency is not important here as long as all stocks are in the same currency). Let’s say our Target Allocation ratio (TAR) for each of these stocks is 33%, 33%, and 34% respectively as shown in the following table.
Instrument |
Current Investment |
Current TAR |
Target TAR |
---|---|---|---|
INFOSYS |
₹30 |
30% |
33% |
TCS |
₹50 |
50% |
33% |
WIPRO |
₹20 |
20% |
34% |
Total |
₹100 |
|
|
Now, what’s the optimal choice if you want to invest a total of ₹30 to get closer to your TAR for each of these stocks? I googled for some tools but couldn't find any that could help my case. So, I sat in to derive an equation that could provide me with an optimal allocation.
You have a portfolio of n
financial instruments:
and your current investments in each financial instrument is:
This means that your current investment allocation ratio (between 0
and 1
) is:
Now, suppose you want to invest an additional amount of SN
split between these n
financial instruments. You want to achieve a target allocation ratio of the following for each of these instruments.
This implies that:
And, our objective is to find out these N_i
i.e. how much investment to make in each of the n
financial instruments.
To solve this problem, we have converted it into an optimization problem. If we assume some initial allocation for each instrument, we calculate the Mean Squared Error MSE
between the objective allocation ratio and the actual allocation ratio. Then, we can simply use any (bounded and constrained) optimizing tool to solve the problem.
So,
Now, we can simply minimize this function to find all N_i
No selling. The whole point of writing this code is that we don't want to sell any existing instruments to achieve the target allocation.
We assume that we would want to invest the full amount of new_investment
i.e. if you input ₹100 as a new investment to be allocated, the model would allocate the full amount between the financial instruments.
We don't solve the above problem rigorously. The correct way would be to create n
equations for n
variables and solve them using an equation solver. However, if that were easy, we wouldn't have invented Machine Learning. So, we instead solve the problem by minimizing the MSE
between target and actual allocation ratio using a minimizer function (similar to how Gradient Descent would minimize the error function in Machine Learning).
I have converted the above problem into a simple GitHub project. You can find the same here. The README
file details how to use the same. Essentially, I used scipy.optimizers.minimize
to minimize the MSE
function above.
To showcase the same, you can edit the file src/views/file.py
to try out the example that we took in the beginning.
# list all financial instruments
FINANCIAL_INSTRUMENT_NAMES: list[str] = ["INFOSYS", "TCS", "WIPRO"]
# Mention initial investment in each instrument in your currency (all should be in same units)
FINANCIAL_INSTRUMENTS_INITIAL_INVESTMENT: list[float] = [30, 50, 20]
# Mention new investment to be made in same unit as initial investment
NEW_INVESTMENT: float = 30
# Mention TAR for each instrument that you want to achieve
FINANCIAL_INSTRUMENTS_TARGET_INVESTMENT_RATIO: list[float] = [0.33, 0.33, 0.34]
Once you have edited the above values in src/views/file.py
, you can run the code by running the command python -m src.views.file
. It would yield something like the following:
Optimizing the MSE to compute optimal allocation. Sit tight!
Final allocation of investment of unit 30 is as follows:
Instrument Initial Investment % Target Investment % Suggested Investment Final %
------------ ---------------------- --------------------- ---------------------- ---------
INFOSYS 30 33 10 30.7692
TCS 50 33 10 46.1538
WIPRO 20 34 10 23.0769
Please manually verify the results as Mathematics can only aid you but can't absolve you!
As we can see, the model recommended allocation of ₹10 for each instrument. Note that with the given investment amount, it might not be possible to achieve exact TAR but the model would help to move towards it. In the above case, INFOSYS
has moved from 30%
to 30.7%
. TCS
has come down from 50%
to 46%
and WIPRO
has moved from 20%
to 23%
.
Feel free to try out different values and let me know if you have any comments.
Lead Image generated using Adobe Express