# How to Achieve Target Allocation Ratio (TAR) in Your Investment Portfolio

> Deriving an equation that splits a new investment across holdings to get as close as possible to target allocation ratios, by minimising mean squared error.

- Author: [Ashutosh Kumar](https://uncommonlydone.com/profile)
- Published: 2024-02-19 (first published on HackerNoon: https://hackernoon.com/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio)
- Web page: https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio
- Tags: investing, mathematics, python

---

![Illustrated bar chart of five stocks (Apple, Google, Nvidia, Adobe, Tesla) as pink bars of different heights](https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio/cover.webp)
_Lead image generated using [Adobe Express](https://new.express.adobe.com/)_

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.

## Mathematics

You have a portfolio of `n` financial instruments:

![A portfolio of n financial instruments: F1, F2, …, Fi, …, Fn](https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio/01.webp)

and your current investments in each financial instrument is:

![Current investment in each instrument: I1, I2, …, Ii, …, In](https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio/02.webp)

This means that your current investment allocation ratio (between `0` and `1`) is:

![Current allocation ratios I1/SI, I2/SI, …, In/SI, where SI is the sum of all Ii (total current investment)](https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio/03.webp)

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.

![Target allocation ratios: T1, T2, …, Ti, …, Tn](https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio/04.webp)

This implies that:

![Equations (Ii + Ni) / (SI + SN) = Ti for each instrument i, where Ni is the new investment in instrument Fi and the Ni sum to SN, the total new investment](https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio/05.webp)

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,

![MSE = (1/n) × sum over i of ((Ii + Ni) / (SI + SN) − Ti)², where (Ii + Ni) / (SI + SN) is the actual allocation ratio after investing Ni in Fi and Ti is the target ratio](https://uncommonlydone.com/blog/how-to-achieve-target-allocation-ratio-tar-in-your-investment-portfolio/06.webp)

Now, we can simply minimize this function to find all `N_i`

## Assumptions

- 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).

  ## Implementation

  I have converted the above problem into a simple GitHub project. You can find the same [here](https://github.com/ashu-tosh-kumar/stock-investment-split). 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.

```python
# 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:

```bash
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.
