Skip to content
All library documents

Integer Portfolio Rebalancing by Minimizing Dollar Weight Errors

Article Quant Q&A · Author: AathakA

Summary

The exchange considers how to choose whole-share quantities under a cash budget to approximate target portfolio weights. It critiques an objective that minimizes squared differences in share counts: equal share-count errors can represent very different dollar exposures when asset prices vary. Instead, it recommends minimizing deviations in dollars invested from the target allocation, while retaining integer quantity and budget constraints.

This makes the allocation task a mixed-integer optimization problem, for which the answer points to general-purpose optimization solvers. The central modeling lesson is to express the objective in units that match the portfolio goal: dollar allocation or weight accuracy, rather than raw share counts. The response does not provide a closed-form solution, implementation details, or a comparison of solvers, and practical constraints such as transaction costs and lot sizes are outside its scope.

Key ideas

  • Whole-share allocation with a budget is a mixed-integer optimization problem.
  • Minimizing share-count errors can distort allocations when asset prices differ substantially.
  • A dollar-based objective more directly measures deviation from target portfolio weights.
  • Integer quantities and the total investment limit must be represented as constraints.
  • The answer identifies optimization solvers but does not develop an implementation.

Tags

Full text
# Optimization algorithm for maintaning portfolio weights


# Optimization algorithm for maintaning portfolio weights












I'm writing an algorithm that outputs the number of stocks I have to buy for each product in order to get as close as possible to my target weights.

I was thinking at this minimization problem:

$$\min_{x_i}\sum_{i=0}^n (x_i - \frac{T * W_i}{P_i})^2$$

with costraint:

- $\sum_{i=0}^n x_i * P_i < T $

- $x_0, x_1, ...,x_n$ are non negative Integers

where:

- there are $n$ different products

- $x_i$ are (non negative) integers that indicate the quantity to hold (fractional stocks not allowed)

- $T$ is the (non negative) total amount of money I can invest at this time

- $W_i$ is my (non negative) target weight for each product

- $P_i$ is the (non negative) last closing price for the product

I would like to know if there is a closed formula to solve problems such as this, or what kind of numerical techniques should I look into.

For now I'm not considering brute-force solutions.

(Bonus points if you mention a lightweight C++ library that could help here)

## Answer by Michael Williamson (score 1, accepted)

https://quant.stackexchange.com/a/69530

The problem that you have suggested minimizes the difference in the number of shares. If the share prices of the products that you are considering are very different, this could lead to undesirable outcomes. I would suggest that instead you minimize the difference in the dollars invested, which will give you the closest possible portfolio to your target.

So the objective function would become:

$x_i * P_i - T * W_i$

This is a mixed integer optimization problem. You could use:

- Google Optimization Tools: https://developers.google.com/optimization/mip/mip_example

- lp_solve: http://lpsolve.sourceforge.net/5.5/

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.